#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
C# 面向大栅格影像(像元值读取并统计相同数据值,以字典形式输出)
最新推荐文章于 2024-06-12 17:35:04 发布