HOW TO

Use the Dissolve method of IBasicGeoprocessor in VBA

Last Published: April 25, 2020

Summary

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

Dissolve creates a new table by aggregating records in the input table based on values of a specified field. This article explains how to use the output table from Dissolve to create a new feature layer and a stand-alone table.

Procedure



The Dissolve syntax is:

Code:
Set variable = object.Dissolve (InputTable, useSelected, dissolveField, summaryFields, outputName )

where:
   object - is an object expression that evaluates to an IBasicGeoprocessor object.
variable - is a reference to an object that implements ITable.
InputTable - is an ITable object.
useSelected - is a Boolean expression that represents the useSelected state. It controls whether or not a selected subset of the InputTable will be dissolved. True signifies a selected subset will be dissolved. False signifies Dissolve will ignore any selected subset and will dissolve the entire set.
dissolveField - is a string expression that represents the dissolveField.
summaryFields - is a string expression that represents the summaryFields.
outputName - is an IDatasetName object.


The summaryFields argument is a comma-delimited string that lists the generated summary fields. The summary operations include:

· Dissolve
· Count
· Minimum
· Maximum
· Sum
· Average
· Variance
· StdDev

The syntax for the summaryFields argument is:

Code:
"operation.field_name1, operation2.field_name2,..., operationN.field_nameN"

If you plan to use the output table to create a new feature layer you must insert Dissolve.Shape at the start of the summaryFields string.

Example:

Code:
"Dissolve.Shape, Sum.Area, StdDev.Population"

If you wish to create a standalone summary table you can omit Dissolve.Shape from the summaryFields string.

Note:
A field in the output representing the dissolve field is not automatically calculated as it is in the GeoProcessing Wizard. You may want to perform an operation such as minimum on the dissolve field in order to return the dissolve field value for the output.


Example:

Code:
"Sum.Area, StdDev.Population"

The following VBA examples assume you have a layer named STATES with fields named SUB_REGION and AREA.

Example 1:

Public Sub Dissolve()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap

' Find the layer named STATES
Dim pLayer As ILayer
Dim pInputFeatLayer As IFeatureLayer
Dim intCount As Integer
For intCount = 0 To pMap.LayerCount - 1
Set pLayer = pMap.Layer(intCount)
If TypeOf pLayer Is IFeatureLayer Then
If pLayer.Name = "STATES" Then
Set pInputFeatLayer = pLayer
Exit For
End If
End If
Next
If pInputFeatLayer Is Nothing Then
MsgBox "The STATES layer was not found"
Exit Sub
End If

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

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

' Make sure there is a field call SUB_REGION in the input layer
If pInputTable.FindField("SUB_REGION") = -1 Then
MsgBox "There must be a field named SUB_REGION in STATES"
Exit Sub
End If

' Get feature class properties needed for output
Dim pInputFeatCLass As IFeatureClass
Set pInputFeatCLass = pInputFeatLayer.FeatureClass
Dim pFeatClassName As IFeatureClassName
Set pFeatClassName = New FeatureClassName
With pFeatClassName
.FeatureType = esriFTSimple
.ShapeFieldName = "Shape"
.ShapeType = pInputFeatCLass.ShapeType
End With

' Set output location and output 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 = "Dissolve_result"

Set pDatasetName.WorkspaceName = pNewWSName

' Perform the dissolve.
' Note the summary fields string (Dissolve.Shape, Minimum.state_name ...)
' below. This is a comma-delimited string that lists the generated summary
' fields. The syntax for the summaryFields argument is
' <operation_code1>.<field_name1>, <operation_codeN>.<field_nameN>�
' Operation codes include: Dissolve, Count, Minimum, Maximum, Sum,
' Average, Variance and StdDev.
'
' Since we are performing a spatial dissolve, we must use the operation code
' Dissolve' on the Shape field.
Dim iBGP As IBasicGeoprocessor
Set iBGP = New BasicGeoprocessor
Dim pOutputTable As ITable
Set pOutputTable = iBGP.Dissolve(pInputTable, False, "SUB_REGION", _
"Dissolve.Shape, Minimum.SUB_REGION, Count.SUB_REGION, Average.AREA", _
pDatasetName)

' Add the output to the map
Dim pOutputFeatClass As IFeatureClass
Set pOutputFeatClass = pOutputTable

' Error checking
If pOutputFeatClass Is Nothing Then
MsgBox "FeatureClass QI Failed"
Exit Sub
End If

Dim pOutputFeatLayer As IFeatureLayer
Set pOutputFeatLayer = New FeatureLayer
Set pOutputFeatLayer.FeatureClass = pOutputFeatClass
pOutputFeatLayer.Name = pOutputFeatClass.AliasName
pMap.AddLayer pOutputFeatLayer

End sub


Example 2:

Option Explicit

Sub CreateSummaryTable()
' creates a summary table using Dissolve and adds it to ArcMap
' as a standalone table. run the macro, then make sure the Source
' tab is active in the Table of Contents to see the new table.

Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap

' Find the layer named STATES
Dim pLayer As ILayer
Dim pInputFeatLayer As IFeatureLayer
Dim intCount As Integer
For intCount = 0 To pMap.LayerCount - 1
Set pLayer = pMap.Layer(intCount)
If TypeOf pLayer Is IFeatureLayer Then
If pLayer.Name = "STATES" Then
Set pInputFeatLayer = pLayer
Exit For
End If
End If
Next
If pInputFeatLayer Is Nothing Then
MsgBox "The STATES layer was not found"
Exit Sub
End If

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

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

' Make sure there is a field call SUB_REGION in the input layer
If pInputTable.FindField("SUB_REGION") = -1 Then
MsgBox "There must be a field named SUB_REGION in STATES"
Exit Sub
End If

' Set up the output location
Dim pNewWSName As IWorkspaceName
Set pNewWSName = New WorkspaceName
' shapefile flavor will work just fine
pNewWSName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"
pNewWSName.PathName = "c:\temp"

' Set up the output table
Dim pOutTabName As ITableName
Set pOutTabName = New TableName
Dim pDatasetName As IDatasetName
Set pDatasetName = pOutTabName
pDatasetName.Name = "dissolve_summary_tab"
Set pDatasetName.WorkspaceName = pNewWSName

' Perform the dissolve.
' Note the summary fields string (Minimum.SUB_REGION...)
' below. This is a comma-delimited string that lists the generated summary
' fields. The syntax for the summaryFields argument is
' <operation_code1>.<field_name1>, <operation_codeN>.<field_nameN>�
' Operation codes include: Dissolve, Count, Minimum, Maximum, Sum,
' Average, Variance and StdDev.
'
' We are using the output table to build a standalone table, so we
' can omit Shape.Dissolve from the beginning of the string
Dim iBGP As IBasicGeoprocessor
Set iBGP = New BasicGeoprocessor
Dim pOutputTable As ITable
Set pOutputTable = iBGP.Dissolve(pInputTable, False, "SUB_REGION", _
"Minimum.SUB_REGION, Count.SUB_REGION, Average.AREA", _
pDatasetName)

' add the table to map
Dim pStTab As IStandaloneTable
Set pStTab = New StandaloneTable
Set pStTab.Table = pOutputTable
Dim pStTabColl As IStandaloneTableCollection
Set pStTabColl = pMap
pStTabColl.AddStandaloneTable pStTab

' Refresh the TOC
pDoc.UpdateContents

End Sub

Article ID:000004529

Software:
  • ArcMap 8 x
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic