Gp工具的使用总结

一、使用工具及对应命名空间

    

     这些是ArcMap的工具箱及对应的引用命名空间,这些引用都是以Tools为后缀的。

二、执行GP工具的两种方式

1)通过对应的工具执行

      public bool Corridor(string in_distance_raster1, string in_distance_raster2, string out_raster)

        {

            try

            {

                if (System.IO.File.Exists(out_raster))

                {

                    //删除已经存在的文件                    

                    System.IO.File.Delete(out_raster);

                }

                ESRI.ArcGIS.SpatialAnalystTools.Corridor corridor = new ESRI.ArcGIS.SpatialAnalystTools.Corridor();

                corridor.in_distance_raster1 = in_distance_raster1;

                corridor.in_distance_raster2 = in_distance_raster2;

                corridor.out_raster = out_raster;

 

                Geoprocessor gp = new Geoprocessor();

                IGeoProcessorResult result = (IGeoProcessorResult)gp.Execute(corridor, null);

 

                if (result == null || result.Status != esriJobStatus.esriJobSucceeded)

                {

                    MessageBox.Show("廊道分析失败");

                }

                else

                {

                    //获取分析的结果

                    MessageBox.Show("廊道分析成功!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                }

                return true;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

这里通过执行对应的工具“ESRI.ArcGIS.SpatialAnalystTools.Corridor”来实现GP工具操作。Geoprocessor 是一个托管程序集是。

2)通过对应的工具名执行

 

//矢量转栅格
        public static bool ShpToRaster(IFeatureClass feaureClass, string fieldName, String rasterWorkspace, String rasterName, string cell_assignment,double cellSize)
        {
            string fullPath;
            IGeoProcessor pGP;
            IGeoProcessorResult pGPR;
            IVariantArray pParameterArray;
            try
            {
                fullPath = System.IO.Path.Combine(rasterWorkspace, rasterName);
                if (System.IO.File.Exists(fullPath))
                {
                    //删除已经存在的文件                   
                    System.IO.File.Delete(fullPath);
                }
                pGP = new GeoProcessorClass();
                pParameterArray = new VarArrayClass();
                pParameterArray.Add(feaureClass);
                pParameterArray.Add(fieldName);
                pParameterArray.Add(rasterWorkspace + @"\" + rasterName);
                pParameterArray.Add(cell_assignment);
                pParameterArray.Add(null);
                pParameterArray.Add(cellSize);
                if (feaureClass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    pGPR = pGP.Execute("PointToRaster_conversion", pParameterArray, null);
                }
                if (feaureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    pGPR = pGP.Execute("PolylineToRaster_conversion", pParameterArray, null);
                }
                if (feaureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    pGPR = pGP.Execute("PolygonToRaster_conversion", pParameterArray, null);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

这里通过执行对应的工具名“pGPR = pGP.Execute("PointToRaster_conversion", pParameterArray, null);”来实现GP工具操作。IGeoProcessor 一个COM Interop程序集

注意事项:

1、确保填入参数有正确的设置顺序

2、如果你想跳过可选参数,你只需要将它们填入一个空字符串,如""

3、如果你填入空字符串,处理工具将使用默认值

 

三、Gp操作有的需要环境设置

默认情况下是输入的坐标系统作为输出坐标,如果要设置新的,需要改变以下值。

public void setCoordinateSystem(IGeoProcessor2 gp)

{

    // Set overwrite option to true.

    gp.OverwriteOutput = true;

 

    // Set workspace environment.

    gp.SetEnvironmentValue("workspace", @"C:\data\saltlake.gdb");

 

    // Set the output coordinate system environment.            

    gp.SetEnvironmentValue("outputCoordinateSystem", @

        "C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\Nad 1983\NAD 1983 UTM Zone 12N.prj");

 

    IVariantArray parameters = new VarArrayClass();

    parameters.Add("roads");

    parameters.Add("roads_copy");

 

    gp.Execute("CopyFeatures_management", parameters, null);

}

以下是重新改变环境变量值

// Get the cell size environment value.

object env = gp.GetEnvironmentValue("cellsize");

// Reset the environment values to their defaults.

gp.ResetEnvironments();

 

详见Esri官网:

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Using_environment_settings/0001000001n5000000/

 

四、Gp工具参数设置

这是Gp工具最麻烦的一步,也最容易出错。在ArcMap帮助文档可以搜到对应的工具的帮助,里面有一个介绍参数的。

 

一般对于FeatureLayer就是IFeatureLayer对象,RasterLayer就是IRasterLayer对象,或者是文件的路径。Field就是IField对象或者字段名。Table就算是ITable对象。大多数参数就是string或者object类型。具体的可以实际中测试选择。

 

五、参考资料或网址

1.http://edndoc.esri.com/arcobjects/9.2/NET/c4ff8b68-0410-435f-b8e5-682d5cea47cf.htm

2.http://blog.csdn.net/liuguobo/article/details/16965987

3.http://www.cnblogs.com/zhangjun1130/archive/2010/05/26/1744472.html

4.http://blog.csdn.net/mytudousi/article/details/31388607

 

posted @ 2015-08-21 23:29  羽过天晴  阅读(6629)  评论(0编辑  收藏  举报