English

How To: Clip a feature layer using IBasicGeoprocessor in VBA

Summary

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

Clip extracts features of a point, line, or polygon layer based on the spatial extent of an overlay polygon layer. The attributes of the input feature class are carried over to the output feature class.

Procedure

The syntax for Clip is:

Code:
Set variable = object.Clip(InputTable, useSelectedInput, clipTable, useSelectedClip, 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 to clip only the selected features in InputTable. False signifies that the operation will ignore any selected subset in InputTable.
  • clipTable - is an ITable object
  • useSelectedClip - is a Boolean expression that represents the selected state of the clip layer. It specifies whether to use only the selected features in ClipTable during the operation. False signifies that the operation will ignore any selected subset in ClipTable.
  • Tolerance - is a double that represents the tolerance. It is the clipping tolerance. Pass 0.0 to use the default tolerance which is 1/10,000 of the extent of the data frame.
  • outputName - is an IFeatureClassName object.
Here is a VBA example.
Public Sub Clip()
	' 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
	Dim pInputTable As ITable
	Set pInputTable = player

	' Get the input feature class.
	' Use the Itable interface from the Layer (not from the FeatureClass)
	' 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 clip/overlay layer
	' Use the Itable interface from the Layer (not from the FeatureClass)
	Set pLayer = pMxDoc.FocusMap.Layer(1)
	Dim pClipTable As ITable
	Set pClipTable = pLayer

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

	If pClipTable 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
	' For ArcGIS 8.x, uncomment and use this line:
	'pNewWSName.WorkspaceFactoryProgID = "esriCore.ShapeFileWorkspaceFactory"
	' For ArcGIS 9.0 and 9.1, use this line instead:
	pNewWSName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory"
	pNewWSName.PathName = "C:\temp"

	Dim pDatasetName As IDatasetName
	Set pDatasetName = pFeatClassName
	pDatasetName.Name = "Clip_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 clip
	Dim pBGP As IBasicGeoprocessor
	Set pBGP = New BasicGeoprocessor
	Dim pOutputFeatClass As IFeatureClass
	Set pOutputFeatClass = pBGP.clip(pInputTable, False, pClipTable, False, _
		tol, pFeatClassName)

	' Add the output layer (clipped features) 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

To run this in ArcGIS Desktop 8.3, uncomment a single line of code; look for comments to this effect approximately halfway through the code.There is a known issue with ArcGIS 9.2 at Service Pack 1 and earlier, which prevents this code from running successfully. For this version, please invoke the Clip geoprocessing tool to accomplish the same task.
Public Sub Clip_GP()
	Dim pParams As IVariantArray
	Set pParams = New VarArray
	' Input feature layer name in TOC
	pParams.Add "states"
	' Clip feature layer name in TOC
	pParams.Add "Clip_layer"
	' Output feature class name and location
	pParams.Add "C:\temp\Clip_states.shp"

	' Execute the tool
	Dim pGP As IGeoProcessor
	Set pGP = New GeoProcessor

	Dim pGPResults As IGeoProcessorResult
	Set pGPResults = pGP.Execute("Clip_analysis", pParams, Nothing)
End Sub

Related Information