C# 面向大栅格影像(像元值读取并统计相同数据值,以字典形式输出)

 #region 1.栅格影像数据读取;2.统计相同数据值;3.以字典形式输出
        /// <summary>
        /// 1.栅格影像数据读取;2.统计相同数据值;3.以字典形式输出
        /// </summary>
        /// <param name="inRasPath">输入栅格影像数据</param>
        /// <param name="accuracy">统计数据精准度(保留几位小数)</param>
        /// <returns></returns>
        public Dictionary<double, int> getRasValToDic(string inRasPath,int accuracy=2)
        {
            
            IRaster pRaster = (GetLayerByPath(inRasPath) as IRasterLayer).Raster;
            IRaster2 pRaster2 = pRaster as IRaster2;
            IRasterProps pRasterProps = pRaster as IRasterProps;

            //获取图层的行列值
            int Height = pRasterProps.Height;
            int Width = pRasterProps.Width;

            //定义并初始化数组,用于存储栅格内所有像员像素值
            List<string> savetxt = new List<string>();

            System.Array pixels;

            //定义RasterCursor初始化,参数设为null,内部自动设置PixelBlock大小
            IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(null);

            //用于存储PixelBlock的长宽
            long blockwidth = 0;
            long blockheight = 0;

            IPixelBlock3 pPixelBlock3;
            int asd = 0;//记录读取栅格像元值次数
            //创建字典
            Dictionary<double, int> test = new Dictionary<double, int>(101);

            double DValue = -1;//存储读取的数据
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            do
            {
                //获取Cursor的左上角坐标
                int left = (int)pRasterCursor.TopLeft.X;
                int top = (int)pRasterCursor.TopLeft.Y;

                pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;

                blockheight = pPixelBlock3.Height;
                blockwidth = pPixelBlock3.Width;

                pixels = (System.Array)pPixelBlock3.get_PixelData(0);

                //获取该Cursor的PixelBlock中像素的值
                for (int i = 0; i < blockheight; i++)
                {
                    for (int j = 0; j < blockwidth; j++)
                    {
                        if (Convert.ToDouble(pixels.GetValue(j, i)) * 100 > 1)
                        {
                            asd += 1;
                            int result = 0;
                            DValue = Math.Round(Convert.ToDouble(pixels.GetValue(j, i)), accuracy);
                            if (test.TryGetValue(DValue, out result))
                            {
                                test[DValue] += 1;
                            }
                            else
                            {
                                test.Add(DValue, 1);
                            }
                            if (asd % 3000000 == 0)
                            {
                                Console.WriteLine("\"" + System.IO.Path.GetFileNameWithoutExtension(inRasPath) + "\"" + "像元已读: " + asd + "");
                            }
                        }
                    }
                }

            }
            while (pRasterCursor.Next() == true);

            stopwatch.Stop();                             //结束计时
            Console.WriteLine(stopwatch.Elapsed);

            return test;
        }
        #endregion

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用ArcObjects来实现这个功能。具体步骤如下: 1. 在ArcMap中打开需要添加属性的要素类 2. 打开ArcMap的开发环境,创建一个新的C#项目 3. 在项目中添加对ESRI.ArcGIS.Geodatabase和ESRI.ArcGIS.DataSourcesRaster引用 4. 编写代码来读取图像的像素,并将其添加到要素类的属性表中 以下是一个示例代码,可以帮助你读取图像的像素并将其添加到要素类的属性表中: ```csharp //获取要素类 IFeatureClass featureClass = GetFeatureClass(); //获取数据集 IRasterDataset rasterDataset = GetRasterDataset(); //获取数据 IRaster2 raster = (IRaster2)rasterDataset.CreateDefaultRaster(); //获取范围 IRasterProps rasterProps = (IRasterProps)raster; IRasterGeometry geo = rasterProps.Extent as IRasterGeometry; //获取分辨率 double cellSize = rasterProps.MeanCellSize().X; //循环要素 IFeatureCursor cursor = featureClass.Update(null, false); IFeature feature = cursor.NextFeature(); while (feature != null) { //获取要素中心点 IPoint point = (IPoint)feature.ShapeCopy; point.Project(rasterProps.SpatialReference); double x = point.X; double y = point.Y; //获取 IPnt pnt = new PntClass(); pnt.SetCoords((int)((x - geo.Envelope.XMin) / cellSize), (int)((geo.Envelope.YMax - y) / cellSize)); IRasterProps2 rasterProps2 = (IRasterProps2)raster; object value = rasterProps2.GetPixelValue(0, pnt); //将添加到要素属性表中 feature.set_Value(feature.Fields.FindField("PixelValue"), value); //更新要素 cursor.UpdateFeature(feature); //获取下一个要素 feature = cursor.NextFeature(); } ``` 在代码中,GetFeatureClass()和GetRasterDataset()是自定义的函数,用于获取要素类和数据集。你需要根据自己的数据来修改这些函数。此外,你还需要将"PixelValue"替换为你要添加的属性字段名称。 希望这个示例代码可以帮助你实现读取图像的像素并将其添加到要素类的属性表中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HPUJQT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值