Geocode Hosted Table in AGOL with Python

345
3
11-18-2022 10:35 AM
DustinDavis
New Contributor

Hello,

I am trying to develop a python script that calls data from an API and updates a pre-existing hosted table in AGOL. I have the script updating the table correctly. The issue I have is the data points are not geocoding even with lat/long provided by the API. I have tried to geocode it a few ways and converting it to a feature layer but haven't had much luck. Anyone have tips for geocoding the updated data in the hosted table? What specific geocode operation should I use?

 

Here is what I have so far:

 

from arcgis.geocoding import geocode_from_items
import requests, json
from arcgis import GIS

# Variables
username = ""                 # AGOL Username
password = ""                               # AGOL Password
itemID = ''         # AGOL Table Item ID
dataURL = 'API Key'

# Disable warnings
requests.packages.urllib3.disable_warnings()

# Connect to AGOL
print("Connecting to AGOL")
gis = GIS('https://www.arcgis.com', username, password)

# Get Table
print("Get Hosted Table")
fLayer = gis.content.get(itemID)
editTable = fLayer.tables[0]
# Truncate Table
print("Truncate table")
editTable.manager.truncate()

# Get Data
print("Retrieving Data")
r = requests.get(dataURL, verify=False)
response = json.loads(r.content)
data = response['results']

# Create Dictionary of attributes and update table
print("Updating hosted table")
for attr in data:
    addFeatures =  {
            "attributes" : {
                "id" : attr['id'],
                "latitude" : attr['latitude'],
                "longitude" : attr['longitude'],
                "location" : attr['location'],
                "description": attr['description'],
                "gategory": attr['category'],
                "direction": attr['direction'],
                "routeName": attr['routeName'],
                "roadStatus":attr['roadStatus'],
            }
        }

    # Update Table
    editTable.edit_features(adds=[addFeatures])

print("Updated")

locate_item = editTable
locate_item

fl_item = geocode_from_items(locate_item, output_type='Feature Layer',
                             output_name="locate_OHGOapi",
                             gis=gis)

 

 

Thanks!

0 Kudos
3 Replies
by Anonymous User
Not applicable

Is your geocoder set to geocode lat/long, or X,Y, or is it an address locator?  You'll have to match your inputs (lat/long) to the appropriate geocoder that can accept lat/long as the input.

0 Kudos
DustinDavis
New Contributor

When I published the hosted table to AGOL I did not set the geocode. I am trying to do that via the python script.

0 Kudos
by Anonymous User
Not applicable

Ah, I misread your intentions and the docs 'geocode_from_items geocodes the entire file regardless of size.' ... 'data points'- are they different from the points you are getting from the API?

It looks like you turned the response data (editTable) from the API into the geocoder, rather than geocoding the response from the API so fl_item is now a geocoder. I think what you need is .geocode, where you can set the geocoder you have in your gis that accepts x,y fields.

0 Kudos