此文为利用栅格计算器改变raster的高程的功能,具体如下:
///
/// 更改栅格影像的高程
///
///
裁剪后的raster
/// 高程众数(你想把高程减去的数)
/// 要输出的raster的路径
private void SetRasterElevation(string inClipRaster, string outRasterPath, int elevationMode)
{
try
{
//输出路径和名称
int iindx = outRasterPath.LastIndexOf("\\");
//输出raster的名称
string suffixRasterName = (outRasterPath.Substring(iindx + 1));
//输出raster的路径(无名称)
string rasterPath = outRasterPath.Remove(iindx);
//得到raster
IRaster raster = GetRaster(inClipRaster);
//控制地图代数的操作
IMapAlgebraOp mapAlgebraOp;
mapAlgebraOp = new RasterMapAlgebraOpClass();
//控制raster分析的环境
IRasterAnalysisEnvironment rasterAnalysisEnvironment = default(IRasterAnalysisEnvironment);
rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)mapAlgebraOp;
//设置工作空间
IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(rasterPath, 0);//这里应该是输出raster的路径
rasterAnalysisEnvironment.OutWorkspace = workspace;
//在地理数据集中绑定栅格数据
//GeoDataset可以是输入的raster、rasterDataset、RasterBand、RasterDescriptor;clipRaster应该是要修改的栅格数据的名称(string),(这里我的名称是固定的clipRaster)
mapAlgebraOp.BindRaster((IGeoDataset)raster, "clipRaster");
//定义表达式(elevationMode为要减去的数值)不要忘了"[ ]"
string strOut = "[clipRaster] " + " - " + elevationMode;
//执行表达式
IRaster outRaster = (IRaster)mapAlgebraOp.Execute(strOut);
//保存
ISaveAs2 saveAs;
saveAs = (ISaveAs2)outRaster;
saveAs.SaveAs(suffixRasterName, workspace, "TIFF");//输出名称(注意:名称中加后缀名,例:test.tif),工作空间,格式
MessageBox.Show("栅格计算器成功!");
}
catch (Exception ex)
{
MessageBox.Show("栅格计算器失败!");
Console.WriteLine(ex.Message);
}
}