1、思想
①获取栅格数据集。
IRasterLayer rstlyer = GetLayerByPath(RasterPath) as IRasterLayer;
②获取栅格数据集的行数和列数,并进行循环。
//获取当前栅格行数和列数
IRasterProps pRasterProps = (IRasterProps)(rst);
int row = pRasterProps.Height;//行数
int col = pRasterProps.Width; //列数
③读取每一个栅格的像元值(x,y),设定保留小数位数,去除null值,存入List<String>。
ret += Math.Round(Convert.ToDouble(rst2.GetPixelValue(0, i, j)), Num) + "" + ",";
SaveAll.Add(ret);
④将List转为TXT输出。
general.ListToTxt(SaveAll, outPath);
2、处理数据及结果查看
3.代码展示
①完整代码
#region 获取栅格数据集各像元值并以txt格式输出
/// <summary>
/// 获取栅格数据集所有像元值并以txt格式输出
/// </summary>
/// <param name="RasterPath">操作栅格数据集路径</param>
/// <param name="outPath">输出txt文件路径</param>
/// <param name="Num">存储数据保留小数位数</param>
public void GetRasterVal(string RasterPath, string outPath, int Num = 2)
{
IRasterLayer rstlyer = GetLayerByPath(RasterPath) as IRasterLayer;
IRaster rst = rstlyer.Raster;
IRaster2 rst2 = rst as IRaster2;
//获取当前栅格行数和列数
IRasterProps pRasterProps = (IRasterProps)(rst);
int row = pRasterProps.Height;//行数
int col = pRasterProps.Width; //列数
//存储所有数据类型
string ret = "";
List<String> SaveAll = new List<string>();
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (rst2.GetPixelValue(0, i, j) != null)
ret += Math.Round(Convert.ToDouble(rst2.GetPixelValue(0, i, j)), Num) + "" + ",";
}
if (ret != "")
{
SaveAll.Add(ret);
ret = "";
}
}
general.ListToTxt(SaveAll, outPath);
}
#endregion