Skip to content

Instantly share code, notes, and snippets.

@henryjameslau
Created May 17, 2019 14:38
Show Gist options
  • Save henryjameslau/258fcf764601b88774e6fb03f5532504 to your computer and use it in GitHub Desktop.
Save henryjameslau/258fcf764601b88774e6fb03f5532504 to your computer and use it in GitHub Desktop.
---
title: "Travel time wales"
output: html_notebook
---
```{r}
library("propeR")
library("progress")
library("MASS")
library("foreach")
library("doParallel")
library("doSNOW")
```
connect to OTP
```{r}
otpcon<-otpConnect()
```
change importLocationData to not drop point if no lat lon
```{r}
insertSource("~/Documents/GitHub/access-to-services/propeR/R/data.R", package = "propeR", functions = "importLocationData")
```
Read all the locations
```{r}
originPoints <- importLocationData("./origin.csv")
destinationPoints <-importLocationData("./destination.csv")
```
Here's a function that takes just one argument which is the row number of the locations to process. The mode, starttime, arriveby, date are all hard coded.
We've done it this way so we can process stuff in parallel
```{r}
fx<-function(k){
message("processing number ",k)
point_to_point<-propeR::otpTripTime(otpcon, from = originPoints[k,]$lat_lon,to=destinationPoints[k,]$lat_lon,modes="CAR", detail = T,date = "2019-03-25",time = "09:00am", arriveBy = 'true')
from=originPoints[k,]
to=destinationPoints[k,]
if (!is.null(point_to_point$errorId)){
if (point_to_point$errorId == "OK") {
point_to_point_table_overview_tmp <- point_to_point$itineraries
point_to_point_table_overview_tmp["origin"] <- from$name
point_to_point_table_overview_tmp["destination"] <- to$name
point_to_point_table_overview_tmp["distance_km"] <- round(sum(point_to_point$output_table$distance) / 1000, digits = 2)
point_to_point_table_overview_tmp["journey_details"] <- jsonlite::toJSON(point_to_point$output_table)
} else {
point_to_point_table_overview_tmp <- data.frame(
"start" = NA,
"end" = NA,
"duration" = NA,
"driveTime" = NA,
"transitTime" = NA,
"waitingTime" = NA,
"transfers" = NA,
"origin" = from$name,
"destination" = to$name,
"distance_km" = NA,
"journey_details" = NA)
}
return(point_to_point_table_overview_tmp)
}
message(k)
} #end of function
```
Do things in parallel
```{r}
numCores<-detectCores()
cl<-makeCluster(numCores)
#registerDoParallel(numCores)
registerDoSNOW(cl)
totalRows <- nrow(destinationPoints)
#make a loop for to spit out csvs periodically
iterations = ceiling(totalRows/1000)
startingSequence<-seq(from=1,to=totalRows,by=1000)
endSequence<-if(iterations==1){}else{seq(from=1000,to=totalRows,by=1000)}
endSequence<-append(endSequence,totalRows,after = length(endSequence))
for(i in 1:iterations){
if(iterations>1){message("\n Starting iteration ",i," of ",iterations,"\n")}
message("processing rows ", startingSequence[i]," to ",endSequence[i])
stamp <- format(Sys.time(), "%Y_%m_%d_%H_%M_%S")
pb <- txtProgressBar(max=if(i==iterations){if(totalRows%%1000==0){1000}else{totalRows%%1000}}else{1000},style=3)
progress <- function(n) setTxtProgressBar(pb,n)
opts <- list(progress=progress)
res <- foreach(k=startingSequence[i]:endSequence[i],.combine=rbind, .inorder = FALSE,.options.snow=opts) %dopar% {
fx(k)
}
point_to_point_table_overview_out <- res[, c(8, 9, 1, 2, 10, 3, 4, 5, 6, 7, 11)]
colnames(point_to_point_table_overview_out) <-
c(
"origin",
"destination",
"start_time",
"end_time",
"distance_km",
"duration_mins",
"drive_time_mins",
"transit_time_mins",
"waiting_time_mins",
"transfers",
"journey_details")
write.csv(
point_to_point_table_overview_out,
file = paste0("./", "/pointToPointLoop-", stamp, ".csv"),
row.names = F)
#copy the time to complete a loop thing here
}
#clean up the cluster
stopImplicitCluster()
close(pb)
stopCluster(cl)
```
Read all csvs in folder and merge
```{r}
folder <- "./"
filenames <- list.files(folder, pattern="^pointToPoint")
all_files <- Reduce(rbind, lapply(filenames, read.csv))
write.csv(all_files,'all_postcodes.csv', row.names=FALSE)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment