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
Answer
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
pEditor.StartOperation
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)
pLargestAdj.Store
pFeat.Delete
Else
Debug.Print "nothing adjacent to: " & pFeat.OID
End If
Set pFeat = pFCur.NextFeature
Loop
pEditor.StopOperation "merge"
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.ActiveView.Refresh
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)
Else
If GetArea(pFeat2.Shape) > dMaxArea Then
Set pLargestFeat = pFeat2
dMaxArea = GetArea(pFeat2.Shape)
End If
End If
End If
Set pFeat2 = pFCur.NextFeature
Loop
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
No comments:
Post a Comment