-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimizing the readability of the code
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
title: "Bomen delineatie" | ||
output: html_notebook | ||
--- | ||
```{r} | ||
library(terra) # For raster processing | ||
# Load DSM and DTM | ||
dsm_drone <- rast("E:/Thesis Thomas/DHT/DEM update/m_geclipt_DEM Dilsen 1.tif") | ||
dtm <- rast("Z:/Vlaanderen/Hoogte/DHMVII/DHMVIIDTMRAS1m.tif") | ||
# Check if CRS match | ||
identical(crs(dsm_drone), crs(dtm)) | ||
``` | ||
|
||
```{r} | ||
# Reproject DSM to match the DTM's CRS | ||
dsm_reprojected <- project(dsm_drone, crs(dtm), method = "bilinear") | ||
# Save the reprojected DSM | ||
writeRaster(dsm_reprojected, "../output/dsm Dilsen 1.tif", overwrite = TRUE) | ||
``` | ||
|
||
```{r} | ||
# Clip DTM to DSM extent | ||
dtm_clipped <- crop(dtm, dsm_reprojected) | ||
# Resample DTM to match DSM resolution | ||
dtm_resampled <- resample(dtm_clipped, dsm_reprojected, method = "bilinear") | ||
# Save the clipped and resampled DTM | ||
writeRaster(dtm_resampled, "../output/dtm Dilsen 1.tif", overwrite = TRUE) | ||
``` | ||
|
||
```{r} | ||
# CHM calculation | ||
chm <- dsm_reprojected - dtm_resampled | ||
# Save the CHM | ||
writeRaster(chm, "../output/chm Dilsen 1.tif", overwrite = TRUE) | ||
``` | ||
|
||
```{r} | ||
# Calculate the 1st percentile | ||
p1 <- quantile(values(chm), probs = 0.01, na.rm = TRUE) | ||
print(p1) # Display the 1st percentile value | ||
# Rescale by subtracting the 1st percentile | ||
chm_rescaled <- chm - p1 | ||
# Set all values lower than 0 to 0 | ||
chm_rescaled[chm_rescaled < 0] <- 0 | ||
# Save the rescaled CHM | ||
writeRaster(chm_rescaled, "../output/chm_rescaled Dilsen 1.tif.tif", overwrite = TRUE) | ||
``` | ||
```{r} | ||
library(ForestTools) | ||
library(raster) | ||
chm_raster <- raster(chm_rescaled) | ||
# Detect tree tops | ||
tree_tops <- vwf(CHM = chm_raster, winFun = function(x) 0.05 * x + 0.6, minHeight = 2) | ||
# Plot tree tops | ||
plot(chm_raster, main = "Tree Tops") | ||
plot(tree_tops, add = TRUE, col = "red", pch = 20) | ||
``` | ||
|