Last active
November 9, 2015 15:41
-
-
Save mrecos/6a7297bf444e7586f14b to your computer and use it in GitHub Desktop.
Function and code for comparing drive times from multiple origins to multiple destinations; with charting
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
# Driving distance from multuple starts to multilpe destinations | |
# modified from the code of gmapsdistance package by Rodrigo Azuero Melo | |
# https://cran.r-project.org/web/packages/gmapsdistance | |
gdist2 <- function (origin, destination, mode, key) | |
{ | |
typeKEY = class(key) | |
if (typeKEY != "character") { | |
stop("Key should be string") | |
} | |
if (mode != "driving" & mode != "walking" & mode != "bicycling" & | |
mode != "transit") { | |
stop("Mode of transportation not recognized. Mode should be one of 'bicycling', 'transit', 'driving', 'walking' ") | |
} | |
url0 = "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" | |
modeT = paste0("|&mode=", mode, "&language=en-EN") | |
urlfin = paste0(url0, origin, "|&destinations=", destination, | |
modeT, "&key=", key) | |
# urlfin <- "https://maps.googleapis.com/maps/api/distancematrix/xml?origins=Flourtown+PA|Philadelphia+PA&destinations=Burlington+NJ|&mode=driving&language=en-EN&key=AIzaSyBCkmICVDoIkV055x6l-gM-1Le82A3VsEI" | |
webpageXML <- getURL(urlfin) | |
webpageXML2 <- readLines(tc <- textConnection(webpageXML)) | |
close(tc) | |
webpageXMLA <- xmlParse(webpageXML2) | |
root = xmlRoot(webpageXMLA) | |
first = xmlChildren(root) | |
# for multi origin | |
initstatus = first$status[[1]] | |
initstat = as(initstatus, "character") | |
length_origin <- strsplit(origin,"|",fixed=TRUE) | |
length_origin <- length(length_origin[[1]]) | |
origins_xml <- first[c(2:(length_origin+1))] | |
origins <- NULL | |
for(i in 1:length_origin){ | |
origin_parse <- as(origins_xml[[i]][[1]], "character") | |
origin_parse <- strsplit(origin_parse,",")[[1]][1] | |
origins <- c(origins, origin_parse) | |
} | |
dest <- as(first[[length_origin+2]][[1]], "character") | |
rows = first[c((length_origin+3):(length_origin*2+3))] | |
TDS <- NULL | |
for(i in 1:length_origin){ | |
row_xml <- rows[[i]] | |
Status = xmlChildren(row_xml[[1]]) | |
Status = Status$status[1] | |
Status = Status$text | |
Status = as(Status, "character") | |
Time = xmlChildren(row_xml[[1]]) | |
Time = Time$duration[1] | |
Time = Time$value[1]$text | |
Time = round(as(Time, "numeric")/60,1) | |
Distance = xmlChildren(row_xml[[1]]) | |
Distance = Distance$distance[1] | |
Distance = Distance$value[1]$text | |
Distance = round(as(Distance, "numeric")*0.000621371,1) # convert meter to mile | |
MPH <- round(Distance/(Time/60),2) | |
output = data.frame(Time, Distance, MPH, Status) | |
TDS <- rbind(TDS, output) | |
} | |
TDS <- cbind(origins, TDS) | |
return(TDS) | |
} | |
require(RCurl) | |
require(XML) | |
require(ggplot2) | |
key <- "YOUR KEY HERE!!!" | |
mode <- "driving" | |
towns <- c("Wayne", "Plymouth+Meeting", "Germantown", "Flourtown","Oreland", | |
"Phoenixville", "Ambler", "Horsham", "King+of+Prusia", "Royersford", | |
"Erdehiem", "Wyndmoor", "Lafayette+Hill", "Ardmore", "Willowgrove", | |
"Blue+Bell", "Trooper", "Audubon", "Trappe", "Skippack", "Collegeville", | |
"Landsdale", "North+Whales", "Pottstown", "Fort+Washington", "East+Norriton") | |
origin <- paste(towns, collapse="+PA|") | |
origin <- paste(c(origin, "+PA"), collapse = "") | |
destination1 <- "Burlington+NJ" | |
destination2 <- "Conshohocken+PA" | |
results_burl <- gdist2(origin, destination1, mode, key) | |
results_conchy <- gdist2(origin, destination2, mode, key) | |
both_locals <- cbind(results_burl[,1:4], results_conchy[,1:4]) | |
both_locals$joint <- (both_locals[,2] + both_locals[,6])/2 | |
both_locals <- (both_locals[,c(1,2,3,4,6,7,8,9)]) | |
colnames(both_locals) <- c("Origin", "Burl-Time", "Burl-Miles", "Burl-MPH", | |
"Conchy-Time", "Conchy-Miles", "Conchy-MPH", | |
"Avg-Time") | |
both_locals$`Weighted-Avg` <- apply(both_locals[,c("Burl-Time","Conchy-Time")],1, | |
weighted.mean,w=c(0.65,0.35)) | |
both_locals <- both_locals[order(both_locals$`Weighted-Avg`),] | |
bcost <- both_locals$`Burl-MPH` | |
print(both_locals) | |
ggplot(both_locals, aes(x = Origin, y = `Weighted-Avg`, fill = `Weighted-Avg`)) + | |
geom_bar(stat="identity") + | |
scale_x_discrete(limits=rev(both_locals$Origin)) + | |
geom_hline(yintercept = both_locals[which(both_locals$Origin == "Germantown"),"Weighted-Avg"], | |
size = 1, color = "red", linetype="dashed") + | |
coord_flip() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment