Geoprocessor gp = new Geoprocessor();
in_raster = Global.InputData;
in_mask_data = Global.InputMask;
out_raster = Global.OutData + "\\\\" + Global.OutDataName;
/*=====================================ExtractByMask=============================*
* Extracts the cells of a raster that correspond to the areas defined by a mask.*
* 三个必须参数:in_raster、in_mask_data、out_raster *
*===============================================================================*/
ExtractByMask extractByMask = new ExtractByMask();
extractByMask.in_raster = in_raster;
extractByMask.in_mask_data = in_mask_data;
extractByMask.out_raster = out_raster;
gp.Execute(extractByMask, null);
MessageBox.Show("裁剪成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
必须的有三个参数,其中前两个没有啥问题,第三个参数的描述,也是让人醉的一个。The output raster dataset to be created. (Out, Required)怎么看都是创建一个输出数据集啊,结果,就是出错。随后跟一个大神要了参考的代码,发现他的代码中是路径,并不是filename,好吧,于是我就尝试用filename。结果是第三个参数需要的是文件名就可以了。代码如下:
public void ChangeRasterCoordinate(string inputRaster, AxMapControl axMap, string outRaster)
Geoprocessor gp = new Geoprocessor();
/*======================ProjectRaster================================*
* in require *
* in_raster:The input raster dataset. *
* out_coor_system The coordinate system for the geometry column. *
* out_raster The output raster dataset to be created. *
* Optional *
* in_coor_system The coordinate system of the input raster dataset. *
*===================================================================*/
ProjectRaster projectRaster = new ProjectRaster();
//创建输入栅格数据的数据集
IRasterDataset inputRasterData = new RasterDataset();
inputRasterData.OpenFromFile(inputRaster);
projectRaster.in_raster = inputRasterData;
try
//创建输出栅格数据数据集
//IRasterDataset outputRasterData = new RasterDataset();
Open output workspace
//IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
//长度不包括\\
int index = outRaster.LastIndexOf("\\\\");
string outputFolder = outRaster.Substring(0, index);
string name = outRaster.Substring(index + 1);
//提供一个personal或者file geodatabase的file后者directory,这里提供一个directory
//IRasterWorkspace workspace = workspaceFactory.OpenFromFile(outputFolder, 0) as IRasterWorkspace;
//outputRasterData = workspace.OpenRasterDataset(name);
/*此处应该是api的一个错误,api中是这样写的:
out_raster The output raster dataset to be created. (Out, Required)
*/
projectRaster.out_raster = outRaster;
catch (Exception e)
MessageBox.Show(e.ToString());
projectRaster.out_coor_system = axMap.SpatialReference;
/*=====================================执行GP的两种写法=================================*
* 1、 gp.Execute(projectRaster, null); *
* 2、用IGeoProcessorResultL: *
* @1、The Geoprocessor Result. Provides access to the results of tools. *
*======================================================================================*/
IGeoProcessorResult tGPResult =(IGeoProcessorResult) gp.Execute(projectRaster, null);
if (tGPResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
MessageBox.Show("栅格数据坐标转换成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);;
else if(tGPResult.Status==ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobFailed)
MessageBox.Show("栅格数据坐标转换失败!", "警示", MessageBoxButtons.OK, MessageBoxIcon.Warning);