-
Notifications
You must be signed in to change notification settings - Fork 0
/
rain_garden_driveway.R
439 lines (269 loc) · 12.4 KB
/
rain_garden_driveway.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#' Rain Garden Sizing for Driveways
#'
#' This function computes the final rain garden dimensions based on the size of
#' the impervious roof surfaces and the initial rain garden size. This function
#' uses the \code{\link{surface_area}} values and units in the calculations.
#'
#' @param dw_length numeric vector containing the length value(s) in one of the
#' lw_units values for the driveway(s).
#' @param dw_width numeric vector containing the width value(s) in one of the
#' lw_units values for the driveway(s).
#' @param driveway_table data.frame/data.table/tibble, list, or matrix
#' containing the length in column 1 and the width in column 2 for the
#' driveway(s).
#' @param rf_length numeric vector containing the length value(s) in one of the
#' lw_units values for the roof(s).
#' @param rf_width numeric vector containing the width value(s) in one of the
#' lw_units values for the roof(s).
#' @param roof_table data.frame/data.table/tibble, list, or matrix
#' containing the length in column 1 and the width in column 2 for the
#' roof(s).
#' @param lw_units character vector containing the units for the length and the
#' width (default = "feet"). The other possible units are "inch",
#' "survey_foot", "yard", "mile", "centimeter", "meter", or "kilometer". The
#' units should be consistent and not mixed.
#' @param rainfall_depth numeric vector containing the rainfall depth in one of
#' the rainfall_depth_units.
#' @param rainfall_depth_units character vector containing the units for the
#' rainfall depth (default = "feet"). The other possible units are "inch",
#' "centimeter", or "meter".
#' @param rain_garden_depth numeric vector containing the rain garden depth in
#' one of the rain_garden_depth_units.
#' @param rain_garden_depth_units character vector containing the units for the
#' rain garden depth (default = "feet"). The other possible units are "inch",
#' "centimeter", or "meter".
#'
#' @return a \code{\link[data.table]{data.table}} with the following columns:
#' Driveway Drainage Area, One-Quarter of Roof Area, Depth of Rain, Design
#' Storm Volume, Rain Garden Depth, Rain Garden Initial Size, Rain Garden
#' Initial Dimensions, Total Drainage Area, New Design Storm Volume, Rain
#' Garden Final Size, Rain Garden Final Dimensions
#'
#'
#'
#'
#' @seealso \code{\link{surface_area}} for calculating the linear surface area
#'
#'
#'
#'
#' @author Irucka Embry
#'
#'
#'
#' @encoding UTF-8
#'
#' @references
#' Green Infrastructure Champion Training: Part 7: "How To Design and Build a Rain Garden", April 10, 2019, pages 41 - 43 of the PDF document, \url{https://water.rutgers.edu/Projects/GreenInfrastructureChampions/Talks_2020/Part_7_04102020.pdf}.
#'
#'
#'
#' @examples
#'
#' # Note: the units must be consistent for the lengths and widths
#'
#' # Example 1 (from the Reference)
#'
#' library(iemisc)
#'
#' dw_width1 <- c(15, 10)
#' dw_length1 <- c(50, 25)
#' lw_units <- "feet"
#' rf_width1 <- 50
#' rf_length1 <- 25
#' rainfall_depth1 <- 1.5
#' rainfall_depth_units <- "inch"
#' rain_garden_depth <- 6
#' rain_garden_depth_units <- "inch"
#'
#' rain_garden_driveway(dw_length = dw_length1, dw_width = dw_width1, rf_length =
#' rf_length1, rf_width = rf_width1, lw_units = lw_units, rainfall_depth =
#' rainfall_depth1, rainfall_depth_units = rainfall_depth_units, rain_garden_depth
#' = rain_garden_depth, rain_garden_depth_units = rain_garden_depth_units)
#'
#'
#'
#' # Example 2
#' # from https://www.ecoccs.com/R_Examples/Simple-Rain-Garden-Sizing_with-R.html
#' # Irucka Embry modified the Example from the Reference for this example
#'
#' install.load::load_package("iemisc", "data.table")
#'
#' dw_length2 <- c(construction_decimal("50 feet 3 1/2 inch", result <- "traditional",
#' output <- "vector"), construction_decimal("25 feet 5 7/8 inch", result <- "traditional",
#' output <- "vector"))
#' dw_width2 <- c(construction_decimal("15 feet 10 3/4 inch", result <- "traditional",
#' output <- "vector"), construction_decimal("10 feet 7 3/8 inch", result <- "traditional",
#' output <- "vector"))
#' lw_units <- "feet"
#' rf_length2 <- construction_decimal("25 feet 10 1/4 inch", result <- "traditional",
#' output <- "vector")
#' rf_width2 <- construction_decimal("12.5 feet 1 1/8 inch", result <- "traditional",
#' output <- "vector") * 4
#' rainfall_depth2 <- 2.25
#' rainfall_depth_units <- "inch"
#' rain_garden_depth <- 6
#' rain_garden_depth_units <- "inch"
#'
#' driveway_table <- data.table(length = dw_length2, width = dw_width2)
#'
#' roof_table <- data.table(length = rf_length2, width = rf_width2)
#'
#' rain_garden_driveway(driveway_table = driveway_table, roof_table = roof_table,
#' lw_units = lw_units, rainfall_depth = rainfall_depth2, rainfall_depth_units =
#' rainfall_depth_units, rain_garden_depth = rain_garden_depth,
#' rain_garden_depth_units = rain_garden_depth_units)
#'
#'
#'
#' @importFrom data.table as.data.table
#' @importFrom units set_units make_units drop_units
#' @importFrom assertthat assert_that
#' @importFrom checkmate qtest testDataTable
#' @importFrom round round_r3
#'
#' @export
rain_garden_driveway <- function (dw_length = NULL, dw_width = NULL, rf_length = NULL, rf_width = NULL, driveway_table = NULL, roof_table = NULL, lw_units = c("inch", "feet", "survey_foot", "yard", "mile", "centimeter", "meter", "kilometer"), rainfall_depth, rainfall_depth_units = c("inch", "feet", "centimeter", "meter"), rain_garden_depth, rain_garden_depth_units = c("inch", "feet", "centimeter", "meter")) {
ft <- mi <- km <- US_survey_foot <- cm <- feet <- inch <- meter <- yd <- NULL
# due to NSE notes in R CMD check
# for the driveway
if (missing(dw_length) & missing(dw_width)) {
# convert data.frame/data.table/tibble, list, matrix to data.table & then to numeric vector
# assume column 1 is dw_length and column 2 is dw_width
driveway_table <- as.data.table(driveway_table)
assert_that(testDataTable(driveway_table, types = "numeric", any.missing = FALSE, all.missing = FALSE, min.rows = 1, min.cols = 1, ncols = 2), msg = "Any row of driveway_table contains a value that is NA, NaN, empty, or a string. Please try again.")
# only process with enough known length and width values and provide a stop warning if not enough
dw_length <- driveway_table[, 1][[1]]
dw_width <- driveway_table[, 2][[1]]
}
# for the roof
if (missing(rf_length) & missing(rf_width)) {
# convert data.frame/data.table/tibble, list, matrix to data.table & then to numeric vector
# assume column 1 is rf_length and column 2 is rf_width
roof_table <- as.data.table(roof_table)
assert_that(testDataTable(roof_table, types = "numeric", any.missing = FALSE, all.missing = FALSE, min.rows = 1, min.cols = 1, ncols = 2), msg = "Any row of roof_table contains a value that is NA, NaN, empty, or a string. Please try again.")
# only process with enough known length and width values and provide a stop warning if not enough
rf_length <- roof_table[, 1][[1]]
rf_width <- roof_table[, 2][[1]]
}
lw_units <- lw_units
# total drainage area (driveway only)
da <- surface_area(width = dw_width, length = dw_length, lw_units = lw_units)
# total roof area
rf <- surface_area(width = rf_width, length = rf_length, lw_units = lw_units)
if (lw_units == "feet") {
da <- da
} else if (lw_units == "inch") {
da <- set_units(da, inch) # in
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "survey_foot") {
da <- set_units(da, US_survey_foot) # US_survey_foot
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "yard") {
da <- set_units(da, yd) # yd
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "mile") {
da <- set_units(da, mi) # mi
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "centimeter") {
da <- set_units(da, cm) # cm
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "meter") {
da <- set_units(da, meter) # meter
units(da) <- make_units(feet) # feet
da <- drop_units(da)
} else if (lw_units == "kilometer") {
da <- set_units(da, km) # km
units(da) <- make_units(feet) # feet
da <- drop_units(da)
}
if (lw_units == "feet") {
rf <- rf
} else if (lw_units == "inch") {
rf <- set_units(rf, inch) # in
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "survey_foot") {
rf <- set_units(rf, US_survey_foot) # US_survey_foot
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "yard") {
rf <- set_units(rf, yd) # yd
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "mile") {
rf <- set_units(rf, mi) # mi
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "centimeter") {
rf <- set_units(rf, cm) # cm
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "meter") {
rf <- set_units(rf, meter) # meter
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
} else if (lw_units == "kilometer") {
rf <- set_units(rf, km) # km
units(rf) <- make_units(feet) # feet
rf <- drop_units(rf)
}
# 1/4 of total roof area
rf1_4 <- 0.25 * rf
# for the rainfall depth
if (rainfall_depth_units == "feet") {
rainfall_depth <- rainfall_depth
} else if (rainfall_depth_units == "inch") {
rainfall_depth <- set_units(rainfall_depth, inch) # in
units(rainfall_depth) <- make_units(feet) # feet
rainfall_depth <- drop_units(rainfall_depth)
} else if (rainfall_depth_units == "centimeter") {
rainfall_depth <- set_units(rainfall_depth, cm) # cm
units(rainfall_depth) <- make_units(feet) # feet
rainfall_depth <- drop_units(rainfall_depth)
} else if (rainfall_depth_units == "meter") {
rainfall_depth <- set_units(rainfall_depth, meter) # meter
units(rainfall_depth) <- make_units(feet) # feet
rainfall_depth <- drop_units(rainfall_depth)
}
# for the rain garden depth
if (rain_garden_depth_units == "feet") {
rain_garden_depth <- rain_garden_depth
} else if (rain_garden_depth_units == "inch") {
rain_garden_depth <- set_units(rain_garden_depth, inch) # in
units(rain_garden_depth) <- make_units(feet) # feet
rain_garden_depth <- drop_units(rain_garden_depth)
} else if (rain_garden_depth_units == "centimeter") {
rain_garden_depth <- set_units(rain_garden_depth, cm) # cm
units(rain_garden_depth) <- make_units(feet) # feet
rain_garden_depth <- drop_units(rain_garden_depth)
} else if (rain_garden_depth_units == "meter") {
rain_garden_depth <- set_units(rain_garden_depth, meter) # meter
units(rain_garden_depth) <- make_units(feet) # feet
rain_garden_depth <- drop_units(rain_garden_depth)
}
# initial volume of water for design storm
design_storm_volume <- da * rainfall_depth
# initial rain garden size
rg_size1 <- design_storm_volume / rain_garden_depth
rg_size1_length <- rg_size1 / 10
# new drainage area
da_new <- sum(da, rg_size1)
# final volume of water for design storm
design_storm_volume_new <- da_new * rainfall_depth
# final rain garden size
rg_size2 <- design_storm_volume_new / rain_garden_depth
rg_size2_length <- rg_size2 / 10
# Rain Garden Table
rain_garden_table <- data.table(`Driveway Drainage Area` = paste0(round_r3(da, d = 1), " sq. ft."), `One-Quarter of Roof Area` = paste0(round_r3(rf1_4, d = 1), " sq. ft."), `Depth of Rain` = paste0(round_r3(rainfall_depth, d = 3), " ft of rain"), `Design Storm Volume` = paste0(round_r3(design_storm_volume, d = 0), " ft^3"), `Rain Garden Depth` = paste0(round_r3(rain_garden_depth, d = 1), " feet"), `Rain Garden Initial Size` = paste0(round_r3(rg_size1, d = 0), " sq. ft."), `Rain Garden Initial Dimensions` = paste0(10, " ft wide x ", round_r3(rg_size1_length, d = 1), " ft long"), `Total Drainage Area` = paste0(round_r3(da_new, d = 1), " sq. ft."), `New Design Storm Volume` = paste0(round_r3(design_storm_volume_new, d = 0), " ft^3"), `Rain Garden Final Size` = paste0(round_r3(rg_size2, d = 0), " sq. ft."), `Rain Garden Final Dimensions` = paste0(10, " ft wide x ", round_r3(rg_size2_length, d = 1), " ft long"))
col.names <- c("Driveway Drainage Area", "One-Quarter of Roof Area", "Depth of Rain", "Design Storm Volume", "Rain Garden Depth", "Rain Garden Initial Size", "Rain Garden Initial Dimensions", "Total Drainage Area", "New Design Storm Volume", "Rain Garden Final Size", "Rain Garden Final Dimensions")
# code block below modified from data.table function
setattr(rain_garden_table, "col.names", setnames(rain_garden_table, col.names))
setattr(rain_garden_table, "class", c("data.table", "data.frame"))
rain_garden_table
}