trying new version
Browse files
TryForestTools/data.R
CHANGED
@@ -11,48 +11,46 @@ library(sf)
|
|
11 |
library(imager)
|
12 |
|
13 |
# Load sample canopy height model provided by ForestTools
|
14 |
-
|
15 |
-
if (file.exists(chm_path)) {
|
16 |
-
chm <- terra::rast(chm_path)
|
17 |
-
} else {
|
18 |
-
stop("CHM file does not exist")
|
19 |
-
}
|
20 |
|
21 |
-
#
|
22 |
-
print("Summary of CHM:")
|
23 |
-
summary(chm)
|
24 |
-
print(paste("Number of NA values in CHM:", sum(is.na(values(chm)))))
|
25 |
-
plot(chm, main = "Canopy Height Model")
|
26 |
|
27 |
-
#
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
plot(ttops$geometry, col = "blue", pch = 20, cex = 0.5, add = TRUE)
|
38 |
-
print(paste("Mean height of treetops:", mean(ttops$height)))
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
print("Crown polygons:")
|
49 |
-
print(head(crowns_poly))
|
50 |
|
51 |
-
# Plot
|
52 |
-
plot(
|
53 |
-
plot(crowns_poly$geometry, border = "blue", lwd = 0.5, add = TRUE)
|
54 |
|
55 |
-
# Compute area and diameter
|
56 |
-
crowns_poly[["area"]] <- st_area(crowns_poly)
|
57 |
-
crowns_poly[["diameter"]] <- sqrt(crowns_poly[["area"]] / pi) * 2
|
58 |
-
print(paste("Mean crown diameter:", mean(crowns_poly$diameter)))
|
|
|
11 |
library(imager)
|
12 |
|
13 |
# Load sample canopy height model provided by ForestTools
|
14 |
+
lin <- function(x) { x * 0.05 + 0.5 }
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
chm_folder <- "/Users/taekim/ecohackathon/first_try_lidar/ForestTools/TryForestTools/first-flight/chm_tiles" # Update this with the path to your CHM tiles folder
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# List all TIFF files in the directory
|
19 |
+
tile_paths <- list.files(path = chm_folder, pattern = "\\.tif$", full.names = TRUE)
|
20 |
+
print(tile_paths)
|
21 |
+
# Initialize an empty list to store sf objects for each tile
|
22 |
+
crown_polygons_list <- list()
|
23 |
|
24 |
+
for (chm_path in tile_paths) {
|
25 |
+
# Load the CHM file
|
26 |
+
chm <- terra::rast(chm_path)
|
27 |
+
if (nlyr(chm) > 1) {
|
28 |
+
chm <- chm[[1]] # Select the first layer if necessary
|
29 |
+
}
|
30 |
+
|
31 |
+
# Detect treetops using the VWF algorithm
|
32 |
+
ttops <- vwf(chm, winFun = lin, minHeight = 2)
|
33 |
+
|
34 |
+
# Create crown polygons using the MCWS method
|
35 |
+
crowns_poly <- mcws(treetops = ttops, CHM = chm, format = "polygons", minHeight = 1.5)
|
36 |
+
|
37 |
+
# Add the results to the list
|
38 |
+
crown_polygons_list[[chm_path]] <- crowns_poly
|
39 |
+
}
|
40 |
|
41 |
+
# Combine all sf objects into one
|
42 |
+
all_crowns <- do.call(rbind, crown_polygons_list)
|
|
|
|
|
43 |
|
44 |
+
# Resolve overlapping polygons using spatial union
|
45 |
+
merged_crowns <- st_union(all_crowns)
|
46 |
|
47 |
+
# Compute additional attributes such as area and diameter
|
48 |
+
merged_crowns[["area"]] <- st_area(merged_crowns)
|
49 |
+
merged_crowns[["diameter"]] <- sqrt(merged_crowns[["area"]] / pi) * 2
|
50 |
|
51 |
+
# Save the combined and merged crown polygons to a shapefile
|
52 |
+
st_write(merged_crowns, "path_to_save/merged_crown_polygons.shp")
|
|
|
|
|
53 |
|
54 |
+
# Plot the merged crown polygons for visualization
|
55 |
+
plot(merged_crowns$geometry, col = 'green', main = "Merged Crown Polygons")
|
|
|
56 |
|
|
|
|
|
|
|
|
TryForestTools/first-flight/convert_to_chm.ipynb
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d654d63e3f7db465503e60e4e8336f909d0512975ec7cebe0e0972680d9e2f36
|
3 |
+
size 155374
|