Sort and Create Sequential ID Field with Web Content - Python Console - Pro

295
5
01-09-2020 08:34 AM
dgray
by
New Contributor III

Hi Everyone,

I was wondering if it's possible to create a sequential ID field with a sorted field that is not Object ID, referencing a web layer in My Content of ArcGIS Online (as opposed to a locally-stored layer).  I've tried to create a Python script to feed into the Python shell of ArcGIS Pro, referencing a web-hosted layer in my active map.  The script is a derivative of the following:

How To: Sort and create a sequentially ordered ID field in an attribute table in ArcGIS Pro 

I'm trying to sort the field Asset_ID_OLD and use this sequence to populate Asset_ID_ with sequential ID numbers, beginning with FH1.  This is what my script looks like:

import arcpy

sortFeat = r'Hydrants'
sortField = 'Asset_ID_OLD'
idField = 'Asset_ID_'
Count = 1
def Renum():
    global Count
    Interval = 1
    if Count == 1:
        New = "FH" + str(Count)
        Count += Interval
        return New
    else:
        New = "FH" + str(Count)
        Count += Interval
        return New

rows = arcpy.UpdateCursor(sortFeat, "", "", "", sortField + " A")

for row in rows:
    row.setValue(idField, Renum())
    rows.updateRow(row)

del row, rows

When I enter this code into the Python console, I get this message:

Traceback (most recent call last):
  File "<string>", line 19, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\__init__.py", line 1213, in UpdateCursor
    return gp.updateCursor(dataset, where_clause, spatial_reference, fields, sort_fields)
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 368, in updateCursor
    self._gp.UpdateCursor(*gp_fixargs(args, True)))
RuntimeError: ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.

What am I doing wrong?  Is it even possible to do this using a web-hosted layer?  Am I referencing the layer incorrectly?  Or is this another problem? 

Also, I'm not really sure what the + " A" part means in the original script...

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

I am about to step out the door, so I can't dive into it now, but I am in disbelief that an Esri Support Technical Article involving ArcGIS Pro would use the older/legacy Python cursors.  Esri has only kept those older cursors around for backwards compatibility, there is no reason to use them when developing new code.

dgray
by
New Contributor III

Hmm, ok. From what I had resarched, it seems like the cursor functions are needed if a sorted field other than Object ID is being referenced for row order.  If this is actually solvable using a simpler method let me know!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Cursors, yes, but there are older/legacy (Cursor—Help | ArcGIS Desktop ) and newer cursors (UpdateCursor—Help | ArcGIS Desktop ). 

dgray
by
New Contributor III

Ok, using arcpy.da.UpdateCursor brought me further along.  But it still complains that it cannot open the layer as I'm referencing it.  I tried referencing the layer name, the alias (as listed in the Source part of Properties), and the URL for the feature layer, but none of them seem to work. The script cannot open the feature layer I'm trying to modify.  Can Python work with feature layers open in ArcGIS Pro, or only locally hosted layers with a defined path?

0 Kudos
dgray
by
New Contributor III

Also, does using the arcpy.da.UpdateCursor change the syntax of referencing the feature class, the ID field, and the reference to sort acending by the sort field? 

0 Kudos