1

To make calls to the census geocoder api I am using tips from the documentation found at http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf under the heading "Batch Geocoding". The shell command they give for calling the api for a batch is

curl --form addressFile=@address.csv --form benchmark=4 http://geocoding.geo.census.gov/geocoder/locations/addressbatch --output geocoderesult.csv

where address.csv is the file to be uploaded.

This works perfectly. However, I would like to create an R script to make the whole process automated.

What would be an equivalent way to do the above command in R? Could postForm() in the RCurl package accomplish this?



BTW, each line of the csv file getting uploaded ("address.csv") must be of the form

Unique ID, Street address, City, State, ZIP

So, for example, it could just contain addresses for Apple and Facebook like

1, 1 Infinite Loop, Cupertino, CA, 95014
2, 1 Hacker Way, Menlo Park, CA, 94025

Thanks!

efh0888
  • 328
  • 3
  • 12

1 Answers1

2

Using the httr library, a direct translation would be

library('httr')

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

addresses <- "1, 1 Infinite Loop, Cupertino, CA, 95014
2, 1 Hacker Way, Menlo Park, CA, 94025"

addresseFile <- "addresses.csv"
writeLines(addresses , addresseFile )

resp<-POST(apiurl, body=list(addressFile=upload_file(addresseFile), benchmark=4), 
     encode="multipart")
content(resp)

If you wanted to skip writing the csv file to disk, you could do

resp <- POST(apiurl, body=list(
    addressFile = RCurl::fileUpload(
      filename = "data.csv", 
      contents = addresses, 
      contentType = "application/octet-stream"
    ), 
    benchmark=4
  ), 
  encode="multipart"
)
MrFlick
  • 195,160
  • 17
  • 277
  • 295