首页 > 代码库 > c# 调用ArcEngine的GP工具

c# 调用ArcEngine的GP工具

转自原文c# 调用ArcEngine的GP工具,AE调用GP工具

 

IAoInitialize m_AoInitialize = new AoInitializeClass();   
esriLicenseStatuslicenseStatus = esriLicenseStatus.esriLicenseUnavailable;   
licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);  
if (licenseStatus == esriLicenseStatus.esriLicenseCheckedOut)  
{  
    GeoprocessortGp = newGeoprocessor();  
    tGp.OverwriteOutput = true;   
    licenseStatus = m_AoInitialize.IsExtensionCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine,esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);  
    licenseStatus = m_AoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);  
      
    //LICenced arcengine  
    ESRI.ArcGIS.ConversionTools.ASCIIToRastertASC = newESRI.ArcGIS.ConversionTools.ASCIIToRaster();  
    tASC.data_type = "FLOAT";  
    tASC.in_ascii_file = pWorkSpacePath + @"a1.txt";  
    tASC.out_raster = pWorkSpacePath + "a1.img";  
    //ScrollToBottom("ASCIIToRaster");  
    IGeoProcessorResulttGeoResult = (IGeoProcessorResult)tGp.Execute(tASC, null);  
    if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)  
    {  
         ESRI.ArcGIS.SpatialAnalystTools.ReclassifytReclass = newESRI.ArcGIS.SpatialAnalystTools.Reclassify();  
        tReclass.in_raster = pWorkSpacePath + "a1.img";  
        tReclass.missing_values = "NODATA";  
        tReclass.out_raster = pWorkSpacePath + @"Out3.img";  
        tReclass.reclass_field = "VALUE";  
        tReclass.remap = "0 958 10;958 988 20;988 990 30;990 1000 40;1000 1100 50";  
       // ScrollToBottom("Reclassify");  
        tGeoResult = (IGeoProcessorResult)tGp.Execute(tReclass, null);  
        if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)  
        {  
            ESRI.ArcGIS.ConversionTools.RasterToPolygontRTP = newESRI.ArcGIS.ConversionTools.RasterToPolygon();  
            tRTP.in_raster = pWorkSpacePath + "Out3.img";  
            tRTP.out_polygon_features = pWorkSpacePath + "a2_shp.shp";  
            tRTP.raster_field = "VALUE";  
            tRTP.simplify = "SIMPLIFY";  
            //ScrollToBottom("RasterToPolygon");  
            tGeoResult = (IGeoProcessorResult)tGp.Execute(tRTP, null);  
            if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)  
            {  
                licenseStatus = m_AoInitialize.CheckInExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);  
                {  
                    ESRI.ArcGIS.DataManagementTools.SmoothPolygontSP = newESRI.ArcGIS.DataManagementTools.SmoothPolygon();  
                    tSP.algorithm = "PAEK";  
                    tSP.in_features = pWorkSpacePath + "a2_shp.shp";  
                    tSP.out_feature_class = pWorkSpacePath + "a2_shp_smooth.shp";  
                    tSP.tolerance = "2";  
                    tSP.endpoint_option = "FIXED_ENDPOINT";  
                    tSP.error_option = "FLAG_ERRORS";  
                    ScrollToBottom("SmoothPolygon");  
                    tGeoResult = (IGeoProcessorResult)tGp.Execute(tSP, null);  
                }  
            }  
        }  
    }  
    // arcengine liencesd 当使用高版本的liencesd在使用中,如果需要低版本的liencesd,需要动态签出liencesd,不然将不会执行成功!  
   // writeMessage(tGp);  
}  
  
MessageBox.Show("ok");  

 

 

giszhouGeoprocessing调用ArcToolBox工具使用总结

         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中的部分参数是否设置了,有可能没有像软件操作界面中那样进行默认设置。

 

c# 调用ArcEngine的GP工具