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")
# A function that extracts items from a list
def address_extractor_roof(api_response_list)
"""
This function extracts relevant information
from API response.
"""
# Create an empty dataframe
extracted_df = pd.DataFrame()
# Create an empty list
latitude, longitude, match, score, addr_type = [],[],[],[],[]
# Create a FOR loop to extract the data
for api_response in api_response_list:
try:
# Extract the variables
latitude.append(api_response[0]['attributes']['DisplayY'])
longitude.append(api_response[0]['attributes']['DisplayX'])
match.append(api_response[0]['attributes']['Status'])
score.append(api_response[0]['attributes']['Score'])
addr_type.append(api_response[0]['attributes']['Addr_type'])
except IndexError:
print(api_response)
# Append columns to form a dataframe
extracted_df['latitude'] = latitude
extracted_df['longitude'] = longitude
extracted_df['match'] = match
extracted_df['score'] = score
extracted_df['addr_type'] = addr_type
# Return the dataframe
return extracted_df