-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
280 additions
and
33,832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#' Update the data inside the UK2GTFS package | ||
#' | ||
#' As UK2GTFS has large datasets that update separately to the R package they | ||
#' are checked and downloaded at package load time. This function checks for and | ||
#' downloaded any updated to the data. | ||
#' | ||
#' Raw data can be viewed and contributed to at | ||
#' https://github.com/ITSLeeds/UK2GTFS-data | ||
#' | ||
#' @export | ||
#' | ||
update_data <- function(){ | ||
|
||
check <- check_data() | ||
|
||
if(check$date_package != check$date){ | ||
|
||
if (interactive()) { | ||
|
||
response <- readline("UK2GTFS data is out of date. Do you want to update? (yes/no): ") | ||
response <- tolower(response) | ||
|
||
if (response == "yes" || response == "y") { | ||
message("Updating internal package data") | ||
download_data(check$tag_name, check$package_location, check$date) | ||
} else if (response == "no" || response == "n") { | ||
cat("You can rerun this check with update_data()") | ||
} else { | ||
cat("Invalid response.\n") | ||
} | ||
|
||
|
||
} else { | ||
message("Data not updated, run update_data()") | ||
#message("Updating internal package data") | ||
#download_data(check$tag_name, check$package_location, check$date) | ||
} | ||
|
||
} else { | ||
message("Your UK2GTFS data is up to date") | ||
} | ||
|
||
} | ||
|
||
#' Update the data inside the UK2GTFS package | ||
#' | ||
#' As UK2GTFS has large datasets that update separately to the R package they | ||
#' are checked and downloaded a package load time | ||
#' | ||
#' @noRd | ||
#' | ||
download_data <- function(tag_name, package_location, date){ | ||
|
||
dir.create(file.path(tempdir(),"UK2GTFS_load")) | ||
download.file(paste0("https://github.com/ITSLeeds/UK2GTFS-data/releases/download/", | ||
tag_name,"/all.zip"), | ||
destfile = file.path(tempdir(),"UK2GTFS_load/all.zip"), | ||
mode = "wb") | ||
utils::unzip(file.path(tempdir(),"UK2GTFS_load/all.zip"), | ||
exdir = file.path(package_location, "extdata")) | ||
unlink(file.path(tempdir(),"UK2GTFS_load"), recursive = TRUE) | ||
writeLines(date, file.path(package_location, "extdata/date.txt")) | ||
|
||
} | ||
|
||
|
||
#' Check if data in package is up to date | ||
#' | ||
#' As UK2GTFS has large datasets that update separately to the R package they | ||
#' are checked and downloaded a package load time | ||
#' @return TRUE if data is up-to-date or if unable to check | ||
#' @noRd | ||
|
||
check_data <- function(){ | ||
# Check date on data repo | ||
res = try(httr::GET("https://api.github.com/repos/ITSleeds/UK2GTFS-data/releases"), | ||
silent = TRUE) | ||
if(inherits(res, "try-error")){ | ||
message("Unable to check for latest data") | ||
return(TRUE) | ||
} | ||
|
||
res = RcppSimdJson::fparse(res$content) | ||
date = res$published_at[1] | ||
tag_name = res$tag_name[1] | ||
|
||
#Check if date.txt in package | ||
package_location <- system.file(package = "UK2GTFS") | ||
if(!file.exists(file.path(package_location, "extdata/date.txt"))){ | ||
writeLines("nodata", file.path(package_location, "extdata/date.txt")) | ||
} | ||
|
||
date_package <- readLines(file.path(package_location, "extdata/date.txt")) | ||
|
||
return(list(date_package = date_package, date = date, tag_name = tag_name, | ||
package_location = package_location)) | ||
} | ||
|
||
#' Load a built-in UK2GTFS dataset | ||
#' | ||
#' As UK2GTFS has large datasets that update separately to the R package they | ||
#' are checked and downloaded at package load time. | ||
#' | ||
#' This function loads a dataset. Examples are: | ||
#' | ||
#' | ||
#' "atco_areas" ATCO Admin Areas | ||
#' | ||
#' Association of Transport Coordinating Officers | ||
#' | ||
#' Boundaries of the ATCO Admin Areas. Note there are 4 national areas | ||
#' represented by a box around the UK. | ||
#' | ||
#' "atoc_agency" Agency.txt for ATOC data | ||
#' | ||
#' The ATOC data does not included sufficient information to build agency.txt | ||
#' So this data is provided in the package. | ||
#' | ||
#' "tiplocs" Tiploc Locations | ||
#' | ||
#' The ATOC data has inaccurate locations for many tiplocs, this is an improved dataset | ||
#' | ||
#' "naptan_missing" Bus Stop Locations missing from NapTAN | ||
#' | ||
#' A database of bus stops that are missing from the NAPTAN but are known to | ||
#' have been used. For some reason the official NAPTAN file is missing a small | ||
#' number of bus stops. This file contains a selection of bus stops that have | ||
#' appears in TransXchange files, but are missing in the NAPTAN. The have been | ||
#' assembled from a range of sources and may be of varying quality. | ||
#' | ||
#' In some cases the name of the Bus Stop has been identified but not the location. | ||
#' | ||
#' "naptan_replace" Bus Stop Locations wrong in the NaPTAN | ||
#' | ||
#' A database of bus stops that are wrong in the NAPTAN. These have been | ||
#' corrected with a mix of manual and automatic techniques. Contributions are | ||
#' welcome of improved locations. | ||
#' | ||
#' "rail_light" Light Rail Network | ||
#' | ||
#' A simplified version of the UK light rail network. | ||
#' | ||
#' "rail_heavy" Heavy Rail Network | ||
#' | ||
#' A simplified version of the UK heavy rail network. | ||
#' | ||
#' "historic_bank_holidays" Historic Bank Holidays | ||
#' | ||
#' Bank holidays from 2001 to 2018 in the UK. Note Wales has the same holidays | ||
#' as England. | ||
#' | ||
#' | ||
#' @param type name of data to be loaded e.g. "tiplocs" | ||
#' @return Loads named object into the global environment | ||
#' @export | ||
|
||
load_data <- function(type){ | ||
|
||
package_location <- system.file(package = "UK2GTFS") | ||
load(file.path(package_location, "extdata", paste0(type,".rda")), | ||
envir = globalenv()) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Run when package loads | ||
.onLoad <- function(libname, pkgname){ | ||
update_data() | ||
} | ||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nodata |
Oops, something went wrong.