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); //執行緩衝區分析
ArcToolBox中各個工具調用時需添加的引用分別如下圖所示:
參考網頁:http://edndoc.esri.com/arcobjects/9.2/NET/c4ff8b68-0410-435f-b8e5-682d5cea47cf.htm
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");
//關於Environment Settings的設置可以參考ArcMap操作界面提供的文檔,如圖所示:
Environment Settings設置
參數名稱參考幫助,如上圖所示爲Extent,其取值有三種形式
//具體操作窗口的方法設置
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); //執行
//Intersect參數設置跟彈出的Intersect對話框對應,如圖所示:
參考網頁:http://edndoc.esri.com/arcobjects/9.2/NET/552ca115-f23b-4a74-a2c5-069c50d6cdcf.htm
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); //加載圖層對象
參考網頁:http://edndoc.esri.com/arcobjects/9.2/NET/1b14f488-84de-4e7f-8009-cfe612f8dcbe.htm
其實總的說來,ESRI的官方幫助和各類在線幫助文檔中都提供了相應的說明,可以很容易搞清楚一些內容,但是在具體的操作過程中,有時候經常得不到結果,這時候就需要關注下Environment Settings中的部分參數是否設置了,有可能沒有像軟件操作界面中那樣進行默認設置。