前几天在网上查找资料想实现ArcGIS中的统计功能,也就是Summarise,在费尽一番波折之后,终于在网上找到了解决方案:http://blog.csdn.net/gisoracle/article/details/4297823
通过这个案例的介绍,我自己整理下,封装成自己所需的函数,方法如下:
public static DataTable Summar(ILayer pLayer, string SummField, string OutField, string Method)
{
IFeatureLayer pFLayer = pLayer as IFeatureLayer;
IFeatureClass pFC = pFLayer.FeatureClass;
ICursor cursor = (ICursor)pFC.Search(null, false);//游标
IDataStatistics pDatdaS = new DataStatisticsClass();
pDatdaS.Field = SummField;
pDatdaS.Cursor=cursor;
System.Collections.IEnumerator Em = pDatdaS.UniqueValues;
Em.Reset();
//IFields pField = new FieldClass();4
//IFieldEdit pFieldEdit=pField as p
DataTable pTable = new DataTable();
pTable.Columns.Add(SummField);
pTable.Columns.Add(OutField);
//……
while (Em.MoveNext())
{
string OutStr=null;
DataRow pRow = pTable.NewRow();
object obj=Em.Current;
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = SummField + "=" + obj;
ICursor pCursorOut = (ICursor)pFC.Search(pQueryFilter, false);
IDataStatistics pDataSOut = new DataStatisticsClass();
pDataSOut.Field = OutField;
pDataSOut.Cursor = pCursorOut;
ESRI.ArcGIS.esriSystem.IStatisticsResults pResults = pDataSOut.Statistics;
pRow[SummField] = obj;
//
if(Method=="Minimum")
{
OutStr=pResults.Minimum.ToString();
}
else if(Method=="Maximum")
{
OutStr=pResults.Maximum.ToString();
}
else if(Method=="Mean")
{
OutStr=pResults.Mean.ToString();
}
else if(Method=="Sum")
{
OutStr=pResults.Sum.ToString();
}
else
{
OutStr = pResults.StandardDeviation.ToString();
}
pRow[OutField] = OutStr;
pTable.Rows.Add(pRow);
}
return pTable;
}
这里总结下几个功能模块:
一、获取唯一值
唯一字段统计,GeoDatabase没有提供DISTINCT这样的关键字进行唯一值查询,只有通过IDataStatistics::UniqueValues方式来获取唯一值
ICursor cursor = (ICursor)featureClass.Search(null, false);
IDataStatistics dataStatistics = new DataStatisticsClass();
dataStatistics.Field = "PIP_SIZE";
dataStatistics.Cursor = cursor;
//求唯一值
System.Collections.IEnumerator enumerator = dataStatistics.UniqueValues;
enumerator.Reset();
while (enumerator.MoveNext())
{
object myObject = enumerator.Current;
Console.WriteLine("Value - {0}", myObject.ToString());
}
二、调用IStatisticsResults
属性
Count--值总数
Maximum --最大值
Mean--算术平均值
Minimum--最小值
StandardDeviation--标准差
Sum--求和