3

I've written a script that should remove holes from polygons with use of cursors and geometry objects. However, holes remain even though I'm constructing new polygons from the vertices from the first part of the input polygon geometry.

Here's the script:

def RemoveHoles (inFc, workspace):
    outFc = UniqueFileName(workspace) #creates a file path/name string
    array = arcpy.Array ()
    sr = arcpy.Describe (inFc).spatialReference
    outPath, outName = os.path.split (outFc)
    arcpy.CreateFeatureclass_management (outPath, outName, "POLYGON", spatial_reference = sr)
    with arcpy.da.InsertCursor (outFc, "SHAPE@") as iCurs:
        with arcpy.da.SearchCursor (inFc, "SHAPE@") as sCurs:
            for geom, in sCurs:
                for i in range (geom.partCount):
                    part = geom.getPart (i)
                    for pnt in part:
                        print pnt
                        if not pnt:
                            break
                        array.add (geom.getPart (0))
                    print array.count
                    polygon = arcpy.Polygon (array)
                    array.removeAll ()
                    row = (polygon,)
                    iCurs.insertRow (row)
    del iCurs
    del sCurs
    return outFc

For this polygon:

enter image description here

The output reads as so:

571048.0859 4230953.3243 NaN NaN
571762.4623 4225079.5626 NaN NaN
566206.2012 4227698.9428 NaN NaN
571048.0859 4230953.3243 NaN NaN
None
4

And yet, despite the fact that I'm constructing a new polygon from three points, the hole is still preserved in the output feature class. Does anyone have any idea why?

3

I think you were adding the whole shape when you added to your array. I've changed slightly to only get part 0, and add pnt to the array

with arcpy.da.InsertCursor (outFc, "SHAPE@") as iCurs:
    with arcpy.da.SearchCursor (inFc, "SHAPE@") as sCurs:
        for geom, in sCurs:
            for i in range (geom.partCount):
                part = geom.getPart (i)
                if i > 0:
                    break
                for pnt in part:
                    print pnt
                    if not pnt:
                        break
                    array.add (pnt)
                print array.count
                polygon = arcpy.Polygon (array)
                array.removeAll ()
                row = (polygon,)
                iCurs.insertRow (row)
  • there are probably smarter ways of limiting the part - I just made the modifications to your code rather than rewriting. – Midavalo Jan 14 '16 at 0:06
  • Each of my features only has one part, so this doesn't solve the issue. – Emil Brundage Jan 14 '16 at 0:14
  • I believe the donut hole is counted as a separate part. I created a polygon that looks the same as your image above - I had the same result using your script, but with my modification above I get just the polygon and not the donut hole. – Midavalo Jan 14 '16 at 0:17
  • Ah, it works. Thanks! The only issue is with multipart polygons, but I can work around that. – Emil Brundage Jan 14 '16 at 0:21
  • Take a look at Reading Geometries which may give an idea of how to identify multiparts and donut holes separately – Midavalo Jan 14 '16 at 0:56

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.