Skip to content

Commit

Permalink
Deprecate gtfs2sldf for #383
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Apr 5, 2020
1 parent 3710a76 commit 820791d
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 113 deletions.
9 changes: 8 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# stplanr (development version)
# stplanr 0.5.2

## BUG FIXES

- Documentation fixes (#384)
- A bug affecting `route()` function calls when `pbapply` was not installed has been fixed (#386)
- `oneway()` has been deprecated in favour of faster and easier-to-maintain function `od_oneway()` (also see the in-development `od` package): https://github.com/ropensci/stplanr/pull/387
- Various other changes have been made to accomodate the dev version of `dplyr` (#383)

# stplanr 0.5.1

Expand Down
5 changes: 5 additions & 0 deletions R/SpatialLinesNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ validity = function(object) {
#' plot(sln)
#' points(sln2points(sln)[1, ], cex = 5)
#' points(sln2points(sln)[50, ], cex = 5)
#' \donttest{
#' shortpath <- sum_network_routes(sln, 1, 50, sumvars = "length")
#' plot(shortpath, col = "red", lwd = 4, add = TRUE)
#' points(sln2points(sln)[35, ], cex = 5)
Expand All @@ -96,6 +97,7 @@ validity = function(object) {
#' plot(sln_sf)
#' shortpath <- sum_network_routes(sln_sf, 1, 50, sumvars = "length")
#' plot(shortpath$geometry, col = "red", lwd = 4, add = TRUE)
#' }
SpatialLinesNetwork <- function(sl, uselonglat = FALSE, tolerance = 0.000) {
UseMethod("SpatialLinesNetwork")
}
Expand Down Expand Up @@ -517,11 +519,14 @@ find_network_nodes <- function(sln, x, y = NULL, maxdist = 1000) {
#' @family rnet
#'
#' @examples
#' \donttest{
#' # tests fail on dev version of dplyr
#' sln <- SpatialLinesNetwork(route_network)
#' weightfield(sln) # field used to determine shortest path
#' shortpath <- sum_network_routes(sln, start = 1, end = 50, sumvars = "length")
#' plot(shortpath, col = "red", lwd = 4)
#' plot(sln, add = TRUE)
#' }
#' @export
sum_network_routes <- function(sln, start, end, sumvars, combinations = FALSE) {
if (!is(sln, "SpatialLinesNetwork") & !is(sln, "sfNetwork")) {
Expand Down
123 changes: 123 additions & 0 deletions R/deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,126 @@ onewayid.SpatialLines <- function(x, attrib, id1 = names(x)[1], id2 = names(x)[2

return(x_oneway)
}

#' Import GTFS shapes and route data to SpatialLinesDataFrame.
#'
#' Takes a string with the file path of the zip file with the GTFS feed,
#' imports the shapes (geometry), route and agency data and returns a
#' SpatialLinesDataFrame for the GTFS feed.
#'
#' @param gtfszip String with the file path of the GTFS feed zip file
#' @export
#' @examples
#' f <- system.file("extdata", "beartransit-ca-us.zip", package = "stplanr")
#' # update file to latest version
#' # see https://code.google.com/p/googletransitdatafeed/wiki/PublicFeeds
#' u <- "http://data.trilliumtransit.com/gtfs/beartransit-ca-us/beartransit-ca-us.zip"
#' # download.file(u, f)
#' gtfs <- gtfs2sldf(gtfszip = f)
#' plot(gtfs, col = gtfs$route_long_name)
#' plot(gtfs[gtfs$route_long_name == "Central Campus", ])
#' \dontrun{
#' # An example of a larger gtfs feed
#' download.file(
#' "http://www.yrt.ca/google/google_transit.zip",
#' paste0(tempdir(), "/gtfsfeed.zip")
#' )
#' yrtgtfs <- gtfs2sldf(paste0(tempdir(), "/gtfsfeed.zip"))
#' sp::plot(yrtgtfs, col = paste0("#", yrtgtfs$route_color))
#' }
gtfs2sldf <- function(gtfszip = "") {
.Deprecated(new = "read_gtfs", package = "gtfs2gps", msg = "Many packages read gtfs files")
if (gtfszip == "") {
stop("Zip file required")
}
if (file.exists(gtfszip) == FALSE) {
stop("Specified zip file does not exist")
}

gtfsfiles <- unzip(gtfszip, exdir = tempdir())

gtfstrips <-
read.csv(stringsAsFactors = TRUE, paste0(tempdir(), "/trips.txt"))
if (all(charToRaw(substr(colnames(gtfstrips)[1], 1, 3)) == c(as.raw(239), as.raw(46), as.raw(46)))) {
gtfstrips <-
read.csv(
stringsAsFactors = TRUE,
paste0(tempdir(), "/trips.txt"),
fileEncoding = "UTF-8-BOM"
)
gtfsroutes <-
read.csv(
stringsAsFactors = TRUE,
paste0(tempdir(), "/routes.txt"),
fileEncoding = "UTF-8-BOM"
)
gtfsagency <-
read.csv(
stringsAsFactors = TRUE,
paste0(tempdir(), "/agency.txt"),
fileEncoding = "UTF-8-BOM"
)
gtfsshape <-
read.csv(
stringsAsFactors = TRUE,
paste0(tempdir(), "/shapes.txt"),
fileEncoding = "UTF-8-BOM"
)
}
else {
gtfsroutes <-
read.csv(stringsAsFactors = TRUE, paste0(tempdir(), "/routes.txt"))
gtfsagency <-
read.csv(stringsAsFactors = TRUE, paste0(tempdir(), "/agency.txt"))
gtfsshape <-
read.csv(stringsAsFactors = TRUE, paste0(tempdir(), "/shapes.txt"))
}

if (nrow(gtfsshape) == 0) {
stop("GTFS shapes.txt file is empty.")
}

unlink(gtfsfiles)

gtfslines <- sp::SpatialLinesDataFrame((
gtfsshape %>%
dplyr::group_by_( ~ shape_id) %>%
dplyr::arrange_( ~ shape_pt_sequence) %>%
dplyr::do_(gtfsline = "sp::Lines(sp::Line(as.matrix(.[,c('shape_pt_lon','shape_pt_lat')])),unique(.$shape_id))") %>%
dplyr::ungroup() %>%
dplyr::do_(
gtfsline = "sp::SpatialLines(.[[2]],
proj4string = sp::CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'))"
)
)[[1]][[1]],
data = gtfstrips %>%
dplyr::inner_join(gtfsroutes) %>%
dplyr::distinct_(
~ route_id,
~ shape_id,
~ route_short_name,
~ route_long_name,
~ route_desc,
~ route_type,
~ route_color,
~ route_text_color,
~ agency_id
) %>%
dplyr::select_(
~ route_id,
~ shape_id,
~ route_short_name,
~ route_long_name,
~ route_desc,
~ route_type,
~ route_color,
~ route_text_color,
~ agency_id
) %>%
dplyr::inner_join(gtfsagency) %>%
dplyr::do_("`rownames<-`(.,.$shape_id)")
)
rm(gtfstrips, gtfsshape, gtfsagency, gtfsroutes)
return(gtfslines)
}

102 changes: 0 additions & 102 deletions R/gtfs.R

This file was deleted.

2 changes: 2 additions & 0 deletions man/SpatialLinesNetwork.Rd

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

2 changes: 1 addition & 1 deletion man/gtfs2sldf.Rd

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

2 changes: 1 addition & 1 deletion man/onewayid.Rd

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

2 changes: 1 addition & 1 deletion man/stplanr-deprecated.Rd

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

3 changes: 3 additions & 0 deletions man/sum_network_routes.Rd

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

18 changes: 11 additions & 7 deletions vignettes/stplanr-route-nets.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ xy_nodes <- stplanr::find_network_nodes(sln = sln, x = x, y = y)

# Routing on route networks

Currently not running due to issues with dev version of `dplyr`:

https://github.com/ropensci/stplanr/issues/383

```{r, out.width="49%", fig.show='hide'}
plot(rnet$geometry)
plot(sln_nodes, add = TRUE)
xy_path <- sum_network_routes(sln = sln, start = xy_nodes[1], end = xy_nodes[2], sumvars = "length")
# xy_path = sum_network_links(sln = sln, start = xy_nodes[1], end = xy_nodes[2])
plot(rnet$geometry)
plot(xy_sf$geometry, add = TRUE)
plot(xy_path$geometry, add = TRUE, lwd = 5)
# plot(rnet$geometry)
# plot(sln_nodes, add = TRUE)
# xy_path <- sum_network_routes(sln = sln, start = xy_nodes[1], end = xy_nodes[2], sumvars = "length")
# # xy_path = sum_network_links(sln = sln, start = xy_nodes[1], end = xy_nodes[2])
# plot(rnet$geometry)
# plot(xy_sf$geometry, add = TRUE)
# plot(xy_path$geometry, add = TRUE, lwd = 5)
```

# Adding new nodes
Expand Down

0 comments on commit 820791d

Please sign in to comment.