Select to view content in your preferred language

Update Cursor for Deleting Rows in Hosted Layer

773
3
10-14-2022 09:27 AM
kjseitz2
New Contributor II

I'm trying to delete rows in a hosted layer by searching my local layer to feed an update cursor. I'm hitting an error claiming "AttributeError: 'Polygon' object has no attribute 'FACILITYID'". Below is my current code:

 

arcpy.env.overwriteOutput = True

# Create GIS object
gis = GIS('https://www.arcgis.com', username, password)
print("Logged in as " + str(gis.properties.user.username))

#lyr that will be search cursored
lyrSearch = r"C:\blahblah\JOINED_STRUCTURES_1"

#Hosted Layer Item ID
fsItemId = "2e1f37aab41746afacfcf7c8534384e8" 

#lyr that will be inserted into
lyrInsert = gis.content.get(fsItemId)
#needs to point directly to the layer in the Feature Service
tableInsert = lyrInsert.layers[0].url
print("Got Feature Service")

#a list of the fields needed to be loaded
fieldNames = ['SHAPE@', 'FACILITYID', 'ADDRESS', 'Editor', 'COMMENTS', 'ADDRESS', 'CITY', 'STATE', 'ZIP', 'DISPLAYOWNERNAME', 'CROSSBORESTATUS', 'CROSSBORECOMMENT', 'REPAIRSTATUS', 'ContractorName', 'Static_FacilityID']

#Update function
uCurs = arcpy.da.UpdateCursor(tableInsert, 'FACILITYID')

#cursor for searching and then feeding uCurs

n = 0
with arcpy.da.SearchCursor(lyrSearch, fieldNames) as sCurs:
    for row in sCurs:
        if row[0].FACILITYID == sCurs:
            uCurs.deleteRow(row)
            n +=1
            continue

print("Deleted " + str(n))
print('FIN')

If I remove the .FACILITYID in line 30 and just have "row[0] == sCurs:", then the script will run through, but not delete anything. Any suggestions?

0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

Couldn't you just use FeatureLayer.delete_features() instead? If you have a reliable method to identify your deletes, passing a list of OIDs to that method is very simple.

- Josh Carlson
Kendall County GIS
kjseitz2
New Contributor II

So I would need to create a list containing the FACILITYID from the JOINED_STRUCTURES_1 layer and feed that into the FeatureLayer.delete_features() command as the where clause right?

0 Kudos
jcarlson
MVP Esteemed Contributor

Yeah, you could create a list, but it needs to be in valid SQL, so you'd need to convert it to something you can pipe into "some_field IN('list','of','values')".

delete_where = f"""FACILITYID IN({','.join(["'" + i + "'" for i in your_list])})"""

Spits out something like:

FACILITYID IN('a','list','of','values')
- Josh Carlson
Kendall County GIS
0 Kudos