cluelessresearch.com

political methodology, brazilian politics, etc.

Geocoding

geopy is a geocoding toolbox for python. See the website for installation instructions. It uses third-party geocoders (such as google maps) so you can add geographic coordinates to the addresses in your application. I cooked up my first python script to use it. You give it a csv file with addresses and it returns a csv file with addresses + latitude and longitude. It might be useful to someone out there.

from geopy import geocoders
g = geocoders.Google('your-google-maps-api-here')

import csv
writer = csv.writer(open("out.csv", "wb"))
writer.writerow(("endereco","cidade","estado","pais","latitude","longitude"))
reader = csv.reader(open("endereco.csv"))
for row in reader:
    now = row[0]+","+row[1]+","+row[2]+","+row[3]
    try: place, (lat, lng) = g.geocode(now)
    except: place, (lat, lng) = "NA", ("NA", "NA")
    writer.writerow((row[0],row[1],row[2],row[3],lat, lng))

No comments yet. Be the first.

Leave a reply

You must be logged in to post a comment.