Skip to content

Commit

Permalink
Completely remove raster #332
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Apr 4, 2022
1 parent d877d52 commit 9fb103f
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 163 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Imports:
httr (>= 1.3.1),
jsonlite (>= 1.5),
stringr (>= 1.3.1),
raster (>= 2.6.7),
methods,
geosphere (>= 1.5.7),
Rcpp (>= 0.12.1),
Expand Down
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ S3method(geo_projected,sf)
S3method(geo_select_aeq,sf)
S3method(geo_select_aeq,sfc)
S3method(gsection,sf)
S3method(line2df,Spatial)
S3method(line2df,sf)
S3method(line2points,Spatial)
S3method(line2points,sf)
S3method(line2points,sfc)
S3method(line2points,sfg)
S3method(line2pointsn,Spatial)
S3method(line2pointsn,sf)
S3method(line2vertices,sf)
S3method(n_vertices,sf)
S3method(od2line,Spatial)
S3method(od2line,sf)
S3method(onewaygeo,sf)
S3method(overline,sf)
S3method(points2line,Spatial)
S3method(points2line,matrix)
S3method(points2line,sf)
S3method(points2odf,Spatial)
S3method(points2odf,sf)
Expand Down Expand Up @@ -125,8 +120,6 @@ importFrom(methods,as)
importFrom(methods,is)
importFrom(methods,new)
importFrom(methods,slot)
importFrom(raster,crop)
importFrom(raster,extent)
importFrom(rlang,.data)
importFrom(stats,setNames)
importFrom(utils,download.file)
Expand Down
19 changes: 4 additions & 15 deletions R/geo-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bbox_scale <- function(bb, scale_factor) {
#' plot(shp_bb, col = "red", reset = FALSE)
#' plot(geo_bb(routes_fast_sf, scale_factor = 0.8), col = "green", add = TRUE)
#' plot(routes_fast_sf$geometry, add = TRUE)
#' geo_bb(shp, output = "point")
geo_bb <- function(shp, scale_factor = 1, distance = 0, output = c("polygon", "points", "bb")) {
UseMethod("geo_bb")
}
Expand All @@ -56,11 +57,7 @@ geo_bb.sf <- function(shp, scale_factor = 1, distance = 0, output = c("polygon",
if (output == "polygon") {
return(bb)
} else if (output == "points") {
warning("Points currently unsupported")
# bb_point <- sp::SpatialPoints(raster::geom(bb_sp)[1:4, c(5, 6)])
# bb_point <- sf::st_as_sf(bb_point)
# sf::st_crs(bb_point) <- sf::st_crs(shp)
# return(bb_point)
return(sf::st_cast(x = bb, to = "POINT")[1:4])
} else if (output == "bb") {
return(geo_bb_matrix(bb))
}
Expand All @@ -77,11 +74,7 @@ geo_bb.bbox <- function(shp, scale_factor = 1, distance = 0, output = c("polygon
if (output == "polygon") {
return(bb)
} else if (output == "points") {
warning("Points currently unsupported")
# bb_point <- sp::SpatialPoints(raster::geom(bb_sp)[1:4, c(5, 6)])
# bb_point <- sf::st_as_sf(bb_point)
# sf::st_crs(bb_point) <- sf::st_crs(shp)
# return(bb_point)
return(sf::st_cast(x = bb, to = "POINT")[1:4])
} else if (output == "bb") {
return(geo_bb_matrix(bb))
}
Expand All @@ -100,11 +93,7 @@ geo_bb.matrix <- function(shp, scale_factor = 1, distance = 0, output = c("polyg
if (output == "polygon") {
return(bb)
} else if (output == "points") {
warning("Points currently unsupported")
# bb_point <- sp::SpatialPoints(raster::geom(bb_sp)[1:4, c(5, 6)])
# bb_point <- sf::st_as_sf(bb_point)
# sf::st_crs(bb_point) <- sf::st_crs(shp)
# return(bb_point)
return(sf::st_cast(x = bb, to = "POINT")[1:4])
} else if (output == "bb") {
return(geo_bb_matrix(bb))
}
Expand Down
70 changes: 2 additions & 68 deletions R/od-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ od2odf <- function(flow, zones) {
#' od_coords(from = cents[1, ], to = cents[2, ]) # Spatial points
#' od_coords(cents_sf[1:3, ], cents_sf[2:4, ]) # sf points
#' # od_coords("Hereford", "Leeds") # geocode locations
#' od_coords(flowlines[1:3, ])
#' od_coords(flowlines_sf[1:3, ])
od_coords <- function(from = NULL, to = NULL, l = NULL) {
if (is(object = from, class2 = "sf")) {
Expand Down Expand Up @@ -341,17 +340,6 @@ line2df.sf <- function(l) {
tx = dplyr::last(!!X), ty = dplyr::last(!!Y)
)
}
#' @export
line2df.Spatial <- function(l) {
ldf_geom <- raster::geom(l)
dplyr::group_by(dplyr::as_tibble(ldf_geom), object) %>%
dplyr::summarise(
fx = dplyr::first(x),
fy = dplyr::first(y),
tx = dplyr::last(x),
ty = dplyr::last(y)
)
}

#' Convert a spatial (linestring) object to points
#'
Expand Down Expand Up @@ -387,23 +375,6 @@ line2points <- function(l, ids = rep(1:nrow(l))) {
UseMethod("line2points")
}
#' @export
line2points.Spatial <- function(l, ids = rep(1:nrow(l), each = 2)) {
if (!requireNamespace("sp", quietly = TRUE)) stop("sp package required")
for (i in 1:length(l)) {
lcoords <- sp::coordinates(l[i, ])[[1]][[1]]
pmat <- matrix(lcoords[c(1, nrow(lcoords)), ], nrow = 2)
lpoints <- sp::SpatialPoints(pmat)
if (i == 1) {
out <- lpoints
} else {
out <- raster::bind(out, lpoints)
}
}
sp::proj4string(out) <- sp::proj4string(l)
out <- sp::SpatialPointsDataFrame(coords = out, data = data.frame(id = ids))
out
}
#' @export
line2points.sf <- function(l, ids = rep(1:nrow(l), each = 2)) {
y_coords <- x_coords <- double(length = length(ids)) # initiate coords
coord_matrix <- cbind(x_coords, y_coords)
Expand Down Expand Up @@ -437,15 +408,6 @@ line2pointsn <- function(l) {
UseMethod("line2pointsn")
}
#' @export
line2pointsn.Spatial <- function(l) {
if (!requireNamespace("sp", quietly = TRUE)) stop("sp package required")
if (!requireNamespace("raster", quietly = TRUE)) stop("raster package required")
spdf <- raster::geom(l)
p <- sp::SpatialPoints(coords = spdf[, c("x", "y")])
raster::crs(p) <- raster::crs(l)
p
}
#' @export
line2pointsn.sf <- function(l) {
suppressWarnings(sf::st_cast(l, "POINT"))
}
Expand Down Expand Up @@ -574,22 +536,13 @@ od_dist <- function(flow, zones) {

#' Convert a series of points, or a matrix of coordinates, into a line
#'
#' This is a simple wrapper around [spLines()] that makes the creation of
#' `SpatialLines` objects easy and intuitive
#' This function makes that makes the creation of `sf`
#' objects with LINESTRING geometries easy.
#'
#' @param p A spatial (points) obect or matrix representing the coordinates of points.
#' @family lines
#' @export
#' @examples
#' p <- matrix(1:4, ncol = 2)
#' library(sp)
#' l <- points2line(p)
#' plot(l)
#' l <- points2line(cents)
#' plot(l)
#' p <- line2points(routes_fast)
#' l <- points2line(p)
#' plot(l)
#' l_sf <- points2line(cents_sf)
#' plot(l_sf)
points2line <- function(p) {
Expand All @@ -599,25 +552,6 @@ points2line <- function(p) {
points2line.sf <- function(p) {
points2flow(p = p)
}
#' @export
points2line.Spatial <- function(p) {
if (!requireNamespace("sp", quietly = TRUE)) stop("sp package required")
if (!requireNamespace("raster", quietly = TRUE)) stop("raster package required")
if (is(p, "SpatialPoints")) {
p_proj <- sp::proj4string(p)
p <- sp::coordinates(p)
} else {
p_proj <- NA
}
l <- points2line(p)
raster::crs(l) <- p_proj
l
}
#' @export
points2line.matrix <- function(p) {
l <- raster::spLines(p)
l
}
#' Summary statistics of trips originating from zones in OD data
#'
#' This function takes a data frame of OD data and
Expand Down
1 change: 0 additions & 1 deletion R/stplanr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#' @importFrom methods as slot
#' @importFrom stats setNames
#' @importFrom utils read.csv
#' @importFrom raster extent crop
#' @importFrom geosphere distHaversine
#' @importFrom Rcpp evalCpp
#' @importFrom methods is new
Expand Down
1 change: 1 addition & 0 deletions man/geo_bb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/od_coords.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions man/points2line.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 0 additions & 51 deletions tests/benchmarks/line2route-parallel.R

This file was deleted.

8 changes: 0 additions & 8 deletions tests/benchmarks/test-error-handling-line2route.R

This file was deleted.

0 comments on commit 9fb103f

Please sign in to comment.