update
Browse files
TryForestTools/first-flight/combine_tiles.ipynb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1cd5ac1077f24e427a57d393d7cfa4a45248cefea41b413a7beecaeead9ff8c1
|
3 |
+
size 11371
|
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:4cc2ce9a3a51f78cb98b680cb85277354758ec1e71e00697fd88c4f255b51606
|
3 |
+
size 249142
|
TryForestTools/first-flight/data.R
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install the ForestTools package if it's not already installed
|
2 |
+
if (!require(ForestTools)) install.packages("ForestTools")
|
3 |
+
if (!require(terra)) install.packages("terra")
|
4 |
+
if (!require(sf)) install.packages("sf")
|
5 |
+
if (!require(imager)) install.packages("imager")
|
6 |
+
install.packages("dplyr")
|
7 |
+
install.packages("ggplot2")
|
8 |
+
|
9 |
+
|
10 |
+
# Load the necessary libraries
|
11 |
+
library(ForestTools)
|
12 |
+
library(terra)
|
13 |
+
library(sf)
|
14 |
+
library(imager)
|
15 |
+
library(ggplot2)
|
16 |
+
|
17 |
+
|
18 |
+
# Load sample canopy height model provided by ForestTools
|
19 |
+
lin <- function(x) { x * 0.08 + 0.8 }
|
20 |
+
|
21 |
+
chm_folder <- "/Users/taekim/ecohackathon/WeCanopy/TryForestTools/first-flight/chm_tiles_shiftxy"
|
22 |
+
|
23 |
+
# List all TIFF files in the directory
|
24 |
+
tile_paths <- list.files(path = chm_folder, pattern = "\\.tif$", full.names = TRUE)
|
25 |
+
print(tile_paths)
|
26 |
+
# Initialize an empty list to store sf objects for each tile
|
27 |
+
crown_polygons_list <- list()
|
28 |
+
|
29 |
+
counter <- 0
|
30 |
+
for (chm_path in tile_paths) {
|
31 |
+
print(counter)
|
32 |
+
|
33 |
+
counter <- counter + 1
|
34 |
+
chm <- terra::rast(chm_path)
|
35 |
+
if (nlyr(chm) > 1) {
|
36 |
+
chm <- chm[[1]] # Select the first layer if necessary
|
37 |
+
}
|
38 |
+
|
39 |
+
ttops <- vwf(chm, winFun = lin, minHeight = 2)
|
40 |
+
crowns_poly <- mcws(treetops = ttops, CHM = chm, format = "polygons", minHeight = 5)
|
41 |
+
crowns_sf <- st_as_sf(crowns_poly)
|
42 |
+
crowns_sf$tile_path <- chm_path
|
43 |
+
|
44 |
+
heights_list <- lapply(seq_len(nrow(crowns_sf)), function(i) {
|
45 |
+
crown_mask <- mask(chm, crowns_sf[i, ])
|
46 |
+
max(crown_mask[], na.rm = TRUE)
|
47 |
+
})
|
48 |
+
heights <- unlist(heights_list)
|
49 |
+
|
50 |
+
print(heights)
|
51 |
+
|
52 |
+
base_name <- tools::file_path_sans_ext(basename(chm_path))
|
53 |
+
output_file_path <- file.path("/Users/taekim/ecohackathon/WeCanopy/TryForestTools/first-flight/result_polygons_shiftxy", paste0(base_name, "_crowns.geojson"))
|
54 |
+
|
55 |
+
st_write(crowns_sf, output_file_path, driver = "GeoJSON")
|
56 |
+
|
57 |
+
crown_polygons_list[[chm_path]] <- crowns_sf
|
58 |
+
}
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|