在做一个项目的过程中,发现GP运算方法
Execute(string name, IVariantArray parameters, ITrackCancel trackCancel) 与Execute(IGPProcess process, ITrackCancel trackCancel)
的执行效率竟然有差别,很是奇怪,后用反编译软件,查看dll中的代码,发现两者确实不同,代码如下:
public object Execute(string name, IVariantArray parameters, ITrackCancel trackCancel)
{
return this._gp.Execute(name, parameters, trackCancel);
}
public object Execute(IGPProcess process, ITrackCancel trackCancel)
{
IVariantArray iva;
if (this._isRemote)
{
iva = (this._ctx.CreateObject("esriSystem.VarArray") as IVariantArray);
}
else
{
iva = new VarArrayClass();
}
return this.ExecuteInner(process, trackCancel, this._gp, iva);
}
private object ExecuteInner(IGPProcess process, ITrackCancel trackCancel, IGeoProcessor igp, IVariantArray iva)
{
object result = null;
try
{
this.AddToolbox(Utils.MapToolboxConfigDir(process.ToolboxDirectory) + "\\" + process.ToolboxName);
object[] parameterInfo = process.ParameterInfo;
for (int i = ; i < parameterInfo.Length; i++)
{
if (parameterInfo[i] != null)
{
iva.Add(parameterInfo[i]);
}
else
{
iva.Add("#");
}
}
result = this._gp.Execute(process.ToolName + "_" + process.Alias, iva, trackCancel);
for (int j = ; j < iva.Count; j++)
{
object obj = iva.get_Element(j);
if (!(obj is string) || !((string)obj == "#"))
{
parameterInfo[j] = obj;
}
}
}
catch (Exception)
{
throw;
}
finally
{
Marshal.ReleaseComObject(iva);
}
return result;
}
从两者的代码中,可以看出,三参数的Execute的执行效率要远远高于两参数的Execute。原因很明显,两参数的Execute执行了很多本可以无须执行的代码。三参数直接执行运算,效率就高了。
代码中 _gp接口,实现类是ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.所以在执行GP的时候,完全可以不需要定义ESRI.ArcGIS.Geoprocessor中的Geoprocessor(大多时间我们使用这个类),而是直接定义GeoProcessorClass,运行三参数方法来实现GP的运算。效果更好点
从网上也查找到一些相关的资料,仅供参考!
引用自链接!
http://blog.csdn.net/liuguobo/article/details/16965987
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
[C#]
using System;
using System.Threading;
// Add references to esriSystem for licensing and IVariantArray.
using ESRI.ArcGIS.esriSystem;
// Add a reference to the geoprocessing namespace.
using ESRI.ArcGIS.Geoprocessing; // Call this method from your main.
private static void RunBuffer()
{
// Create the geoprocessor.
IGeoProcessor2 gp = new GeoProcessorClass(); //注意左边接口的写法,更要注意右边类的名称后缀带个Class!!
//这个地方和Geoprocessor不一样!!
gp.OverwriteOutput = true;
IGeoProcessorResult result = new GeoProcessorResultClass();
// Create a variant array to hold the parameter values.
IVariantArray parameters = new VarArrayClass();
object sev = null;
try
{
// Populate the variant array with parameter values.
parameters.Add(@"C:\data\california.gdb\cities");
parameters.Add(@"C:\data\california.gdb\cities_buff");
parameters.Add("1000 Meters");
// Execute the tool."Buffer_analysis" 这个字符串可以在ArcGIS帮助下面的功能项介绍中语法说明中找到,就是那个函数名称!!!!
result = gp.Execute("Buffer_analysis", parameters, null);
// Wait until the execution completes.
while (result.Status == esriJobStatus.esriJobExecuting)
Thread.Sleep(1000);
// Wait for 1 second.
// Print geoprocessring messages.
Console.WriteLine(gp.GetMessages(ref sev));
}
catch (Exception ex)
{
// Print a generic exception message.
Console.WriteLine(ex.Message);
// Print geoprocessing execution error messages.
Console.WriteLine(gp.GetMessages(ref sev));
}
}
Executing a custom tool
[C#]
public void SampleCalculateBestPathToolGping()
{
// Initialize the geoprocessor.
IGeoProcessor2 gp = new GeoProcessorClass();
// Add the BestPath toolbox.
gp.AddToolbox(@"C:\SanDiego\BestPath.tbx");
// Generate the array of parameters.
IVariantArray parameters = new VarArrayClass();
parameters.Add(@"C:\SanDiego\source.shp");
parameters.Add(@"C:\SanDiego\destination.shp");
parameters.Add(@"C:\SanDiego\bestpath.shp");
object sev = null;
try
{
// Execute the model tool by name.
gp.Execute("CalculateBestPath", parameters, null);
Console.WriteLine(gp.GetMessages(ref sev));
}
catch (Exception ex)
{
// Print geoprocessing execution error messages.
Console.WriteLine(gp.GetMessages(ref 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)
[C#]
// Add the geoprocessor namespace.
using ESRI.ArcGIS.Geoprocessor;
// Add the toolbox assembly.需要哪个就得引用这个工具所在的程序集
using ESRI.ArcGIS.AnalysisTools; public void SampleBufferTool()
{
// Create the geoprocessor.
Geoprocessor GP = new Geoprocessor();
// Create the tool process object.
ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new
ESRI.ArcGIS.AnalysisTools.Buffer();
// Set parameter values.
bufferTool.in_features = @"D:\St_Johns\data.mdb\roads";
bufferTool.out_feature_class = @"D:\St_Johns\data.mdb\roads_Buffer";
bufferTool.buffer_distance_or_field = "distance";
object sev = null;
try
{
// Execute the tool.
GP.Execute(bufferTool, null);
Console.WriteLine(GP.GetMessages(ref sev));
}
catch (Exception ex)
{
// Print geoprocessing execution error messages.
Console.WriteLine(GP.GetMessages(ref sev));
}
}
Executing a custom tool with managed assembly
[C#]
public void SampleCalculateBestPathTool()
{
// Initialize the geoprocessor.
Geoprocessor GP = new Geoprocessor();
// Add the BestPath toolbox.
GP.AddToolbox(@"C:\SanDiego\BestPath.tbx");
// Generate the array of parameters.
IVariantArray parameters = new VarArrayClass();
parameters.Add(@"C:\SanDiego\source.shp");
parameters.Add(@"C:\SanDiego\destination.shp");
parameters.Add(@"C:\SanDiego\bestpath.shp");
object sev = null;
try
{
// Execute the model tool by name.
GP.Execute("CalculateBestPath", parameters, null);
Console.WriteLine(GP.GetMessages(ref sev));
}
catch (Exception ex)
{
// Print geoprocessing execution error messages.
Console.WriteLine(GP.GetMessages(ref 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栏目,最下方有参数名称,可以*设置。