Skip to content

Commit 883bebb

Browse files
committed
Get rid of "sealed", "area_main", "area_road"
1 parent c79fe5d commit 883bebb

12 files changed

Lines changed: 56 additions & 92 deletions

R/distribute_measures.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ distribute_measures <- function(blocks, targets, intermediates = FALSE)
3838
blocks$pvd <- unpaved_area_table$pvd
3939
blocks$to_swale <- swale_connection_table$to_swale
4040

41-
# Recalculate "sealed"
42-
blocks$sealed <- get_sealed(blocks)
43-
4441
# Return the tables with intermediate values as attributes, if requested
4542
if (intermediates) {
4643
return(structure(
@@ -153,7 +150,8 @@ get_paved_area <- function(blocks)
153150
# get_sealed_area --------------------------------------------------------------
154151
get_sealed_area <- function(blocks)
155152
{
156-
select_columns(blocks, "sealed") * get_main_area(blocks)
153+
(select_columns(blocks, "roof") + select_columns(blocks, "pvd")) *
154+
get_main_area(blocks)
157155
}
158156

159157
# get_swale_connection_table ---------------------------------------------------

R/generate_rabimo_area.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ generate_rabimo_area <- function(code, ..., column_info = read_column_info())
3131
key_value_pairs[is_integer] <- lapply(key_value_pairs[is_integer], as.integer)
3232

3333
# Compose a one-row data frame from the key-value pairs
34-
result <- kwb.utils::callWith(data.frame, key_value_pairs, ...)
34+
further_args <- list(...)
35+
#further_args <- list()
36+
args <- key_value_pairs
37+
args[names(further_args)] <- further_args
3538

36-
# Add columns "code", "main_frac"
37-
fetch <- create_accessor(result)
39+
result <- do.call(data.frame, args)
40+
41+
# Add column "code"
3842
result["code"] <- code
39-
result["main_frac"] <- round(fetch("area_main")/fetch("total_area") , 2L)
4043
result
4144
}

R/prepare_input_data.R

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
prepare_input_data <- function(data, config, dbg = TRUE)
1818
{
1919
#kwb.utils::assignPackageObjects("kwb.rabimo")
20+
21+
#data <- sf::read_sf("~/../Downloads/raw_combined.gpkg")
22+
#data$geom <- NULL
23+
#data <- as.data.frame(data)
24+
2025
#data <- kwb.abimo::abimo_input_2019
2126
#data <- berlin_2020_data
2227
#data <- kwb.utils:::get_cached("berlin_2020_data")
@@ -57,7 +62,7 @@ prepare_input_data <- function(data, config, dbg = TRUE)
5762
data$berlin_usage[is_road] <- 300L
5863

5964
# Copy district information into the correct column (not needed anymore)
60-
if("BEZIRK_1" %in% names(data)){
65+
if ("BEZIRK_1" %in% names(data)) {
6166
stopifnot(identical(data$BEZIRK_1, data$district))
6267
}
6368
# data$district[is_road] <- select_columns(data, "BEZIRK_1")[is_road]
@@ -92,11 +97,20 @@ prepare_input_data <- function(data, config, dbg = TRUE)
9297
data[["prec_yr"]] <- fetch_data("prec_yr") *
9398
fetch_config("precipitation_correction_factor")
9499

95-
# Calculate total area
96-
data[["total_area"]] <- fetch_data("area_main") + fetch_data("area_road")
97-
98100
# Convert percentages to fractions
99-
data <- calculate_fractions(data)
101+
data[["total_area"]] <- fetch_data("FLGES") + fetch_data("STR_FLGES")
102+
103+
# Transform percentage to fractions
104+
data[["main_frac"]] <- fetch_data("FLGES") / data[["total_area"]]
105+
data[["road_frac"]] <- fetch_data("STR_FLGES") / data[["total_area"]]
106+
107+
# Determine names of columns that need to be divided by 100
108+
columns_to_divide_by_100 <- read_column_info() %>%
109+
dplyr::filter(.data[["by_100"]] == "x") %>%
110+
select_columns("rabimo_berlin") %>%
111+
intersect(names(data))
112+
113+
data[columns_to_divide_by_100] <- lapply(data[columns_to_divide_by_100], `/`, 100)
100114

101115
# insert empty to_swale column (fraction of the area connected to a swale)
102116
data[["to_swale"]] <- 0
@@ -169,7 +183,7 @@ identify_data_format_or_stop <- function(data)
169183
# get_column_renamings ---------------------------------------------------------
170184
get_column_renamings <- function()
171185
{
172-
read_column_info() %>%
186+
renamings <- read_column_info() %>%
173187
dplyr::filter(nzchar(.data[["abimo_berlin"]])) %>%
174188
to_lookup_list(data = select_columns(., c("abimo_berlin", "rabimo_berlin")))
175189
}
@@ -194,31 +208,6 @@ read_column_info <- function()
194208
)
195209
}
196210

197-
# calculate_fractions ----------------------------------------------------------
198-
calculate_fractions <- function(data)
199-
{
200-
# Column accessor
201-
fetch_data <- create_accessor(data)
202-
203-
total_area <- fetch_data("total_area")
204-
205-
# Transform percentage to fractions
206-
data[["main_frac"]] <- fetch_data("area_main") / total_area
207-
data[["road_frac"]] <- fetch_data("area_road") / total_area
208-
209-
# Determine names of columns that need to be divided by 100
210-
columns <- read_column_info() %>%
211-
dplyr::filter(.data[["by_100"]] == "x") %>%
212-
select_columns("rabimo_berlin") %>%
213-
intersect(names(data))
214-
215-
for (column in columns) {
216-
data[[column]] <- fetch_data(column) / 100
217-
}
218-
219-
data
220-
}
221-
222211
# get_usage_tuple --------------------------------------------------------------
223212

224213
#' Get Usage Tuple (Land_type, Veg_class, Irrigation) from NUTZUNG and TYP

R/run_rabimo_with_measures.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ run_rabimo_with_measures <- function(
1414
config = kwb.rabimo::rabimo_inputs_2020$config
1515
)
1616
{
17+
#kwb.utils::assignPackageObjects("kwb.rabimo")
1718
rescaled_targets <- rescale_target_values(
1819
new_targets = measures,
1920
blocks = blocks

data/rabimo_inputs_2020.rda

-496 KB
Binary file not shown.

inst/extdata/column-names.csv

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ epot_yr,,,Long-term average potential evapotranspiration per year,mm,required,in
66
epot_s,,,Long-term average potential evapotranspiration per summer half year (May to October),mm,required,integer,500
77
district,BEZIRK,,Specific to Berlin: identifier of city district,-,required?,character,0
88
total_area,,,Total block area,m2,required,numeric,100
9-
area_main,FLGES,,Total non-road area within the block,m2,required?,numeric,100
10-
area_road,STR_FLGES,,Total road area within the block,m2,required?,numeric,0.0
11-
main_frac,,,Non-road fraction of total block area (area_main/total_area),0..1,required,numeric,
9+
main_frac,,,Non-road fraction of total_area,0..1,required,numeric,1.0
1210
roof,PROBAU,x,Roof fraction of non-road built area,0..1,required,numeric,0.2
1311
green_roof,,,Green roof fraction of roof area,0..1,required,numeric,0.0
1412
swg_roof,KAN_BEB,x,Fraction of roof area connected to the sewer ,0..1,required,numeric,1.0
@@ -19,7 +17,7 @@ srf2_pvd,BELAG2,x,Fraction of paved area belonging to surface class 2,0..1,requi
1917
srf3_pvd,BELAG3,x,Fraction of paved area belonging to surface class 3,0..1,required,numeric,0.1
2018
srf4_pvd,BELAG4,x,Fraction of paved area belonging to surface class 4,0..1,required,numeric,0.1
2119
srf5_pvd,BELAG5,x,Fraction of paved area belonging to surface class 5,0..1,required,numeric,0.1
22-
road_frac,,,Road fraction of total block area (area_road/total_area),0..1,required,numeric,0.0
20+
road_frac,,,Road fraction of total_area,0..1,required,numeric,0.0
2321
pvd_r,VGSTRASSE,x,Paved fraction of road area,0..1,required,numeric,0.9
2422
swg_pvd_r,KAN_STR,x,Fraction of paved road area connected to the sewer,0..1,required,numeric,1.0
2523
srf1_pvd_r,STR_BELAG1,x,Fraction of road area belonging to surface class 1,0..1,required,numeric,0.9
@@ -31,7 +29,7 @@ gw_dist,FLUR,,Depth to the water table,m,required,numeric,3.0
3129
ufc30,FELD_30,,Usable field capacity 0..30 cm,% by volume,required,numeric,13.0
3230
ufc150,FELD_150,,Usable field capacity 0..150 cm,% by volume,required,numeric,13.0
3331
land_type,,,Land type,-,required,character,urban
34-
veg_class,,,Vegetation class,-,required,integer,35.0
35-
irrigation,,,Average irrigation per year,mm,required,integer,0.0
32+
veg_class,,,Vegetation class,-,required,numeric,35.0
33+
irrigation,,,Average irrigation per year,mm,required,integer,0
3634
berlin_usage,NUTZUNG,,Specific to Berlin: Main usage of block,-,berlin-specific,numeric,
3735
berlin_type,TYP,,Specific to Berlin: Type of block,-,berlin-specific,numeric,

inst/scripts/distribute-measures.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ create_example_blocks <- function(block_areas)
128128
blocks <- do.call(rbind, lapply(codes, kwb.rabimo:::generate_rabimo_area))
129129

130130
blocks$total_area <- total_areas
131-
blocks$area_main <- total_areas
132131
blocks$green_roof <- get_green_roof(block_areas)
133132
blocks$pvd <- get_paved(block_areas)
134133

inst/scripts/test-rabimo.R

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,15 @@ if (FALSE)
9494
#kwb.utils::hsOpenWindowsExplorer(get_path("results"))
9595
}
9696

97-
# MAIN: Convert column names (max 10 chars) in input data ----------------------
97+
# MAIN: Update data stored in the package --------------------------------------
9898
if (FALSE)
9999
{
100100
inputs <- kwb.rabimo::rabimo_inputs_2020
101-
inputs$data <- kwb.utils::renameColumns(inputs$data, list(
102-
area_rd = "area_road",
103-
main_fraction = "main_frac",
104-
road_fraction = "road_frac",
105-
pvd_rd = "pvd_r",
106-
swg_pvd_rd = "swg_pvd_r",
107-
srf1_pvd_rd = "srf1_pvd_r",
108-
srf2_pvd_rd = "srf2_pvd_r",
109-
srf3_pvd_rd = "srf3_pvd_r",
110-
srf4_pvd_rd = "srf4_pvd_r"
101+
str(inputs$data)
102+
inputs$data$veg_class <- as.numeric(inputs$data$veg_class)
103+
inputs$data <- kwb.utils::removeColumns(inputs$data, c(
104+
"sealed", "area_main", "area_road"
111105
))
112-
113-
names_1 <- names(inputs$data)
114-
names_1 <- setdiff(names_1, "block_type")
115-
116-
names_2 <- kwb.rabimo::read_column_info()$rabimo_berlin
117-
names_2 <- setdiff(names_2, c("berlin_usage", "berlin_type"))
118-
119-
stopifnot(all(names_1 %in% names_2))
120-
121106
rabimo_inputs_2020 <- inputs
122107
usethis::use_data(rabimo_inputs_2020, overwrite = TRUE)
123108
}

tests/testthat/test-function-calculate_fractions.R

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/testthat/test-function-prepare_berlin_inputs.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,31 @@ test_that("prepare_berlin_inputs() works", {
77
expect_error(f())
88

99
data_file <- tempfile(fileext = ".dbf")
10-
1110
foreign::write.dbf(kwb.abimo::abimo_input_2019, data_file)
1211

1312
result <- f(data_file, dbg = FALSE)
1413

1514
expect_type(result, "list")
16-
1715
expect_identical(names(result), c("data", "config"))
1816

1917
expect_identical(names(result$data), c(
20-
"code", "prec_yr", "prec_s", "epot_yr", "epot_s", "district", "total_area",
21-
"area_main", "area_road", "main_frac", "roof", "green_roof", "swg_roof",
18+
"code",
19+
"prec_yr", "prec_s",
20+
"epot_yr", "epot_s",
21+
"district",
22+
"total_area",
23+
24+
# main
25+
"main_frac",
26+
"roof", "green_roof", "swg_roof",
2227
"pvd", "swg_pvd",
2328
"srf1_pvd", "srf2_pvd", "srf3_pvd", "srf4_pvd", "srf5_pvd",
24-
"road_frac", "pvd_r", "swg_pvd_r", "srf1_pvd_r", "srf2_pvd_r",
25-
"srf3_pvd_r", "srf4_pvd_r", "to_swale", "gw_dist", "ufc30",
29+
30+
# road
31+
"road_frac", "pvd_r", "swg_pvd_r",
32+
"srf1_pvd_r", "srf2_pvd_r", "srf3_pvd_r", "srf4_pvd_r",
33+
34+
"to_swale", "gw_dist", "ufc30",
2635
"ufc150", "land_type", "veg_class", "irrigation", "block_type"
2736
))
2837

0 commit comments

Comments
 (0)