0

I have a shapefile with polygons. I can merge them manually by going into edit session and, selecting a some features to be merged, and then using merge button under Editor.

How to do this programmatically (in VBA)?

I've found the analogous ways but it doesn't solve the problem:

  1. Standard Dissolve tool (from ArcToolbox) doesn't work correctly for this purpose, because it cuts off many fields and because it operates with whole FeatureClass but not with a separate features.
  2. As I know by reading some examples, using an IBasicGeoprocessor (VBA) have the same problem - it operates with whole FeatureClass, producing a new FeatureClass, but not with separate features, mearging them within the same FeatureClass.

@kenbuja, Here is my try in vba (not in vb.net) : Using a CType causes the Compile error: Sub or Function not defined. What do I do wrong?

Dim pMxDoc As IMxDocument
Dim pCountyLayer As IFeatureSelection
Dim pCountySelection As ISelectionSet
Dim pCountyCursor As IFeatureCursor
Dim pCountyCursor2 As IFeatureCursor
Dim pFirstFeature As IFeature
Dim pSecondFeature As IFeature
Dim pThirdFeature As IFeature

Set pMxDoc = ThisDocument
Set pCountyLayer = pMxDoc.FocusMap.Layer(1)
Set pCountySelection = pCountyLayer.SelectionSet

' Create a cursors from the selected feature in Layer (1).
pCountySelection.Search Nothing, True, pCountyCursor
pCountySelection.Search Nothing, True, pCountyCursor2
' Return the selected features from the cursor.

Set pFirstFeature = pCountyCursor.NextFeature


Set pThirdFeature = pCountyCursor2.NextFeature
Set pSecondFeature = pCountyCursor2.NextFeature


Dim pFirstGeometry As IGeometry
Set pFirstGeometry = CType(pFirstFeature.Shape, IGeometry)
Dim pTopoOperator As ITopologicalOperator
Set pTopoOperator = CType(pFirstGeometry, ITopologicalOperator)
Dim pSecondGeom As IGeometry
Set pSecondGeom = CType(pSecondFeature.Shape, IGeometry)
Dim pMergedGeom As IGeometry
Set pMergedGeom = pTopoOperator.Union(pSecondGeom)
8
  • What version of ArcGIS are you using?
    – MaryBeth
    Commented Dec 1, 2015 at 12:47
  • Have you tried the Merge command?: resources.arcgis.com/en/help/main/10.2/index.html#//…
    – Aaron
    Commented Dec 1, 2015 at 12:51
  • @Alex does a spatial join works for you?
    – wittich
    Commented Dec 1, 2015 at 15:06
  • 1. You don't use good tools if you want merge differents FC use Merge. 2. Merge need FC in same projection and first FC in input set type and size if size is different you need to modified value if type is different you need to create a new fields and calculate it before. Dissolve combine geometries with same values (checked in statistics fields) it is not necessary to go through a vba macro for simple operation unless the data are large or you need to create big manipulation. You must also check if all featurclass have valid geometry with tool CheckGeometry Commented Dec 1, 2015 at 15:29
  • 1
    "it is not necessary to go through a vba macro for simple operation" - this simple operation is a part of my project in vba, so it is necessary for me to do it programmatically.
    – Alex
    Commented Dec 1, 2015 at 16:32

2 Answers 2

1

You can use the ITopologicalOperator's Union method to programmatically merge two polygons together. This example (written in VB.NET) shows how that's done.

Dim pFirstGeometry As IGeometry = CType(pFirstFeature.shape, IGeometry)
Dim pTopoOperator As ITopologicalOperator = CType(pFirstGeometry, ITopologicalOperator)
Dim pSecondGeom As IGeometry = CType(pSecondFeature.shape, IGeometry)
Dim pMergedGeom As IGeometry = pTopoOperator.Union(pSecondGeom)
10
  • thenks for attempt to help, but Union method corrupts the attribute table, like Dissolve do it
    – Alex
    Commented Dec 1, 2015 at 16:41
  • Can you be more specific with how it corrupts the attribute table? Does it leave the attributes empty or do the attributes not contain the expected information?
    – kenbuja
    Commented Dec 1, 2015 at 16:54
  • I tried the Union operation (in Editor menu ) manually - it creates extra Feature in the same FC. It's not a problem to delete a initial features, but result featere has a empty fields. Very soon I'll try your way with Union method programmatically and report the results; It wood be greate if result feature will appear in the same FC.
    – Alex
    Commented Dec 1, 2015 at 17:19
  • You will have to delete the second feature that was merged into the first. The code above doesn't include that step.
    – kenbuja
    Commented Dec 1, 2015 at 19:55
  • kenbuja, above (I edited a guestion) is my try in vba (not in vb.net) : Using a CType causes the Compile error: Sub or Function not defined. What do I do wrong?
    – Alex
    Commented Dec 2, 2015 at 7:46
0

I've found the analog of Editor_Merge in vba:

Application.Document.CommandBars.Find(ArcID.Editor_Merge).Execute

but the window appears which allows to select the feature where the result geometry will be assigned to, and I don't know how to choose the feature programmatically.

So I've found another code - it merges selected features (and deletes initial features after merging), the result stays in the same feature class

On Error GoTo EH

Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pFeatureSelection As IFeatureSelection
Dim pTopologicalOperator As ITopologicalOperator
Dim pEnumGeometry As IEnumGeometry
Dim pEnumGeometryBind As IEnumGeometryBind
Dim pEnumFeature As IEnumFeature
Dim pFeature As IFeature
Dim pNewFeature As IFeature
Dim pOutputGeometry As IGeometry
Dim pUID As New UID
Dim pEditor As IEditor
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass


Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap
Set pActiveView = pMxDocument.FocusMap
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureClass = pFeatureLayer.FeatureClass

' Get a handle to the Editor extension.
pUID = "esriEditor.Editor"
Set pEditor = Application.FindExtensionByCLSID(pUID)

Set pFeatureSelection = pFeatureLayer
Set pEnumGeometryBind = New EnumFeatureGeometry
pEnumGeometryBind.BindGeometrySource Nothing, pFeatureSelection.SelectionSet

If pFeatureSelection.SelectionSet.Count = 0 Then Exit Sub

Set pEnumGeometry = pEnumGeometryBind
Set pTopologicalOperator = New Polygon
pTopologicalOperator.ConstructUnion pEnumGeometry

' Get selected set and put into cursor.
Set pEnumFeature = pMap.FeatureSelection
Set pFeature = pEnumFeature.Next

' Get geometry of each polygon, add it to the new polyon
' and delete the original one.
    Do While Not pFeature Is Nothing
        If pOutputGeometry Is Nothing Then
        Set pOutputGeometry = pFeature.Shape
        Else
        Set pOutputGeometry = pTopologicalOperator.Union(pFeature.Shape)
        End If
        Set pTopologicalOperator = pOutputGeometry
        pFeature.Delete
        Set pFeature = pEnumFeature.Next
    Loop

' Create new feature and store the application number.
Set pNewFeature = pFeatureClass.CreateFeature
Set pNewFeature.Shape = pOutputGeometry

' Store new merged feature.
pNewFeature.Store

' Stop the edit operation.
'pEditor.StopOperation ("Merge Polygons")

' Refresh the map view
pActiveView.Refresh

Exit Sub

EH:
MsgBox Err.Description, vbInformation, "MergeCEPolygons"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.