Geocoding using Geopy and Python

I am trying a geocode CSV file that contains a location name and a parsed address that includes an address number, street name, city, zip code, country. I want to use Geocy and ArcGIS Geocodes through Geopy. I need to create code that scrolls through my csv from 5000+ records and gives me latitude and longitude in separate columns in my CSV. I want to use ArcGIS Geocoding through Geopy. Can someone provide me a code to get started? Thank you

Here is my script:

import csv from geopy.geocoders import ArcGIS geolocator = ArcGIS() # here some parameters are needed with open('C:/Users/v-albaut/Desktop/Test_Geo.csv', 'rb') as csvinput: with open('output.csv', 'w') as csvoutput: output_fieldnames = ['Name','Address', 'Latitude', 'Longitude'] writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames) reader = csv.DictReader(csvinput) for row in reader: # here you have to replace the dict item by your csv column names query = ','.join(str(x) for x in (row['Name'], row['Address'])) Address, (latitude, longitude) = geolocator.geocode(query) # here is the writing section output_row = {} output_row['Name'] = Name output_row['Address'] = Address output_row['Latitude'] = Latitude output_row['Longitude'] =Longitude writer.writerow(output_row) 
+6
source share
2 answers

I used this script to do some batch geocoding from .csv. This requires that one column contains the full text address that you want to geocode, and one column called "UniqueID", which has a unique identifier for each element in the CSV. It also prints a list of addresses that geocoding failed. It also performs a quick check to see if the zip code may be incorrect / throw geocoding:

 def main(path, filename): # path to where your .csv lives, and the name of the csv. import geopy from geopy.geocoders import ArcGIS import pandas as pd Target_Addresses = pd.read_csv(path+'\\'+filename) Target_Addresses['Lat'] = np.nan Target_Addresses['Long'] = np.nan Indexed_Targets = Target_Addresses.set_index('UniqueID') geolocator = ArcGIS() #some parameters here Fails = [] for index, row in Indexed_Targets.iterrows(): Address = row['Address'] Result = geolocator.geocode(Address) if Result == None: Result = geolocator.geocode(Address[:-7]) if Result == None: Fails.append[Address] else: Indexed_Targets.set_value(index, 'Lat', Result.latitude) Indexed_Targets.set_value(index, 'Long', Result.longitude) else: Indexed_Targets.set_value(index, 'Lat', Result.latitude) Indexed_Targets.set_value(index, 'Long', Result.longitude) for address in Fails: print address Indexed_Targets.to_csv(filename[:-4]+"_RESULTS.csv") if __name__ == '__main__': main(path, filename) # whatever these are for you... 

This will output a new csv with "_RESULTS" (for example, the input "addresses.csv" will output "addresses_RESULTS.csv") with two new columns for "Lat" and "Long".

+3
source

It's just the beginning, tell me if that helps. He does not write to CSV, but I will edit my answer later if you need this part.

 import csv from geopy.geocoders import ArcGIS geolocator = ArcGIS() #here some parameters are needed with open('C:/Users/v-albaut/Desktop/Test_Geo.csv', 'rb') as csvinput: with open('output.csv', 'w') as csvoutput: output_fieldnames = ['Name','Address', 'Latitude', 'Longitude'] writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames) reader = csv.DictReader(csvinput) for row in reader: #here you have to replace the dict item by your csv column names query = ','.join(str(x) for x in (row['Name'], row['Address'])) try: address, (latitude, longitude) = geolocator.geocode(query) except: latitude = 'N/A' longitude = 'N/A' #here is the writing section output_row = {} output_row['Name'] = row['Name'] output_row['Address'] = row['Address'] output_row['Latitude'] = latitude output_row['Longitude'] = longitude writer.writerow(output_row) 

document:

+3
source

All Articles