Created
December 30, 2014 18:31
-
-
Save chrislkeller/703f2036398504552467 to your computer and use it in GitHub Desktop.
Quick method of geocoding addresses...
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 | |
import csv | |
import logging | |
import time | |
import datetime | |
from googlegeocoder import GoogleGeocoder | |
logger = logging.getLogger("root") | |
logging.basicConfig( | |
format = "\033[1;36m%(levelname)s: %(filename)s (def %(funcName)s %(lineno)s): \033[1;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: | |
try: | |
address = address.replace("\n", "") | |
search = geocoder.get(address) | |
except Exception, exception: | |
logger.error("%s" % (exception)) | |
if len(search) > 0: | |
first_result = search[0] | |
address_for_output = '%s\t%s\t%s\t%s\t%s\n' % ( | |
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 | |
) | |
print address_for_output | |
f.write(address_for_output) | |
else: | |
print "Can't geocode this" | |
time.sleep(5) | |
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