English

How To: Use the Union method of IBasicGeoprocessor in VBA

Summary

IBasicGeoprocessor contains five methods: Dissolve, Merge, Clip, Intersect and Union. Access these methods using VBA or VB.

Union creates a new feature class by overlaying a polygon input layer with a polygon overlay layer. The output feature class has the combined attributes of both input layers, and contains features from the full spatial extent of both input layers.

Procedure

The Union syntax is:

Code:
Set variable = object.Union(InputTable, useSelectedInput, overlayTable, useSelectedOverlay, Tolerance, outputName)


where:

   variable - is a reference to an object that implements IFeatureClass.
object - is an object expression that evaluates to an IBasicGeoprocessor object.
InputTable - is an ITable object.
useSelectedInput - is a Boolean expression that represents the selected state of the input layer. It specifies the selected subset of the InputTable that will be intersected. False signifies that the union operation will ignore any selected subset on the InputTable.
overlayTable - is an ITable object
useSelectedOverlay - is a Boolean expression that represents the selected state of the overlay layer. It specifies the selected subset of the overlayTable that will be used. False signifies that the union operation will ignore any selected subset of the overlayTable.
Tolerance - is a double representing the tolerance. It is the Union tolerance. Pass 0.0 to use the default tolerance, which is 1/10,000 of the extent of the dataframe.
outputName - is an IFeatureClassName object.


This VBA example Unions two feature layers.

Public Sub Union()

' Get the Input layer and feature class
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pLayer As ILayer
Set pLayer = pMxDoc.FocusMap.Layer(0)

' Get the input table
' Use the Itable interface from the Layer (not from the FeatureClass)
Dim pInputTable As ITable
Set pInputTable = player

' Get the overlay layer and table
' Use the Itable interface from the Layer (not from the FeatureClass)
Set pLayer = pMxDoc.FocusMap.Layer(1)
Dim pOverlayTable As ITable
Set pOverlayTable = pLayer

' Error checking
If pInputTable Is Nothing Then
MsgBox "Table QI failed"
Exit Sub
End If

If pOverlayTable Is Nothing Then
MsgBox "Table QI failed"
Exit Sub
End If

' Define the output feature class name
Dim pFeatClassName As IFeatureClassName
Set pFeatClassName = New FeatureClassName

' Set output location and feature class name
Dim pNewWSName As IWorkspaceName
Set pNewWSName = New WorkspaceName
pNewWSName.WorkspaceFactoryProgID = "esriCore.ShapeFileWorkspaceFactory.1"
pNewWSName.PathName = "C:\temp"

Dim pDatasetName As IDatasetName
Set pDatasetName = pFeatClassName
pDatasetName.Name = "Union_result"

Set pDatasetName.WorkspaceName = pNewWSName

' Set the tolerance. Passing 0.0 causes the default tolerance to be used.
' The default tolerance is 1/10,000 of the extent of the data frame’s spatial domain
Dim tol As Double
tol = 0#

' Perform the union
Dim pBGP As IBasicGeoprocessor
Set pBGP = New BasicGeoprocessor
Dim pOutputFeatClass As IFeatureClass
Set pOutputFeatClass = pBGP.union(pInputTable, False, pOverlayTable, False, _
tol, pFeatClassName)

' Add the output layer 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

Related Information