-
Notifications
You must be signed in to change notification settings - Fork 2
/
postCodeMatrixLimitRateThenBind.R
104 lines (77 loc) · 2.99 KB
/
postCodeMatrixLimitRateThenBind.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
library(rgdal)
library(rgeos)
library(httr)
library(jsonlite)
library(reshape2)
#Base URL, will knock query together below
testURL <- "https://maps.googleapis.com/maps/api/distancematrix/json"
venuesNPostcodes <- read.csv("tramlineVenues_n_Postcodes.csv")
postcodes <- venuesNPostcodes[,2]
postcodes <- gsub(" ", "", postcodes, fixed=TRUE)
venues <- venuesNPostcodes[,1]
#turn postcodes into a melted list of pairs
#(This is one way of avoiding the matrix API limits)
postcodematrix <- matrix(nrow=length(postcodes) , ncol=length(postcodes))
rownames(postcodematrix) <- postcodes
colnames(postcodematrix) <- postcodes
postcodepairs <- melt(postcodematrix)
#Do the same with venue names - these will be used below for dcast
venuematrix <- matrix(nrow=length(venues) , ncol=length(venues))
rownames(venuematrix) <- venues
colnames(venuematrix) <- venues
venuepairs <- melt(venuematrix)
results <- matrix(nrow=nrow(postcodepairs) , ncol=4)
#loop over pairs, thus only two `elements' per query
for(i in 1:nrow(postcodepairs)) {
#set google distance matrix query
#See https://developers.google.com/maps/documentation/distancematrix/ for option info
qry <- paste("origins=", postcodepairs$Var1[i],
"&destinations=", postcodepairs$Var2[i] ,
"&sensor=FALSE",
"&mode=walking",
#"&key=",
#apikey,
sep=""#no spaces
)
#Get the JSON
gimme <- GET(
testURL,
query = qry,
#If using in Leeds University, obv: comment this out if not, or if using Leeds Uni wifi
#Use this to see details of proxy connection: c(use_proxy("www-cache.leeds.ac.uk:3128", 8080), verbose())
c(use_proxy("www-cache.leeds.ac.uk:3128", 8080))
)
#http://blog.rstudio.org/2014/03/21/httr-0-3/
stop_for_status(gimme)
store <- content(gimme)
#Being conservative: 0.3 seconds should hit ~66 elements per 10 seconds
Sys.sleep(0.3)
if(store$rows[[1]]$elements[[1]]$status=="OK") {
results[i,1] <- store$rows[[1]]$elements[[1]]$distance$value
results[i,2] <- store$rows[[1]]$elements[[1]]$duration$value
results[i,3] <- store$rows[[1]]$elements[[1]]$distance$text
results[i,4] <- store$rows[[1]]$elements[[1]]$duration$text
} else {
results[i,1] <- "xxx"
results[i,2] <- "xxx"
results[i,3] <- "xxx"
results[i42] <- "xxx"
}
}#end for
#Just verbal description of time and distance
textresults <- results[,3:4]
#join up postcodes. One matrix for distance, one for time
distance <- cbind(venuepairs, textresults)
distance <- distance[,c(1,2,4)]
distancematrix <- dcast(distance, Var1 ~ Var2, value.var = "1")
time <- cbind(venuepairs, textresults)
time <- time[,c(1,2,5)]
timematrix <- dcast(time, Var1 ~ Var2, value.var = "2")
#get ride of odd diagonal. 1 minute to get to where I'm standing?
distancematrix[distancematrix == "1 m"] <- "xx"
timematrix[timematrix == "1 min"] <- "xx"
#transpose so origins are columns
distancematrix <- t(distancematrix)
timematrix <- t(timematrix)
write.csv(distancematrix, file="distresults.csv")
write.csv(timematrix, file="timeresults.csv")