如何运行geoprocessing(GP)工具

简介: 运行geoprocessing工具  每个geoprocessing工具都有一组固定需要设置的参数,他们为工具的执行提供必须的信息。

运行geoprocessing工具

  每个geoprocessing工具都有一组固定需要设置的参数,他们为工具的执行提供必须的信息。工具通常需要输入参数,定义数据或数据集,这些通常用于生产新的输出数据。参数中有一些重要的属性:

    Name—所有的工具参数有一个唯一的名称

    Type—数据预期的类型,如feature class、interger、string和raster。

    Required—必须提供一个值参数或可选

  当一个工具用在程序中时,他的参数值必须立即设置,以便当程序运行的时候他能被执行。工具的文档清楚地定义了他的参数和属性。一旦提供一套有效的参数值,该工具可随时执行。

  参数都被指定为string或者objects类型。string类型是文本值,唯一定义参数值,比如一个数据集的路径或关键字。

  大多数工具的参数都可以被指定为一个简单的string类型。然而,复杂的参数,如空间参考(spatial refercence),也许使用object来定义比较容易。在下面的示例代码中,Buffer(缓冲区)工具定义了一些必须的参数。在这个示例中,string类型被用来定义Buffer工具的输入、输出和缓冲半径属性,因此工具的调用代码可读性更强。

  下面的Execute方法使用了空参数代替了ITrackCancel接口,ITrackCancel接口提供的属性和方法的访问,用户可以确定或取消执行和操作还允许开发人员指定取消的动作构成。

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.AnalysisTools;

public void SampleBufferTool()
{

  // Initialize the geoprocessor. 
  Geoprocessor GP = new Geoprocessor();

  ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new
    ESRI.ArcGIS.AnalysisTools.Buffer();

  bufferTool.in_features = @"D:\St_Johns\data.mdb\roads_Buffer";
  bufferTool.out_feature_class = @"D:\St_Johns\data.mdb\roads";
  bufferTool.buffer_distance_or_field = "distance";

  GP.Execute(bufferTool, null);
}

Toolbox(工具箱)的名称和命名空间

  下表列出了系统工具箱的名称和命名空间

Toolbox 名称 命名空间
3D Analyst tools ESRI.ArcGIS.Analyst3DTools
Analysis tools ESRI.ArcGIS.AnalysisTools
Conversion tools ESRI.ArcGIS.ConversionTools
Data Management tools ESRI.ArcGIS.DataManagementTools
Cartography tools ESRI.ArcGIS.CartographyTools
Coverage tools ESRI.ArcGIS.CoverageTools
Geocoding tools ESRI.ArcGIS.GeocodingTools
Geostatistical Analyst tools ESRI.ArcGIS.GeostatisticalAnalystTools
Linear Referencing tools ESRI.ArcGIS.LinearReferencingAnalystTools
Multidimension tools ESRI.ArcGIS.MultidimensionTools
Network Analyst tools ESRI.ArcGIS.NetworkAnalystTools
Samples ESRI.ArcGIS.SamplesTools
Spatial Analyst tools ESRI.ArcGIS.SpatialAnalystTools
Spatial Statistics tools ESRI.ArcGIS.SpatialStatisticsTools

运行自定义的geoprocessing工具

  除了使用现有的工具和ESRI提供的工具箱,也可以执行你自定义的工具,比如model tools(模型工具)和script tools(脚本工具),他们存在于自定义工具箱中。使用集成开发环境(IDE)框架嵌入在Visual Studio .NET中,你可以生成geoprocessing程序集用来重现任何自定义工具箱。这样做,使用 ArcGIS Toolbox Reference对话框。


引用名称执行工具

  声明你的自定义工具箱并不是生成geoprocessing程序集的先决条件。这里有另外一种使用geoprocessor的方式来使用Execute方法。Execute方法已经被重载和附加,允许您通过指定工具的名称、工具的参数和ITrackCancel接口来执行工具。

  下面是一个执行计算最优路径模型的工具,它位于BestPath工具箱中:

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;

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");

  // Execute the model tool by name.
  GP.Execute("CalculateBestPath", parameters, null);
}


执行geoprocessing server工具

  通过ArcGIS Desktop和ArcGIS Engine(9.2及以上版本),你可以执行发布在ArcGIS Server上的geoprocessing工具。Server工具能通过像自定义工具一样执行。首先你必须添加工具箱,这个工具箱是生成一个 geoprocessing toolbox assembly 来表示或使用AddToolbox方法添加自定义的。

  工具箱能够发布在局域网(LAN)或者作为一个Web服务发布在Internet上。要想使用geoprocessing服务工具,你必须添加工具箱。下面的示例代码显示了如何使用AddToolbox方法添加发布在ArcGIS Server的工具。

using ESRI.ArcGIS.Geoprocessor;

public void SampleGeoprocessingServerTool(Geoprocessor GP)
{

  // Add the BestPath toolbox published on a LAN.
  // Entered as server name;folder/toolbox.
  GP.AddToolbox(@"flame7;SanDiego/BestPath");

  // Add the BestPath toolbox published as a geoprocessing Web service.
  // Entered as Web service;folder/toolbox.
  GP.AddToolbox(@"http://flame7/arcgis/services;SanDiego/BestPath");

  //TODO: Add your code here...

}


在服务中发布toolboxes

  在服务上发布的Toolboxes,将包含需要你键入需要输入的参数值的tools。发布模型和工具时,被用来输入和输入的参数的数据类型有一些限制。不允许带有Feature Class或 Table data类型的变量作为输入的参数。在这个示例中,输入需要被指定为一个feature set或者record set。

  在另外一个实例中,一个tool可以有一个layer作为输入,因此,输入的参数将引用一个在地图文档里面的图层。

  下面的例子将为你展示当输入为layers或record sets时,怎样执行一个server tools:

    1. 当数据存储在服务上并且引用地图文档中的图层时,如何执行一个服务工具。

    2. 当数据存储在客户端上,数据已经加载到一个feature set时,如何执行一个服务工具。

    3. 当输入的是一个使用insert cursor创建的feature set时,如何执行一个服务工具。

  更多关于在ArcGIS Server上发布geoprocessing tools和支持的数据类型,参见 An overview of sharing tools on an ArcGIS Server

1. 
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;

public void SampleExecuteServerToolByReference()
{
  //1.) The input parameters reference layers in a map document
  //    that contain the toolbox and tools.
  // Initialize the geoprocessor.
  Geoprocessor GP = new Geoprocessor();

  // Add the BestPath toolbox.
  GP.AddToolbox(@"http://flame7/arcgis/services;GP/Bestpathtoolbox");

  // Inputs reference layers in a map document on the server. 
  IVariantArray parameters = new VarArrayClass();
  parameters.Add("source");
  parameters.Add("destination");

  // Execute the server tool by reference.
  IGeoProcessorResult result;
  result = (IGeoProcessorResult)GP.Execute("CalculateBestPath", parameters,
    null);

}
2. 
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase 

public void SampleExecuteServerToolByValue()
{
  //2.)The input parameter is a feature set referencing a feature class on the client.
  //     Load the feature class into a record set to be passed to the server.

  // Initialize the geoprocessor.
  Geoprocessor GP = new Geoprocessor();

  // Add the BestPath toolbox.
  GP.AddToolbox(@"http://flame7/arcgis/services;GP/Bestpathtoolbox");

  // Create a record set from an input featureclass 
  IGPUtilities2 gputils = new GPUtilitiesClass();
  IFeatureClass fc = (IFeatureClass)gputils.OpenDatasetFromLocation(@
    "C:\sandiego\source.shp");
  IRecordSetInit recordset = new RecordSetClass();
  recordset.SourceTable((ITable)fc, null);
  IGPRecordSet gprecordset = new GPFeatureRecordSetLayerClass();
  gprecordset.Recordset = recordset;

  IVariantArray parameters = new VarArrayClass();
  parameters.Add(gprecordset);

  // TODO:Do the same for the destination feature class and add to the variant array.

  // Execute the server tool by reference 
  IGeoProcessorResult result;
  result = GP.Execute("CalculateBestPath", parameters, null);

}
3. 
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase using ESRI.ArcGIS.Geometry;

public void SampleExecuteServerToolByValue()
{
  //3.) The input parameter is a feature set created using an insert cursor.

  // Initialize the geoprocessor.
  Geoprocessor GP = new Geoprocessor();

  // Add the BestPath toolbox.
  GP.AddToolbox(@"http://flame7/arcgis/services;GP/Bestpathtoolbox");

  // Create an OID Field
  IFieldsEdit fieldsEdit = new FieldsClass();
  IFieldEdit fieldEdit = new FieldClass();
  fieldEdit.Name_2 = "ObjectID";
  fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
  fieldsEdit.AddField(fieldEdit);

  // Create a SHAPE Field and assign a geometry definition
  IGeometryDefEdit geomEdit = new GeometryDefClass();
  geomEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

  // Create a spatial reference to assing to the geometry definition
  ISpatialReferenceFactory srf = new SpatialReferenceEnvironmentClass();
  ISpatialReference sr = srf.CreateESRISpatialReferenceFromPRJFile(@
    "C:\Program Files\ArcGIS\Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 21N.prj");
  ISpatialReferenceResolution srRes = sr as ISpatialReferenceResolution;
  srRes.ConstructFromHorizon();
  srRes.SetDefaultXYResolution();
  IControlPrecision2 cp2 = sr as IControlPrecision2;
  cp2.IsHighPrecision = true;
  geomEdit.SpatialReference_2 = sr;

  fieldEdit = new FieldClass();
  fieldEdit.Name_2 = "SHAPE";
  fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
  fieldEdit.GeometryDef_2 = geomEdit;
  fieldsEdit.AddField(fieldEdit);

  // Create the RecordSet
  IRecordSetInit rsInit = new RecordSetClass();
  rsInit.CreateTable(fieldsEdit);
  IRecordSet rs = rsInit as IRecordSet;

  // Create a new point feature and insert into the feature set
  ICursor cursor = rsInit.Insert();
  IPoint pnt = new PointClass();
  pnt.PutCoords(736204, 5269724);
  IRowBuffer rowBuffer = rsInit.CreateRowBuffer();
  rowBuffer.set_Value(1, pnt);
  cursor.InsertRow(rowBuffer);
  IGPRecordSet gpRS = new GPFeatureRecordSetLayerClass();
  gpRS.RecordSet = rs;

  IVariantArray parameters = new VarArrayClass();
  parameters.Add(gpRS);

  // TODO: Do the same for the destination point.  

  // Execute the server tool by reference 
  IGeoProcessorResult result;
  result = GP.Execute("CalculateBestPath", parameters, null);
}

生成geoprocessing 结果

  所有的geoprocessing tools 都将生成有结果。Execute方法返回一个 IGeoProcessorResult 对象,这个对象用来管理生成的结果。在ArcSGIS server中,结果对象是必须的用来支持geoprocessing的。当工具执行后,结果对象将有一个返回值:它在返回一个在服务中的执行状态,返回结果ID和提供 geoprocessing messages信息。
  下面是一个执行server tool和获取它的状态和信息的示例:
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;

public void SampleGeneratingGeoprocessingResults()
{

  // Initialize the geoprocessor.
  Geoprocessor GP = new Geoprocessor();

  // Add the BestPath toolbox.
  GP.AddToolbox(@"http://flame7/arcgis/services;GP/Bestpathtoolbox");

  // Input values are layers on the server.
  IVariantArray parameters = new VarArrayClass();
  parameters.Add(@"source");
  parameters.Add(@"destination");

  // Execute the server tool
  IGeoProcessorResult result;
  result = (IGeoProcessorResult)GP.Execute("CalculateBestPath", parameters,
    null);

  if (result.Status == esriJobStatus.esriJobSucceeded)
  {
    for (int Count = 0; Count <= result.MessageCount - 1; Count++)
    {
      Console.WriteLine(result.GetMessage(Count));
    }
  }

}

另请参阅:


欢迎转载,转载请注明来源: http://blog.csdn.net/zy332719794/article/details/9213435



相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
相关文章
|
测试技术 API 定位技术
ArcGIS API for Silverlight 调用GP服务准备---GP模型建立、发布、测试
                                                                                                ...
1070 0
|
开发工具
在AE10.1环境下调试其他版本的程序
不同人的可能使用的开发环境不一样,使用SDK版本也不一样,比如用ArcEngine9.3开发的程序在ArcEngine10.1下就不能运行,需要重新调试,才能运行。 这里的例子程序是其他网友在ArcEngine10.0的环境下写的 http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=120017&extra=page%3D1 打开Debug的应用程序不能正常执行,这是ArcEngine版本不对的现象。
995 0
|
存储 固态存储 关系型数据库
基于ARM的AWS EC2实例上的PG跑起来性能怎么样?
基于ARM的AWS EC2实例上的PG跑起来性能怎么样?
163 0
|
IDE 开发工具
WRF安装运行过程中遇到的问题总结
安装和运行WRF时遇到的一些问题,进行汇总,后续根据实际情况可能会进行补充
|
定位技术 数据库
GP的使用心得
在ArcEngine时,GP无疑是GIS开发者的神器。自ArcEngine9.2开始新增一个程序集ESRI.ArcGIS.Geoprocessor,它能调用包含扩展模块在内的所有Geoprocessing工具。
1504 0
|
Java 关系型数据库 测试技术
GP两种连接方式性能测试
GP两种连接方式性能测试
2073 0
|
10月前
LabVIEW配置可执行程序以运行更高版本的运行引擎
LabVIEW配置可执行程序以运行更高版本的运行引擎
175 1
|
Ubuntu 编译器 网络安全
RK3568开发笔记(七):在宿主机ubuntu上搭建Qt交叉编译开发环境,编译一个Demo,目标板运行Demo测试
在之前的博文中已经搭建好了一个比较完善的ubuntu宿主机,都很完善了但是发现没有Qt交叉编译开发环境,所以还需要搭建一套Qt交叉编译开发环境。
CAD启动找不到AC1ST16.DLL
今天在安装Win7 x64上CAD2006启动报错:找不到ac1st16.dll文件。 一查,是系统变量的问题。在系统变量Path中cad的路径为: C:\Program Files (x86)\Common Files\Autodesk Shared\ 然后将其他修改为如下即可: ...
1165 0
|
算法 .NET Windows
MF研究:TinyCLR运行时原理
.Net Micro Framework系统架构如下图所示,其中移植工作主要在平台抽象层(PAL)和硬件抽象层(HAL),大部分常用的PAL层的程序已经写好,基本上不需要什么修改,只有HAL会根据特定的硬件进行微调。
778 0