File size: 1,830 Bytes
f62e36f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Install the ForestTools package if it's not already installed
if (!require(ForestTools)) install.packages("ForestTools")
if (!require(terra)) install.packages("terra")
if (!require(sf)) install.packages("sf")
if (!require(imager)) install.packages("imager")
install.packages("dplyr")
install.packages("ggplot2")


# Load the necessary libraries
library(ForestTools)
library(terra)
library(sf)
library(imager)
library(ggplot2)


# Load sample canopy height model provided by ForestTools
lin <- function(x) { x * 0.08 + 0.8 }

chm_folder <- "/Users/taekim/ecohackathon/WeCanopy/TryForestTools/first-flight/chm_tiles_shiftxy"

# List all TIFF files in the directory
tile_paths <- list.files(path = chm_folder, pattern = "\\.tif$", full.names = TRUE)
print(tile_paths)
# Initialize an empty list to store sf objects for each tile
crown_polygons_list <- list()

counter <- 0
for (chm_path in tile_paths) {
  print(counter)
  
  counter <- counter + 1
  chm <- terra::rast(chm_path)
  if (nlyr(chm) > 1) {
    chm <- chm[[1]]  # Select the first layer if necessary
  }
  
  ttops <- vwf(chm, winFun = lin, minHeight = 2)
  crowns_poly <- mcws(treetops = ttops, CHM = chm, format = "polygons", minHeight = 5)
  crowns_sf <- st_as_sf(crowns_poly)
  crowns_sf$tile_path <- chm_path
  
  heights_list <- lapply(seq_len(nrow(crowns_sf)), function(i) {
    crown_mask <- mask(chm, crowns_sf[i, ])
    max(crown_mask[], na.rm = TRUE)
  })
  heights <- unlist(heights_list)
  
  print(heights)
  
  base_name <- tools::file_path_sans_ext(basename(chm_path))
  output_file_path <- file.path("/Users/taekim/ecohackathon/WeCanopy/TryForestTools/first-flight/result_polygons_shiftxy", paste0(base_name, "_crowns.geojson"))
  
  st_write(crowns_sf, output_file_path, driver = "GeoJSON")
  
  crown_polygons_list[[chm_path]] <- crowns_sf
}