We have some feature classes or shape files and we want to get distinct values from a particular column and its count. This is a common task which we need when we are working with feature classes/Shape files. Suppose we have a feature class or shape file and its name is "Landuse" and in Landuse feature class we have attribute "FEAT_TYPE". We want to get the distinct value from "FEAT_TYPE" column and also find the count of each "FEAT_TYPE". To get the distinct values of a particular column from shape file or feature class is very time consuming if we try to find a distinct value using loop. Because using loop it will take time and it will hurt the performance of the application also. To solve this problem we can use data statistics interface of ArcObject to find the distinct value and its count as well.This technique requires fewer lines of code and performance is also very good.
To implement this approach without using loop first we get the object of workspacefactory. Once we get the object of workspacefactory after that using method OpenfromFile() we get the object of workspace. Once we get the object of workspace we can cast this workspace object to ifeatureworkspace and use method Openfeatureclass to get object of featureclass. Use the Search method of featureclass we will get icursor and then we are ready to use IDatastatistics to get desired result.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using ESRI.ArcGIS.DataSourcesFile;
- using ESRI.ArcGIS.DataSourcesGDB;
- using ESRI.ArcGIS.Geodatabase;
- using ESRI.ArcGIS.esriSystem;
- using ESRI.ArcGIS.Geometry;
- using System.Collections;
- namespace WindowsApp {
- publicpartialclassForm1: Form {
- public Form1() {
- InitializeComponent();
- }
- privatevoid button1_Click(object sender, EventArgs e) {
- IWorkspaceFactory pwsf = newShapefileWorkspaceFactory();
- IWorkspace pws = pwsf.OpenFromFile(@ "D:\YS\ShapeFiles", 0);
- IFeatureWorkspace pfeatWs = (IFeatureWorkspace) pws;
- IFeatureClass pfc = pfeatWs.OpenFeatureClass("LandUseA.shp");
- IFeatureCursor pfeatCur = pfc.Search(null, false);
- IDataStatistics pDataStat = newDataStatisticsClass();
- pDataStat.Cursor = (ICursor) pfeatCur;
- pDataStat.Field = "FEAT_TYPE";
- IEnumerator pEnum = pDataStat.UniqueValues;
- Boolean chk = pEnum.MoveNext();
- List < string > distinctValues = newList < string > ();
- while (chk) {
- string Name = (String) pEnum.Current;
- distinctValues.Add(Name);
- chk = pEnum.MoveNext();
- }
- }
- }
- }