@@ -64,11 +64,11 @@
#' @seealso line2route
#' @aliases route_cyclestreets
#' @examples
#'
#'
#' \dontrun{
#' from = c(-1.55, 53.80) # geo_code("leeds")
#' to = c(-1.76, 53.80) # geo_code("bradford uk")
#' json_output = route_cyclestreet(from = from, to = to, plan = "quietest", save_raw = TRUE)
#' from <- c(-1.55, 53.80) # geo_code("leeds")
#' to <- c(-1.76, 53.80) # geo_code("bradford uk")
#' json_output <- route_cyclestreet(from = from, to = to, plan = "quietest", save_raw = TRUE)
#' str(json_output) # what does cyclestreets give you?
#' rf_lb <- route_cyclestreet(from, to, plan = "fastest")
#' rf_lb@data
@@ -78,113 +78,118 @@
#' # Plan a 'balanced' route from Pedaller's Arms to the University of Leeds
#' rb_pa <- route_cyclestreet("Pedaller's Arms, Leeds", "University of Leeds, UK", "balanced")
#' }
#'
#'
route_cyclestreet <-
route_cyclestreets <- function(from, to, plan = "fastest", silent = TRUE, pat = NULL,
base_url = "https://www.cyclestreets.net", reporterrors = TRUE,
save_raw = "FALSE"){
base_url = "https://www.cyclestreets.net", reporterrors = TRUE,
save_raw = "FALSE") {

# Convert sp object to lat/lon vector
if(class(from) == "SpatialPoints" | class(from) == "SpatialPointsDataFrame" )
from <- coordinates(from)
if(class(to) == "SpatialPoints" | class(to) == "SpatialPointsDataFrame" )
to <- coordinates(to)
# Convert sp object to lat/lon vector
if (class(from) == "SpatialPoints" | class(from) == "SpatialPointsDataFrame") {
from <- coordinates(from)
}
if (class(to) == "SpatialPoints" | class(to) == "SpatialPointsDataFrame") {
to <- coordinates(to)
}

# Convert character strings to lon/lat if needs be
if(is.character(from))
from <- geo_code(from)
if(is.character(to))
to <- geo_code(to)
# Convert character strings to lon/lat if needs be
if (is.character(from)) {
from <- geo_code(from)
}
if (is.character(to)) {
to <- geo_code(to)
}

orig <- paste0(from, collapse = ",")
dest <- paste0(to, collapse = ",")
ft_string <- paste(orig, dest, sep = "|")
orig <- paste0(from, collapse = ",")
dest <- paste0(to, collapse = ",")
ft_string <- paste(orig, dest, sep = "|")

if(is.null(pat))
pat = api_pat("cyclestreet")
if (is.null(pat)) {
pat <- api_pat("cyclestreet")
}

httrmsg = httr::modify_url(
base_url,
path = "api/journey.json",
query = list(
key = pat,
itinerarypoints = ft_string,
plan = plan,
reporterrors = ifelse(reporterrors == TRUE, 1, 0)
httrmsg <- httr::modify_url(
base_url,
path = "api/journey.json",
query = list(
key = pat,
itinerarypoints = ft_string,
plan = plan,
reporterrors = ifelse(reporterrors == TRUE, 1, 0)
)
)
)

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

httrreq <- httr::GET(httrmsg)
if (silent == FALSE) {
print(paste0("The request sent to cyclestreets.net was: ", httrmsg))
}

if (grepl('application/json', httrreq$headers$`content-type`) == FALSE) {
stop("Error: CycleStreets did not return a valid result")
}
httrreq <- httr::GET(httrmsg)

txt <- httr::content(httrreq, as = "text", encoding = "UTF-8")
if (txt == "") {
stop("Error: CycleStreets did not return a valid result")
}
if (grepl("application/json", httrreq$headers$`content-type`) == FALSE) {
stop("Error: CycleStreets did not return a valid result")
}

obj <- jsonlite::fromJSON(txt, simplifyDataFrame = TRUE)
txt <- httr::content(httrreq, as = "text", encoding = "UTF-8")
if (txt == "") {
stop("Error: CycleStreets did not return a valid result")
}

if (is.element("error", names(obj))) {
stop(paste0("Error: ", obj$error))
}
obj <- jsonlite::fromJSON(txt, simplifyDataFrame = TRUE)

if(save_raw){
return((obj))
}else{
# 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[[1]], pattern = ",") #only take first set of data
h <- as.numeric(unlist(h)[-1])
hdif <- diff(h)
htot <- sum(abs(hdif))
hchng <- h[length(h)] - h[1]
hmxmn <- max(h) - min(h)
hup <- sum(hdif[which(hdif>0)])
hdown <- -1* sum(hdif[which(hdif<0)])

# busyness overall
bseg <- obj$marker$`@attributes`$busynance
bseg <- stringr::str_split(bseg, pattern = ",")
bseg <- as.numeric(unlist(bseg)[-1])
bseg <- sum(bseg)

df <- data.frame(
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 = as.numeric(obj$marker$`@attributes`$time[1]),
waypoint = nrow(coords),
cum_hill = htot, #total up and down
change_elev = hchng, # diff between start and end
dif_max_min = hmxmn, # diff between highest and lowest
up_tot = hup, # total climbing
down_tot = hdown, # total descending
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]),
busyness = bseg
)
if (is.element("error", names(obj))) {
stop(paste0("Error: ", obj$error))
}

row.names(df) <- route@lines[[1]]@ID
route <- sp::SpatialLinesDataFrame(route, df)
sp::proj4string(route) <- sp::CRS("+init=epsg:4326")
route
if (save_raw) {
return((obj))
} else {
# 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[[1]], pattern = ",") # only take first set of data
h <- as.numeric(unlist(h)[-1])
hdif <- diff(h)
htot <- sum(abs(hdif))
hchng <- h[length(h)] - h[1]
hmxmn <- max(h) - min(h)
hup <- sum(hdif[which(hdif > 0)])
hdown <- -1 * sum(hdif[which(hdif < 0)])

# busyness overall
bseg <- obj$marker$`@attributes`$busynance
bseg <- stringr::str_split(bseg, pattern = ",")
bseg <- as.numeric(unlist(bseg)[-1])
bseg <- sum(bseg)

df <- data.frame(
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 = as.numeric(obj$marker$`@attributes`$time[1]),
waypoint = nrow(coords),
cum_hill = htot, # total up and down
change_elev = hchng, # diff between start and end
dif_max_min = hmxmn, # diff between highest and lowest
up_tot = hup, # total climbing
down_tot = hdown, # total descending
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]),
busyness = bseg
)

row.names(df) <- route@lines[[1]]@ID
route <- sp::SpatialLinesDataFrame(route, df)
sp::proj4string(route) <- sp::CRS("+init=epsg:4326")
route
}
}
}
#' Plan a route with the graphhopper routing engine
#'
#' Provides an R interface to the graphhopper routing engine,
@@ -223,41 +228,44 @@ route_cyclestreet <-
#' @seealso route_cyclestreet
#' @examples
#' \dontrun{
#' from = c(-0.12, 51.5); to = c(-0.14, 51.5)
#' r1 = route_graphhopper(from = from, to = to, silent = FALSE)
#' r2 = route_graphhopper("London Eye", "Westminster", vehicle = "foot")
#' r3 = route_graphhopper("London Eye", "Westminster", vehicle = "car")
#' plot(r1); plot(r2, add = TRUE, col = "blue") # compare routes
#' from <- c(-0.12, 51.5)
#' to <- c(-0.14, 51.5)
#' r1 <- route_graphhopper(from = from, to = to, silent = FALSE)
#' r2 <- route_graphhopper("London Eye", "Westminster", vehicle = "foot")
#' r3 <- route_graphhopper("London Eye", "Westminster", vehicle = "car")
#' plot(r1)
#' plot(r2, add = TRUE, col = "blue") # compare routes
#' plot(r3, add = TRUE, col = "red")
#' }
route_graphhopper <- function(from, to, l = NULL, vehicle = "bike", silent = TRUE, pat = NULL, base_url = "https://graphhopper.com"){
route_graphhopper <- function(from, to, l = NULL, vehicle = "bike", silent = TRUE, pat = NULL, base_url = "https://graphhopper.com") {

# Convert character strings to lon/lat if needs be
coords <- od_coords(from, to, l)

if(is.null(pat))
pat = api_pat("graphhopper")
if (is.null(pat)) {
pat <- api_pat("graphhopper")
}

httrmsg = httr::modify_url(
httrmsg <- httr::modify_url(
base_url,
path = "/api/1/route",
query = list(
point = paste0(coords[1, c("fy", "fx")], collapse = ","),
point = paste0(coords[1, c("ty", "tx")], collapse = ","),
vehicle = vehicle,
locale = "en-US",
debug = 'true',
points_encoded = 'false',
debug = "true",
points_encoded = "false",
key = pat
)
)
if(silent == FALSE){
if (silent == FALSE) {
print(paste0("The request sent was: ", httrmsg))
}
httrreq <- httr::GET(httrmsg)
httr::stop_for_status(httrreq)

if (grepl('application/json', httrreq$headers$`content-type`) == FALSE) {
if (grepl("application/json", httrreq$headers$`content-type`) == FALSE) {
stop("Error: Graphhopper did not return a valid result")
}

@@ -273,14 +281,14 @@ route_graphhopper <- function(from, to, l = NULL, vehicle = "bike", silent = TRU
stop("Invalid API key")
}
}
route <- sp::SpatialLines(list(sp::Lines(list(sp::Line(obj$paths$points[[1]][[1]][,1:2])), ID = "1")))
route <- sp::SpatialLines(list(sp::Lines(list(sp::Line(obj$paths$points[[1]][[1]][, 1:2])), ID = "1")))

climb <- NA # to set elev variable up

# get elevation data if it was a bike trip
if(vehicle == "bike"){
if (vehicle == "bike") {
change_elev <- obj$path$descend + obj$paths$ascend
}else{
} else {
change_elev <- NA
}

@@ -294,7 +302,6 @@ route_graphhopper <- function(from, to, l = NULL, vehicle = "bike", silent = TRU
route <- sp::SpatialLinesDataFrame(route, df)
sp::proj4string(route) <- sp::CRS("+init=epsg:4326")
route

}

#' Retrieve personal access token.
@@ -309,26 +316,26 @@ route_graphhopper <- function(from, to, l = NULL, vehicle = "bike", silent = TRU
#' api_pat(api_name = "cyclestreet")
#' }
api_pat <- function(api_name, force = FALSE) {
api_name_caps = toupper(api_name)
api_name_caps <- toupper(api_name)
env <- Sys.getenv(api_name_caps)
if (!identical(env, "") && !force) return(env)

if (!interactive()) {
stop(paste0("Set the environment variable ", api_name_caps, " e.g. with .Renviron or Sys.setenv()"),
call. = FALSE)
stop(paste0("Set the environment variable ", api_name_caps, " e.g. with .Renviron or Sys.setenv()"),
call. = FALSE
)
}

message("Couldn't find the environment variable ", api_name_caps, ". See documentation, e.g. ?route_cyclestreet, for more details.")
message("Couldn't find the environment variable ", api_name_caps, ". See documentation, e.g. ?route_cyclestreet, for more details.")
message("Please enter your API key to access the ", api_name, "and press enter:")
pat <- readline(": ")

if (identical(pat, "")) {
stop("Personal access token entry failed", call. = FALSE)
}

message("Updating ", api_name_caps, " environment variable. Save this to .Renviron for future use.")
message("Updating ", api_name_caps, " environment variable. Save this to .Renviron for future use.")
Sys.setenv(api_name_caps = pat)

pat

}
@@ -15,7 +15,7 @@
#' @aliases toptail
#' @export
#' @examples
#' l = routes_fast[2:4,]
#' l <- routes_fast[2:4, ]
#' l_toptail <- geo_toptail(l, toptail_dist = 300)
#' plot(l)
#' plot(l_toptail, col = "red", add = TRUE, lwd = 3)
@@ -28,36 +28,36 @@ geo_toptail <- function(l, toptail_dist, ...) {
UseMethod("geo_toptail")
}
#' @export
geo_toptail.Spatial <- toptail <- function(l, toptail_dist, ...){

if(length(toptail_dist) > 1 & length(toptail_dist) != length(l)) {
geo_toptail.Spatial <- toptail <- function(l, toptail_dist, ...) {
if (length(toptail_dist) > 1 & length(toptail_dist) != length(l)) {
stop("toptail_dist is vector but not of equal length to spatial object")
}

lpoints <- line_to_points(l)

if(length(toptail_dist) == 1) {
toptail_dist = rep(toptail_dist, length(l))
if (length(toptail_dist) == 1) {
toptail_dist <- rep(toptail_dist, length(l))
}

for(i in 1:length(l)){

sel_points <- lpoints[lpoints$id == i,]
for (i in 1:length(l)) {
sel_points <- lpoints[lpoints$id == i, ]

# Create buffer for geographic or projected crs
if(!sp::is.projected(l)){
if (!sp::is.projected(l)) {
sel <- geo_buffer(lpoints, width = toptail_dist[i], ..., silent = TRUE)
} else {
sel <- rgeos::gBuffer(lpoints, dist = toptail_dist[i], ...)
}

if(rgeos::gContainsProperly(sel, l[i,])){
message(paste0("Line ", i, " is completely removed by the clip and",
" is omitted from the results"))
if (rgeos::gContainsProperly(sel, l[i, ])) {
message(paste0(
"Line ", i, " is completely removed by the clip and",
" is omitted from the results"
))
next
}
l2 <- rgeos::gDifference(l[i,], sel)
if(!exists("out")){
l2 <- rgeos::gDifference(l[i, ], sel)
if (!exists("out")) {
out <- l2
} else {
out <- raster::bind(out, l2)
@@ -66,7 +66,7 @@ geo_toptail.Spatial <- toptail <- function(l, toptail_dist, ...){
out
}
#' @export
geo_toptail.sf <- function(l, toptail_dist, ...){
geo_toptail.sf <- function(l, toptail_dist, ...) {
l_sp <- as(l, "Spatial")
res_sp <- geo_toptail(l = l_sp, toptail_dist = toptail_dist, ...)
sf::st_as_sf(res_sp)
@@ -81,19 +81,19 @@ geo_toptail.sf <- function(l, toptail_dist, ...){
#' @inheritParams geo_buffer
#' @export
#' @examples
#' r = routes_fast[1:3, ]
#' r <- routes_fast[1:3, ]
#' buff <- buff_geo(r, width = 100)
#' plot(buff)
#' plot(r, add = TRUE)
#' # Test it works the same on projected data
#' shp <- sp::spTransform(r, sp::CRS("+init=epsg:27700"))
#' buff2 = buff_geo(shp, 50) # test if it works the same on projected data
#' buff2 <- buff_geo(shp, 50) # test if it works the same on projected data
#' plot(buff2)
#' plot(r, add = TRUE) # note they do not show
#' buff3 = sp::spTransform(buff2, sp::CRS("+init=epsg:4326"))
#' buff3 <- sp::spTransform(buff2, sp::CRS("+init=epsg:4326"))
#' plot(buff)
#' plot(buff3, add = TRUE, col = "black")
buff_geo <- function(shp, width, ...){
buff_geo <- function(shp, width, ...) {
gprojected(shp = shp, fun = rgeos::gBuffer, width = width, ...)
}
#' Clip the first and last n metres of SpatialLines
@@ -112,13 +112,12 @@ buff_geo <- function(shp, width, ...){
#' @export
#' @examples
#' data("routes_fast")
#' rf = routes_fast[2:3, ]
#' rf <- routes_fast[2:3, ]
#' r_toptail <- toptailgs(rf, toptail_dist = 300)
#' plot(rf, lwd = 3)
#' plot(r_toptail, col = "red", add = TRUE)
#' plot(cents, add = TRUE)
toptailgs <- function(l, toptail_dist, tail_dist = NULL) {

if (length(toptail_dist) > 1) {
if (length(toptail_dist) != length(l)) {
stop("toptail_dist is vector but not of equal length to SpatialLines object")
@@ -139,29 +138,29 @@ toptailgs <- function(l, toptail_dist, tail_dist = NULL) {
tail_disto <- tail_dist

i <- 1
while(i <= length(l)) {
while (i <= length(l)) {
toptail_dist <- ifelse(length(toptail_disto) == 1, toptail_disto, toptail_disto[i])
linecoords <- coordinates(l@lines[[i]])[[1]]
topdists <- geosphere::distHaversine(linecoords[1,],linecoords)
topdists <- geosphere::distHaversine(linecoords[1, ], linecoords)
linecoords <- rbind(
tail(linecoords[which(topdists < toptail_dist),,drop=FALSE],n=1)+(
linecoords[which(topdists >= toptail_dist),,drop=FALSE][1,]-
tail(linecoords[which(topdists < toptail_dist),,drop=FALSE],n=1)
)*(
(toptail_dist-tail(topdists[which(topdists < toptail_dist)],n=1))/(topdists[which(topdists >= toptail_dist)][1]-tail(topdists[which(topdists < toptail_dist)],n=1))
tail(linecoords[which(topdists < toptail_dist), , drop = FALSE], n = 1) + (
linecoords[which(topdists >= toptail_dist), , drop = FALSE][1, ] -
tail(linecoords[which(topdists < toptail_dist), , drop = FALSE], n = 1)
) * (
(toptail_dist - tail(topdists[which(topdists < toptail_dist)], n = 1)) / (topdists[which(topdists >= toptail_dist)][1] - tail(topdists[which(topdists < toptail_dist)], n = 1))
),
linecoords[which(topdists >= toptail_dist),,drop=FALSE]
linecoords[which(topdists >= toptail_dist), , drop = FALSE]
)
bottomdists <- geosphere::distHaversine(linecoords[nrow(linecoords),],linecoords)
bottomdists <- geosphere::distHaversine(linecoords[nrow(linecoords), ], linecoords)
tail_dist <- ifelse(length(tail_disto) == 1, tail_disto, tail_disto[i])

linecoords <- rbind(
linecoords[which(bottomdists >= tail_dist),,drop=FALSE],
tail(linecoords[which(bottomdists >= tail_dist),,drop=FALSE],n=1)+(
linecoords[which(bottomdists < tail_dist),,drop=FALSE][1,]-
tail(linecoords[which(bottomdists >= tail_dist),,drop=FALSE],n=1)
)*
((tail(bottomdists[which(bottomdists >= tail_dist)],n=1)-tail_dist)/(tail(bottomdists[which(bottomdists >= tail_dist)],n=1)-bottomdists[which(bottomdists < tail_dist)][1]))
linecoords[which(bottomdists >= tail_dist), , drop = FALSE],
tail(linecoords[which(bottomdists >= tail_dist), , drop = FALSE], n = 1) + (
linecoords[which(bottomdists < tail_dist), , drop = FALSE][1, ] -
tail(linecoords[which(bottomdists >= tail_dist), , drop = FALSE], n = 1)
) *
((tail(bottomdists[which(bottomdists >= tail_dist)], n = 1) - tail_dist) / (tail(bottomdists[which(bottomdists >= tail_dist)], n = 1) - bottomdists[which(bottomdists < tail_dist)][1]))
)
l@lines[[i]]@Lines[[1]]@coords <- unname(linecoords)
i <- i + 1
@@ -182,28 +181,28 @@ toptailgs <- function(l, toptail_dist, tail_dist = NULL) {
#' @examples
#' r_toptail <- toptail_buff(routes_fast, zones)
#' sel <- row.names(routes_fast) %in% row.names(r_toptail)
#' rf_cross_poly <- routes_fast[sel,]
#' rf_cross_poly <- routes_fast[sel, ]
#' plot(zones)
#' plot(routes_fast, col = "blue", lwd = 4, add = TRUE)
#' # note adjacent lines removed
#' plot(rf_cross_poly, add = TRUE, lwd = 2)
#' plot(r_toptail, col = "red", add = TRUE)
toptail_buff <- function(l, buff, ...){
toptail_buff <- function(l, buff, ...) {
# force same crs
if(!sp::identicalCRS(l, buff)){
if (!sp::identicalCRS(l, buff)) {
sp::proj4string(buff) <- sp::proj4string(l)
}
for(i in 1:length(l)){
lpoints <- line2points(l[i,])
for (i in 1:length(l)) {
lpoints <- line2points(l[i, ])
# Select zones per line
sel <- buff[lpoints,]
l2 <- rgeos::gDifference(l[i,], sel)
if(is.null(l2)){
sel <- buff[lpoints, ]
l2 <- rgeos::gDifference(l[i, ], sel)
if (is.null(l2)) {
next
}else{
row.names(l2) <- row.names(l[i,])
} else {
row.names(l2) <- row.names(l[i, ])
}
if(!exists("out")){
if (!exists("out")) {
out <- l2
} else {
out <- raster::bind(out, l2)
@@ -1,15 +1,19 @@
# Aim: test the performance of parallel code
library(stplanr)
# Relies on having large lines dataset
n = 1000 # number of lines to route
ii = round(n / nrow(flowlines))
for(i in 1:ii) {
if(i == 1)
l = flowlines else
l = rbind(l, flowlines)
n <- 1000 # number of lines to route
ii <- round(n / nrow(flowlines))
for (i in 1:ii) {
if (i == 1) {
l <- flowlines
} else {
l <- rbind(l, flowlines)
}
}

system.time({r1 = line2route(l)})
system.time({
r1 <- line2route(l)
})
# result1 - rl
# user system elapsed
# 55.864 1.384 198.586
@@ -18,10 +22,12 @@ system.time({r1 = line2route(l)})
# 44.336 0.392 125.790
# user system elapsed
# 36.476 2.500 186.043
detach("package:stplanr", unload=TRUE)
detach("package:stplanr", unload = TRUE)
devtools::install_github(repo = "ropensci/stplanr", ref = "0.1.8")
library(stplanr)
system.time({r2 = line2route(l = l, n_processes = 8)})
system.time({
r2 <- line2route(l = l, n_processes = 8)
})
# result1 - rl
# user system elapsed
# 0.620 0.148 30.679
@@ -43,4 +49,3 @@ nrow(r1) == nrow(r2) # identical
identical(raster::geom(r1), raster::geom(r2)) # not identical geometries
plot(r1)
plot(r2) # very different appearance...

@@ -1,9 +1,8 @@
devtools::install_github("nikolai-b/stplanr", ref = "add_error_handelling")
r = line2route(flowlines, reporterror = TRUE)
r <- line2route(flowlines, reporterror = TRUE)
r$error # shows error

# now switch off internet partway (manually)
r = line2route(flowlines, reporterror = TRUE)

r2 = stplanr:::line2routeRetry(flowlines, silent = F)
r <- line2route(flowlines, reporterror = TRUE)

r2 <- stplanr:::line2routeRetry(flowlines, silent = F)
@@ -4,19 +4,20 @@ test_that(
desc = "calc_catchment returns a SpatialPolygonsDataFrame",
code = {
data_dir <- system.file("extdata", package = "stplanr")
unzip(file.path(data_dir, 'smallsa1.zip'))
unzip(file.path(data_dir, 'testcycleway.zip'))
sa1income <- readOGR(".","smallsa1")
testcycleway <- readOGR(".","testcycleway")
unzip(file.path(data_dir, "smallsa1.zip"))
unzip(file.path(data_dir, "testcycleway.zip"))
sa1income <- readOGR(".", "smallsa1")
testcycleway <- readOGR(".", "testcycleway")
t1 <- calc_catchment(
polygonlayer = sa1income,
targetlayer = testcycleway,
calccols = c('Total'),
calccols = c("Total"),
distance = 800,
projection = 'austalbers',
projection = "austalbers",
dissolve = TRUE
)
expect_is(t1, "SpatialPolygonsDataFrame")
files_to_remove = list.files(pattern = "smallsa|testcycleway")
file.remove(files_to_remove) # tidy up
})
files_to_remove <- list.files(pattern = "smallsa|testcycleway")
file.remove(files_to_remove) # tidy up
}
)
@@ -8,4 +8,5 @@ test_that(
l <- od2line(flow = flow, zones = cents)
data("flowlines")
expect_true(class(l) == "SpatialLinesDataFrame")
})
}
)
@@ -3,6 +3,7 @@ context("Test overline function")
test_that(
desc = "overline generates a SpatialLinesDataFrame",
code = {
rnet <- overline(sl = routes_fast[2:4,], attrib = "length")
rnet <- overline(sl = routes_fast[2:4, ], attrib = "length")
expect_is(object = rnet, class = "SpatialLinesDataFrame")
})
}
)
@@ -4,6 +4,7 @@ test_that(
desc = "read_table_builder returns a data.frame",
code = {
data_dir <- system.file("extdata", package = "stplanr")
t1 <- read_table_builder(file.path(data_dir, 'SA1Population.csv'))
t1 <- read_table_builder(file.path(data_dir, "SA1Population.csv"))
expect_is(t1, "data.frame")
})
}
)
@@ -3,8 +3,9 @@ context("Test route_cyclestreet function")
test_that(
desc = "route_cyclestreet generates a SpatialLinesDataFrame output",
code = {
if(!Sys.getenv("CYCLESTREET") == ""){ # only run test if user has set an api key
if (!Sys.getenv("CYCLESTREET") == "") { # only run test if user has set an api key
route_f <- route_cyclestreet(c(-1.55, 53.80), c(-1.76, 53.80))
expect_true(grepl(pattern = "SpatialLinesDataFrame|sf", class(route_f)))
}
})
}
)