Skip to content

Commit

Permalink
updates for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hodge committed Jun 19, 2019
1 parent 9824019 commit 3dd5808
Show file tree
Hide file tree
Showing 29 changed files with 928 additions and 368 deletions.
2 changes: 1 addition & 1 deletion propeR/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: propeR
Title: An R tool for analysing multimodal transport
Version: 1.4.0
Version: 1.4.1
Description: An R tool for analysing multimodal transport based on a graph built using OpenTripPlanner (OTP).
The generation of the OTP graph is based on a GTFS feed. The package includes a pre-OTP GTFS feed cleaner.
The output functions include a: point-to-point map, which analyses from a single origin to destination;
Expand Down
5 changes: 5 additions & 0 deletions propeR/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ export(isochroneMultiIntersect)
export(isochroneMultiIntersectSensitivity)
export(isochroneMultiIntersectTime)
export(isochroneTime)
export(locationValidatorIsochrone)
export(nominatimNodeSearch)
export(nominatimObjectSearch)
export(otpChoropleth)
export(otpConnect)
export(otpIsochrone)
export(otpJourneyChecker)
export(otpTripDistance)
export(otpTripTime)
export(pointToPoint)
Expand All @@ -21,3 +25,4 @@ export(pointToPointNearest)
export(pointToPointTime)
export(postcodeToDecimalDegrees)
export(postcodeToDecimalDegrees_backup)
export(stopFIX)
2 changes: 2 additions & 0 deletions propeR/R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ importLocationData <- function(src,
colnames(data_points)[which(names(data_points) == latcol)] <- "lat"
colnames(data_points)[which(names(data_points) == postcodecol)] <- "postcode"

data_points$name <- as.character(data_points$name)

if ("lat" %in% colnames(data_points))
{
if (!("lon" %in% colnames(data_points))) {
Expand Down
85 changes: 85 additions & 0 deletions propeR/R/gtfs.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ cleanGTFS <- function(gtfs.dir,
message("the length of stop_times.txt after cleaning was: ",
nrow(stop_times))

message("the number of stops with coords 0,0 are: ",
nrow(stops[stops$stop_lat == 0,]))

if (nrow(stops[stops$stop_lat == 0,]) > 0){
unzip(zipfile = system.file("extdata", "stops_light.zip", package = "propeR"), exdir = tmp.dir)

stops_light <- read.csv(paste0(tmp.dir,'/stops_light.csv'),
sep = ",",
as.is = TRUE)

for (i in 1:nrow(stops)){
if (stops$stop_lat[i] == 0){

idx <- which(stops$stop_id[i] == stops_light$ATCOCode)
if (length(idx) == 0){
if (length(which(stops$stop_code[i] == stops_light$NaptanCode)) == 1){
idx <- which(stops$stop_code[i] == stops_light$NaptanCode)
}
}
if (length(idx) == 0){
idx <- which(substring(stops$stop_id[i],2) == stops_light$ATCOCode)
}

stops$stop_lon[i] <- stops_light$Longitude[idx]
stops$stop_lat[i] <- stops_light$Latitude[idx]

cat('Stop ', stops$stop_id[i], ' is now at: ', stops$stop_lon[i], ', ', stops$stop_lat[i],'\n')
}
}
}
# todo: no need to read/write all of these
# Writes files to txt

Expand All @@ -107,3 +137,58 @@ cleanGTFS <- function(gtfs.dir,
unlink(tmp.dir, recursive = TRUE) # Deletes tmp_folder

}

##' Fixes 0,0 in stops GTFS file
##'
##' Fixes 0,0 in stops GTFS file
##'
##' @param gtfs.dir The directory for the stop file
##' @param stops.filename The name of the stop file
##' @return Returns a stop file with fixed lats and lons
##' @author Michael Hodge
##' @examples
##' cleanGTFS(stopsfile)
##'
##' @export
stopFIX <- function(gtfs.dir,
stops.filename) {

stops.file <- paste0(gtfs.dir, "/", stops.filename)

stops <-
read.csv(stops.file,
sep = ",",
as.is = TRUE)

tmp.dir <- paste0(gtfs.dir, "/tmp_folder")
unlink(tmp.dir, recursive = TRUE) # Deletes tmp_folder
dir.create(tmp.dir)
prefix <- paste0(tmp.dir, "/")

unzip(zipfile = system.file("extdata", "stops_light.zip", package = "propeR"), exdir = tmp.dir)

message("the number of stops with coords 0,0 are: ",
nrow(stops[stops$stop_lat == 0,]))

stops_light <- read.csv(paste0(tmp.dir,'/stops_light.csv'),
sep = ",",
as.is = TRUE)

for (i in 1:nrow(stops)){
if (stops$stop_lat[i] == 0){

idx <- which(stops$stop_id[i] == stops_light$ATCOCode)

stops$stop_lon[i] <- stops_light$Longitude[i]
stops$stop_lat[i] <- stops_light$Latitude[i]

cat('Stop ', stops$stop_id[i], ' is now at: ', stops$stop_lat[i], ', ', stops$stop_lon[i],'\n')
}
}

write.csv(stops, file = (paste0(gtfs.dir, "/stops_new.txt")), row.names =
FALSE)

unlink(tmp.dir, recursive = TRUE) # Deletes tmp_folder

}
142 changes: 142 additions & 0 deletions propeR/R/location-validator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
##' Checks the validity of locations for isochrones
##'
##' Calculates the journey time and details between multiple origins and/or destinations.
##' A comma separated value file of journey details is saved in the output folder.
##'
##' @param output.dir The directory for the output files
##' @param otpcon The OTP router URL, see ?otpcon for details
##' @param locationPoints The variable containing origin(s), see ?importLocationData for details
##' @param modes The mode of the journey, defaults to 'WALK'
##' @param cutoff Specify the isochrone cutoff distance, defaults to 20 mins
##' @param infoPrint Specifies whether you want some information printed to the console or not, default is TRUE
##' @author Michael Hodge
##' @examples
##' locationValidatorIsochrone(
##' output.dir = 'C:\Users\User\Documents',
##' otpcon,
##' locationPoints
##' )
##' @export
locationValidatorIsochrone <- function(output.dir,
otpcon = otpcon,
locationPoints = originPoints,
modes = 'WALK',
cutoff = 20,
infoPrint = T) {

#########################
#### SETUP VARIABLES ####
#########################

num.total <- nrow(locationPoints)

file_name <- format(Sys.time(), "%Y_%m_%d_%H_%M_%S")

unlink(paste0(output.dir, "/locationChecker-", file_name) , recursive = T)
dir.create(paste0(output.dir, "/locationChecker-", file_name))
dir.create(paste0(output.dir, "/locationChecker-", file_name, "/csv"))

if (infoPrint == T) {
cat("Now running the propeR locationValidatorIsochrone tool.\n", sep="")
}

###########################
#### CALL OTP FUNCTION ####
###########################

num.run <- 0

startDateAndTime = format(Sys.time(), "%Y-%m-%d %H:%M:%S")

start_time <- format(as.POSIXct(startDateAndTime), "%I:%M %p")
start_date <- as.Date(startDateAndTime)
time.taken <- vector()
calls.list <- c(0)
if (infoPrint == T) {
cat("Creating ", num.total, " checks, please wait...\n")
}

pb <- progress::progress_bar$new(
format = " running, changes made :what [:bar] :percent eta: :eta",
total = num.total, clear = FALSE, width= 60)

calls = {}

locationPoints$mod <- 0

check <- function(otpcon, from_origin, start_date, start_time, modes, cutoff){
isochrone <- propeR::otpIsochrone(
otpcon,
batch = T,
from = from_origin$lat_lon,
to = from_origin$lat_lon,
modes = modes,
cutoff = cutoff,
date = start_date,
time = start_time
)
return(isochrone)
}

i <- 1

repeat {
from_origin <- locationPoints[i,]

isochrone <- check(otpcon, from_origin, start_date, start_time, modes, cutoff)

if (isochrone$status == "ERROR"){

if (locationPoints$mod[i] > 0){
locationPoints$lat[i] <- locationPoints$lat[i] + ((runif(1, 0.01, 0.05) * sample(c(-1,1),size=1)) * locationPoints$mod[i])
locationPoints$lon[i] <- locationPoints$lon[i] + ((runif(1, 0.01, 0.05) * sample(c(-1,1),size=1)) * locationPoints$mod[i])
locationPoints$lat_lon[i] <- paste0(locationPoints$lat[i], ",", locationPoints$lon[i])
} else {
locationPoints$lat[i] <- locationPoints$lat[i]
locationPoints$lon[i] <- locationPoints$lon[i]
locationPoints$lat_lon[i] <- paste0(locationPoints$lat[i], ",", locationPoints$lon[i])
}

from_origin <- locationPoints[i,]

df <- propeR::nominatimNodeSearch(from_origin$lat, from_origin$lon)
sp::coordinates(from_origin) <- c("lon","lat")
sp::coordinates(df) <- c("lon","lat")

g = FNN::get.knnx(sp::coordinates(df), sp::coordinates(from_origin), k = 1)
pair = g$nn.index

locationPoints$lat[i] <- df[pair[1,1],]$lat
locationPoints$lon[i] <- df[pair[1,1],]$lon
locationPoints$lat_lon[i] <- paste0(locationPoints$lat[i], ",", locationPoints$lon[i])

locationPoints$mod[i] <- locationPoints$mod[i] + 1

if (!(i %in% calls)){
calls <- cbind(i,calls)
}

} else {
i <- i + 1
pb$tick(tokens = list(what = length(calls)))
}

if (i == nrow(locationPoints)){
break
}
}

if (infoPrint == T) {
cat("\nValidation complete, now saving outputs to ", output.dir, ", please wait.\n", sep="")
# cat("Journey details:\n", sep = "")
# cat("Trips possible: ", nrow(point_to_point_table_overview[!is.na(point_to_point_table_overview$duration_mins),]),"/",num.total,"\n", sep = "")
}

write.csv(
locationPoints,
file = paste0(output.dir, "/locationChecker-", file_name, "/csv/locationChecker-", file_name, ".csv"),
row.names = F)
if (infoPrint == T) {
cat("Outputs saved. Thanks for using propeR.\n")
}
}
Loading

0 comments on commit 3dd5808

Please sign in to comment.