GP工具通过直接调用ArcToolBox中的工具进行地理处理,可以很大程度减轻我们的编码工作。
用到的引用
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
调用步骤
1、定义GP工具
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true; //输出文件可以覆盖
IGeoProcessorResult result = new GeoProcessorResultClass(); //执行结果
2、定义分析工具
根据分析操做所用到的工具,添加相应的引用,并定义相应的类以及传入相应的参数
例如,ToolBox中的分析工具对应 ESRI.ArcGIS.AnalystTools
调用分析工具中的交集制表工具
ESRI.ArcGIS.AnalysisTools.TabulateIntersection ti = new ESRI.ArcGIS.AnalysisTools.TabulateIntersection(); //创建交集制表工具
ti.in_zone_features = pFeatureLayer;
ti.zone_fields = pField;
ti.in_class_features = qFeatureLayer;
ti.out_table = outpath;
3、执行分析操作获得分析结果
注意此处应使用try-catch包围:一是因为此处经常出错,传参数过程中稍不小心就出错。二是用try-catch以查看函数执行状态信息,如果出错,可查看具体错误情况。
try
{
result =(IGeoProcessorResult)gp.Execute(ti, null); //null表示程序一旦运行无法中止
object sev = null;
MessageBox.Show(gp.GetMessages(ref sev));
}
catch
{
object sev = null;
MessageBox.Show(gp.GetMessages(ref sev));
}
4、将分析结果转为相应的要素并返回图层
IFeatureClass pFeatureClass = gp.Open(result.ReturnValue) as IFeatureClass;
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass = pFeatureClass;
return pFeatureLayer;
示例代码:
GP工具计算dem坡度:
private IRasterLayer getSlope(string inraster,string outraster)
{
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;
IGeoProcessorResult result = null;
Slope pSlope = new Slope();
pSlope.in_raster = inraster;
pSlope.out_raster = outraster;
try
{
object sev = null;
result=(IGeoProcessorResult)gp.Execute(pSlope, null);
MessageBox.Show(gp.GetMessages(ref sev));
}
catch
{
object sev = null;
MessageBox.Show(gp.GetMessages(ref sev));
}
//获得输出内容
IRasterDataset pRatserDataset = gp.Open(result.ReturnValue) as IRasterDataset;
IRasterLayer pRasterLayer = new RasterLayer();
pRasterLayer.CreateFromDataset(pRatserDataset);
return pRasterLayer;
}