Skip to content

Instantly share code, notes, and snippets.

@rgdonohue
Last active January 11, 2023 21:43
Show Gist options
  • Select an option

  • Save rgdonohue/c4beedd3ca47d29aef01 to your computer and use it in GitHub Desktop.

Select an option

Save rgdonohue/c4beedd3ca47d29aef01 to your computer and use it in GitHub Desktop.

Revisions

  1. rgdonohue revised this gist Nov 16, 2015. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions geocode.py
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,13 @@

    print 'creating geocoding objects!'

    arcgis = ArcGIS(timeout=200)
    bing = Bing('your-API-key',timeout=200)
    nominatim = Nominatim(timeout=200)
    opencage = OpenCage('your-API-key',timeout=200)
    geocoderDotUS = GeocoderDotUS(timeout=200)
    googlev3 = GoogleV3(timeout=200)
    openmapquest = OpenMapQuest(timeout=200)
    arcgis = ArcGIS(timeout=100)
    bing = Bing('your-API-key',timeout=100)
    nominatim = Nominatim(timeout=100)
    opencage = OpenCage('your-API-key',timeout=100)
    geocoderDotUS = GeocoderDotUS(timeout=100)
    googlev3 = GoogleV3(timeout=100)
    openmapquest = OpenMapQuest(timeout=100)

    # choose and order your preference for geocoders here
    geocoders = [googlev3, bing, nominatim]
  2. rgdonohue revised this gist Nov 16, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    This Python script utilizes the [GeoPy geocoding library](https://github.com/geopy/geopy) to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
    This Python script utilizes the [GeoPy geocoding library](https://github.com/geopy/geopy) to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.

    [https://gist.github.com/rgdonohue/c4beedd3ca47d29aef01](https://gist.github.com/rgdonohue/c4beedd3ca47d29aef01)
  3. rgdonohue revised this gist Nov 10, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    This Python script utilizes the [GeoPy Geocoding](https://github.com/geopy/geopy) library to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
    This Python script utilizes the [GeoPy geocoding library](https://github.com/geopy/geopy) to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
  4. rgdonohue revised this gist Nov 10, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    This Script utilizes the [GeoPy Geocoding](https://github.com/geopy/geopy) library to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
    This Python script utilizes the [GeoPy Geocoding](https://github.com/geopy/geopy) library to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
  5. rgdonohue renamed this gist Nov 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. rgdonohue renamed this gist Nov 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. rgdonohue revised this gist Nov 10, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions geocode.py
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,10 @@ def geocode(address):
    # try to geocode using a service
    location = geocoders[i].geocode(address)

    # if it returns a value
    # if it returns a location
    if location != None:

    #
    # return those values
    return [location.latitude, location.longitude]
    else:
    # otherwise try the next one
    @@ -35,7 +35,7 @@ def geocode(address):
    print sys.exc_info()[0]
    return ['null','null']

    # if all values have failed, return null values
    # if all services have failed to geocode, return null values
    return ['null','null']


  8. rgdonohue created this gist Nov 10, 2015.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This Script utilizes the [GeoPy Geocoding](https://github.com/geopy/geopy) library to batch geocode a number of addresses, using various services until a pair of latitude/longitude values are returned.
    78 changes: 78 additions & 0 deletions geocode.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    # import the geocoding services you'd like to try
    from geopy.geocoders import ArcGIS, Bing, Nominatim, OpenCage, GeocoderDotUS, GoogleV3, OpenMapQuest
    import csv, sys

    print 'creating geocoding objects!'

    arcgis = ArcGIS(timeout=200)
    bing = Bing('your-API-key',timeout=200)
    nominatim = Nominatim(timeout=200)
    opencage = OpenCage('your-API-key',timeout=200)
    geocoderDotUS = GeocoderDotUS(timeout=200)
    googlev3 = GoogleV3(timeout=200)
    openmapquest = OpenMapQuest(timeout=200)

    # choose and order your preference for geocoders here
    geocoders = [googlev3, bing, nominatim]

    def geocode(address):
    i = 0
    try:
    while i < len(geocoders):
    # try to geocode using a service
    location = geocoders[i].geocode(address)

    # if it returns a value
    if location != None:

    #
    return [location.latitude, location.longitude]
    else:
    # otherwise try the next one
    i += 1
    except:
    # catch whatever errors, likely timeout, and return null values
    print sys.exc_info()[0]
    return ['null','null']

    # if all values have failed, return null values
    return ['null','null']


    print 'geocoding addresses!'

    # list to hold all rows
    dout = []

    with open('data.csv', mode='rb') as fin:

    reader = csv.reader(fin)
    j = 0
    for row in reader:
    print 'processing #',j
    j+=1
    try:
    # configure this based upon your input CSV file
    street = row[4]
    city = row[6]
    state = row[7]
    postalcode = row[5]
    country = row[8]
    address = street + ", " + city + ", " + state + " " + postalcode + " " + country

    result = geocode(address)
    # add the lat/lon values to the row
    row.extend(result)
    # add the new row to master list
    dout.append(row)
    except:
    print 'you are a beautiful unicorn'

    print 'writing the results to file'

    # print results to file
    with open('geocoded.csv', 'wb') as fout:
    writer = csv.writer(fout)
    writer.writerows(dout)

    print 'all done!'