English

How To: Intersect two feature layers using IBasicGeoprocessor in VBA

Summary

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

Intersect creates a new feature class by overlaying a line or polygon input layer with the polygons in an overlay layer. The output feature class has the combined attributes of both input layers, and only contains features that fall within the outline of the overlay polygons.

Procedure

The syntax for Intersect is:

Code:
Set variable = object.Intersect(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 whether only the selected subset of the InputTable will be intersected. False signifies that the operation will ignore any selected subset in InputTable.
overlayTable - is an e object
useSelectedOverlay - is a Boolean expression that represents the selected state of the overlay layer. It specifies whether only the selected subset of the overlayTable will be used. False signifies that the operation will ignore any selected subset in overlayTable.
Tolerance - is a double that represents the tolerance. It is the Intersect 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 Intersects two feature layers.

Public Sub Intersect()

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

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

' Get the input feature class.
' The Input feature class properties, such as shape type,
' will be needed for the output.
Dim pInputFeatClass As IFeatureClass
Set pInputFeatClass = pInputFeatLayer.FeatureClass

' Get the overlay layer
' 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 and shape type (taken from the
' properties of the input feature class)
Dim pFeatClassName As IFeatureClassName
Set pFeatClassName = New FeatureClassName
With pFeatClassName
.FeatureType = esriFTSimple
.ShapeFieldName = "Shape"
.ShapeType = pInputFeatClass.ShapeType
End With

' 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 = "Intersect_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 intersect
Dim pBGP As IBasicGeoprocessor
Set pBGP = New BasicGeoprocessor
Dim pOutputFeatClass As IFeatureClass
Set pOutputFeatClass = pBGP.intersect(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