Skip to content

Commit 92e2ec8

Browse files
authored
Merge pull request #539 from HARPgroup/parent
added function to allow raster ts summary for any feature
2 parents d7c8ffd + c3e9dec commit 92e2ec8

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

R/RomFeature.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,94 @@ RomFeature <- R6Class(
219219
related_entities <- related_entities[,retcols]
220220
}
221221
return(related_entities)
222+
},
223+
#' @param varkey what variable to retrieve
224+
#' @param starttime begin tstime (default FALSE)
225+
#' @param endtime last tsendtime (default FALSE)
226+
#' Other options are 'overlaps' or 'st_within'
227+
#' @param band which band to query (default = 1)
228+
#' @param aggregate FALSE will return 1 record for every date
229+
#' @param metric default mean, which aspect of the ST_SUMMARIZE() to return
230+
#' @return dataframe of spatially related entities
231+
get_raster_ts = function(
232+
varkey = 'prism_mod_daily',
233+
starttime = FALSE,
234+
endtime = '2025-10-13',
235+
band = '1',
236+
aggregate = FALSE,
237+
metric = 'mean'
238+
) {
239+
# looks at the table dh_timeseries_weather
240+
# coverage raster summary
241+
ftype = self$ftype
242+
bundle = self$bundle
243+
hydrocode = self$hydrocode
244+
startclause = "(1 = 1)"
245+
endclause = "(1 = 1)"
246+
if (!is.logical(starttime)) {
247+
startclause = fn$paste0("met.tstime >= extract(epoch from '$starttime'::date)")
248+
}
249+
if (!is.logical(endtime)) {
250+
endclause = fn$paste0("met.tsendtime <= extract(epoch from '$endtime'::date)")
251+
}
252+
if (!is.logical(aggregate)) {
253+
rastercalc = fn$paste0("$aggregate((ST_summarystats(st_clip(met.rast, fgeo.dh_geofield_geom), 1, TRUE)).$metric)")
254+
groupby = "GROUP BY met.featureid"
255+
startcol = "to_timestamp(min(met.tsendtime))"
256+
endcol = "to_timestamp(max(met.tsendtime))"
257+
} else {
258+
rastercalc = fn$paste0("(ST_summarystats(st_clip(met.rast, fgeo.dh_geofield_geom), 1, TRUE)).$metric")
259+
groupby = ""
260+
startcol = "to_timestamp(met.tsendtime)"
261+
endcol = "to_timestamp(met.tsendtime)"
262+
}
263+
sql = fn$paste0(
264+
"WITH feature_coverages AS (
265+
SELECT *
266+
FROM dh_feature
267+
WHERE hydrocode = '$hydrocode'
268+
AND ftype = '$ftype'
269+
AND bundle = '$bundle'
270+
),
271+
metUnion as (
272+
Select met.featureid, met.tsendtime,
273+
st_union(met.rast) as rast
274+
FROM feature_coverages as f
275+
left outer join field_data_dh_geofield as fgeo
276+
on (
277+
fgeo.entity_id = f.hydroid
278+
and fgeo.entity_type = 'dh_feature'
279+
)
280+
JOIN(
281+
select *
282+
from dh_timeseries_weather as met
283+
left outer join dh_variabledefinition as b
284+
on (met.varid = b.hydroid)
285+
WHERE $startclause
286+
AND $endclause
287+
and b.varkey='$varkey'
288+
) AS met
289+
ON ST_Intersects(ST_ConvexHull(met.rast),fgeo.dh_geofield_geom)
290+
291+
group by met.featureid, met.tsendtime
292+
)
293+
SELECT met.featureid,
294+
$startcol as start_date,
295+
$endcol as end_date,
296+
$rastercalc as value
297+
FROM feature_coverages as f
298+
left outer join field_data_dh_geofield as fgeo
299+
on (
300+
fgeo.entity_id = f.hydroid
301+
and fgeo.entity_type = 'dh_feature'
302+
)
303+
JOIN metUnion AS met
304+
ON ST_Intersects(ST_ConvexHull(met.rast),fgeo.dh_geofield_geom)
305+
$groupby"
306+
)
307+
# to debug set ds$debug = TRUE instead of message(sql)
308+
raster_records <- dbGetQuery(conn = self$datasource$connection, sql)
309+
return(raster_records)
222310
}
223311
)
224312
)

0 commit comments

Comments
 (0)