Skip to content

Commit

Permalink
Add journey2, close to solving cyclestreets#46
Browse files Browse the repository at this point in the history
  • Loading branch information
atumscott committed Feb 16, 2023
1 parent 63c1651 commit da6ef8b
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions R/journey2.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ journey2 <- function(fromPlace = NA,
results$coordinates <- NULL
results <- sf::st_as_sf(results)

message("results may not be in the order they were provided")
return(results)
# message("results may not be in the order they were provided")
add_columns(results)

}

Expand Down Expand Up @@ -262,25 +262,35 @@ get_sum = function(v) get_values(v, fun = sum)
get_min = function(v) get_values(v, fun = min)
get_max = function(v) get_values(v, fun = max)

add_columns = function(r) {

# Aim: add these columns
# [17] "gradient_segment" "elevation_change" "gradient_smooth"
# Tests:
# r1 = sf::read_sf("data-raw/r_1.geojson")
# add_columns(r1)

elevations_list = extract_values(r_3$elevations)
add_columns = function(r) {

r_3$elevations
r_3$elevation_min = get_min(elevations_list)
r_3$elevation_max = get_max(elevations_list)
distances_list = extract_values(r_3$distances)
r_3$segment_length = get_sum(distances_list)
r_3$elevation_change = r_3$elevation_max - r_3$elevation_min
r_3$gradient_segment = r_3$elevation_change / r_3$segment_length
r_3$gradient_smooth = cyclestreets::smooth_with_cutoffs(
r_3$gradient_segment,
r_3$elevation_change,
r_3$segment_length,
elevations_list = extract_values(r$elevations)

elevation_min = get_min(elevations_list)
elevation_max = get_max(elevations_list)
distances_list = extract_values(r$distances)
# # Should be this for clearer name:
# r$segment_length = get_sum(distances_list)
# But for compatibility with original journey() we'll go with this:
r$distances = get_sum(distances_list)
elevation_change = elevation_max - elevation_min
# Order for compatibility with journey:
r$gradient_segment = elevation_change / r$distances
r$elevation_change = elevation_max - elevation_min
r$gradient_smooth = cyclestreets::smooth_with_cutoffs(
r$gradient_segment,
r$elevation_change,
r$distances,
distance_cutoff = 50,
gradient_cutoff = 0.1,
n = 3,
)
r
}

0 comments on commit da6ef8b

Please sign in to comment.