Clustering Engine


This sample consists of an engine written in C++ that performs cluster analysis on a set of observations. The Clustering Engine performs a hierarchical cluster analysis on a feature class, generating lists of features in each cluster. Options are available for the number of clusters, clustering algorithm, distance measurement, etc.

Look for other samples under Analysis and Visualization that demonstrate tools that use the Clustering Engine.

How to use:

    Register the component:

  1. Open this sample's workspace and change the paths to esriCore.olb in ClusteringEngine.idl and stdafx.h so they both point to your ArcGIS installation.
  2. Build this sample's dll.

    Use the component:

  1. Start ArcMap and add at least one feature class.
  2. Open the VBA editor and reference the ClusteringEngine.dll (Tools > References).
  3. Paste the following VBA code into ThisDocument.
    Option Explicit
    Const c_ClusterCount = 3
    Const c_FieldName = "Pop1990"
    Sub DebugPrintClusters()
      Dim pMxDoc As IMxDocument
      Set pMxDoc = Application.Document
      Dim pFeatLayer As IFeatureLayer
      Set pFeatLayer = pMxDoc.ActiveView.FocusMap.Layer(0)		' get first feature layer in map
      Dim pClusterEngine As IClusteringEngine
      Set pClusterEngine = New ClusteringEngine
      Set pClusterEngine.FeatureLayer = pFeatLayer
      pClusterEngine.ClusterCount = c_ClusterCount 			' specify how many clusters
      pClusterEngine.ClusteringAlgorithm = esriHCRMinimumVariance  	' specify cluster algorithm
      pClusterEngine.SimilarityMeasure = esriHCREuclidean 		' specify similarity measure
      Dim pNormFields As INormalizedFields
      Set pNormFields = pClusterEngine
      pNormFields.AddField c_FieldName, c_FieldName, "", "", 1	' specify field(s)
      pClusterEngine.PopulateClusters				' generate clusters
      ' print out Feature IDs to the Visual Basic Immediate window
      Dim i As Integer
      Dim j As Integer
      Dim pFeatIDs As IFeatureIDSet
      Dim Features As Variant
      For i = 0 To pClusterEngine.ClusterCount - 1
        Debug.Print ""
        Debug.Print "FIDs in Cluster " & i & ":"
        pClusterEngine.GetClusterFIDs i, pFeatIDs
        Features = pFeatIDs.Members
        For j = 0 To UBound(Features) - 1
          Debug.Print Features(j)
        Next j
      Next i
    End Sub
  4. Run the macro. Open the VB editor's Immediate window to see the lists of features in each cluster.
  5. Use the VB object browser to explore the Clustering Engine interfaces: IClusteringEngine, IDendrogram, and INormalizedFields.
  6. Check out the other samples under Analysis and Visualization to see how the Clustering Engine can be integrated with other tools.


Difficulty: Intermediate

Visual C++
File Description
ClusteringEngine.cpp Implementation file for ClusteringEngine class.
ClusteringEngine.h Header file for ClusteringEngine class.
ClusteringEngine.rgs Registry script file for the component.
ClusteringEngine.dsp Project file.
ClusteringEngine.dsw Workspace file.
ClusteringEngine.dll Compiled DLL for project.
ClusteringEngineApp.cpp Implementation of DLL Exports.
ClusteringEngineApp.h Header file for DLL Exports.
ClusteringEngine.def Module Definition file declaring the module parameters.
ClusteringEngine.idl The IDL file for the sample.
ClusteringEngine.rc Resource file.
Resource.h Resource file.
StdAfx.cpp Source file that includes standard system include files.
StdAfx.h Header file that includes standard system include files.

Key CoClasses: FeatureIdSet, FeatureLayer
Key Interfaces: IFeatureIDSet, IFeatureLayer, IGeoFeatureLayer, IPersist, ITable