Skip to content

Commit

Permalink
Refactor route_cyclestreet, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Dec 10, 2015
1 parent 72172e4 commit e86b22d
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions R/routes.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,42 @@ route_cyclestreet <- function(from, to, plan = "fastest", silent = TRUE, pat = c

orig <- paste0(from, collapse = ",")
dest <- paste0(to, collapse = ",")
api_base <- sprintf("https://api.cyclestreets.net/v2/")
api_base <- sprintf("http://www.cyclestreets.net/api/")
ft_string <- paste(orig, dest, sep = "|")
journey_plan <- sprintf("journey.plan?waypoints=%s&plan=%s", ft_string, plan)
journey_plan <- sprintf("journey.json?key=%s&itinerarypoints=%s&plan=%s",
pat, ft_string, plan)
request <- paste0(api_base, journey_plan)
request <- paste0(request, "&key=", pat)

if(silent == FALSE){
print(paste0("The request sent to cyclestreets.net was: ", request))
if (silent == FALSE) {
print(paste0("The request sent to cyclestreets.net was: ",
request))
}

txt <- httr::content(httr::GET(request), as = "text")
obj <- jsonlite::fromJSON(txt)

route <- sp::SpatialLines(list(sp::Lines(list(sp::Line(obj$features[3,]$geometry$coordinates)), ID = 1)))
# obj$marker$`@attributes`$elevations
# obj$marker$`@attributes`$points
coords <- obj$marker$`@attributes`$coordinates[1]
coords <- stringr::str_split(coords, pattern = " |,")[[1]]
coords <- matrix(as.numeric(coords), ncol = 2, byrow = TRUE)

route <- sp::SpatialLines(list(sp::Lines(list(sp::Line(coords)), ID = 1)))
h <- obj$marker$`@attributes`$elevations # hilliness
h <- stringr::str_split(h, pattern = ",")
h <- as.numeric(unlist(h)[-1])
htot <- sum(abs(diff(h)))

df <- data.frame(
plan = obj$features[3,]$properties$plan,
start = obj$properties$start,
finish = obj$properties$finish,
length = obj$features[3,]$properties$length,
time = obj$features[3,]$properties$time,
waypoint = nrow(route@lines[[1]]@Lines[[1]]@coords)
plan = obj$marker$`@attributes`$plan[1],
start = obj$marker$`@attributes`$start[1],
finish = obj$marker$`@attributes`$finish[1],
length = as.numeric(obj$marker$`@attributes`$length[1]),
time = sum(as.numeric(obj$marker$`@attributes`$time)),
waypoint = nrow(coords),
change_elev = htot,
av_incline = htot / as.numeric(obj$marker$`@attributes`$length[1]),
co2_saving = as.numeric(obj$marker$`@attributes`$grammesCO2saved[1]),
calories = as.numeric(obj$marker$`@attributes`$calories[1])
)

row.names(df) <- route@lines[[1]]@ID
Expand Down

0 comments on commit e86b22d

Please sign in to comment.