Why is this code deleting rows within a feature class?

538
5
03-23-2023 01:19 PM
ccowin_odfw
Occasional Contributor

Hello,

My organization has a couple administrative boundaries that are used for modeling and I'm trying to split them against each other and then figure out the percentage of state, federal, tribal and private land is for each of the subsections.

I wrote the following code it splits one feature by the other that contains ownership data. When I then go to process each output of the split it just completely deletes all the rows in some of the features and I have no idea why or how that can even happen.

import arcpy

gdb = <path redacted>
if arcpy.Exists(gdb):
arcpy.Delete_management(gdb)

arcpy.management.CreateFileGDB(<path redacted>, 'DAU_by_Ownership.gdb')
all_wmus = <path redacted>
jurisdiction = <path redacted>
split = <path redacted>

arcpy.analysis.Split(jurisdiction, all_wmus, 'UNIT_NAME', gdb)

arcpy.env.workspace = gdb
WMUs = arcpy.ListFeatureClasses()


def get_acres(features, field):
acres = 0
with arcpy.da.SearchCursor(features, [field]) as cursor:
for row in cursor:
acres = row[0] + acres

return acres


for WMU in WMUs:
# Calculate Area and Put that
print('Getting area for ' + WMU)
arcpy.management.CalculateGeometryAttributes(WMU, [['Acres', 'AREA_GEODESIC']], area_unit='ACRES_US')

wmu_acres = get_acres(WMU, 'ACRES')
print(f'...{wmu_acres} Acres')

arcpy.management.AddField(WMU, 'WMU_Acres', 'LONG')
arcpy.management.AddField(WMU, 'DAU_Acres', 'LONG')
arcpy.management.AddField(WMU, 'WMU_pct', 'FLOAT')
arcpy.management.AddField(WMU, 'DAU_pct', 'FLOAT')

arcpy.management.CalculateField(WMU, 'UNIT_NAME', f'"{WMU}"')
arcpy.management.CalculateField(WMU, 'WMU_Acres', f'{wmu_acres}')
arcpy.management.CalculateField(WMU, 'WMU_pct', f'!Acres! / !WMU_Acres!')
0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

from Split (Analysis)—ArcGIS Pro | Documentation

are all the inputs overlapped by the splitting polygon?


... sort of retired...
0 Kudos
ccowin_odfw
Occasional Contributor

No, there is no documentation saying they need to be. If nothing is under the split feature then no output feature should be generated at all. The damage to the feature class is coming later, I just split them from code fine but the only thing I do after is calculate geometry and fields so you can imagine how frustrated I am with this. How do either of those two things delete every single row?

0 Kudos
RhettZufelt
MVP Frequent Contributor

Did not test the split layer part, but started with a polygon layer that has multiple, nearly identical polygons.

Testing from this line arcpy.env.workspace = gdb down, I got some really weird results.

Once I changed this line:

 

arcpy.management.CalculateGeometryAttributes(WMU, [['Acres', 'AREA_GEODESIC']], area_unit='ACRES_US')

TO

arcpy.management.CalculateGeometryAttributes(WMU, [['Acres', 'AREA_GEODESIC']], area_unit='ACRES')

 

 

the rest of the code seems to be working as expected.


R_

 

0 Kudos
ccowin_odfw
Occasional Contributor

That part actually works fine for me, I get an error without the '_US'

0 Kudos
RhettZufelt
MVP Frequent Contributor

That is weird.  In either case, it shouldn't have anything to do with deleting rows.

I'm at a loss on this one.

R_

0 Kudos