如何运行geoprocessing(GP)工具

28 篇文章 0 订阅

运行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 toolsESRI.ArcGIS.Analyst3DTools
Analysis toolsESRI.ArcGIS.AnalysisTools
Conversion toolsESRI.ArcGIS.ConversionTools
Data Management toolsESRI.ArcGIS.DataManagementTools
Cartography toolsESRI.ArcGIS.CartographyTools
Coverage toolsESRI.ArcGIS.CoverageTools
Geocoding toolsESRI.ArcGIS.GeocodingTools
Geostatistical Analyst toolsESRI.ArcGIS.GeostatisticalAnalystTools
Linear Referencing toolsESRI.ArcGIS.LinearReferencingAnalystTools
Multidimension toolsESRI.ArcGIS.MultidimensionTools
Network Analyst toolsESRI.ArcGIS.NetworkAnalystTools
SamplesESRI.ArcGIS.SamplesTools
Spatial Analyst toolsESRI.ArcGIS.SpatialAnalystTools
Spatial Statistics toolsESRI.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



Although I’d taken a lot of programming classes in college, I never fully appreciated programming until I had a job that involved a lot of repetitive tasks. After amusing myself by automating much of that job, I decided to return to school and study biol- ogy, which is when I took my first GIS course. I was instantly in love, and managed to convince someone to give me a biology degree for writing an extension for ArcView GIS (a precursor to A rc GIS , for you Esri fans out there). After finishing that up, I went to work for the Remote Sensing/Geographic Information Systems Laboratory at Utah State University. One of my first projects involved some web mapping, and I soon became a big fan of the open source UMN M ap S erver software. That was my introduc- tion to open source geospatial software, including GDAL . I’m fairly certain that I didn’t appreciate the power of the GDAL/OGR library when I first learned about it, but I came to my senses once I started using it in my C++ and C# code. In the College of Natural Resources, there weren’t many people around who were interested in coding, but I did get to point people to the GDAL command-line utilities on a regular basis. But then Esri introduced Python as the scripting language of choice for A rc GIS , and things started to change. I don’t think I had used Python much before then, but playing with arcgisscripting (the original Esri Python module) made me realize how much I enjoyed working with Python, so naturally I had to start using GDAL with it as well. More importantly for this book, my coworker John Lowry suggested that we team- teach a Python-for- GIS class. He taught students how to use Python with A rc GIS , and I taught them about GDAL . The class turned out to be popular, so we taught it that way for another few years until John moved away. I took over the entire class and have been teaching it in various configurations ever since. I’ve never bothered to take the class material from the first two years off the web, however, which is how Manning found me. They asked if I would write a book on using GDAL with Python. I’d never had the desire to write a book, so it took a bit of persuasion to convince me to do it. In the end, it was my love for teaching that won me over. I’ve discovered over the years that I really enjoy teaching, mostly because I love watching students incorporate what they’re learning into the rest of their work. This is especially true of graduate students, some of whom might not have completed their research in a timely manner (or at all) if they hadn’t learned how to write code. I know that these skills will continue to assist them throughout their careers, and my hope is that this book will provide the same help to you, no matter if you’re a student, professional, or a hobbyist. This is fun stuff, and I hope you enjoy it as much as I do!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值