參考:http://www.cnblogs.com/zhangjun1130/archive/2010/05/26/1744472.html
Geoprocessing是ArcGIS提供的一個非常實用的工具,借由Geoprocessing工具可以方便的調用ArcToolBox中提供的各類工具,本文在ArcEngine9.2平台環境下總結了調用ArcToolBox工具的使用方法:
1、調用ArcToolBox工具方法
以ArcToolBox->Analysis Tools->Proximity->Buffer工具的調用為例,C#代碼如下:
using ESRI.ArcGIS.AnalysisTools; //添加引用
using ESRI.ArcGIS.Geoprocessor;
Geoprocessor gp = new Geoprocessor(); //初始化Geoprocessor
gp.OverwriteOutput = true; //允許運算結果覆蓋現有文件
ESRI.ArcGIS.AnalysisTools.Buffer pBuffer = new ESRI.ArcGIS.AnalysisTools.Buffer(); //定義Buffer工具
pBuffer.in_features = pVorLineLayer; //輸入對象,既可是IFeatureLayer對象,也可是完整文件路徑如“D:\\data.shp”
pBuffer.out_feature_class = pBuffer; //輸出對象,一般是包含輸出文件名的完整文件路徑,如“D:\\buffer.shp”
//設置緩沖區的大小,即可是帶單位的具體數值,如0.1 Decimal Degrees;也可是輸入圖層中的某個字段,如“BufferLeng”
pBuffer.buffer_distance_or_field = "BufferLeng";
pBuffer.dissolve_option = "ALL"; //支持融合緩沖區重疊交叉部分
gp.Execute(pBuffer, null); //執行緩沖區分析
2、參數設置
在調用ArcToolBox執行具體的分析操作時,需要設置各類輸入輸出參數,簡單概括起來說主要分為兩類:對應於Environment Settings對話框的Geoprocessor對象設置、對應於具體操作窗口的方法設置。以ArcToolBox->Analysis Tools->Overlay->Intersect為例,C#代碼如下:
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true; //覆蓋原有文件並重寫
//Environment Settings對話框參數設置,具體名稱參考操作界面Help中對應參數文檔
object obj = gp.GetEnvironmentValue("Extent"); //設置Exten,大小寫無關;
gp.SetEnvironmentValue("Extent", "MAXOF"); //或者"113.697050 115.074770 29.969986 31.362495"
obj = gp.GetEnvironmentValue("OutputZFlag"); //設置Output has Z Values
gp.SetEnvironmentValue("OutputZFlag", "DEFAULT");
obj = gp.GetEnvironmentValue("OutputMFlag"); //設置Output has M Values
gp.SetEnvironmentValue("OutputMFlag", "DEFAULT");
obj = gp.GetEnvironmentValue("OutputCoordinateSystem"); //設置Output Coordinate System
gp.SetEnvironmentValue("OutputCoordinateSystem", Application.StartupPath + "\\zhouyang.prj");
obj = gp.GetEnvironmentValue("QualifiedFieldNames"); //設置Maintain fully qualifid field names
gp.SetEnvironmentValue("QualifiedFieldNames", "QUALIFIED");
//具體操作窗口的方法設置
Intersect pIntersect = new Intersect();
//多個對象的輸入:用分號隔開包含完整路徑的文件名
pIntersect.in_features = pInputFeature1 + ";" + pInputFeature2;
//多個對象的輸入:使用IGpValueTableObject接口,該接口可以設置Rank(http://resources.esri.com/help/9.3/arcgisengine/dotnet/84349562-e062-44ee-8db0-9fcdcd64708b.htm)
//object inputfeature1 = @"D:\周楊\貝貝\WuhanCity\ThiessenPolygons_Line_Buffer.shp";
//object inputfeature2 = @"D:\周楊\貝貝\wuhanCity_shp\poi Point.shp";
//IGpValueTableObject pObject = new GpValueTableObjectClass();
//pObject.SetColumns(2);
//pObject.AddRow(ref inputfeature1);
//pObject.AddRow(ref inputfeature2);
//pIntersect.in_features = pObject;
pIntersect.out_feature_class = pOutputFeature;
pIntersect.join_attributes = "All";
pIntersect.output_type = "POINT";
gp.Execute(pIntersect, null); //執行
3、運行結果對象提取
Geoprocessor對象通過Execute方法執行后將結果保存到指定輸出路徑下,通過也可以通過IGeoProcessorResult接口讀取存儲在內容中的結果對象,C#代碼如下:
//執行圖層求交運算
IGeoProcessorResult pResult = (IGeoProcessorResult)gp.Execute(pIntersect, null);
IGPUtilities pGPUtil = new GPUtilitiesClass();
IFeatureClass pFC;
IQueryFilter pQF;
pGPUtil.DecodeFeatureLayer(pResult.GetOutput(0),out pFC,out pQF);
int count = pFC.FeatureCount(null); //統計Feature對象個數
IFeatureCursor pCursor = pFC.Insert(true); //提取FeatureCursor對象
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFC;
m_mapControl.Map.AddLayer(pFeatureLayer); //加載圖層對象
運行自定義的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...
其中在arccatalog中添加GIS Server時 host Name同 arcgis server manager 打開時的網址中的內容如:


則添加工具代碼為:
GP.AddToolbox(@"http://192.168.0.200:8399/arcgis/services;tt/tbx");
如果無端口號信息則:


則添加工具代碼為:
GP.AddToolbox(@"http://temp-pc/arcgis/services;tt/tbx");
