如何运行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盲盒。
相关文章
|
网络协议 Oracle 安全
loadrunner 运行场景-常见Graph简介
loadrunner 运行场景-常见Graph简介
153 0
|
测试技术 Apache
并发测试工具apache benchmark(ab)
并发测试工具apache benchmark(ab)
299 0
|
运维 JavaScript 前端开发
使用zx 库在 Node 中编写 Shell 脚本
在文章《2021 年 6 个GitHub推荐前端项目》中提到了脚本库 zx.js,一个可以使用 Node.js 编写 Shell 脚本的工具。在本文中,将介绍如何来使用 Google 的 zx 库编写 Shell 脚本。
370 0
使用zx 库在 Node 中编写 Shell 脚本
|
Python
ZZULIOJ-1078,a+b(多实例测试1)(Python)
ZZULIOJ-1078,a+b(多实例测试1)(Python)
|
Python
ZZULIOJ-1079,a+b(多实例测试2)(Python)
ZZULIOJ-1079,a+b(多实例测试2)(Python)
|
Python
ZZULIOJ-1080,a+b(多实例测试3)(Python)
ZZULIOJ-1080,a+b(多实例测试3)(Python)
|
C++ Python
ZZULIOJ-1095,时间间隔(多实例测试)(Python)
ZZULIOJ-1095,时间间隔(多实例测试)(Python)
|
Python
ZZULIOJ-1099,角谷猜想(多实例测试)(Python)
ZZULIOJ-1099,角谷猜想(多实例测试)(Python)
|
机器学习/深度学习 Python
ZZULIOJ-1082,敲7(多实例测试)(Python)
ZZULIOJ-1082,敲7(多实例测试)(Python)
|
SQL 大数据 Linux
KYLIN&GP性能测试报告
KYLIN&GP性能测试报告
2248 0