0

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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 27, 2023 at 20:43
1
  • "arcpy" is for desktop GIS. "ArcGIS API for Python" is for Web GIS. See: pro.arcgis.com/en/pro-app/latest/arcpy/main/… where it says: 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. Commented Apr 28, 2023 at 1:30

1 Answer 1

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'

answered Apr 28, 2023 at 20:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.