Hello All,
I'm trying to do a batch geocode, then write the results to a CSV file with the lat and long column added on.
Everything works fine, but the final written result to the CSV file is mixed up causing different XY values to be added to the wrong address.
I'm following this YT video: https://www.youtube.com/watch?v=ZbOD8mrX3c8
Please see attached code and screenshots. Python 3.9.16 shipped with ArcPro 3.2
Any help or idea as to why this is happening? Thanks in advanced.
Python Terminal results
CSV file output result - does not match what's in the terminal
code:
import pandas as pd
from arcgis.gis import GIS
from arcgis.geocoding import get_geocoders, batch_geocode, geocode, Geocoder
gis = GIS("https://arcgis.com","user","password")
print(gis.properties.user.role)
GeocodeURL = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"
orgGeoCoder = Geocoder(GeocodeURL, gis)
input_file = "C:\\Users\\temp\\coffeeshops.csv"
output_file = "C:\\Users\\temp\\coffeeshops_coded.csv"
address_column_name = "Address"
data = pd.read_csv(input_file, encoding='utf8')
print(data.head(20))
if address_column_name not in data.columns:
raise ValueError("Missing Address column in input data")
addresses = (data[address_column_name]+", " + data['City'] + ", " + data['State']).tolist()
results = batch_geocode(addresses=addresses, geocoder=orgGeoCoder)
latcoords = []
longcoords = []
for coordinates in results:
print("Score "+ str(coordinates['score']) + " : " + coordinates['address'] + " " + str(coordinates['location']))
latitude = coordinates['location']['y']
longitude = coordinates['location']['x']
latcoords.append(latitude)
longcoords.append(longitude)
data['Long']=longcoords
data['Lat']=latcoords
pd.DataFrame(data).to_csv(output_file, encoding='utf8')
print("done")