I am trying to calculate the area of a polygon within my Python script. I create a new polygon from merging two together, and I'd like to add the area of the resulting polygon to a field in the output file. The polygon is stored in a regular shapefile, and is projected. Area preferably in map units.
I would have thought this to be a quite common and simple task, but despite a lot of Googleing I have been unable to find a working solutions so far.
I was planning on using
arcpy.updateCursor to insert the value once it's calculated (there's only one feature in the FC at this stage), so easiest is if it can be returned as a variable. Any alternative solution that accomplishes the same task (getting the area value into correct field) will also work.
I have also tried the Field calculator from Python. Modified from the help pages I thought the following would work, but so far no luck.
arcpy.AddField_management(tempPgs, "Shape_area", 'DOUBLE') exp = "float(!SHAPE.AREA!.split())" arcpy.CalculateField_management(tempPgs, "Shape_area", exp)
Running ArcGIS Basic 10.1 SP1 with Python 2.7 on Windows 7.
Relevant parts of my current code looks like this:
#/.../ arcpy.Copy_management(inpgs, outpgs) arcpy.AddField_management(outpgs, 'Shape_area', 'LONG') fields = AM.FieldLst(outpgs) #/.../ # Identify and search for shapes smaller than minimum area where1 = '"' + 'Shape_Area' + '" < ' + str(msz) polyrows = arcpy.SearchCursor(inpgs, where1) for prow in polyrows: grd1 = prow.GridID # GridID on the current polygon grd2 = nDD.get(grd1) # GridID on the polygon downstream # Update features if grd2 geometry1 = prow.Shape geometry2 = geometryDictionary[grd2] # Update temporary features arcpy.Merge_management([geometry1, geometry2], tempMerged) arcpy.Dissolve_management(tempMerged, tempPgs) fds = AM.FieldLst(tempPgs) for field in fields[2:]: arcpy.AddField_management(tempPgs, field, 'DOUBLE') for fd in fds[2:]: arcpy.DeleteField_management(tempPgs, fd) exp = "float(!SHAPE.AREA!.split())" arcpy.CalculateField_management(tempPgs, "Shape_area", exp) # Append them to output FC try: arcpy.Append_management(tempPgs, outpgs, "TEST") except arcgisscripting.ExecuteError: arcpy.Append_management(tempPgs, outpgs, "NO_TEST") elif ... else ...