-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess-tiles.R
More file actions
146 lines (119 loc) · 2.84 KB
/
process-tiles.R
File metadata and controls
146 lines (119 loc) · 2.84 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
## Prepare thematic
## 2025-12-19
## D.E. Beaudette
## TODO: careful with NA handling
# library(DBI)
# library(RSQLite)
library(purrr)
library(furrr)
library(terra)
# library(sf)
library(soilDB)
source('local-functions.R')
source('config.R')
lut <- readRDS('LUT.rds')
# load optimized grid, with all-NA tiles removed
# g <- readRDS(file = 'A_grid.rds')
# tiles go here
output.dir <- 'processed-tiles'
# start fresh
unlink(output.dir, recursive = TRUE)
dir.create(output.dir)
# pre-tiled mukey grids
# must exclude any other accessory files in this dir: e.g. .tif.aux.xml
g.files <- list.files(path = 'temporary-mukey-tiles', pattern = '\\.tif$', full.names = TRUE)
## iterate over tiles
.tileIndex <- seq_along(g.files)
## test that these work as expected
# map(.x = 227, .f = makeThematicTile, tiles = g.files, vars = v, output.dir = output.dir, .progress = TRUE)
#
# map(.x = 9, .f = makeThematicTile, tiles = g.files, vars = v, output.dir = output.dir, .progress =
#
## STATSGO 300m, tile 90, all NA
# map(.x = 90, .f = makeThematicTile, tiles = g.files, vars = v, output.dir = output.dir, .progress = TRUE)
## component-level data: top/bottom arguments are ignored
# map(.x = 9, .f = makeThematicTile, tiles = g.files, vars = 'wei', output.dir = output.dir, .progress = TRUE)
# 2025-12-19: split into sequential steps:
# unique mukeys, SDA aggregation, RAT -> value grids
#
## TODO: this is much slower than it should be, why?
## init multiple cores
# plan(multicore) # linux
plan(multisession) # windows
system.time(
future_walk(
.tileIndex,
.f = makeThematicTile,
tiles = g.files,
vars = v,
output.dir = output.dir,
.progress = TRUE
)
)
# stop parallel back-ends
plan(sequential)
# ## check for errors or failed SDA requests
# idx <- which(!sapply(z, is.null))
# zz <- z[idx]
#
#
# if (length(zz) > 0) {
#
# print('second pass...')
#
# ## TODO: check on these
# sapply(zz, function(i) {
# nrow(i$rat)
# })
#
# .secondPass <- sapply(zz, function(i) {
# i$i
# })
#
# plan(multisession)
#
# system.time(
# z <- future_map(
# .secondPass,
# .f = makeThematicTileSDA,
# tiles = g.files,
# vars = v,
# top = depth.interval[1],
# bottom = depth.interval[2],
# output.dir = output.dir,
# .progress = TRUE
# )
# )
#
# plan(sequential)
#
#
# ## TODO: find a more elegant solution
#
# ## check for errors or failed SDA requests
# idx <- which(!sapply(z, is.null))
# zz <- z[idx]
#
# sapply(zz, function(i) {
# nrow(i$rat)
# })
#
#
# .thirdPass <- sapply(zz, function(i) {
# i$i
# })
#
# }
#
#
#
#
# ## final check
#
# n.expected <- length(v) * length(g.files)
# n.output <- length(list.files(output.dir))
#
# stopifnot(n.expected == n.output)
## cleanup
rm(list = ls())
gc(reset = TRUE)