Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Census Bulk Geocoder with python requests library

I am experimenting with the census bulk geocode API documentation

The following curl command works:

curl --form [email protected] --form benchmark=9 http://geocoding.geo.census.gov/geocoder/locations/addressbatch --output geocoderesult.csv

But when I attempt to port this to python requests:

url = 'http://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':9}
files = {'addressFile': ('Addresses.csv', open('Addresses.csv', 'rb'), 'text/csv')}
r = requests.post(url, files=files, data = payload)
print r.text

I am apparently not sending a well formed request and only receiving "There was an internal error" in response. Any idea what I am doing wrong in forming this request?

like image 596
Jake Lowen Avatar asked Mar 16 '23 05:03

Jake Lowen


2 Answers

Got it! Turns out that the geographies request type required some parameters that the locations type did not. Working solution:

url = 'http://geocoding.geo.census.gov/geocoder/geographies/addressbatch'
payload = {'benchmark':'Public_AR_Current','vintage':'ACS2013_Current'}
files = {'addressFile': ('Addresses.csv', open('Addresses.csv', 'rb'), 'text/csv')}
r = requests.post(url, files=files, data = payload)
print r.text
like image 190
Jake Lowen Avatar answered Mar 17 '23 17:03

Jake Lowen


May be this is a simpler way to do the same thing.

You will get a clean output in pandas dataframe :)

# pip install censusgeocode    

import censusgeocode
import pandas as pd

cg = censusgeocode.CensusGeocode(benchmark='Public_AR_Census2010',vintage='Census2010_Census2010')
k = cg.addressbatch('D:\WORK\Addresses.csv')

# Bonus
# Get clean output in Dataframe

df = pd.DataFrame(k, columns=k[0].keys())

# PS: I tried with 9990 records in single batch

Reference:

https://pypi.org/project/censusgeocode/

https://geocoding.geo.census.gov/geocoder/benchmarks

https://geocoding.geo.census.gov/geocoder/vintages?form

https://geocoding.geo.census.gov/geocoder/geographies/addressbatch?form

like image 36
Suhas_Pote Avatar answered Mar 17 '23 19:03

Suhas_Pote