3

I have a list of street addresses I would like to geocode to county. I am working in R. Simplified example follows. Unfortunately, due to the new Google API terms of service, you will need to have your own API key to run my code--it should not be shared, so please do not include it in your solution code.

I suspect this is a formatting issue but I am too new to R to know the solution.

library(tidyverse)
library(ggmap)

register_google(key = <YOUR GOOGLE KEY>)

ltr <- letters %>% head(5)
adr <- c('110 State St, Albany, NY' , 
           '100 State Cir, Annapolis, MD' ,
           '206 Washington St SW, Atlanta, GA' ,
           '210 State St, Augusta, ME' ,
           '1100 Congress Ave, Austin, TX')

rawAdr <- data.frame(ltr , adr)

# the following only retrieves latitude and longitude
latlonAdr <- geocode(location = rawAdr$adr) %>%
  bind_cols(rawAdr , .)

# the following retrieves county (among much other information), 
# but it is formatted in a way that is 
# impossible to use. For instance, county a is in variable long_name...17, 
# but the same name is repeated for all addresses. The county for address b is given in
# long_name...64, again the same name for all addresses. 

geoAdr <- geocode(location = rawAdr$adr , output = 'all') %>%
  bind_cols(rawAdr , . )

I would like to have a file that lists ltr, adr and (correct) county. Thanks for your any help! (Apologies, I will not be able to look at answers for a couple of hours.)

David
  • 307
  • 1
  • 10

1 Answers1

2

The return value from the geocode function is a long nested list, you need to drill through the list to find the field of interest.

Here is a solution which bypasses the ggmap package and returns a JSON response, this is slightly easier to parse:

library(magrittr)

ltr <- letters %>% head(5)
adr <- c('110 State St, Albany, NY' , 
         '100 State Cir, Annapolis, MD' ,
         '206 Washington St SW, Atlanta, GA' ,
         '210 State St, Augusta, ME' ,
         '1100 Congress Ave, Austin, TX')
rawAdr <- data.frame(ltr , adr)

#replace spaces with + for a valid web address
adrs <-gsub(" ", "+", adr)
#add the key here to the http address
urls <-paste("https://maps.googleapis.com/maps/api/geocode/json?","address=",adrs,"&key=***keyGoesHere***",sep="")

#query the API, and search for the row that contains the word County and return that value
counties <- sapply(urls, function(url) {
   print(url)
   rgc <- jsonlite::fromJSON(url)
   county <-rgc$results$address_components[[1]]$long_name[grep("County", rgc$results$address_components[[1]]$long_name)]
   county
})
rawAdr$County <- counties
rawAdr
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • this is great, do you know how to add state as well? – John Clegg May 03 '21 at 15:24
  • @JohnClegg, all that information should be stored in the response variable rgc. – Dave2e May 03 '21 at 22:09
  • the "counties" variable above contains only url and the name of the county – John Clegg May 03 '21 at 23:16
  • 1
    Yes that is true. Inside the function this line `jsonlite::fromJSON(url)` returns the entire location record. One will need to parse out the desired information from here. If you need help please ask a new question as oppose to using the comment section. – Dave2e May 03 '21 at 23:27