Skip to content

Commit 74919f3

Browse files
committed
Move get_measure_stats_new() into the package
and rename it to get_measure_stats()
1 parent 8ca2299 commit 74919f3

5 files changed

Lines changed: 113 additions & 79 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(data_to_natural)
99
export(define_controls)
1010
export(distribute_measures)
1111
export(generate_rabimo_area)
12+
export(get_measure_stats)
1213
export(get_potential_evaporation)
1314
export(get_soil_properties)
1415
export(get_usage_tuple)

R/get_measure_stats.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# get_measure_stats ------------------------------------------------------------
2+
3+
#' Get Mean/Max Statistics on Measures
4+
#'
5+
#' @param blocks data frame similar to \code{\link{rabimo_inputs_2020}$data},
6+
#' with each row representing a block area
7+
#' @param reference_system indicator for the "reference system" in which the
8+
#' returned values are to be given. 1: all values refer to percentages of
9+
#' specific areas (green roof: roof area, unsealed: total area, to_swale:
10+
#' sealed area); 2: all values refer to percentages of the total area. The
11+
#' default is 2.
12+
#' @returns list with elements "mean" and "max" each of which is a list with one
13+
#' element per measure
14+
#' @importFrom kwb.utils createAccessor
15+
#' @export
16+
get_measure_stats <- function(blocks, reference_system = 2)
17+
{
18+
stopifnot(reference_system %in% 1:2)
19+
20+
refer_to_total <- reference_system == 2
21+
22+
#blocks <- kwb.rabimo::rabimo_inputs_2020$data
23+
get <- kwb.utils::createAccessor(blocks)
24+
25+
total_areas <- get("total_area")
26+
roofs <- get("roof")
27+
pvds <- get("pvd")
28+
29+
area_total <- sum(total_areas)
30+
area_roof <- sum(total_areas * roofs)
31+
area_green_roof <- sum(total_areas * roofs * get("green_roof"))
32+
area_pvd <- sum(total_areas * pvds)
33+
area_unpvd <- sum(total_areas * (1 - roofs - pvds))
34+
area_sca <- sum(total_areas * (roofs + pvds) * get("to_swale"))
35+
area_sealed <- area_roof + area_pvd
36+
37+
mean_roof <- area_roof / area_total
38+
mean_pvd <- area_pvd / area_total
39+
40+
mean_green_roof <- if (refer_to_total) {
41+
area_green_roof / area_total
42+
} else {
43+
area_green_roof / area_roof
44+
}
45+
46+
mean_unpvd <- area_unpvd / area_total
47+
48+
mean_sca <- if (refer_to_total) {
49+
area_sca / area_total
50+
} else {
51+
area_sca / area_sealed
52+
}
53+
54+
max_green_roof <- if (refer_to_total) mean_roof else 1
55+
max_unpvd <- 1 - mean_roof
56+
max_sca <- if (refer_to_total) {
57+
1 - mean_unpvd
58+
} else {
59+
1
60+
}
61+
62+
list(
63+
green_roof = c(mean = mean_green_roof, max = max_green_roof),
64+
unpaved = c(mean = mean_unpvd, max = max_unpvd),
65+
to_swale = c(mean = mean_sca, max = max_sca)
66+
)
67+
}
68+

inst/scripts/distribute-measures.R

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ if (FALSE)
7878
# Working with targets in the "new" reference system ---------------------------
7979
if (FALSE)
8080
{
81-
# Statistics on measure implementation in new reference system (share of total
82-
# area)
83-
get_measure_stats_new(blocks)
81+
# Statistics on measures in new reference system (shares of total area)
82+
kwb.rabimo::get_measure_stats(blocks, reference_system = 2)
8483

85-
# Convert new targets (all percentages referring to total area) to targets
86-
# that are expected by distribute_measures()
84+
# Define targets in new reference system (all shares referring to total area)
8785
new_targets <- list(green_roof = 0.259, unpaved = 0.5, to_swale = 0.67)
8886

87+
# Convert targets in new reference system to targets in old reference system
88+
# that is expected by distribute_measures(). In the old system, the shares
89+
# relate to areas that are specific to the measure (e.g. green_roof is related
90+
# to the area of roofs, to swale is related to the area of sealed surfaces).
8991
targets <- rescale_target_values(
9092
new_targets,
9193
total_area = sum(kwb.rabimo:::get_main_area(blocks)),
@@ -358,78 +360,6 @@ rescale_target_values <- function(
358360
list(green_roof = green_roof, unpaved = unpaved, to_swale = to_swale)
359361
}
360362

361-
# get_measure_stats_new --------------------------------------------------------
362-
get_measure_stats_new <- function(blocks, refer_to_total = TRUE)
363-
{
364-
#blocks <- kwb.rabimo::rabimo_inputs_2020$data
365-
get <- kwb.utils::createAccessor(blocks)
366-
367-
total_areas <- get("total_area")
368-
roofs <- get("roof")
369-
pvds <- get("pvd")
370-
371-
area_total <- sum(total_areas)
372-
area_roof <- sum(total_areas * roofs)
373-
area_green_roof <- sum(total_areas * roofs * get("green_roof"))
374-
area_pvd <- sum(total_areas * pvds)
375-
area_unpvd <- sum(total_areas * (1 - roofs - pvds))
376-
area_sca <- sum(total_areas * (roofs + pvds) * get("to_swale"))
377-
area_sealed <- area_roof + area_pvd
378-
379-
mean_roof <- area_roof / area_total
380-
mean_pvd <- area_pvd / area_total
381-
382-
mean_green_roof <- if (refer_to_total) {
383-
area_green_roof / area_total
384-
} else {
385-
area_green_roof / area_roof
386-
}
387-
388-
mean_unpvd <- area_unpvd / area_total
389-
390-
mean_sca <- if (refer_to_total) {
391-
area_sca / area_total
392-
} else {
393-
area_sca / area_sealed
394-
}
395-
396-
max_green_roof <- if (refer_to_total) mean_roof else 1
397-
max_unpvd <- 1 - mean_roof
398-
max_sca <- if (refer_to_total) {
399-
1 - mean_unpvd
400-
} else {
401-
1
402-
}
403-
404-
# area_total <- sum(kwb.rabimo:::get_main_area(blocks))
405-
# area_green_roof <- sum(kwb.rabimo:::get_green_roof_area(blocks))
406-
# area_roof <- sum(kwb.rabimo:::get_roof_area(blocks))
407-
# area_unpvd <- sum(kwb.rabimo:::get_unpaved_area(blocks))
408-
# area_pvd <- sum(kwb.rabimo:::get_paved_area(blocks))
409-
# area_sca <- sum(kwb.rabimo:::get_to_swale_area(blocks))
410-
# area_sealed <- sum(kwb.rabimo:::get_sealed_area(blocks))
411-
#
412-
# mean_green_roof <- area_green_roof / area_total
413-
# max_green_roof <- area_roof / area_total
414-
# mean_unpvd <- area_unpvd / area_total
415-
# max_unpvd <- area_pvd / area_total
416-
# mean_sca <- area_sca / area_total
417-
# max_sca <- area_sealed / area_total
418-
419-
cbind(
420-
mean = c(
421-
green_roof = mean_green_roof,
422-
unpaved = mean_unpvd,
423-
to_swale = mean_sca
424-
),
425-
max = c(
426-
green_roof = max_green_roof,
427-
unpaved = max_unpvd,
428-
to_swale = max_sca
429-
)
430-
)
431-
}
432-
433363
# check_distribution -----------------------------------------------------------
434364
check_distribution <- function(
435365
green_roof_table,

inst/scripts/plumber.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ function(req, n_records = 3L, seed = as.integer(Sys.time()), output_only = TRUE)
8686
#* @post data_to_natural
8787
function(data_json, type = "undeveloped")
8888
{
89-
data <-
90-
9189
data <- kwb.rabimo:::check_or_convert_data_types(
9290
data = jsonlite::fromJSON(data_json),
9391
types = kwb.rabimo:::get_expected_data_type(),
@@ -130,3 +128,15 @@ function()
130128

131129
config
132130
}
131+
132+
# /get_measure_stats -----------------------------------------------------------
133+
134+
#* Statistics (mean, max) on measures within selected blocks
135+
#* @param blocks_json Selected blocks
136+
#* @param reference_system "Reference system" (1:old, 2:new = percentages of total area)
137+
#* @post get_measure_stats
138+
function(req, blocks_json, reference_system = 2)
139+
{
140+
blocks <- jsonlite::fromJSON(blocks_json)
141+
kwb.rabimo::get_measure_stats(blocks, reference_system)
142+
}

man/get_measure_stats.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)