Created
July 18, 2014 17:52
-
-
Save chrislkeller/bc9bb417b961666dfaaf to your computer and use it in GitHub Desktop.
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
# Uses wrapper found here: https://github.com/datadesk/python-googlegeocoder | |
# pip install python-googlegeocoder | |
from googlegeocoder import GoogleGeocoder | |
import time | |
import logging | |
logging.basicConfig(format='\033[1;36m%(levelname)s:\033[0;37m %(message)s', level=logging.DEBUG) | |
def create_list_addresses_from_file(): | |
''' convert text file of addresses to a list ''' | |
list_of_addresses = [] | |
address_file = open("addresses.txt","r") | |
list_of_addresses = address_file.readlines() | |
address_file.close() | |
geocode_addresses_from_file(list_of_addresses) | |
def geocode_addresses_from_file(list_of_addresses): | |
''' run a list of addresses through the geocoder ''' | |
geocoder = GoogleGeocoder() | |
f = open("results.txt", "a") | |
for address in list_of_addresses: | |
time.sleep(5) | |
try: | |
address = address.replace("\n","") | |
search = geocoder.get(address) | |
except ValueError: | |
continue | |
first_result = search[0] | |
output = [ | |
first_result.formatted_address.encode('ascii', 'ignore'), | |
first_result.geometry.location.lat, | |
first_result.geometry.location.lng, | |
first_result.geometry.location_type, | |
first_result.geometry.partial_match | |
] | |
address_for_output = '%s\t%s\t%s\t%s\t%s\n' % (output[0], output[1], output[2], output[3], output[4]) | |
print address_for_output | |
f.write(address_for_output) | |
f.close() | |
if __name__ == "__main__": | |
create_list_addresses_from_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment