Skip to content

Commit

Permalink
Optimizing the readability of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
stieninbo committed Jan 14, 2025
1 parent b924758 commit 80c3e11
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions source/TreeDelineation.Rmd
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")

Check warning on line 9 in source/TreeDelineation.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/TreeDelineation.Rmd,line=9,col=20,[absolute_path_linter] Do not use absolute paths.
dtm <- rast("Z:/Vlaanderen/Hoogte/DHMVII/DHMVIIDTMRAS1m.tif")

Check warning on line 10 in source/TreeDelineation.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/TreeDelineation.Rmd,line=10,col=14,[absolute_path_linter] Do not use absolute paths.
# 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)

Check warning on line 55 in source/TreeDelineation.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/TreeDelineation.Rmd,line=55,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 86 characters.
```
```{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)

Check warning on line 63 in source/TreeDelineation.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/TreeDelineation.Rmd,line=63,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 86 characters.
# Plot tree tops
plot(chm_raster, main = "Tree Tops")
plot(tree_tops, add = TRUE, col = "red", pch = 20)

Check warning on line 68 in source/TreeDelineation.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/TreeDelineation.Rmd,line=68,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.
```

0 comments on commit 80c3e11

Please sign in to comment.