-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
214 lines (157 loc) · 5.71 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#' Check whether "INDEX_RC" provided is present in KOSTRA-2010R data set
#'
#' @param idx A string representing the grid cell index.
#'
#' @return A boolean.
#' @export
#'
#' @examples
#' \dontrun{
#' idx_exists("49011")
#' }
idx_exists <- function(idx = NULL) {
# input validation -----------------------------------------------------------
checkmate::assert_character(idx, len = 1, min.chars = 1, max.chars = 6)
# main -----------------------------------------------------------------------
# get file names
files <- list.files(system.file(package = "kostra2010R"),
pattern = "*.shp",
full.names = TRUE,
recursive = TRUE
)
# read shapefile as sf
shp <- sf::st_read(files[1], quiet = TRUE)
# return boolean
idx %in% shp[["INDEX_RC"]]
}
#' Construct "INDEX_RC" based on given X and Y information
#'
#' @param col Integer {1:79}
#' @param row Integer {1:107}
#'
#' @return A string containing the unique representation of the relevant
#' "INDEX_RC" field.
#' @export
#'
#' @examples
#' \dontrun{
#' idx_build(11, 49)
#' }
idx_build <- function(col = NULL, row = NULL) {
# debugging ------------------------------------------------------------------
# col <- 11
# row <- 49
# input validation -----------------------------------------------------------
checkmate::assert_numeric(col, len = 1, lower = 0, upper = 78)
checkmate::assert_numeric(row, len = 1, lower = 0, upper = 106)
# main -----------------------------------------------------------------------
# line number multiplied by 1000 plus column number
as.character(row * 1000 + col)
}
#' Construct an geometry set object of type POINT based on user input
#'
#' @param input Vector of length 2 containing numeric representing coordinates,
#' or string of length 1 representing the name of a municipality,
#' or string of length 5 representing a postal zip code.
#' @param crs (optional) Coordinate reference system definition.
#'
#' @return An object of type `sfc_POINT`.
#' @export
#'
#' @examples
#' \dontrun{
#' p1 <- get_centroid(input = c(367773, 5703579))
#' p2 <- get_centroid(input = c(6.09, 50.46), crs = 4326)
#' p3 <- get_centroid(input = "Aachen")
#' p4 <- get_centroid(input = "52070")
#' }
get_centroid <- function(input,
crs = 25832) {
# vector of length 2 containing numeric representing coordinates
if (inherits(input, "numeric") && length(input) == 2) {
sf::st_point(input) |> sf::st_sfc(crs = crs)
# string of length 1 representing the name of a municipality
} else if (inherits(input, "character") && length(input) == 1 && as.numeric(input) |> suppressWarnings() |> is.na()) {
vg250_gem_centroids <- NULL
GEN <- NULL
system.file("data/vg250_gem_centroids.rda", package="kostra2010R") |> load()
vg250_gem_centroids |> dplyr::filter(GEN == input) |> sf::st_geometry()
# string of length 5 representing a postal zip code
} else if (inherits(input, "character") && length(input) == 1 && as.numeric(input) |> is.numeric()) {
osm_plz <- NULL
plz <- NULL
system.file("data/osm_plz.rda", package="kostra2010R") |> load()
osm_plz |> dplyr::filter(plz == input) |> sf::st_geometry()
}
}
#' Get index of KOSTRA-2010R cell by means of intersection with given location
#'
#' @param location An object of type `sfc_POINT`.
#'
#' @return A string containing the unique representation of the relevant
#' "INDEX_RC" field.
#' @export
#'
#' @examples
#' \dontrun{
#' get_idx(p)
#' }
get_idx <- function(location = NULL) {
# input validation -----------------------------------------------------------
checkmate::assert_class(location, "sfc_POINT")
# main -----------------------------------------------------------------------
# get file names
files <- list.files(system.file(package = "kostra2010R"),
pattern = "*.shp",
full.names = TRUE,
recursive = TRUE
)
# reproject sf point to target crs of the data set
location <- sf::st_transform(location, 3034)
# read shapefile
shp <- sf::st_read(files[1], quiet = TRUE)
# determine index based on topology relation: intersect
ind <- lengths(sf::st_intersects(shp, location)) > 0
# returns index of relevant grid
shp[["INDEX_RC"]][ind] |> as.character()
}
#' Convert precipitation depth in precipitation yield as a function of duration.
#'
#' @param hn Precipitation depth in mm.
#' @param d Duration in minutes.
#'
#' @return Precipitation yield in l/(s*ha).
#' @export
#'
#' @examples as_yield(hn = 45.7, d = 60)
as_yield <- function(hn = NULL,
d = NULL) {
# debugging ------------------------------------------------------------------
# hn <- 45.7
# d <- 60
# input validation -----------------------------------------------------------
checkmate::assert_numeric(hn, len = 1)
checkmate::assert_numeric(d, len = 1)
# main -----------------------------------------------------------------------
(as.numeric(hn) * 10000 / 60 / d) |> round(1) |> units::as_units("l/(s*ha)")
}
#' Convert precipitation yield in precipitation depth as a function of duration.
#'
#' @param rn Precipitation yield in l/(s*ha).
#' @param d Duration in minutes.
#'
#' @return Precipitation depth in mm.
#' @export
#'
#' @examples as_depth(rn = 126.94, d = 60)
as_depth <- function(rn = NULL,
d = NULL) {
# debugging ------------------------------------------------------------------
# rn <- 126.94
# d <- 60
# input validation -----------------------------------------------------------
checkmate::assert_numeric(rn, len = 1)
checkmate::assert_numeric(d, len = 1)
# main -----------------------------------------------------------------------
(as.numeric(rn) / 10000 * 60 * d) |> round(1) |> units::as_units("mm")
}