2323
2424logger :: 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-
3126cache_dir <- Sys.getenv(" HFSUBSET_API_CACHE_DIR" , " UNSET" )
3227if (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
0 commit comments