ArcEngine + C# 获取栅格数据像元值
程序员文章站
2022-06-11 16:05:30
...
此文问获得栅格数据的像元值(即高程),有可能部分见解不到位,望大神看到了不惜指教!
///
/// 得到高程(通过像素值)
/// </summary>
/// <param name="maskTifPath">Raster路径</param>
private List<int> GetElevation(string maskTifPath, List<IPoint> pointColList)//pointcollist是点集合,可以参照博文获得点集合
{
IRaster raster = GetRaster(maskTifPath);//参见博文获得raster数据
//得到一段光栅带
IRasterBandCollection rasterBandCollection = (IRasterBandCollection)raster;
for (int icount = 0; icount < rasterBandCollection.Count; icount++)//测试数据count为1
{
IRasterBand rasterBand = rasterBandCollection.Item(icount);
//像素
IRawPixels rawPixels = (IRawPixels)rasterBand;
IRasterProps rasterProps = (IRasterProps)rawPixels;
//IGeoDataset geodataset = (IGeoDataset)raster;
//IEnvelope2 envelop = new EnvelopeClass();
//envelop = (IEnvelope2)geodataset.Extent;
//IPoint point = envelop.UpperLeft;
// 像元大致范围
//double blockX = (double)rasterProps.MeanCellSize().X;//网格X间距
//double blockY = (double)rasterProps.MeanCellSize().Y;//网格Y间距
//double blockArea = blockX * blockY;//网格面积
//int columns = rasterProps.Width;//dem列数
//int rows = rasterProps.Height;//dem行数
// 指定像素块大小
IPnt blockSize = new DblPnt();
//blockSize.X = columns;
//blockSize.Y = rows;
blockSize.X = 5;//赋值为多少比较合适?
blockSize.Y = 5;
//blockSize.X = blockX;
//blockSize.Y = blockY;
//指定像素块大小来创建像素快
IPixelBlock3 pixelBlock = (IPixelBlock3)rawPixels.CreatePixelBlock(blockSize);
IPnt blockOrigin = new DblPnt();
IPoint point = new PointClass();
List<int> pixels = new List<int>();
for (int j = 0; j < pointColList.Count; j++)
{
point = pointColList[j];
blockOrigin.X = point.X;
blockOrigin.Y = point.Y;
// 读取指定位置的像素块(blockOrigin为指定位置)
rawPixels.Read(blockOrigin, (IPixelBlock)pixelBlock);
//存储格网dem的二维数组
System.Array pixelData = (System.Array)pixelBlock.get_PixelDataByRef(icount);
// 获得每一个栅格的高程值
for (int col = 0; col < pixelData.GetLength(0); col++)
{
for (int row = 0; row < pixelData.GetLength(1); row++)
{
pixels.Add(Convert.ToInt32(pixelData.GetValue(col, row)));
}
}
return pixels;
}
}
}