using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
namespace yanShi
{
public partial class frmStatistics : Form
{
IActiveView mActiveView;
IFeatureClass mFeatureClass;
ICursor mCursor;
public frmStatistics()
{
InitializeComponent();
}
public frmStatistics(IActiveView fActiveView)
{
InitializeComponent();
// 将传入的fActiveView参数赋值给类的私有成员变量pActiveView
mActiveView = fActiveView;
// 通过pActiveView的FocusMap属性获取当前活动视图中的地图,并将其赋值给类的私有成员变量pMap
IMap pMap = fActiveView.FocusMap;
// 清空cbxTuCeng控件的所有项
cbxTuCeng.Items.Clear();
// 遍历地图pMap中的图层
for (int i = 0; i < pMap.LayerCount; i++)
{
// 获取地图中索引为i的图层,并调用其Name属性获取图层名称
// 将当前图层的名称添加到toolStripComboBoxTuCeng控件的项集合中
cbxTuCeng.Items.Add(pMap.get_Layer(i).Name);
}
}
private void cbxTuCeng_SelectedIndexChanged(object sender, EventArgs e)
{
// 从cbxTuCeng控件的选定项中获取图层名称,并将其转换为字符串
string layerName = cbxTuCeng.SelectedItem.ToString();
// 创建一个SnippetLayer对象,该对象可能包含一些与图层相关的辅助方法
SnippetLayer sl = new SnippetLayer();
// 调用SnippetLayer的GetIndexNumberFromLayerName方法,根据图层名称获取图层在地图中的索引号
int layerIndex = sl.GetIndexNumberFromLayerName(mActiveView, layerName);
// 使用获取的索引号,从地图中获取相应的FeatureLayer对象
IFeatureLayer pFeatureLayer = sl.GetFeatureLayerFromLayerIndexNumber(mActiveView, layerIndex);
// 从FeatureLayer对象中获取FeatureClass对象,FeatureClass代表了一组具有相同属性和几何类型的要素
mFeatureClass = pFeatureLayer.FeatureClass;
// 创建一个查询过滤器,此处WhereClause为空,表示查询所有要素
// 创建一个QueryFilterClass的实例,用于构建查询过滤器
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = "";
// 执行查询,获取FeatureCursor对象,FeatureCursor用于遍历FeatureClass中的要素
// 使用FeatureClass的Search方法,并传入查询过滤器和false(表示不保留对FeatureClass的引用)
IFeatureCursor pFeatureCursor = mFeatureClass.Search(pQueryFilter, false);
// 尝试从FeatureCursor中获取第一个Feature
IFeature pFeature = pFeatureCursor.NextFeature();
// 获取FeatureClass的字段集合
IFields pFields = mFeatureClass.Fields;
// 清除下拉列表框cbxField中的所有项,以确保不会显示旧的字段信息。
cbxField.Items.Clear();
// 遍历字段集合
for (int i = 0; i < pFields.FieldCount; i++)
{
// 从pFields中获取索引为i的字段,并将其赋值给pField变量
IField pField = pFields.get_Field(i);
// 从pFeature中获取索引为i的字段值,并将其赋值给pValue变量
object pValue = pFeature.get_Value(i);
// 判断pValue的类型是否为数值类型
if (pValue is int || pValue is double || pValue is float || pValue is decimal || pValue is long || pValue is short)
// 如果满足上述条件之一,则将pField的Name属性添加到cbxField的Items集合中
cbxField.Items.Add(pField.Name);
}
// 将pFeatureCursor强制转换为ICursor接口,并赋值给mCursor变量
mCursor = pFeatureCursor as ICursor;
}
private void cbxField_SelectedIndexChanged(object sender, EventArgs e)
{
// 创建一个DataStatisticsClass的实例,用于统计数据的各项指标
IDataStatistics pDataStatistics = new DataStatisticsClass();
// 将pDataStatistics的Cursor属性设置为mCursor,表示要对mCursor中的数据进行统计
pDataStatistics.Cursor = mCursor;
// 设置pDataStatistics要统计的字段为cbxField下拉列表中选中的项(转换为字符串)
pDataStatistics.Field = cbxField.SelectedItem.ToString();
// 调用pDataStatistics的Statistics属性来获取统计结果,并赋值给pStatisticsResults
IStatisticsResults pStatisticsResults = pDataStatistics.Statistics;
// 清空tbxResults文本框的内容
tbxResults.Clear();
// 在tbxResults文本框中添加统计结果,包括要素个数、最大值、最小值、平均值、标准差和总和
tbxResults.Text = "要素个数:" + pStatisticsResults.Count +
"\r\n最大值:" + pStatisticsResults.Maximum +
"\r\n最小值:" + pStatisticsResults.Minimum +
"\r\n平均值:" + pStatisticsResults.Mean +
"\r\n标准差:" + pStatisticsResults.StandardDeviation +
"\r\n总和:" + pStatisticsResults.Sum;
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Chckx/article/details/139812855