Geoprocessing

Created:10/3/2001
Description:

This sample merges the features of two layers of the same geometry type (polygons with polygons, lines with line, and points with points) into a single layer. The output is saved as a shapefile in the temp directory of the C drive (C:\temp).

How to use:
  1. Paste the code into VBA.
  2. Add two layers of the same geometry to the map. The sample merges these two layers together to create a third layer. You can adjust the code accordingly if you wish to merge more than two layers or to interactively select the layers you wish to merge.
  3. The sample creates a new shapefile called Merge_result. You can adjust the code to specify a different name for the output or to save the result to a geodatabase.
  4. Run the sample.
Public Sub Merge()
    ' Get the first layer in the map
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pLayer As ILayer
    Set pLayer = pMxDoc.FocusMap.Layer(0)
    Dim pFeatLayer As IFeatureLayer
    Set pFeatLayer = pLayer
    Dim pFirstFeatClass As IFeatureClass
    Set pFirstFeatClass = pFeatLayer.FeatureClass
    
    ' Get the first layer's table
    ' Use the Itable interface from the Layer (not from the FeatureClass)
    ' This table defines which fields are to be used in the output
    Dim pFirstTable As ITable
    Set pFirstTable = pLayer
    
    ' Get the second layer and its table
    ' Use the Itable interface from the Layer (not from the FeatureClass)
    Set pLayer = pMxDoc.FocusMap.Layer(1)
    Dim pSecondTable As ITable
    Set pSecondTable = pLayer
    
    ' Error checking
    If pFirstTable Is Nothing Then
        MsgBox "Table QI failed"
        Exit Sub
    End If
    
    If pSecondTable Is Nothing Then
        MsgBox "Table QI failed"
        Exit Sub
    End If
    
    ' Define the output feature class name and shape type
    Dim pFeatClassName As IFeatureClassName
    Set pFeatClassName = New FeatureClassName
    
    With pFeatClassName
        .FeatureType = esriFTSimple
        .ShapeFieldName = "Shape"
        .ShapeType = pFirstFeatClass.ShapeType
    End With
        
    ' Set the output location and feature class name
    Dim pNewWSName As IWorkspaceName
    Set pNewWSName = New WorkspaceName
    
    With pNewWSName
        .WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"
        .PathName = "C:\temp"
    End With
    
    Dim pDatasetName As IDatasetName
    Set pDatasetName = pFeatClassName
    pDatasetName.Name = "Merge_result"
    
    Set pDatasetName.WorkspaceName = pNewWSName
    
    ' Build the input set/array - these are the layers to be merged
    Dim inputArray As IArray
    Set inputArray = New esriCore.Array
    inputArray.Add pFirstTable
    inputArray.Add pSecondTable
    
    ' Perform the merge
    Dim pBGP As IBasicGeoprocessor
    Set pBGP = New BasicGeoprocessor
    Dim pOutputFeatClass As IFeatureClass
    Set pOutputFeatClass = pBGP.Merge(inputArray, pFirstTable, pFeatClassName)
    
    ' Add the output to the map
    Dim pOutputFeatLayer As IFeatureLayer
    Set pOutputFeatLayer = New FeatureLayer
    Set pOutputFeatLayer.FeatureClass = pOutputFeatClass
    pOutputFeatLayer.Name = pOutputFeatClass.AliasName
    pMxDoc.FocusMap.AddLayer pOutputFeatLayer

End Sub