如何运行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盲盒。
相关文章
|
1月前
|
Go
在Go中如何停止程序
在Go中如何停止程序
|
2月前
|
运维 Serverless Go
函数计算产品使用问题之创建层时,如何添加Go环境
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
3月前
|
数据采集 Python
环境调试——EA-LSS
对比 E-H:同样增加速度增强之后,放大图像的调整范围,DAL 会比 BEVFusion 略微提升。作者说速度增强挑战了点云线索的回归任务预测,这迫使模型利用图像线索。(没懂,插个眼)
86 1
|
11月前
|
前端开发 芯片 Python
【python脚本】ICer的脚本入门训练——gen_tc
【python脚本】ICer的脚本入门训练——gen_tc
|
存储 数据可视化 NoSQL
Qt Creator的CDB调试器--使用技巧与解决调试很慢的心得,重点是Symbols Path设置
Qt Creator的CDB调试器--使用技巧与解决调试很慢的心得,重点是Symbols Path设置
2518 0
Qt Creator的CDB调试器--使用技巧与解决调试很慢的心得,重点是Symbols Path设置
|
Go
go:快速升级Go版本,我几分钟就解决了
go:快速升级Go版本,我几分钟就解决了
4371 0
go:快速升级Go版本,我几分钟就解决了
|
Python
Python 检测PE所启用保护方式
Python 通过pywin32模块调用WindowsAPI接口,实现对特定进程加载模块的枚举输出并检测该PE程序模块所启用的保护方式,此处枚举输出的是当前正在运行进程所加载模块的DLL模块信息,需要用户传入进程PID才可实现输出。
216 0
Python 检测PE所启用保护方式
|
分布式计算 开发工具 计算机视觉
ps,pr ,ae,dw等软件简短解析(含安装包)
Adobe photoshop 专长在于图像处理,而不是图形创作 平面设计是Photoshop应用最为广泛的领域,
|
小程序 编译器 C++
如何使用trace模式运行BRF+应用
Created by Wang, Jerry, last modified on Dec 12, 2014
如何使用trace模式运行BRF+应用