In this topic
How to run a geoprocessing tool(这个地方在仔细整理下!!)
- Name—Each tool parameter has a unique name.
- Type—The type of data expected, such as a feature class, integer, string, and raster.
- Required—Either a value must be provided for a parameter, or it is optional.
Using the geoprocessing assembly(是一个COM Interop程序集)
- Add a reference toESRI.ArcGIS.Geoprocessingto your project. This is the only reference you need if you use the geoprocessing assembly.
- Create thegeoprocessor object(通过IGeoProcessor2接口 ,注意:P大写,且是第2接口).
- Add the path to the custom toolbox if you are running a custom tool.
- Create an IVariantArray and populate(板上组装、填入) it with tool parameter values. The IVariantArray is available through the esriSystem library(参数值通过IVariantArray 设置).
- Call the Execute methodon the geoprocessor.
Executing a system tool
<span class="kwrd">using</span> System; <span class="kwrd">using</span> System.Threading; <span class="rem">// Add references to esriSystem for licensing and IVariantArray.</span> <span class="kwrd">using</span> ESRI.ArcGIS.esriSystem; <span class="rem">// Add a reference to the geoprocessing namespace.</span> <span class="kwrd">using</span> ESRI.ArcGIS.Geoprocessing; <span class="rem">// Call this method from your main.</span> <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> RunBuffer() { <span class="rem">// Create the geoprocessor.</span> <span style="background-color: rgb(51, 255, 51);"><span style="background-color: rgb(255, 153, 0);">IGeoProcessor2</span> gp = <span class="kwrd">new</span> GeoProcessorClass(); //注意左边接口的写法,更要注意右边类的名称后缀带个Class!!</span>
<span style="background-color: rgb(51, 255, 51);"> //这个地方和Geoprocessor不一样!!</span> gp.OverwriteOutput = <span class="kwrd">true</span>; <span style="background-color: rgb(255, 153, 102);">IGeoProcessorResult</span> result = <span class="kwrd">new</span> GeoProcessorResultClass(); <span class="rem">// Create a variant array to hold the parameter values.</span> IVariantArray parameters = <span class="kwrd">new</span> VarArrayClass(); <span class="kwrd">object</span> sev = <span class="kwrd">null</span>; <span class="kwrd">try</span> { <span class="rem">// Populate the variant array with parameter values.</span> parameters.Add(<span class="str" style="color: maroon;">@"C:\data\california.gdb\cities"</span>); parameters.Add(<span class="str" style="color: maroon;">@"C:\data\california.gdb\cities_buff"</span>); parameters.Add(<span class="str" style="color: maroon;">"1000 Meters"</span>); <span class="rem">// Execute the tool.<span style="color: rgb(128, 0, 0);">"Buffer_analysis" 这个字符串可以在ArcGIS帮助下面的功能项介绍中语法说明中找到,就是那个函数名称!!!!</span></span> <span style="background-color: rgb(255, 153, 102);">result</span> = gp.Execute(<span class="str" style="color: maroon;">"Buffer_analysis"</span>, parameters, <span class="kwrd">null</span>); <span class="rem">// Wait until the execution completes.</span> <span class="kwrd">while</span> (result.Status == esriJobStatus.esriJobExecuting) Thread.Sleep(1000); <span class="rem">// Wait for 1 second.</span> <span class="rem">// Print geoprocessring messages.</span> Console.WriteLine(gp.GetMessages(<span class="kwrd">ref</span> sev)); } <span class="kwrd">catch</span> (Exception ex) { <span class="rem">// Print a generic exception message.</span> Console.WriteLine(ex.Message); <span class="rem">// Print geoprocessing execution error messages.</span> Console.WriteLine(gp.GetMessages(<span class="kwrd">ref</span> sev)); } }
Executing a custom tool
<span class="kwrd">public</span> <span class="kwrd">void</span> SampleCalculateBestPathToolGping() { <span class="rem">// Initialize the geoprocessor.</span> IGeoProcessor2 gp = <span class="kwrd">new</span> GeoProcessorClass(); <span class="rem">// Add the BestPath toolbox.</span> <span style="background-color: rgb(51, 255, 51);">gp.AddToolbox(<span class="str" style="color: maroon;">@"C:\SanDiego\BestPath.tbx"</span>);</span> <span class="rem">// Generate the array of parameters.</span> IVariantArray parameters = <span class="kwrd">new</span> VarArrayClass(); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\source.shp"</span>); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\destination.shp"</span>); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\bestpath.shp"</span>); <span class="kwrd">object</span> sev = <span class="kwrd">null</span>; <span class="kwrd">try</span> { <span class="rem">// Execute the model tool by name.</span> gp.Execute(<span class="str" style="color: maroon;">"CalculateBestPath"</span>, parameters, <span class="kwrd">null</span>); Console.WriteLine(gp.GetMessages(<span class="kwrd">ref</span> sev)); } <span class="kwrd">catch</span> (Exception ex) { <span class="rem">// Print geoprocessing execution error messages.</span> Console.WriteLine(gp.GetMessages(<span class="kwrd">ref</span> sev)); } }
Using the geoprocessor managed assembly(是一个托管程序集)
- Add a reference toESRI.ArcGIS.Geoprocessor.(如果你需要使用工具的执行结果,你也可以引用ESRI.ArcGIS .Geoprocessing:You may also need to add the ESRI.ArcGIS.Geoprocessing assembly if you want to use, for example, the result object or list datasets.)
- Additionally, add areference to the toolbox assembly to which the tool belongs. If you use more than one tool from different toolboxes, also add managed assemblies for those toolboxes.
- Create thegeoprocessor object(注意是通过Geoprocessor类,就是我们平常所说的用某个类实例化一个对象。注意:p是小写).
- Add the path to the custom toolbox if you are running a custom tool.
- Create a tool process object and set the parameter values(参数值直接对具体工具设置).
- Call the Execute method on the geoprocessor.
Executing a system tool with managed assembly(我发现我平常用的比较多的就是这种情况:Geoprocessor)
<span class="rem">// Add the geoprocessor namespace.</span> <span class="kwrd">using</span> ESRI.ArcGIS.Geoprocessor; <span class="rem"><span style="color: rgb(0, 128, 0);">// Add the toolbox assembly.</span><span style="background-color: rgb(51, 255, 51);">需要哪个就得引用这个工具所在的程序集</span></span> <span class="kwrd">using</span> ESRI.ArcGIS.AnalysisTools; <span class="kwrd">public</span> <span class="kwrd">void</span> SampleBufferTool() { <span class="rem">// Create the geoprocessor. </span> <span style="background-color: rgb(51, 255, 51);">Geoprocessor GP = <span class="kwrd">new</span> Geoprocessor();</span> <span class="rem">// Create the tool process object.</span> <span style="background-color: rgb(51, 255, 51);">ESRI.ArcGIS.AnalysisTools.Buffer</span> bufferTool = <span class="kwrd">new</span> ESRI.ArcGIS.AnalysisTools.Buffer(); <span class="rem">// Set parameter values.</span> bufferTool.in_features = <span class="str" style="color: maroon;">@"D:\St_Johns\data.mdb\roads"</span>; bufferTool.out_feature_class = <span class="str" style="color: maroon;">@"D:\St_Johns\data.mdb\roads_Buffer"</span>; bufferTool.buffer_distance_or_field = <span class="str" style="color: maroon;">"distance"</span>; <span class="kwrd">object</span> sev = <span class="kwrd">null</span>; <span class="kwrd">try</span> { <span class="rem">// Execute the tool.</span> GP.Execute(bufferTool, <span class="kwrd">null</span>); Console.WriteLine(GP.GetMessages(<span class="kwrd">ref</span> sev)); } <span class="kwrd">catch</span> (Exception ex) { <span class="rem">// Print geoprocessing execution error messages.</span> Console.WriteLine(GP.GetMessages(<span class="kwrd">ref</span> sev)); } }
Executing a custom tool with managed assembly
<span class="kwrd">public</span> <span class="kwrd">void</span> SampleCalculateBestPathTool() { <span class="rem">// Initialize the geoprocessor.</span> Geoprocessor GP = <span class="kwrd">new</span> Geoprocessor(); <span class="rem">// Add the BestPath toolbox.</span> GP.AddToolbox(<span class="str" style="color: maroon;">@"C:\SanDiego\BestPath.tbx"</span>); <span class="rem">// Generate the array of parameters.</span> IVariantArray parameters = <span class="kwrd">new</span> VarArrayClass(); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\source.shp"</span>); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\destination.shp"</span>); parameters.Add(<span class="str" style="color: maroon;">@"C:\SanDiego\bestpath.shp"</span>); <span class="kwrd">object</span> sev = <span class="kwrd">null</span>; <span class="kwrd">try</span> { <span class="rem">// Execute the model tool by name.</span> GP.Execute(<span class="str" style="color: maroon;">"CalculateBestPath"</span>, parameters, <span class="kwrd">null</span>); Console.WriteLine(GP.GetMessages(<span class="kwrd">ref</span> sev)); } <span class="kwrd">catch</span> (Exception ex) { <span class="rem">// Print geoprocessing execution error messages.</span> Console.WriteLine(GP.GetMessages(<span class="kwrd">ref</span> sev)); } }
IGPProcess Interface(只是一个接口而已,从单词Process来看IGPProcess是一个动词性的接口)
private static void RunTool(Geoprocessor geoprocessor,IGPProcess process, ITrackCancel TC)
{ //Geoprocessor 是名词,是主语;IGPProcess是动词接口,是谓语!!!!
// Set the overwrite output option to true
geoprocessor.OverwriteOutput = true;
// Execute the tool
try
{
geoprocessor.Execute(process, null);
ReturnMessages(geoprocessor);
}
catch (Exception err)
{
Console.WriteLine(err.Message);
ReturnMessages(geoprocessor);
}
}
地理处理的环境设置
在Arcmap下面的操作Toolbox里面的工具的时候,有时候会设置一些环境变量,在AE里面会怎么做呢?
(1)如果知道自己操作的相关接口,可以使用IRasterAnalysisEnvironment,如下代码:
ILocalOp pLocalOp = new RasterLocalOpClass();
IWorkspaceFactory pEnvWf = new RasterWorkspaceFactoryClass();
IWorkspace pEnvRws = pEnvWf.OpenFromFile(strTempDir, 0);
IRasterAnalysisEnvironment pRasAnaEnv = (IRasterAnalysisEnvironment)pLocalOp;
pRasAnaEnv.OutWorkspace = pEnvRws;
(2)如果是通过GP操作,则
public static bool RasterZonalStatistics(IFeatureClass pFc,string strZoneField,
IRaster pRasterValue, string outRaster,string strStatisticType = "MINIMUM",string ignoreNoData = "DATA")
{
try
{
Geoprocessor pGeoprocessor = new Geoprocessor();
ISpatialReference spatialReference = null;
IGeoDataset pGeoDataset = pFc as IGeoDataset;
if (pGeoDataset != null) spatialReference = pGeoDataset.SpatialReference;
ZonalStatistics pZonalStatistics = new ZonalStatistics
{
in_zone_data = pFc,
zone_field = strZoneField,
in_value_raster = pRasterValue,
out_raster = outRaster,
statistics_type = strStatisticType,
ignore_nodata = ignoreNoData
};
pGeoprocessor.OverwriteOutput = true;
//pGeoprocessor.SetEnvironmentValue("workspace", strTempDir);
if (spatialReference != null)
pGeoprocessor.SetEnvironmentValue("outputCoordinateSystem", spatialReference.FactoryCode);
object ob = pGeoprocessor.Execute(pZonalStatistics, null);
return true;
}
catch (Exception ex)
{
return false;
}
}
现在的问题是:SetEnvironmentValue里面的参数怎么设置,我在AO的帮助里面找不到详细的帮助,但是在google里面搜索,找到arcgis的官网帮助连接如下:
http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Using_environment_settings/0001000001n5000000/,有Using environment settings栏目,最下方有参数名称,可以自由设置。