3

I am attempting to create a tool using Model Builder (and Python code) for calculating the average annual precipitation of a study area using the Thiessen Polygon Method. My model runs good other than finding the total area of the study area for further calculations in my model. Here is a sample of my code extracting the total area. I attempting to just use Summary Statistics to find the total area but I don't know how to access that table for input into further calculations.

def MySub (TotalArea1) :

 TotalArea = 0.0
 cursor = arcpy.SearchCursor(feat)
 for row in cursor:
    TotalArea += row.getValue("POLY_AREA")
 del row
 del cursor

 cursor = arcpy.UpdateCursor(feat)
 for row in cursor:
    row.setValue("TotArea", TotalArea)
    cursor.updateRow(row)
 del row
 del cursor
6
  • Is your data a shapefile or geodatabase feature class? Is the field POLY_AREA previously populated with the area? – Michael Stimson Dec 7 '14 at 23:38
  • Could you elaborate more on where you are using "TotalArea1" in your def, what "feat" is and where you intend to use summary statistics tool exactly? In the code, you are first adding up all values in "POLY_AREA" field of "feat" and then writing the sum to all rows of "TotalArea" field. Is it what you want? As highlighted by @Michael, if POLY_AREA has the area already populated, it is simply entering this polygon data as the input to SUmmary Statistics and picking the SUM for POLY_AREA field. – fatih_dur Dec 8 '14 at 12:12
  • My data is in a geodatabase, all feature classes. – Jonathan Nelson Dec 8 '14 at 18:49
  • What @fatih_dur is saying is you're passing a parameter to the function but then not using it, I think it should be feat you're passing to the sub. Fatih, I also know how to use summary statistics and join the table, but asked a question about its legitimacy and got no response, which lead me to assume it was bad therefore the need to use the shape.area instead. As it's a geodatabase feature class you can use shape_area provided it's in a suitable projected coordinate system. This answer is about summing area based on what I know to be correct. – Michael Stimson Dec 8 '14 at 21:42
  • Sorry @MichaelMiles-Stimson I could not attend your comment earlier. I was just seeking an explanation to make more sense from the query and your answer gives more than enough (it may even go to wikis). I appreciate if you can edit "feat" related items in your codes, as you mentioned. I think an approach to populate total area field with the same value is inefficient (i.e., same value for all features) and at this point i would like to ask Jonathan what is the next step in your model with this total area value? – fatih_dur Jan 4 '15 at 11:23
1

It seems your code sums the area covered by the polygons.

Instead of relying on a field that may or may not exist, and may or may not be correct you can get the area of the shapes at runtime:

Assuming that the feature class is in an appropriate spatial reference to sum the area:

def MySub(TotalArea1):
    TotalArea = 0.0
    with arcpy.da.SearchCursor(feat,"SHAPE@AREA") as sCur:
        for Row in sCur:
            TotalArea += Row[0]

    with arcpy.da.UpdateCursor(feat,"TotArea") as uCur:
        for Row in uCur:
            Row[0] = TotalArea

If not, say your data is in geographic coordinates and you want to sum in metres (or feet) simply project-on-the-fly:

def MySub(TotalArea1):
    TotalArea = 0.0
    toSR = arcpy.SpatialReference(28355) # MGA55, find a SRID that suits you
    with arcpy.da.SearchCursor(feat,"SHAPE@") as sCur:
        for Row in sCur:
            thisGeom = Row[0]
            projGeom = thisGeom.projectAs(toSR)
            TotalArea += projGeom.area
            TotalArea += Row[0]

    with arcpy.da.UpdateCursor(feat,"TotArea") as uCur:
        for Row in uCur:
            Row[0] = TotalArea

You will note the with statements.. these do not work for arcpy cursors, only arcpy.da cursors. When you declare a cursor in this way the disposal is automatic. You can of course use arcpy cursors with this method:

def MySub (TotalArea1) :
    desc = arcpy.Describe(feat)
    shapeFieldName = desc.ShapeFieldName
    TotalArea = 0.0
    cursor = arcpy.SearchCursor(feat)
    for row in cursor:
        thisShape = row.getValue(shapeFieldName)
        TotalArea += thisShape.area
    del row
    del cursor

    cursor = arcpy.UpdateCursor(feat)
    for row in cursor:
        row.setValue("TotArea", TotalArea)
        cursor.updateRow(row)
    del row
    del cursor

If you have a question about summary statistics please post that as a separate question.

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.