-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathusps_geocoder.R
More file actions
61 lines (56 loc) · 1.78 KB
/
usps_geocoder.R
File metadata and controls
61 lines (56 loc) · 1.78 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
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
require(httr)
require(jsonlite)
require(utils)
require(plyr)
require(tidyverse)
uspsGeo <- function(locs) {
# Build Empty Data frace
final <- NULL
# Run for each address
for (address in locs) {
# Encode address for API call
address_encode <- URLencode(address, reserved = TRUE, repeated = TRUE)
# Build URL for geocode
url <- paste0("https://gis.usps.com/arcgis/rest/services/locators/US_Street/GeocodeServer/findAddressCandidates?Single+Line+Input=", address_encode, '&outSR=4326&f=pjson')
# Print message for user
message(paste("Source :", url))
# Send geet request
r <- httr::GET(url)
# Load Content
c <- jsonlite::fromJSON(httr::content(r))
# Check status code & number of candidates
if (r$status_code == 200 & length(jsonlite::fromJSON(httr::content(r))$candidates) > 0) {
# Successful add top candidate
longitude <- c$candidates$location[1,1]
latitude <- c$candidates$location[1,2]
} else if (r$status_code == 200) {
# Error if not address candidates
message(" Failed with error (200): No address candidates")
longitude <- NA
latitude <- NA
} else {
# Print other error
message(paste0(" Failed with error (", c$error$code, "): ", c$error$message))
longitude <- NA
latitude <- NA
}
# Build columns for bind
df <- data.frame(longitude, latitude)
# Build Dataframe for results
if (is.null(final)) {
final <- df
} else {
# Merge to results dataframe
final <- plyr::rbind.fill(final, df)
}
}
return(final)
}
mutate_uspsGeo <- function (data, location, ...){
# Get data location
locs <- data[[deparse(substitute(location))]]
# Run geocode script
gcdf <- uspsGeo(locs, ...)
# Bind to x
dplyr::bind_cols(data, gcdf)
}