I have a layer which has small rectangular gaps, I need to merge the gaps into the larger polygon that surrounds it, has anyone got any idea of how to do this, I know i can fill in the gaps with a new feature then go to Editor> Merge, but i have hundreds of these. If theres a way of doing this via vba or python, it would be great.

I'm using ArcGIS 9.3.1 (ArcEditor)

Heres what it looks like gaps is layer


This can be done with a bunch of geoprocessing tools.

  1. Create a new large polygon layer that encompasses all features with holes
  2. Run the Symmetrical Difference tool
  3. Run the Multipart To Singlepart tool
  4. Delete the one very large feature from Multipart To Singlepart result layer
  5. Run Spatial Join, Target feature = result of Multipart To Singlepart layer
  6. Run Merge Tool
  7. Run Dissolve Tool, Dissolve Field option check the common ID field, and check the Unsplit lines check box

Once tested you could create a model within model builder and configure this process into a script. Sorry, links provided are for ArcGIS 10, same methodology though.

  • The symmetrical Difference tool is ArcInfo only, I've only got ArcEditor. – MapMan Feb 29 '12 at 21:12

Thanks both, but I've found some vba code that did exactly what i wanted and was written by Kirk Kuykendall (so thanks Kirk).

Code is below

Option Explicit
Sub Test()
    ' loop through each selected feature
    ' and merge it with the adjacent feature
    ' that is largest.

    Dim pEditor As IEditor
    Set pEditor = _
      Application.FindExtensionByName("ESRI Object Editor")

    If pEditor.EditState <> esriStateEditing Then
        MsgBox "start editing first"
        Exit Sub
    End If

    Dim pEL As IEditLayers
    Set pEL = pEditor
    If pEL.CurrentLayer.FeatureClass.ShapeType _
       <> esriGeometryPolygon Then
        MsgBox "target layer not polygons"
        Exit Sub
    End If

    Dim pFSel As IFeatureSelection
    Set pFSel = pEL.CurrentLayer
    If pFSel.SelectionSet.Count = 0 Then
        MsgBox "nothing selected"
        Exit Sub
    End If

    Dim pFCur As IFeatureCursor
    pFSel.SelectionSet.Search Nothing, False, pFCur

    Dim pFeat As IFeature
    Set pFeat = pFCur.NextFeature
    Do Until pFeat Is Nothing
        Dim pLargestAdj As IFeature
        Set pLargestAdj = GetLargestAdjacent(pFeat)
        If Not pLargestAdj Is Nothing Then
            Dim pTopoOp As ITopologicalOperator
            Set pTopoOp = pLargestAdj.ShapeCopy
            Set pLargestAdj.Shape = pTopoOp.Union(pFeat.ShapeCopy)
            Debug.Print "nothing adjacent to: " & pFeat.OID
        End If
        Set pFeat = pFCur.NextFeature
    pEditor.StopOperation "merge"
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
End Sub

Function GetLargestAdjacent(pFeat As IFeature) As IFeature
    Dim pSF As ISpatialFilter
    Set pSF = New SpatialFilter
    Set pSF.Geometry = pFeat.Shape
    pSF.SpatialRel = esriSpatialRelIntersects

    Dim pFC As IFeatureClass
    Set pFC = pFeat.Class
    Dim pFCur As IFeatureCursor
    Set pFCur = pFC.Search(pSF, False)

    Dim pFeat2 As IFeature, pLargestFeat As IFeature
    Dim dMaxArea As Double
    Set pFeat2 = pFCur.NextFeature
    Do Until pFeat2 Is Nothing
        If pFeat2.OID <> pFeat.OID Then
            If pLargestFeat Is Nothing Then
                Set pLargestFeat = pFeat2
                dMaxArea = GetArea(pFeat2.Shape)
                If GetArea(pFeat2.Shape) > dMaxArea Then
                    Set pLargestFeat = pFeat2
                    dMaxArea = GetArea(pFeat2.Shape)
                End If
            End If
        End If
        Set pFeat2 = pFCur.NextFeature
    Set GetLargestAdjacent = pLargestFeat

End Function

Function GetArea(pArea As IArea) As Double
    GetArea = pArea.Area
End Function

Forum link is here http://forums.esri.com/Thread.asp?c=93&f=992&t=87064#238942


I think that there is a much easier answer to this one which I uncovered via a Discussion Forum posting a few years ago. That is to run the Union tool on the feature class in question (so it is the only feature class in the list) and uncheck the Gaps Allowed checkbox.

You will then need to Eliminate these small polygons which is easy using the tool of the same name in ArcInfo but should also be achievable with an ArcView or ArcEditor license using an ArcScript.

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.