Comments (3)
Our package will not work properly if the GTFS input has no shape info. I agree the appropriate approach here is to include an error message in the read_gtfs()
from gtfs2gps.
It already stops with an error if the data does not exit, therefore I believe this issue can be closed.
Just to register here, in case we need to change, I had implemented a new argument to allow reading gtfs with missing data. In case of needing this in the future, the code is below:
#' @title Read GTFS data into a list of data.tables
#' @description Read files of a zipped GTFS feed and load them to memory as a list of data.tables.
#' It will load the following files: "agency.txt", "calendar.txt", "routes.txt", "shapes.txt",
#' "stop_times.txt", "stops.txt", and "trips.txt". If some of these files do not exit,
#' it will stop with an error.
#' @param gtfszip A zipped GTFS data.
#' @param stopMissingFile Stop if find a missing file? The default value is TRUE.
#' Note that if this value is fales, the resulting data might not work properly
#' in the other functions of the package.
#' @export
#' @examples
#' library(gtfs2gps)
#'
#' poa <- read_gtfs(system.file("extdata/poa.zip", package="gtfs2gps"))
read_gtfs <- function(gtfszip, stopMissingFile = TRUE){
# Unzip files
tempd <- file.path(tempdir(), "gtfsdir") # create tempr dir to save GTFS unzipped files
unlink(normalizePath(paste0(tempd, "/", dir(tempd)), mustWork = FALSE), recursive = TRUE) # clean tempfiles in that dir
unzip(zipfile = gtfszip, exdir = tempd, overwrite = TRUE) # unzip files
unzippedfiles <- list.files(tempd) # list of unzipped files
if(stopMissingFile)
mystop <<- function(value) stop(message(paste0("Error: ", value)))
else
mystop <<- function(value) warning(message(paste0("Warning: ", value)))
result <- list()
# read files to memory
if("agency.txt" %in% unzippedfiles){result$agency <- data.table::fread(paste0(tempd,"/agency.txt"), encoding="UTF-8")} else{mystop("File routes.txt is missing")}
if("routes.txt" %in% unzippedfiles){result$routes <- data.table::fread(paste0(tempd,"/routes.txt"), encoding="UTF-8")} else{mystop("File routes.txt is missing")}
if("stops.txt" %in% unzippedfiles){result$stops <- data.table::fread(paste0(tempd,"/stops.txt"), encoding="UTF-8")} else{mystop("File stops.txt is missing")}
if("stop_times.txt" %in% unzippedfiles){result$stop_times <- data.table::fread(paste0(tempd,"/stop_times.txt"), encoding="UTF-8")} else{mystop("File stop_times.txt is missing")}
if("shapes.txt" %in% unzippedfiles){result$shapes <- data.table::fread(paste0(tempd,"/shapes.txt"), encoding="UTF-8")} else{mystop("File shapes.txt is missing")}
if("trips.txt" %in% unzippedfiles){result$trips <- data.table::fread(paste0(tempd,"/trips.txt"), encoding="UTF-8")} else{mystop("File trips.txt is missing")}
if("calendar.txt" %in% unzippedfiles){result$calendar <- data.table::fread(paste0(tempd,"/calendar.txt"), encoding="UTF-8")} else{mystop("File calendar.txt is missing")}
if("frequencies.txt" %in% unzippedfiles){result$frequencies <- data.table::fread(paste0(tempd,"/frequencies.txt"), encoding="UTF-8")}
mysub <- function(value) sub("^24:", "00:", value)
if(!is.null(result$stop_times)){
result$stop_times[, departure_time := data.table::as.ITime(mysub(departure_time), format = "%H:%M:%OS")]
result$stop_times[, arrival_time := data.table::as.ITime(mysub(arrival_time), format ="%H:%M:%OS")]
}
if(!is.null(result$frequencies)){
result$frequencies[, start_time := data.table::as.ITime(mysub(start_time), format = "%H:%M:%OS")]
result$frequencies[, end_time := data.table::as.ITime(mysub(end_time), format = "%H:%M:%OS")]
}
return(result)
}
A script that uses this functionality with BH data is shown below:
require(gtfs2gps)
require(dplyr)
local_gtfs_path <- "C:/Users/pedro/Dropbox/pesquisa/2019/IPEA/gtfs_bhtransit_20190219.zip"
gtfs <- read_gtfs(local_gtfs_path, stopMissingFile = FALSE)
gtfs
gtfs_shapes_as_sf(gtfs) %>%
sf::st_geometry() %>%
plot()
unique(gtfs$shapes$shape_id)
shape_ids <- paste0("shape", 800:900, "-I")
new_gtfs <- filter_by_shape_id(gtfs, shape_ids) %>%
filter_valid_stop_times()
unique(new_gtfs$shapes$shape_id)
gtfs_shapes_as_sf(new_gtfs) %>%
sf::st_geometry() %>%
plot()
unique(new_gtfs$shapes$shape_id) %>% length()
write_gtfs(new_gtfs, "inst/extdata/bh.zip")
from gtfs2gps.
ok closing issue
from gtfs2gps.
Related Issues (20)
- should gtfs2gps includes as gtfs class, such as gtfstools? HOT 9
- gtfs2gps::gtfs2gps not processing transport network sf object HOT 4
- example of adjust_speed function not working HOT 1
- writing a merged gtfs list got: 'gtfs' must inherit from the 'gtfs' class HOT 2
- gtfs2gps::gtfs2gps function not exporting file with proper units
- incompatibility with gtfstools HOT 1
- Vignette link does not work HOT 1
- Check performance of using gtfstools::string_to_seconds() HOT 2
- adjust_speed approach HOT 2
- Add `progress` parameter to gtfs2gps HOT 1
- New silent parameter to gtfs2gps()
- gtfs2gps function changing input parameter by reference HOT 1
- 'speed' values are NA , but not really HOT 3
- problems with spatial_resolution input argument
- Submit v2.1-0 to CRAN
- Add new parameter ncores
- passar apend_height p/ gtfs2emis
- fix documentation of parameter `ncores`
- Issues with the upcoming release of units 0.8-2 HOT 1
- Reactivate codecov
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gtfs2gps.