-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounty Geocoder.R
More file actions
33 lines (31 loc) · 1.03 KB
/
County Geocoder.R
File metadata and controls
33 lines (31 loc) · 1.03 KB
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
require(httr)
require(jsonlite)
countyGeoSingle <- function(address) {
options(digits = 15)
address2 <- gsub(', USA', '', as.character(address))
address3 <- gsub(' ', '+', as.character(address2))
url <- urlEncode(paste0("http://gisdata.alleghenycounty.us/arcgis/rest/services/Geocoders/Composite/GeocodeServer/findAddressCandidates?Street=&City=&State=&ZIP=&SingleLine=", address5, '&category=&outFields=&maxLocations=&outSR=4326&searchExtent=&location=&distance=&magicKey=&f=pjson'))
r <- httr::GET(url)
if (r$status_code != 400 & length(jsonlite::fromJSON(httr::content(r))$candidates) > 0) {
c <- jsonlite::fromJSON(httr::content(r))
x <- c$candidates$location[1,1]
y <- c$candidates$location[1,2]
} else {
x <- NA
y <- NA
}
return(data.frame(address, x , y))
}
countyGeoCol <- function(addresses) {
first <- T
for (i in addresses) {
if (first){
final <- countyGeoSingle(i)
first <- F
} else {
join <- countyGeoSingle(i)
final <- rbind(final, join)
}
}
return(final)
}