Created
February 26, 2016 16:10
-
-
Save rlieberman/cc1aba461e75405478a4 to your computer and use it in GitHub Desktop.
Python script for geocoding, adapted from http://rowanwinsemius.id.au/blog/geocoding-addresses-from-a-csv-with-python/
This file contains 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
#import the csv module which we'll need to read and write our csv's | |
import csv | |
#import the shutil module which we use to create a copy of our original csv | |
import shutil | |
#import the Nominatim module from geopy, a bit more on this in the text below | |
from geopy.geocoders import Nominatim | |
geolocator = Nominatim() | |
#Let's start by creating a copy of our raw file just to be safe | |
shutil.copyfile("dirtyrestaurants2.csv","dirtyrestaurants2_processed.csv") | |
#open the file and assign it to the var f | |
f = open('dirtyrestaurants2.csv') | |
#create a blank array called addresses | |
addresses = [] | |
#use the csv reader to actually start doing stuff | |
csv_f = csv.reader(f) | |
# iterate through each row, and only grab column 1, and add it to the address array | |
for row in csv_f: | |
addresses.append(row[1]) | |
# print addresses | |
#close our original file | |
f.close() | |
#create a new empty array called locations | |
locations = [] | |
#for each value in the addresses array that we created earlier geocode it and save it to a variable called location, we'll then append that single location to the locations variable | |
for index,val in enumerate(addresses): | |
location = geolocator.geocode([val], timeout=15) | |
# print index + 1, location.latitude | |
locations.append([location.latitude, location.longitude]) | |
#Create a header row for our new csv | |
header = ['Name','Address','Latitude','Longitude'] | |
#open the original csv and grab all the data, place it in a var called data, and close the file again | |
f = open('dirtyrestaurants2.csv') | |
data = [item for item in csv.reader(f)] | |
f.close() | |
# create a blank arraycalled new_data | |
new_data = [] | |
# for each item in data append a location, then add the complete item to the new data variable | |
for i, item in enumerate(data): | |
item.append(locations[i][0]) #append latitude | |
item.append(locations[i][1]) #append longitude | |
new_data.append(item) | |
#open the new csv and write the header row followed by a row for each object in the new_data array | |
f = open('dirtyrestaurants2_processed.csv', 'w') | |
csv.writer(f, lineterminator='\n').writerow(header) | |
csv.writer(f, lineterminator='\n').writerows(new_data) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment