天天看點

c# 調用ArcEngine的GP工具

<a></a>

         Geoprocessing是ArcGIS提供的一個非常實用的工具,借由Geoprocessing工具可以友善的調用ArcToolBox中提供的各類工具,本文在ArcEngine9.2平台環境下總結了調用ArcToolBox工具的使用方法:

        1、調用ArcToolBox工具方法

         以ArcToolBox-&gt;Analysis Tools-&gt;Proximity-&gt;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中各個工具調用時需添加的引用分别如下圖所示:

        2、參數設定

        在調用ArcToolBox執行具體的分析操作時,需要設定各類輸入輸出參數,簡單概括起來說主要分為兩類:對應于Environment Settings對話框的Geoprocessor對象設定、對應于具體操作視窗的方法設定。以ArcToolBox-&gt;Analysis Tools-&gt;Overlay-&gt;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

            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;

     //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對話框對應,如圖所示:

  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);   //加載圖層對象

     其實總的說來,ESRI的官方幫助和各類線上幫助文檔中都提供了相應的說明,可以很容易搞清楚一些内容,但是在具體的操作過程中,有時候經常得不到結果,這時候就需要關注下Environment Settings中的部分參數是否設定了,有可能沒有像軟體操作界面中那樣進行預設設定。

沒有整理與歸納的知識,一文不值!高度概括與梳理的知識,才是自己真正的知識與技能。 永遠不要讓自己的自由、好奇、充滿創造力的想法被現實的架構所束縛,讓創造力自由成長吧! 多花時間,關心他(她)人,正如别人所關心你的。理想的騰飛與實作,沒有别人的支援與幫助,是萬萬不能的。

    本文轉自wenglabs部落格園部落格,原文連結:http://www.cnblogs.com/arxive/p/6262894.html,如需轉載請自行聯系原作者