C#+ ArcEngine 调用GP工具示例

记得添加引用:
using ESRI.ArcGIS.Geoprocessing;
……
引用这部分少什么添加什么,很简单

相交

    public IFeatureClass Intersect2(IFeatureClass A,IFeatureClass B,string name)
    {
        Geoprocessor geoprocessor = new Geoprocessor();
        geoprocessor.OverwriteOutput = true;
        
        IGpValueTableObject gpValueTableObject = new GpValueTableObjectClass();//提供对分派值表对象的访问,理解为添加到表中
        gpValueTableObject.SetColumns(2);
        object o1 = A;//输入IFeatureClass 1 
        object o2 = B;//输入IFeatureClass 2 
        gpValueTableObject.AddRow(ref o1);
        gpValueTableObject.AddRow(ref o2);

        Intersect intersect = new Intersect();
        intersect.in_features = gpValueTableObject;
        intersect.out_feature_class = defaultpath + name;

        geoprocessor.Execute(intersect, null);

        //先保存再读取,还有其他的方法吗?欢迎留言
        Helper helper = new Helper();
        IFeatureClass AB = helper.ReadShpFromFile(defaultpath + name);

        return AB;
    }

按属性选择数据

        /// 
        /// 因为项目需要,这里示意的是从shp图层中选出myclass字段值为field且Area字段值在low-upper范围内的要素,并保存在name文件(路径)中。
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public IFeatureClass SelectByFieldAndExplore(IFeatureClass shp,string field,double low,double upper,string name  )
        {
            Geoprocessor geoprocessor = new Geoprocessor();
            geoprocessor.OverwriteOutput = true;

            Select selected = new Select();
            selected.in_features = shp;
            selected.out_feature_class = defaultpath + name;
            selected.where_clause ="\"myclass\" = \'"+field + "\'AND \"Area\">= "+ low + "AND \"Area\"<= "+ upper;   

            //如果出错,try 的这部分将打印出错误到底在哪
             try
            {
                geoprocessor.Execute(selected, null);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                string message = "";
                for (int i = 0; i < geoprocessor.MessageCount; i++)
                {
                    message += geoprocessor.GetMessage(i) + "\r\n";
                }
                MessageBox.Show(message + e.ToString());
            }

            //先保存再读取
            Helper helper = new Helper();
            IFeatureClass slt = helper.ReadShpFromFile(defaultpath + name);

            return slt;
        }

擦除

        public IFeatureClass Eraser(IFeatureClass A, IFeatureClass B,string name)
        {
            Geoprocessor geoprocessor = new Geoprocessor();
            geoprocessor.OverwriteOutput = true;

            Erase erase = new Erase();
            erase.in_features = A;
            erase.erase_features = B;
            erase.out_feature_class =defaultpath+name ;

            try
            {
                geoprocessor.Execute(erase, null);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                string message = "";
                for (int i = 0; i < geoprocessor.MessageCount; i++)
                {
                    message += geoprocessor.GetMessage(i) + "\r\n";
                }
                MessageBox.Show(message + e.ToString());
            }

你可能感兴趣的:(C#+ ArcEngine 调用GP工具示例)