0

I am using Desktop 10.6, Basic license, Spatial Analyst, Python 2.6, 2019 AD.

I have 4 overlapping features in a polygon feature class. I'd like to sort the features by the depth field, calculate the acreage for the largest depth, then move to the next depth, calculate its acreage and then add it to the previous depth, and so on. This code currently does not work past adding the cumulative field. In the attached photo, the cumulative depth is what I am trying to generate. The acres field is to show the progression.


Working code:

enter image description here

contours = arcpy.GetParameterAsText(0)

desc = arcpy.Describe(contours)
arcpy.env.workspace = desc.path


arcpy.AddField_management(contours, "Cumulative_AC", "DOUBLE")
arcpy.AddField_management(contours, "Acres", "DOUBLE")
CursorFieldNames = ["SHAPE@", "Cumulative_AC", "Acres"]
RunTl = 0

with arcpy.da.UpdateCursor(contours, CursorFieldNames, sql_clause=(None, 'ORDER BY DEPTH DESC')) as cursor:
    for row in cursor:
        AreaValue = row[0].getArea('GEODESIC', 'ACRES')  # Read area value as double
        RunTl += AreaValue  # Capture running total
        row[1] = RunTl  # Write total area value plus the previously added values to field
        row[2] = AreaValue
        cursor.updateRow(row)
del RunTl
  • 2
    Your del statement is not required when using with for your cursor. What happens when you remove that line of code? – PolyGeo Jun 1 at 2:16
  • 2
    You have already sorted the values since you make no attempt at this in the code? – BERA Jun 1 at 7:09
  • @PolyGeo, no change. – rrrrrrrr Jun 3 at 16:23
  • @BERA You're right, didn't add the sort_management tool yet. Will update. – rrrrrrrr Jun 3 at 16:23
  • @SonofaBeach screenshot is showing what I want the results to be in column "cumulative". – rrrrrrrr Jun 3 at 16:24
4

It looks like you’re setting the ‘cumulative’ field to the area of that record only, not the running total.

Try replacing:

row[1] = AreaValue

with:

row[1] = running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.