ArcEngine二次开发API之Bug集(一 )

Posted 流浪若相惜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArcEngine二次开发API之Bug集(一 )相关的知识,希望对你有一定的参考价值。

    今天,在本集(一)中给大家讲三个bug的集,这三个bug害的我弄了好几天,这几天的教训,也让我明白了今后的开发中,也不能完全相信API,难免API也会出现使用描述的错误。

       1、GP之ExtractByMask

      先来看看API是怎么描述属性参数的: Public Instance Constructors
ExtractByMaskOverloaded. Initializes a new instance of the ExtractByMask class.

Public Instance Properties

AliasThe alias for this tool's toolbox.
in_mask_dataInput mask data defining areas to extract. (In, Required)
in_rasterThe input raster from which cells will be extracted. (In, Required)
out_rasterThe raster to be created. (Out, Required)
ParameterInfoThe parameters used by this tool. For internal use only.
ToolboxDirectoryThe directory of this tool's toolbox.
ToolboxNameThe name of this tool's toolbox.
ToolNameThe name of this tool.

Public Instance Methods

Equals (inherited from Object)Determines whether the specified Object is equal to the current Object.
GetHashCode (inherited from Object)Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table.
GetType (inherited from Object)Gets the Type of the current instance.
ToString (inherited from Object)Returns a String that represents the current Object.

Protected Instance Methods

Finalize (inherited from Object)Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
MemberwiseClone (inherited from Object)Creates a shallow copy of the current Object.
       有三个参数是必须的,但是这描述我想也是醉了。并没有说明是数据集还是路径就可以了,或许不说就是默认是路径了吧。之前使用了GP都是要求数据集的,哎,可能我刚刚接触的原因吧。其实这里使用的是路径。好了,为了给初学者提供帮助,我这里将实现代码附上:
     Geoprocessor gp = new Geoprocessor();
                    in_raster = Global.InputData;
                    in_mask_data = Global.InputMask;
                    out_raster = Global.OutData + "\\\\" + Global.OutDataName;
                    /*=====================================ExtractByMask=============================*
                     * Extracts the cells of a raster that correspond to the areas defined by a mask.*
                     * 三个必须参数:in_raster、in_mask_data、out_raster                             *
                     *===============================================================================*/
                    ExtractByMask extractByMask = new ExtractByMask();
                    extractByMask.in_raster = in_raster;
                    extractByMask.in_mask_data = in_mask_data;
                    extractByMask.out_raster = out_raster;

                    gp.Execute(extractByMask, null);
                    MessageBox.Show("裁剪成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);   

    2、GP之ProjectRaster

      同样,还是先上API:

Public Instance Constructors

ProjectRasterOverloaded. Initializes a new instance of the ProjectRaster class.

Public Instance Properties

AliasThe alias for this tool's toolbox.
cell_sizeThe cell size for the output raster dataset. The default cell size is the cell size of the selected raster dataset. (In, Optional)
geographic_transformThe transformation method used between two geographic systems or datums. (In, Optional)
in_coor_systemThe coordinate system of the input raster dataset. (In, Optional)
in_rasterThe input raster dataset. (In, Required)
out_coor_systemThe coordinate system for the geometry column. (In, Required)
out_rasterThe output raster dataset to be created. (Out, Required)
ParameterInfoThe parameters used by this tool. For internal use only.
Registration_PointThe x and y coordinates (in the output space) used for pixel alignment. (In, Optional)
resampling_typeThe resampling algorithm to be used. The default is NEAREST. NEAREST | BILINEAR | CUBIC | MAJORITY (In, Optional)
ToolboxDirectoryThe directory of this tool's toolbox.
ToolboxNameThe name of this tool's toolbox.
ToolNameThe name of this tool.

Public Instance Methods

Equals (inherited from Object)Determines whether the specified Object is equal to the current Object.
GetHashCode (inherited from Object)Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table.
GetType (inherited from Object)Gets the Type of the current instance.
ToString (inherited from Object)Returns a String that represents the current Object.

Protected Instance Methods

Finalize (inherited from Object)Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
MemberwiseClone (inherited from Object)Creates a shallow copy of the current Object.

      必须的有三个参数,其中前两个没有啥问题,第三个参数的描述,也是让人醉的一个。The output raster dataset to be created. (Out, Required)怎么看都是创建一个输出数据集啊,结果,就是出错。随后跟一个大神要了参考的代码,发现他的代码中是路径,并不是filename,好吧,于是我就尝试用filename。结果是第三个参数需要的是文件名就可以了。代码如下:

 public void ChangeRasterCoordinate(string inputRaster, AxMapControl axMap, string outRaster)
        
            Geoprocessor gp = new Geoprocessor();
            /*======================ProjectRaster================================*
             *                       in require                                  *
             * in_raster:The input raster dataset.                               *
             * out_coor_system The coordinate system for the geometry column.    *
             * out_raster The output raster dataset to be created.               * 
             *                        Optional                                   *
             * in_coor_system The coordinate system of the input raster dataset. * 
             *===================================================================*/
            ProjectRaster projectRaster = new ProjectRaster();
            //创建输入栅格数据的数据集
            IRasterDataset inputRasterData = new RasterDataset();
            inputRasterData.OpenFromFile(inputRaster);
            projectRaster.in_raster = inputRasterData;
            try
            
                //创建输出栅格数据数据集
                //IRasterDataset outputRasterData = new RasterDataset();

                Open output workspace
                //IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
                //长度不包括\\
                int index = outRaster.LastIndexOf("\\\\");
                string outputFolder = outRaster.Substring(0, index);
                string name = outRaster.Substring(index + 1);
                //提供一个personal或者file geodatabase的file后者directory,这里提供一个directory
                //IRasterWorkspace workspace = workspaceFactory.OpenFromFile(outputFolder, 0) as IRasterWorkspace;

                //outputRasterData = workspace.OpenRasterDataset(name);

                /*此处应该是api的一个错误,api中是这样写的: 
out_raster The output raster dataset to be created. (Out, Required) 
*/
                projectRaster.out_raster = outRaster;
            
            catch (Exception e)
            
                MessageBox.Show(e.ToString());
            
            projectRaster.out_coor_system = axMap.SpatialReference;

            /*=====================================执行GP的两种写法=================================*
             * 1、 gp.Execute(projectRaster, null);                                                 *
             * 2、用IGeoProcessorResultL:                                                          *
             *    @1、The Geoprocessor Result. Provides access to the results of tools.             *
             *======================================================================================*/
            IGeoProcessorResult tGPResult =(IGeoProcessorResult) gp.Execute(projectRaster, null);
            if (tGPResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
            
                MessageBox.Show("栅格数据坐标转换成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);;
            
            else if(tGPResult.Status==ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobFailed)
            
                MessageBox.Show("栅格数据坐标转换失败!", "警示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            

        

     3、GP之DefineProjection

同样我们先来看看官网提供的API:

Public Instance Constructors

DefineProjectionOverloaded. Initializes a new instance of the DefineProjection class.

Public Instance Properties

AliasThe alias for this tool's toolbox.
coor_systemName of the coordinate system to be applied to the input dataset or feature class. (In, Required)
in_datasetName of dataset or feature class whose projection is to be defined. (In, Required)
out_datasetOutput Dataset or Feature Class (out, Optional)
ParameterInfoThe parameters used by this tool. For internal use only.
ToolboxDirectoryThe directory of this tool's toolbox.
ToolboxNameThe name of this tool's toolbox.
ToolNameThe name of this tool.

Public Instance Methods

Equals (inherited from Object)Determines whether the specified Object is equal to the current Object.
GetHashCode (inherited from Object)Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table.
GetType (inherited from Object)Gets the Type of the current instance.
ToString (inherited from Object)Returns a String that represents the current Object.

Protected Instance Methods

Finalize (inherited from Object)Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
MemberwiseClone (inherited from Object)Creates a shallow copy of the current Object.

有两个必须的参数,API上都是Name,我也是很实在的都用的Name,包裹SpatialReference的Name。实际上它需要的是SpatialReference对象。这里多说一点,如果开发事碰到“未设置引用对象的实例”问题,往往是因为DefineProjection对象参数的问题。代码如下:
 public void DefineCoord(string inputData, ISpatialReference spatialReference,AxMapControl axMap)
        
            /**/
            DefineProjection defineProject = new DefineProjection();
            Geoprocessor gp = new Geoprocessor();
            try
            
                defineProject.in_dataset = inputData;

                defineProject.coor_system = spatialReference;
                //defineProject.out_dataset = inputData;
             <strong>   //如果gpResult==null,往往是因为文件已被打开,无法对其进行操作。</strong>
                IGeoProcessorResult gpResult = (IGeoProcessorResult)gp.Execute(defineProject, null);

                if (gpResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                
                    MessageBox.Show("定义坐标成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                
                else
                
                    MessageBox.Show("定义坐标失败!", "警示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                int formateLen = inputData.Length - (inputData.LastIndexOf("."));
                string formate =inputData.Substring(inputData.LastIndexOf("."));
                Functions.Files.AddMaps addMap = new 图像预处理系统.Functions.Files.AddMaps();
                if (formate == ".img")
                
                    addMap.AddRaster(axMap, inputData);
                
                else if (formate == ".shp")
                
                    addMap.AddShp(axMap, inputData);
                
            

            catch (Exception e)
            
                MessageBox.Show(e.ToString());
            
            finally
            
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int i = 0; i < gp.MessageCount; i++)
                    sb.AppendLine(gp.GetMessage(i));
                if (sb.Capacity > 0)
                    MessageBox.Show(sb.ToString(), "GP Messages");
            

        



       

以上是关于ArcEngine二次开发API之Bug集(一 )的主要内容,如果未能解决你的问题,请参考以下文章

ArcEngine C# 二次开发 PolyLine 多次打断操作

arcengine10.5适配哪版vs

Arcengine 二次开发添加右键菜单

arcengine用c#开发怎么实现选择属性表中的一项属性平移至地图显示?

arcengine判断一个点是否 在一个面中

arcEngine开发之根据点坐标创建Shp图层