R/transxchange.R

Defines functions transxchange2gtfs

Documented in transxchange2gtfs

#' TransXchange to GTFS
#'
#' @details
#' Convert transxchange files to GTFS
#'
#' @param path_in Path to ATOC File
#' @param path_out Path to where GTFS files should be saved
#' @param name name that should be given to the gtfs file, without the .zip extension
#' @param silent Logical, should progress be shown
#' @param ncores Numeric, When parallel processing how many cores to use
#' @param cal Calendar object from get_bank_holidays()
#' @param naptan Naptan stop locations from get_naptan()
#'
#' @details
#'
#' @export


transxchange2gtfs <- function(path_in,
                              path_out,
                              name = "gtfs",
                              silent = TRUE,
                              ncores = 1,
                              cal = get_bank_holidays(),
                              naptan = get_naptan()) {
  if (ncores == 1) {
    message(paste0(Sys.time(), " This will take some time, make sure you use 'ncores' to enable multi-core processing"))
  }
  zips <- list.files(path_in, pattern = ".zip", full.names = TRUE)
  if (length(zips) > 0) {
    if (!silent) {
      message(paste0(Sys.time(), " unzipping folders"))
    }
    dir.create(file.path(tempdir(), "txc"))
    foo <- pbapply::pblapply(zips, function(x) {
      utils::unzip(x, exdir = file.path(tempdir(), "txc"))
    })
  } else {
    stop("No zip folders found in path_in")
  }

  files <- list.files(file.path(tempdir(), "txc"), pattern = ".xml", full.names = TRUE)

  if (ncores == 1) {
    message(paste0(Sys.time(), " Importing TransXchange files"))
    res_all <- pbapply::pblapply(files[12], transxchange_import, run_debug = TRUE, full_import = FALSE)
    message(paste0(Sys.time(), " Converting to GTFS"))
    gtfs_all <- pbapply::pblapply(res_all, transxchange2gtfs, run_debug = TRUE, cal = cal, naptan = naptan)
  } else {
    cl <- parallel::makeCluster(ncores)
    # parallel::clusterExport(
    #   cl = cl,
    #   varlist = c("files", "cal", "naptan"),
    #   envir = environment()
    # )
    parallel::clusterEvalQ(cl, {
      library(UK2GTFS)
    })
    pbapply::pboptions(use_lb = TRUE)
    message(paste0(Sys.time(), " Importing TransXchange files"))
    res_all <- pbapply::pblapply(files,
      transxchange_import,
      run_debug = TRUE,
      full_import = FALSE,
      cl = cl
    )
    message(paste0(Sys.time(), " Converting to GTFS"))
    gtfs_all <- pbapply::pblapply(res_all,
      transxchange2gtfs,
      run_debug = TRUE,
      cal = cal,
      naptan = naptan,
      cl = cl
    )
    parallel::stopCluster(cl)
    rm(cl)
  }
  message(paste0(Sys.time(), " Merging GTFS objects"))
  gtfs_merged <- gtfs_merge(gtfs_all)

  message(paste0(Sys.time(), " Writing GTFS file"))
  write_gtfs(gtfs = gtfs_merged, folder = path_out, name = name)
}
mem48/UK2GTFS documentation built on Sept. 23, 2019, 6:05 p.m.