GithubHelp home page GithubHelp logo

What to do with BH data? about gtfs2gps HOT 3 CLOSED

ipeagit avatar ipeagit commented on July 28, 2024
What to do with BH data?

from gtfs2gps.

Comments (3)

rafapereirabr avatar rafapereirabr commented on July 28, 2024

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.

pedro-andrade-inpe avatar pedro-andrade-inpe commented on July 28, 2024

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.

rafapereirabr avatar rafapereirabr commented on July 28, 2024

ok closing issue

from gtfs2gps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.