-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocodingAddresses.R
36 lines (25 loc) · 1.19 KB
/
geocodingAddresses.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
# guide from https://www.r-bloggers.com/introducing-the-nominatim-geocoding-package/
# install.packages("jsonlite")
require(xml2)
require(jsonlite)
locations <- read.csv(paste0(getwd(),"//locations.csv"),stringsAsFactors = FALSE)
KEY <- "random number string provided by MapQuest" #your key goes here
geocode <- function(address,key) {
x <- gsub(" ", "+", address)
value <- xml_text(read_html(paste0("https://www.mapquestapi.com/geocoding/v1/address?key="
,key
,"&inFormat=kvp&outFormat=json&location="
,x
,"&maxResults=1")))
value
}
locations$json <- sapply(locations$address, FUN=function(x) geocode(x,KEY))
jsonParse <- function(x) {
jsonList <- jsonlite::fromJSON(x)
jsonList$results$locations[[1]]$latLng
}
locations$lat <- sapply(locations$json, FUN=function(x) jsonParse(x)$lat)
locations$lng <- sapply(locations$json, FUN=function(x) jsonParse(x)$lng)
locations <- locations[, !(names(locations) %in% "json")]
write.csv(locations, "locationsGeocoded.csv", row.names=FALSE)
# foo.json <- geocode("7301 Calhoun PI #600, Derwood, MD 20855",KEY)