-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_snic_areas.R
More file actions
267 lines (229 loc) · 6.53 KB
/
generate_snic_areas.R
File metadata and controls
267 lines (229 loc) · 6.53 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
###
# download NAIP for a location then generate the SNIC objects for validation
###
# libraries
pacman::p_load(
rstac,
snic,
sf,
terra,
dplyr,
tmap,
rlang,
httr,
tictoc,
purrr,
furrr,
tidyr
)
tmap::tmap_mode("view")
source("functions/naipScrape.R") # only works for 2016 and 2020
source("functions/snicParameters.R") # snic processing
source("functions/compileAndExportSNICResults.R") # snic processing
# required inputs --------------------------------------------------------
grid100 <- sf::st_read("data/derived/grids/grid100km_aea.gpkg")
# qtm(grid100)
# 1478 - nw NE
# 1351 - se NE
# need to pull some 1k grids from the 2mile areas
grid2 <- sf::st_read("data/derived/grids/two_sq_grid.gpkg")
# subgrids_2010 <- c(13860, 12560, 17182, 22744, 23045, 2510, 6465)
# subgrids_2016 <- c(30823, 6621)
# subgrids_2020 <- c(17663, 10625, 24675)
mlra <- sf::st_read("data/raw/mlra/MLRA_52_2022/MLRA_52.shp")
# working with 76 for next set of values
mlra76 <- mlra[mlra$MLRA_ID == "76", ]
sf::st_area(mlra76)
qtm(mlra76)
# generate random points within the mlra objec
samplePoints <- st_sample(mlra76, size = 15, type = "random")
# parse out lat long
pointsDF <- samplePoints |>
st_as_sf() |>
mutate(
lon = st_coordinates(samplePoints)[,1],
lat = st_coordinates(samplePoints)[,2],
year = c(rep("2016", 7), rep("2020", 8))
)
m76_16 <- pointsDF[pointsDF$year == "2016", ]
m76_20 <- pointsDF[pointsDF$year == "2020", ]
# do a random selection of grid2 features
random_rows <- grid2[sample(nrow(grid2), 16), ]
random_ids <- random_rows$FID_two_grid
#
grids16 <- random_ids[1:8]
grids20 <- random_ids[9:16]
get_grid_centroid <- function(target_id, data) {
# 1. Filter for the specific ID
feature <- data %>%
filter(FID_two_grid == target_id)
# 2. Safety check: ensure the feature exists
if (nrow(feature) == 0) {
stop("ID not found in the dataset.")
}
# 3. Calculate Centroid
# Note: st_centroid works on s2 (spherical) geometry by default in modern sf versions
centroid_geometry <- st_centroid(st_geometry(feature))
# 4. Extract Coordinates (returns a matrix)
coords <- st_coordinates(centroid_geometry)
# 5. Format output
return(data.frame(
ID = target_id,
Lon = coords[1, "X"],
Lat = coords[1, "Y"]
))
}
# gather data from sources
p20 <- purrr::map_dfr(.x = subgrids_2020, .f = get_grid_centroid, data = grid2)
p16 <- purrr::map_dfr(.x = subgrids_2016, .f = get_grid_centroid, data = grid2)
p10 <- purrr::map_dfr(.x = subgrids_2010, .f = get_grid_centroid, data = grid2)
g16 <- purrr::map_dfr(.x = grids16, .f = get_grid_centroid, data = grid2)
g20 <- purrr::map_dfr(.x = grids20, .f = get_grid_centroid, data = grid2)
# download and process NAIP image ----------------------------------------------------------------
process_naip_snic <- function(
year,
lat,
lon,
grid100,
export_base = "data/derived"
) {
# 1. Setup paths and AOI
point <- c(lon, lat)
aoi <- getAOI(grid100 = grid100, point = point)
gridID <- aoi$id
# Define directory structure
naip_dir <- file.path(export_base, "naipExports")
snic_dir <- file.path(export_base, "snicExports")
temp_download_dir <- "naip_grids_1km"
# Ensure directories exist
if (!dir.exists(naip_dir)) {
dir.create(naip_dir, recursive = TRUE)
}
if (!dir.exists(snic_dir)) {
dir.create(snic_dir, recursive = TRUE)
}
out_path <- file.path(
naip_dir,
paste0("naip_", year, "_id_", gridID, "_wgs84.tif")
)
# 2. Download and Merge if file doesn't exist
if (!file.exists(out_path)) {
message(paste(
"--- Downloading & Merging Grid:",
gridID,
"Year:",
year,
"---"
))
downloadNAIP(aoi = aoi, year = year, exportFolder = temp_download_dir)
files <- list.files(
temp_download_dir,
pattern = paste0(year, "_id_", gridID),
full.names = TRUE
)
if (length(files) == 0) {
stop(paste("No NAIP files found for ID:", gridID, "in year:", year))
}
mergeAndExport(files = files, out_path = out_path, aoi = aoi)
} else {
message(paste(
"--- Existing TIF found for Grid:",
gridID,
". Skipping Download. ---"
))
}
# 3. SNIC Processing
message(paste("--- Generating SNIC Segmentation for:", gridID, "---"))
r1 <- terra::rast(out_path)
# Generate seeds (lat/lon spacing)
seeds <- generate_scaled_seeds(r = r1)
# Process segmentations
process_segmentations(
r = r1,
seed_list = seeds,
output_dir = snic_dir,
year = year,
file_id = gridID
)
# 4. Final Bundle
message(paste("--- Bundling Final Data for ID:", gridID, "---"))
bundle_and_export(grid_id = gridID, year = year)
return(invisible(out_path))
}
# download and process NAIP image ----------------------------------------------------------------
## probably best to make this a function accepting either a point or a grid id for better integration into
## the snic workflow
# naip imagery for MLRA 76 --- just assumed that 2016 would have imagery
for (i in 1:nrow(m76_16)) {
process_naip_snic(
year = 2016,
lat = m76_16$lat[i],
lon = m76_16$lon[i],
grid100 = grid100
)
}
for (i in 1:nrow(m76_20)) {
process_naip_snic(
year = 2020,
lat = m76_20$lat[i],
lon = m76_20$lon[i],
grid100 = grid100
)
}
## sites within validation imagery
for (i in 1:nrow(p20)) {
process_naip_snic(
year = 2020,
lat = p20$Lat[i],
lon = p20$Lon[i],
grid100 = grid100
)
}
#
for (i in 1:nrow(p16)) {
process_naip_snic(
year = 2016,
lat = p16$Lat[i],
lon = p16$Lon[i],
grid100 = grid100
)
}
# random selection 2016
for (i in 1:nrow(g16)) {
process_naip_snic(
year = 2016,
lat = g16$Lat[i],
lon = g16$Lon[i],
grid100 = grid100
)
}
# random selection 2020
for (i in 1:nrow(g20)) {
process_naip_snic(
year = 2020,
lat = g20$Lat[i],
lon = g20$Lon[i],
grid100 = grid100
)
}
# one off example for visualization
gridID <- "1415-3-12-4-1"
image <- terra::rast(
"data/ready_for_export/grid_1415-3-12-4-1_2020_bundle/naip_2020_id_1415-3-12-4-1_wgs84.tif"
)
s40 <- terra::rast(
"data/ready_for_export/grid_1415-3-12-4-1_2020_bundle/naip_2020_id_1415-3-12-4-1_wgs84.tif"
)
s10 <- image <- terra::rast(
"data/ready_for_export/grid_1415-3-12-4-1_2020_bundle/naip_2020_id_1415-3-12-4-1_wgs84.tif"
)
seedList <- generate_scaled_seeds(r = image)
naip <- process_segmentations(
r = image,
seed_list = seedList,
output_dir = "temp",
file_id = gridID,
year = "2020"
)
inspect_seed_density(r = image, seed_list = seedList, "s40")
inspect_seed_density(r = image, seed_list = seedList, "s10")