C#+AE 栅格统计

AE的学习仍要继续啊。

文章目录

  • 一、栅格统计
  • 二、代码实现

一、栅格统计

在空间分析的诸多功能中,栅格统计功能虽然是一个小的功能,但是其非常的实用。它可以获取栅格数据某个波段下的像元的最大最小值、平均值、中值、标准差等等,可以很好的反映栅格数据的整体情况。

二、代码实现

代码如下:

 	public partial class RasterStatistics : DevExpress.XtraEditors.XtraForm
    {
        public RasterStatistics()
        {
            InitializeComponent();
        }

        //全局变量
        public AxMapControl axMapControl { get; set; }
        private IMap pMap;

        private IRasterBandCollection bandCol;      //栅格波段的集合
        private double Maximum;//最大值
        private double Mean;//平均值
        private double Median;//中值
        private double Minimum;//最小值
        private double Mode;//最多值
        private double StandardDeviation;//标准差

        //窗体加载事件
        private void RasterStatistics_Load(object sender, EventArgs e)
        {
            pMap = axMapControl.Map;
            if (pMap == null)
                return;

            for (int i = 0; i < pMap.LayerCount; i++)
            {
                comboBoxEdit_Layer.Properties.Items.Add(pMap.Layer[i].Name);
            }
        }

        //确认
        private void simpleButton_RastterStatistics_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBoxEdit_Layer.SelectedItem == null)
                    return;
                IEnumRasterBand enumBand = bandCol.Bands;   //获取波段集合
                IRasterBand rasterBand = enumBand.Next();   //获取单个波段
                while (rasterBand != null)
                {
                    if (rasterBand.Statistics == null) break;
                    IRasterStatistics rasterStatic = rasterBand.Statistics;     //获取栅格波段中的统计器
                    Maximum = rasterStatic.Maximum;
                    Mean = rasterStatic.Mean;
                    Median = rasterStatic.Median;
                    Mode = rasterStatic.Mode;
                    Minimum = rasterStatic.Minimum;
                    StandardDeviation = rasterStatic.StandardDeviation;
                    ShowStaticResult();
                    rasterBand = enumBand.Next();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //通过图层名获取图层
        private ILayer GetLayerByName(IMap pMap, string layerName)
        {
            ILayer pLayer = null;
            ILayer tempLayer = null;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                tempLayer = pMap.Layer[i];
                if (tempLayer.Name.ToUpper() == layerName.ToUpper())
                {
                    pLayer = tempLayer;
                    break;
                }
            }
            return pLayer;
        }

        //图层选择,设置参数
        private void comboBoxEdit_Layer_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ILayer layer = GetLayerByName(pMap,comboBoxEdit_Layer.SelectedItem.ToString());
                IRasterLayer rasterLayer = layer as IRasterLayer;
                IRaster2 raster = rasterLayer.Raster as IRaster2;
                IRasterDataset rd = raster.RasterDataset;
                bandCol = rd as IRasterBandCollection;
            }
            catch
            {
                MessageBox.Show("栅格数据为空,请添加栅格数据!");
            }
        }

        //显示统计结果
        void ShowStaticResult()
        {
            memoEdit_ResultShow.Text+="栅格数据 " + comboBoxEdit_Layer.SelectedItem.ToString() + "统计结果如下:\r\n";
            memoEdit_ResultShow.Text += "所有像元的最小值: " + Minimum.ToString("0.00") + "\r\n";
            memoEdit_ResultShow.Text += "所有像元的最大值: " + Maximum.ToString("0.00") + "\r\n";
            memoEdit_ResultShow.Text += "所有像元的平均值: " + Mean.ToString("0.00") + "\r\n";
            memoEdit_ResultShow.Text += "像元最多的值 : " + Mode.ToString("0.00") + "\r\n";
            memoEdit_ResultShow.Text += "所有像元的中值: " + Median.ToString("0.00") + "\r\n";
            memoEdit_ResultShow.Text += "所有像元的标准差: " + StandardDeviation.ToString("0.00") + "\r\n";
        }

        //关闭
        private void simpleButton_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //清除
        private void simpleButton_Clear_Click(object sender, EventArgs e)
        {
            memoEdit_ResultShow.Text = "";
        }
    }

实现效果:
C#+AE 栅格统计_第1张图片
注:我这是单波段的数据,所以只有一组像元的统计数据。

参考资料:《ArcgisEngine地理信息系统开发教程》

你可能感兴趣的:(C#+AE)