1

I am attempting to utilize the Census Bureau's batch geocoder (http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf) with R. The input addresses are in a data frame, not a CSV. As the addresses are an intermediate step, I do not want to write them to a CSV.

I have read several postings on stackoverflow. Hadley's solution (Posting to and Receiving data from API using httr in R) illustrates how to upload an existing CSV file. MrFlick's solution (Uploading a csv to an api in R) seems close to what I want but uses a string, not a data frame.

Here's what I have in the way of code:

#generate data frame of test addresses for this example
a = c(1, 2, 3) 
b = c("125 Worth Street", "258 Broadway", "8 Centre Street") 
c = rep("New York", 3) 
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)

#names specified by API documentation
colnames(addresses) <- c("Unique ID","Street address","City","State","ZIP")

apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"

req <- POST(apiurl, body=list(
    addressFile = RCurl::fileUpload(
        filename = "test.csv", 
        contents = addresses
    ), 
    benchmark = "Public_AR_Census2010",
    vintage = "Census2010_Census2010"
    ), 
    encode="multipart"
)
stop_for_status(req)

Thanks in advance.

Community
  • 1
  • 1
gmculp
  • 135
  • 1
  • 10
  • Interesting... What's the expected output? – alexwhitworth Feb 16 '16 at 18:47
  • Why can't you write the data to a temporary file, then delete the temp file when you're done/on exit? – sckott Feb 16 '16 at 19:03
  • I would like to use this on a database of hospitalization records in order to retrieve the census geographies (block, tract). As I am dealing with millions of records, I thought I would compare the speed of the batch geocoder to that of the single record geocoder (applied via vectorization). – gmculp Feb 16 '16 at 19:12

1 Answers1

2

if you're willing to write the data to a temp file...

library("httr")
a = c(1, 2, 3) 
b = c("125 Worth Street", "258 Broadway", "8 Centre Street") 
c = rep("New York", 3) 
d = rep("NY", 3)
e = c("10013","10007","10007")
addresses = data.frame(a,b,c,d,e)
colnames(addresses) <- c("Unique_ID","Street address","City","State","ZIP")
apiurl <- "http://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
file <- tempfile(fileext = ".csv")
write.csv(addresses, file, row.names = FALSE)
req <- POST(apiurl, body=list(
    addressFile = upload_file(file), 
    benchmark = "Public_AR_Census2010",
    vintage = "Census2010_Census2010"
  ), 
  encode="multipart"
)
content(req, "text", encoding = "UTF-8")


#> [1] "\"3\",\"8 Centre Street, New York, NY, 10007\",\"Match\",\"Non_Exact\",\"8 Centre St, NEW YORK, NY, 10013\",\"-74.00442,40.712765\",\"59660429\",\"R\",\"36\",\"061\",\"002900\",\"4019\"\n\"2\",\"258 Broadway, New York, NY, 10007\",\"No_Match\"\n\"1\",\"125 Worth Street, New York, NY, 10013\",\"Match\",\"Exact\",\"125 Worth St, NEW YORK, NY, 10013\",\"-74.0027,40.715446\",\"59660405\",\"L\",\"36\",\"061\",\"003100\",\"1012\"\n\"Unique_ID\",\"Street address, City, State, ZIP\",\"No_Match\"\n"
sckott
  • 5,755
  • 2
  • 26
  • 42
  • Can delete the tempfile with `unlink` – sckott Feb 16 '16 at 20:06
  • if this is in a function you can do `on.exit(unlink(file))` so that when function exits it will unlink that file – sckott Feb 16 '16 at 20:33
  • Thanks, sckott...excellent suggestion! Now all I have left is to parse the output. – gmculp Feb 16 '16 at 22:48
  • I tried to use your same script today but it didn't work... I got an error of `"Parameter conditions "form" not met for actual request parameters: "` Spent several hours trying to find a solution but nothing yet. Any idea what it can be? – Guillermo.D Jul 23 '19 at 02:51
  • i'd look at the documentation for the service, it's parameters probably have changed – sckott Jul 23 '19 at 19:15