-
Notifications
You must be signed in to change notification settings - Fork 0
/
engr_survey3.R
600 lines (375 loc) · 16.7 KB
/
engr_survey3.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#' Calculate the Distance between Engineering Survey Points (Length or Number of Stations)
#'
#' Takes engineering survey points in various units (foot, US survey foot,
#' meters, or kilometers) and calculates the horizontal length in various units
#' (foot, US survey foot, US survey mile, mile, meters, or kilometers).
#'
#'
#'
#' @param length1 character vector that contains the beginning engineering
#' survey station value
#' @param station_distance numeric vector that contains the horizontal distance
#' between any 2 points along the survey, the default is 100 feet
#' @param units character vector that contains the system of units for the
#' \code{station_distance} (options are \code{foot}, \code{survey_ft}
#' (United States Customary System) [US survey foot], \code{meters} for
#' International System of Units meters, or \code{kilometers} for International System
#' of Units kilometers)
#' @param output character vector that contains the system of units for the
#' horizontal length (options are \code{foot}, \code{survey_ft}
#' (United States Customary System) [US survey foot], \code{survey_mile}
#' (United States Customary System) [US survey mile], \code{mile},
#' \code{meters} for International System of Units meters, or \code{kilometers} for
#' International System of Units kilometers)
#'
#'
#'
#' @return horizontal length as a numeric vector (ex. 1214.402) or as a
#' character vector with the word stations after the number (ex. 1214.402
#' stations)
#'
#'
#'
#' @references
#' \enumerate{
#' \item udunits.dat, v 1.18 2006/09/20 18:59:18 steve Exp, \url{https://web.archive.org/web/20230202155021/https://www.unidata.ucar.edu/software/udunits/udunits-1/udunits.txt}. Retrieved thanks to the Internet Archive: Wayback Machine
#' \item Engineer Boards: Transportation. "Stationing. Dumb question?" Question asked by By NIKE, August 31, 2013 and answered by ptatohed on September 1, 2013. See \url{https://engineerboards.com/threads/stationing-dumb-question.21935/}.
#' }
#'
#'
#'
#'
#'
#'
#' @author Irucka Embry
#'
#'
#'
#' @encoding UTF-8
#'
#'
#'
#'
#'
#'
#'
#' @examples
#'
#' # Please refer to the iemisc: Engineering Survey Examples vignette for
#' # additional examples
#'
#' # Example 1
#'
#' library(iemisc)
#'
#' # "What the others said is correct. 1 station is equal to 100 feet. So when
#' # asked how many stations are in (3.2mi x 5280ft/mi = ) 16,896 feet, you are being
#' # asked how many 100 foot-segments are in 16,896 feet? The answer of course is
#' # 16,896ft / 100ft/sta = 168.96 sta." Source: Reference 2
#'
#' length1 <- "16,896" # feet
#'
#' engr_survey3(length1, station_distance = 100, units = "foot", output = "numeric")
#' engr_survey3(length1, station_distance = 100, units = "foot", output = "string")
#' # the answer provides the number of stations
#'
#' # Note: Both answers should be the same as 3.2 miles = 16,896 feet.
#'
#' length2 <- 3.2 # mile
#'
#' engr_survey3(length2, station_distance = 100, units = "mile", output = "numeric")
#' engr_survey3(length2, station_distance = 100, units = "mile", output = "string")
#' # the answer provides the number of stations
#'
#'
#'
#'
#'
#'
#' @importFrom stringi stri_replace_all_fixed stri_replace_first_fixed
#' @importFrom units set_units make_units
#' @importFrom checkmate qtest
#' @importFrom assertthat assert_that
#' @importFrom round round_r3
#'
#' @export
engr_survey3 <- function (length1, station_distance = 100, units = c("foot", "survey_ft", "survey_mile", "mile", "meters", "kilometers"), output = c("numeric", "string")) {
US_survey_foot <- m <- km <- ft <- US_survey_mile <- international_mile <- NULL
# due to NSE notes in R CMD check
units <- units
station_distance <- station_distance
output <- output
# Check length1, station_distance, units, and output
assert_that(!any(qtest(length1, c("N==1(0,)", "S==1")) == FALSE), msg = "length1 is a numeric vector or a character vector without any numeric values. length1 should be a character vector that contains numeric values. Please try again.")
# only process with string values with numbers and provide an error message if the check fails
assert_that(!any(qtest(station_distance, "N==1(0,)") == FALSE), msg = "station_distance is 0, NA, NaN, Inf, -Inf, a string, or empty. Please try again.")
# only process with finite values and provide an error message if the check fails
assert_that(isTRUE(any(c("foot", "survey_ft", "survey_mile", "mile", "meters", "kilometers") %in% units)), msg = "Incorrect unit selection. The only possible units are 'foot', 'survey_ft', 'survey_mile', 'mile', 'meters', 'kilometers'. Please try again.")
# only process with a specified unit and provide a stop warning if not
assert_that(isTRUE(any(c("numeric", "string") %in% output)), msg = "Incorrect unit selection. The only possible output are 'foot', 'survey_ft', 'survey_mile', 'mile', 'meters', 'kilometers'. Please try again.")
# only process with a specified unit and provide a stop warning if not
length1 <- as.numeric(stri_replace_all_fixed(length1, ",", ""))
if (units == "foot") {
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
} else if (units == "survey_ft") {
units(length1) <- make_units(US_survey_foot)
units(length1) <- make_units(ft)
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
} else if (units == "survey_mile") {
units(length1) <- make_units(US_survey_mile)
units(length1) <- make_units(ft)
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
} else if (units == "mile") {
units(length1) <- make_units(international_mile)
units(length1) <- make_units(ft)
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
} else if (units == "meters") {
units(length1) <- make_units(m)
units(length1) <- make_units(ft)
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
} else if (units == "kilometers") {
units(length1) <- make_units(km)
units(length1) <- make_units(ft)
station <- as.numeric(length1 / station_distance)
station_string <- paste0(station, " stations")
}
if (output == "numeric") {
return(station)
} else {
return(station_string)
}
}
#' Calculate the Station Distance between Engineering Survey Points
#'
#' Takes engineering survey points in various units (foot, US survey foot,
#' meters, or kilometers) and determines the upstream station location.
#'
#'
#' @param object numeric vector that contains the upstream length
#' @param ds_station character vector that contains the ending engineering
#' survey station value
#' @param station_distance numeric vector that contains the horizontal distance
#' between any 2 points along the survey, the default is 100 feet
#' @param units character vector that contains the system of units for the
#' \code{station_distance} (options are \code{foot}, \code{survey_ft}
#' (United States Customary System) [US survey foot], \code{meters} for
#' International System of Units meters, or \code{kilometers} for International System
#' of Units kilometers)
#'
#'
#'
#' @return the final engineering survey position (ex. Sta. 5+55)
#'
#'
#'
#' @source
#' r - Insert a character at a specific location in a string - Stack Overflow answered and edited by Justin on Dec 13 2012. See \url{https://stackoverflow.com/questions/13863599/insert-a-character-at-a-specific-location-in-a-string}.
#'
#'
#'
#' @references
#' \enumerate{
#' \item udunits.dat, v 1.18 2006/09/20 18:59:18 steve Exp, \url{https://web.archive.org/web/20230202155021/https://www.unidata.ucar.edu/software/udunits/udunits-1/udunits.txt}. Retrieved thanks to the Internet Archive: Wayback Machine
#' \item Engineer Boards: Transportation. "Stationing. Dumb question?" Question asked by By NIKE, August 31, 2013 and answered by ptatohed on September 1, 2013. See \url{https://engineerboards.com/threads/stationing-dumb-question.21935/}.
#' }
#'
#'
#'
#'
#'
#'
#' @author Irucka Embry
#'
#'
#'
#' @encoding UTF-8
#'
#'
#'
#'
#'
#'
#'
#' @examples
#'
#' # Please refer to the iemisc: Engineering Survey Examples vignette for
#' # additional examples
#'
#' library(iemisc)
#'
#' # Example 1
#'
#' # "Conversely, if you were asked at what station a manhole 555 ft upstream of
#' # Sta 0+00 is, your answer would be 0.00 sta + 555 ft / 100 ft/sta = 5.55 sta =
#' # Sta 5+55." Source: Reference 2
#'
#' engr_survey4(555, "0+00", units = "foot")
#'
#'
#'
#'
#'
#'
#' @importFrom stringi stri_split_fixed stri_replace_first_fixed stri_detect_regex
#' @importFrom units set_units make_units
#' @importFrom checkmate qtest testString
#' @importFrom assertthat assert_that
#' @importFrom round round_r3
#'
#' @export
engr_survey4 <- function (object, ds_station, station_distance = 100, units = c("foot", "survey_ft", "survey_mile", "mile", "meters", "kilometers")) {
US_survey_foot <- m <- km <- ft <- US_survey_mile <- international_mile <- NULL
# due to NSE notes in R CMD check
units <- units
station_distance <- station_distance
# Check object, station_distance, units, and output
assert_that(!any(qtest(object, "N==1(0,)") == FALSE), msg = "object is 0, NA, NaN, Inf, -Inf, a string, or empty. Please try again.")
# only process with finite values and provide an error message if the check fails
# check again to makre sure in proper format
assert_that(!any(testString(ds_station, min.chars = 1, pattern = "[0-9]+[0-9]", ignore.case = TRUE) == FALSE), msg = "ds_station is a numeric vector or a character vector without any numeric values. ds_station should be a character vector that contains numeric values. Please try again.")
# only process with string values with numbers and provide an error message if the check fails
assert_that(!any(qtest(station_distance, "N==1(0,)") == FALSE), msg = "station_distance is 0, NA, NaN, Inf, -Inf, a string, or empty. Please try again.")
# only process with finite values and provide an error message if the check fails
assert_that(isTRUE(any(c("foot", "survey_ft", "survey_mile", "mile", "meters", "kilometers") %in% units)), msg = "Incorrect unit selection. The only possible units are 'foot', 'survey_ft', 'survey_mile', 'mile', 'meters', and 'kilometers'. Please try again.")
# only process with a specified unit and provide a stop warning if not
object <- as.numeric(stri_replace_all_fixed(object, ",", ""))
if (units == "foot") {
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)"))
{
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
} else if (units == "survey_ft") {
units(object) <- make_units(US_survey_foot)
units(object) <- make_units(ft)
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)"))
{
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
} else if (units == "survey_mile") {
units(object) <- make_units(US_survey_mile)
units(object) <- make_units(ft)
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)")) {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
} else if (units == "mile") {
units(object) <- make_units(international_mile)
units(object) <- make_units(ft)
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)")) {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
} else if (units == "meters") {
units(object) <- make_units(m)
units(object) <- make_units(ft)
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)")) {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
} else if (units == "kilometers") {
units(object) <- make_units(km)
units(object) <- make_units(ft)
ds_stationa <- stri_split_fixed(ds_station, "+")
ds_stationa_use <- as.numeric(ds_stationa[[1]][1])
ds_stationb <- as.numeric(ds_stationa[[1]][2])
ds_station_use <- sum(c(ds_stationa_use * station_distance, ds_stationb))
us_station <- round_r3(sum(c(ds_station_use, object / station_distance)), d = 4)
us_station_use <- as.character(us_station)
if(stri_detect_regex(us_station_use, "\\.(\\d{3}+)")) {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
split <- stri_split_fixed(us_station_use1, "+")
split1 <- split[[1]][[1]]
split2 <- split[[1]][[2]]
split2a <- sub("(?<=(\\d{2}))", ".", split2, perl = TRUE) # Source 1
return_split <- paste0("Sta. ", split1, "+", split2a)
us_station_use1 <- return_split
} else {
us_station_use1 <- stri_replace_first_fixed(us_station_use, ".", "+")
us_station_use1 <- paste0("Sta. ", us_station_use1)
}
us_station_use1 <- ifelse(stri_detect_fixed(us_station_use1, "+"), us_station_use1, paste0(us_station_use1, "+00"))
}
return(us_station_use1)
}