Skip to content

Commit ac5836b

Browse files
committed
Use "safety factor" to diminish max values
1 parent 906b15e commit ac5836b

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

inst/scripts/plumber.R

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# /example_data ----------------------------------------------------------------
44

55
#* Example data for Abimo (Berlin, 2019)
6-
#* @param n_records number of records (= input rows = "Blockteilflaechen").
7-
#* @param seed seed value for the random number generator used to randomly select rows
8-
#* @param output_only whether to return only the data frame with example data (true, the default) or a list with inputs and output (false).
9-
#* @post /example_data
10-
function(req, n_records = 3L, seed = as.integer(Sys.time()), output_only = TRUE)
6+
#* @param n_records:int number of records (= input rows = "Blockteilflaechen").
7+
#* @param seed seed:int value for the random number generator used to randomly select rows
8+
#* @param output_only:logical whether to return only the data frame with example data (true) or a list with inputs and output (false).
9+
#* @get /example_data
10+
function(n_records = 3L, seed = as.integer(Sys.time()), output_only = TRUE)
1111
{
1212
n_records <- as.integer(n_records)
1313
seed <- as.integer(seed)
@@ -38,28 +38,33 @@ function(req, n_records = 3L, seed = as.integer(Sys.time()), output_only = TRUE)
3838
# /get_measure_stats -----------------------------------------------------------
3939

4040
#* Statistics (mean, max) on measures within selected blocks
41-
#* @param blocks_json Selected blocks
42-
#* @param reference_system "Reference system" (1:old, 2:new = percentages of total area)
41+
#* @param blocks:data.frame Selected blocks
42+
#* @param max_only:logical whether or not to return only the "max" (and not also the "mean") values
43+
#* @param reference_system:int "Reference system" (1:old, 2:new = percentages of total area)
44+
#* @param safety_factor:float Factor that the "max" values are multiplied with to make them a bit smaller
4345
#* @serializer unboxedJSON
4446
#* @post get_measure_stats
45-
function(req, blocks_json, reference_system = 2)
47+
function(blocks = data.frame(), max_only = TRUE, reference_system = 2L, safety_factor = 0.999)
4648
{
47-
blocks <- jsonlite::fromJSON(blocks_json)
48-
kwb.rabimo::get_measure_stats(blocks, reference_system)
49+
stats <- kwb.rabimo::get_measure_stats(blocks, reference_system)
50+
51+
if (max_only) {
52+
lapply(lapply(stats, `[[`, "max"), `*`, as.numeric(safety_factor))
53+
} else {
54+
stats
55+
}
4956
}
5057

5158
# /run_rabimo ------------------------------------------------------------------
5259

5360
#' Run R-Abimo with data and config (optional)
54-
#* @param data_json input data as json string, as returned by /example_data in "output"
61+
#* @param data:data.frame input data as json string, as returned by /example_data in "output"
5562
#* @param measures_json Optional. Target values of measures, as json string, e.g. '{"green_roof":0.1, "unpaved":0.2, "to_swale":0.3}'
5663
#* @param config_json Optional. Configuration as json string, as returned by /default_config
5764
#* @serializer unboxedJSON
5865
#* @post /run_rabimo
59-
function(req, data_json, measures_json = "", config_json = "")
66+
function(data = data.frame(), measures_json = "", config_json = "")
6067
{
61-
data <- jsonlite::fromJSON(data_json)
62-
6368
# Convert json string to data frame and set all columns to expected types
6469
data <- kwb.rabimo:::check_or_convert_data_types(
6570
data = data,
@@ -93,6 +98,7 @@ function(req, data_json, measures_json = "", config_json = "")
9398

9499
list(
95100
data = if (failed) NULL else output,
101+
measures = measures,
96102
weighted_means = if (failed) NULL else {
97103
areas <- output$area
98104
cols <- 3:5

0 commit comments

Comments
 (0)