Skip to content

Commit 837f286

Browse files
committed
api/cli: support weights
1 parent b5fd816 commit 837f286

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

api/api.R

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323

2424
logger::log_formatter(logger::formatter_glue_safe)
2525

26-
mount <- Sys.getenv("HFSUBSET_API_MOUNT", "UNSET")
27-
if (mount == "UNSET") {
28-
mount <- NULL # use default
29-
}
30-
3126
cache_dir <- Sys.getenv("HFSUBSET_API_CACHE_DIR", "UNSET")
3227
if (cache_dir == "UNSET") {
3328
cache_dir <- NULL
@@ -96,30 +91,46 @@ hash_list <- function(l) {
9691
#' - $size: byte length
9792
#' - $data: raw vector
9893
#' - $cache: character(1) of "hit" or "miss"
99-
get_subset <- function(call_args, result) {
94+
get_subset <- function(call_args, result, weight_args = list()) {
10095
key <- hash_list(call_args)
96+
97+
hfdata <- NULL # list
10198
if (subset_cache$exists(key)) {
10299
result$cache <- "hit"
103-
result$data <- subset_cache$get(key)
104-
result$size <- length(result$data)
100+
hfdata <- subset_cache$get(key)
105101
} else {
106102
result$cache <- "miss"
103+
hfdata <- do.call(hfsubsetR::get_subset, call_args)
104+
subset_cache$set(key, hfdata)
105+
}
107106

108-
# Output subset to tempfile and read binary
109-
call_args$outfile <- tempfile(fileext = ".gpkg")
110-
do.call(hfsubsetR::get_subset, call_args)
111-
result$size <- file.size(call_args$outfile)
112-
result$data <- readBin(
113-
call_args$outfile,
114-
"raw",
115-
n = result$size
116-
)
117-
unlink(call_args$outfile)
107+
if (length(weight_args) > 0 && "divides" %in% names(hfdata)) {
108+
for (weight in weight_args) {
109+
hfdata[[paste0("weight_grid_", weight)]] <-
110+
paste0(
111+
"s3://lynker-spatial/gridded-resources/",
112+
weight,
113+
".forcing.tif"
114+
) |>
115+
terra::rast() |>
116+
zonal::weight_grid(
117+
geom = hfdata$divides,
118+
ID = "divide_id",
119+
progress = FALSE
120+
) |>
121+
dplyr::mutate(grid_id = paste0(weight, ".forcing"))
122+
}
123+
}
118124

119-
# Cache result
120-
subset_cache$set(key, result$data)
125+
outfile <- tempfile(fileext = ".gpkg")
126+
for (layer in names(hfdata)) {
127+
sf::write_sf(hfdata[[layer]], outfile, layer)
121128
}
122129

130+
result$size <- file.size(outfile)
131+
result$data <- readBin(outfile, "raw", n = result$size)
132+
unlink(outfile)
133+
123134
return(invisible(NULL))
124135
}
125136

@@ -150,6 +161,7 @@ function(req, res) {
150161
#* @param identifier_type:string Type of identifier passed (one of: `hf`, `comid`, `hl`, `poi`, `nldi`, `xy`]
151162
#* @param layer:[string] Layers to return with a given subset, defaults to: [`divides`, `flowlines`, `network`, `nexus`]
152163
#* @param subset_type:string Type of hydrofabric to subset (related to `version`)
164+
#* @param weights:[string] Forcing weights to generate (any of: `medium_range`)
153165
#* @param version:string Hydrofabric version to subset
154166
#* @get /subset
155167
#* @response 200 GeoPackage subset of the hydrofabric
@@ -161,6 +173,7 @@ function(
161173
identifier,
162174
identifier_type,
163175
layer = c("divides", "flowlines", "network", "nexus"),
176+
weights = list(),
164177
subset_type = c("reference"),
165178
version = c("2.2")
166179
) {
@@ -181,16 +194,13 @@ function(
181194
xy = list(xy = parse_xy(identifier))
182195
)
183196

184-
tmp <- tempfile(fileext = ".gpkg")
185-
on.exit({ unlink(tmp) })
186-
187197
call_args$type <- subset_type
188198
call_args$hf_version <- version
189199
call_args$lyrs <- layer
190200

191201
tryCatch({
192202
result <- new.env()
193-
get_subset(call_args, result)
203+
get_subset(call_args, result, weight_args = weights)
194204
logger::log_success(
195205
"retrieved subset of size {size}, cache: {cache}",
196206
.topenv = result

cli/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type SubsetRequest struct {
7979
IdType *string
8080
Layers []string
8181
SubsetType *string
82+
Weights []string
8283
Version *string
8384
Output *string
8485
}
@@ -127,6 +128,12 @@ func createSubsetEndpointUrl(endpoint string, opts *SubsetRequest) (*url.URL, er
127128
params.Add("subset_type", *opts.SubsetType)
128129
}
129130

131+
if opts.Weights != nil {
132+
for _, weight := range opts.Weights {
133+
params.Add("weights", weight)
134+
}
135+
}
136+
130137
if opts.Version != nil {
131138
params.Add("version", *opts.Version)
132139
}
@@ -210,6 +217,7 @@ func main() {
210217
flag.BoolVar(&verify, "verify", true, "Verify that endpoint is available")
211218
flag.BoolVar(&dryRun, "dryrun", false, "Perform a dry run, only outputting the request that will be sent")
212219
layers := flag.String("l", "divides,flowlines,network,nexus", "Comma-delimited list of layers to subset.")
220+
weights := flag.String("w", "", "Comma-delimited list of weights to generate over the subset.")
213221
flag.Parse()
214222

215223
if len(flag.Args()) == 0 {
@@ -218,6 +226,7 @@ func main() {
218226
}
219227

220228
opts.Layers = append(opts.Layers, strings.Split(*layers, ",")...)
229+
opts.Weights = append(opts.Weights, strings.Split(*weights, ",")...)
221230

222231
args := flag.Args()
223232
for _, arg := range args {

0 commit comments

Comments
 (0)