Created
August 28, 2015 13:39
-
-
Save meule/ccc4f5779d39acec932f to your computer and use it in GitHub Desktop.
Simple csv geocoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# python geocoder. adds lat and lng columns to any csv file with geo reference | |
# | |
# usage: | |
# python geocode.py data_table.csv output_table.csv <list of geocolumns | |
# example: | |
# python geocode.py ib_interactive_map_database_v6.csv ngo_data_6.csv georef,country | |
import urllib2 | |
import sys | |
import httplib | |
import time | |
from geopy.geocoders import GoogleV3 | |
import csv | |
geolocator=GoogleV3(timeout=10) | |
print sys.argv[1], '>', sys.argv[2], ', columns:', sys.argv[3] | |
rIn=open(sys.argv[1], 'rt') | |
rOut=open(sys.argv[2], 'a') | |
rOut0=open(sys.argv[2], 'rb') | |
columns = sys.argv[3].split(',') | |
#rows=rIn.read().decode('utf-8').split('\r\n') | |
rows=rIn.read().decode('utf-8').split('\n') | |
print 'len(rows): ',len(rows) | |
rIn.close(); | |
rIn=open(sys.argv[1], 'rt') | |
start = len(rOut0.read().decode('utf-8').split('\n')) - 1 | |
rOut0.close() | |
header = rows[0] | |
values=[] | |
print 'from line:', start + 1 | |
i = 0 | |
reader = csv.DictReader(rIn) | |
if start == 0: | |
rOut.write((header.replace('\r','').replace('\n','') + ',lat,lng\n').encode('utf-8')) | |
for row in reader: | |
i += 1 | |
if i >= start: | |
addr = '' | |
geo = ',,' | |
for column in columns: | |
addr += row[column].replace('\r','').replace('\n','') + ' ' | |
print i, addr | |
while True: | |
try: | |
location = geolocator.geocode(addr) | |
break | |
except (IOError, httplib.HTTPException) as e: | |
print e | |
print 'Once again...' | |
time.sleep(1) | |
if location and location.latitude and location.longitude: | |
geo=','+str(location.latitude)+','+str(location.longitude) | |
print ' ', geo | |
rOut.write(( rows[i].replace('\r','').replace('\n','') + geo + '\n' ).encode('utf-8')) | |
rIn.close() | |
rOut.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment