-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgeocode-google.r
46 lines (35 loc) · 1.21 KB
/
geocode-google.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
if (!exists("key")) message("You need to define a key variable containing the contents of your google maps API key")
geocode_url <- function(address) {
geo <- "http://maps.google.com/maps/geo?"
params <- c(
key = key,
q = address,
output = "csv"
)
p <- paste(names(params), "=", laply(params,URLencode), sep="", collapse ="&")
paste(geo, p, sep="")
}
geocode <- function(addresses) {
paths <- laply(addresses, geocode_url)
read <- function(path) {
suppressWarnings(read.csv(url(path), header = FALSE))
}
df <- ldply(paths, read, .progress = "text")
names(df) <- c("status", "accuracy", "lat", "long")
df
}
addy <- function(row) {
paste(row$street, ", ", row$city, " ", row$zip, sep="")
}
addresses <- read.csv("addresses.csv")
while(TRUE) {
todo <- which(is.na(addresses$lat))[1:100]
todo <- todo[!is.na(todo)]
stopifnot(length(todo) > 0)
loc <- geocode(addy(addresses[todo, ]))
addresses[todo, names(loc)] <- loc
write.table(addresses, "addresses.csv", sep=",", row=F)
}
addresses[!is.na(addresses$accuracy) & addresses$accuracy == 0, c("lat", "long")] <- NA
addresses[!is.na(addresses$lat) & addresses$lat < 30, c("lat", "long")] <- NA
qplot(long, lat, data=addresses)