-
Notifications
You must be signed in to change notification settings - Fork 2
/
postCodeMatrix.R
94 lines (70 loc) · 2.96 KB
/
postCodeMatrix.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
library(rgdal)
library(rgeos)
library(httr)
library(jsonlite)
apikey <- read.csv("apikey.csv")
apikey <- apikey[1,]
#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 <- c("s118sa","s71bx","ls84dr")
#no whitespace
postcodes <- gsub(" ", "", postcodes, fixed=TRUE)
venues <- venuesNPostcodes[,1]
#OD matrix
distresults <- matrix(nrow=length(postcodes) , ncol=length(postcodes))
timeresults <- matrix(nrow=length(postcodes) , ncol=length(postcodes))
distresultstext <- matrix(nrow=length(postcodes) , ncol=length(postcodes))
timeresultstext <- matrix(nrow=length(postcodes) , ncol=length(postcodes))
#matrix API wants addresses separated by vertical bar
postcodeString <- paste(postcodes, collapse="|")
#set google distance matrix query
#See https://developers.google.com/maps/documentation/distancematrix/ for option info
qry <- paste("origins=", postcodeString,
"&destinations=", postcodeString ,
"&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)
#Parse postcodes
#"results are returned in rows, each row containing one origin paired with each destination"
#So assume origins are columns, cycle through each row and get elements
for(origins in 1:length(postcodes)) {
for(dests in 1:length(postcodes)) {
distresults[dests,origins] <- store$rows[[origins]]$elements[[dests]]$distance$value
timeresults[dests,origins] <- store$rows[[origins]]$elements[[dests]]$duration$value
distresultstext[dests,origins] <- store$rows[[origins]]$elements[[dests]]$distance$text
timeresultstext[dests,origins] <- store$rows[[origins]]$elements[[dests]]$duration$text
}
}
#Add postcode labels
rownames(distresultstext) <- postcodes
colnames(distresultstext) <- postcodes
rownames(timeresultstext) <- postcodes
colnames(timeresultstext) <- postcodes
#get ride of odd diagonal. 1 minute to get to where I'm standing?
distresultstext[distresultstext == "1 m"] <- "0"
timeresultstext[timeresultstext == "1 min"] <- "0 min"
write.csv(distresultstext, file="distresults.csv")
write.csv(timeresultstext, file="timeresults.csv")
#Or use venue names, more logically!
rownames(distresultstext) <- venues
colnames(distresultstext) <- venues
rownames(timeresultstext) <- venues
colnames(timeresultstext) <- venues
write.csv(distresultstext, file="distresults_venues.csv")
write.csv(timeresultstext, file="timeresults_venues.csv")