Skip to content

Commit

Permalink
Merge pull request #34 from richardellison/master
Browse files Browse the repository at this point in the history
Addition of gtfs2sldf function.
  • Loading branch information
richardellison committed Nov 28, 2015
2 parents fb2ee4a + 5a90629 commit fc31ad0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Imports:
rgdal,
rgeos,
dplyr,
magrittr,
geosphere,
RgoogleMaps,
openxlsx,
leaflet,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(format_stats19_ve)
export(gclip)
export(graphhopper_pat)
export(gsection)
export(gtfs2sldf)
export(islines)
export(line2df)
export(line2points)
Expand Down
76 changes: 76 additions & 0 deletions R/gtfs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#' 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 \dontrun{
#' downloader::download("http://www.yrt.ca/google/google_transit.zip",
#' paste0(tempdir(),"/gtfsfeed.zip"))
#' yrtgtfs <- gtfs2sldf(paste0(tempdir(),"/gtfsfeed.zip"))
#' plot(yrtgtfs,col=paste0("#",yrtgtfs$route_color))
#' }
gtfs2sldf <- function(gtfszip = "") {

if (gtfszip == "") {
stop("Zip file required")
}
if (file.exists(gtfszip) == FALSE) {
stop("Specified zip file does not exist")
}

`%>%` <- magrittr::`%>%`
gtfsfiles <- unzip(gtfszip, exdir = tempdir())

gtfstrips <- read.csv(paste0(tempdir(),"/trips.txt"))
gtfsroutes <- read.csv(paste0(tempdir(),"/routes.txt"))
gtfsagency <- read.csv(paste0(tempdir(),"/agency.txt"))
gtfsshape <- read.csv(paste0(tempdir(),"/shapes.txt"))

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)
}
25 changes: 25 additions & 0 deletions man/gtfs2sldf.Rd

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

0 comments on commit fc31ad0

Please sign in to comment.