Hi,
I'm very new to coding
I would like to set up an automated Python function that calls the batch geocoding API for CSV spreadsheets containing addresses in a single column. Once the spreadsheet has been geocoded, I would like to assign polygon values to the CSV files based on an intersection of geometry. If the address or location is within a polygon, assign that value.
I have successfully called the batch geocoding API to produce an output file that looks like this:
| id | original_address | geocoded_address | score | status | addr_type | longitude | latitude |
The following code has produced the table above, listing a range of geocoded addresses with associated results.
from arcgis.gis import GIS
from arcgis.geocoding import batch_geocode
from getpass import getpass
import pandas as pd
import csv
from arcgis.geocoding import batch_geocode
from arcgis.features import FeatureLayer, GeoAccessor
from arcgis.geometry import Point, SpatialReference
input_file = r"C:\path\to\input.csv"
output_file = r"C:\path\to\output.csv"
address_column_name = "address_full"
data = pd.read_csv(input_file)
print(f"Loaded {len(df)} records from {input_file}")
addresses = (data[address_column_name]).tolist()
addresses
results = batch_geocode(addresses, source_country="AU", out_fields="Addr_type,Status")
results
status_map = {
'M' : 'Matched - The address is matched',
'U' : 'Unmatched - The address is not matched',
'T' : 'Tied - The address has more than one candidate with the same best match score but at different locations',
}
# Map using 0-based ResultID
records_mapped = {}
for r in records:
result_id = r.get('attributes', {}).get('ResultID')
if result_id is not None:
records_mapped[result_id] = r
df_results = pd.DataFrame([{
'id' : i,
'original_address' : df_input.iloc[i]['address_full'],
'geocoded_address' : records_mapped.get(i, {}).get('address', ''),
'score' : records_mapped.get(i, {}).get('score', ''),
'status' : status_map.get(records_mapped.get(i, {}).get('attributes', {}).get('Status', ''), ''),
'addr_type' : records_mapped.get(i, {}).get('attributes', {}).get('Addr_type', ''),
'longitude' : records_mapped.get(i, {}).get('location', {}).get('x', ''),
'latitude' : records_mapped.get(i, {}).get('location', {}).get('y', ''),
} for i in range(len(df_input))])
df_results.to_csv(csv1, index=False)
print(f"Saved {len(df_results)} records to {csv1}")From my geocoded results, I would like to write my polygon value to the CSV based on an intersection of geometry using the address and lat, long fields. Not sure if I have to map the geocoded addresses for this...