When distance_cutoff
and gradient_cutoff
thresholds are both broken
for route segments, this function treats them as anomalous and
sets the offending gradient values to the mean of the n
segments closest to (in front of and behind) the offending segment.
smooth_with_cutoffs(
gradient_segment,
elevation_change,
distances,
distance_cutoff = 50,
gradient_cutoff = 0.1,
n = 3
)
The gradient for each segment from CycleStreets.net
The difference between the maximum and minimum elevations within each segment
The distance of each segment
Distance (m) used to identify anomalous gradients
Gradient (%, e.g. 0.1 being 10%) used to identify anomalous gradients
The number of segments to use to smooth anomalous gradents. The default is 3, meaning segments directly before, after and including the offending segment.
f = system.file(package = "cyclestreets", "extdata/journey.json")
obj = jsonlite::read_json(f, simplifyVector = TRUE)
rsf = json2sf_cs(obj, cols = c("distances"))
rsf$gradient_segment
#> [1] 0.01149425 0.02564103 0.05555556 0.01526718 0.00000000
rsf$elevation_change
#> [1] 1 6 1 2 0
rsf$distances
#> [1] 87 234 18 131 36
smooth_with_cutoffs(rsf$gradient_segment, rsf$elevation_change, rsf$distances)
#> [1] 0.01149425 0.02564103 0.05555556 0.01526718 0.00000000
smooth_with_cutoffs(rsf$gradient_segment, rsf$elevation_change, rsf$distances, 20, 0.05)
#> [1] 0.01149425 0.02564103 0.02349869 0.01526718 0.00000000
smooth_with_cutoffs(rsf$gradient_segment, rsf$elevation_change, rsf$distances, 200, 0.02)
#> [1] 0.01149425 0.02564103 0.02349869 0.01526718 0.00000000
smooth_with_cutoffs(rsf$gradient_segment, rsf$elevation_change, rsf$distances, 200, 0.02, n = 5)
#> [1] 0.01149425 0.02564103 0.01976285 0.01526718 0.00000000