My org has an new enterprise GIS setup with most of our editing layers living as hosted layers in AGO Portal. I want to update a field using 2 other fields but this layer has 250k assets to update and so I want to write a script to run it faster (and recalculate in the future as we update the data). Most of the documentation I am finding online for arcpy field calculations seem to only work for layers living in a database/geodatabase, but nothing referring to a rest service/hosted layer. While in ArcGIS Pro, I ran the below code in the python panel with the hosted layer active in the map, and got
ERROR 000732: Input table does not exist or is not supported, failed to calculate table
How do I reference the hosted layer? Or, how do I programmatically calculate the field for a hosted layer?
infc = r'https://portalurl/rest/services/subfolder/hostedlayer'
codeblock = """def matas(pb,pv,ma):
if pb is None and pv is None:
pass
elif pb == "PB" or pv == "PB":
return "PB"
elif pb == "COP" and pv == "COP":
return "COP"
else:
return ma"""
expr = "matas(!PUBLICMATERIAL!,!PRIVATEMATERIAL!,!MATERIALASSUMED!)"
arcpy.CalculateField_management(infc,"MATERIALASSUMED",expr,"PYTHON",codeblock)
1 Answer 1
Using the ArcGIS Python API....
The ID of the item you want is found in the browser URL: enter image description here
from arcgis.gis import GIS
portal = "URL_to_Your_Portal"
u = "AGOL username"
p = "AGOL password"
gis = GIS(portal,u,p)
itemID = 'alphanumeric id of your hosted layer'
# Get item
i = gis.content.get(itemID)
# Get Layer within Item (this is by index)
mylyr = i.layers[0]
# Query of items you want to calculate
test_query = "CLASSIFICATION = 'Arterial'"
# Run calculate against layer
mylyr.calculate(where=test_query,calc_expression={"field":"STATUS","value":"PLOWED"})
That should calculate all features where CLASSIFICATION = 'Arterial' to have a STATUS = 'PLOWED'
Explore related questions
See similar questions with these tags.
ArcPy and ArcGIS API for Python are complementary libraries; ArcPy allows you to use, automate, and extend desktop GIS, and ArcGIS API for Python supports the same for Web GIS.