Accessing MULTI-part polygons in arcpy

4485
1
05-30-2014 06:53 AM
josesanchez1
New Contributor III
By running this piece of code:

import arcpy
arcpy.env.workspace = "C:\working\GEODATABASES\\tests.gdb"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
    with arcpy.da.SearchCursor(fc, ("OID@", "SHAPE@")) as cursor:
    for row in cursor:
        print("Feature {0}".format(row[0]))
        print "   " + str(row[1].partCount) + " parts for THIS PARTICULAR FEATURE"
        for part in row[1]:
            poly = arcpy.Polygon(part)
            print("   Points {0}".format(poly.pointCount))  


In theory I am able to list all polygons for a particular feature class and list whether they are multi-part polygons or not. In addition, it shows the number of points per feature part. This is the target.

HOWEVER, it does not do the job properly as I am dealing with a complicate feature class -I think- that actually have more feature parts than the script can identify and have more points per feature part than the script can identify. So, occasionally, it does not obtain the right number of points per part. I know that these points exist because I can see them by visualizing the layer in ArcMap.

By using the OLD reading cursor system in arcpy (not the above one), I get in some polygons a higher number of points, but still there are some of them missing, therefore the script cannot list them.

Why is that this script stops counting either points or parts sometimes when entering a multipart-polygon within my feature class?

Is it my code not well tuned for complicated multi-part polygons?

Thanks,
Tags (2)
0 Kudos
1 Reply
MattEiben
Occasional Contributor
The following worked for me:

arcpy.env.workspace = r"Your\Workspace"

fcList = arcpy.ListFeatureClasses()

for fc in fcList:

    with arcpy.da.SearchCursor(fc, ["SHAPE@"]) as cursor:
        for row in cursor:

            geometry = row[0]

            if geometry.isMultipart == True:
                print "{0} is Multipart".format(fc)

            partnum = 0
            while partnum < geometry.partCount:
                part = geometry.getPart(partnum)
                print arcpy.Polygon(part).pointCount
                partnum += 1


Though a word of warning.  If your dataset is very large, I'd run this script through the Window's shell instead as it was a bit to much for the ArcMap Python window and crashed.
0 Kudos