1、前言

很久没写ArcEngine的内容了,正好这次有同志提了一个问题:如何用ArcEngine实现批量提取面要素之间的公共边?捣鼓了半天总算是解决了,下面就来说一说解决思路。

2、ArcMap的实现方法

首先准备一份测试数据,如下图所示:

ArcEngine提取面要素公共边的实现方法_List

提取公共边用ArcMap做非常简单,只需要打开Analysis Tools下的Intersect相交工具,将Output Type设置为LINE,运行工具,马上就能得到面要素的公共边。如下图所示:

ArcEngine提取面要素公共边的实现方法_Desktop_02

结果如下图所示:

ArcEngine提取面要素公共边的实现方法_List_03

3、方法一:调用GP提取公共边

既然已经知道了在ArcMap中如何使用Intersect工具来提取公共边,那么我们就可以在ArcEngine中调用GP工具来实现。不过需要注意:ArcEngine代码初始化时需要设置License的权限,代码如下:

using ESRI.ArcGIS.Geoprocessor;
using System;
using System.Windows.Forms;

namespace App
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            // 设置参数
            ESRI.ArcGIS.AnalysisTools.Intersect tool = new ESRI.ArcGIS.AnalysisTools.Intersect();
            tool.in_features = @"C:\Users\Virtual\Desktop\data\面.shp";
            tool.output_type = "LINE";
            tool.out_feature_class = @"C:\Users\Virtual\Desktop\data\线.shp";

            // 执行GP
            Geoprocessor gp = new Geoprocessor();
            gp.OverwriteOutput = true;
            gp.Execute(tool, null);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

运行结果如下图所示:

ArcEngine提取面要素公共边的实现方法_List_03

4、方法二:根据空间关系及拓扑工具提取公共边

获取两个面之间的公共边分以下两步:

  1. 利用IRelationalOperator判断两个Polygon是否为Touches关系?
  2. 如果是Touches关系,利用ITopologicalOperatorIntersect方法提取相交部分即可

代码如下:

using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace App
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            IFeatureClass pFeatureClass = GetFeatureClass(@"C:\Users\Virtual\Desktop\data\面.shp");
            List<IPolygon> polygons = GetPolygonList(pFeatureClass);
            List<IPolyline> polylines = GetPolylineList(polygons);
            CreateFeatureClass(polylines, @"C:\Users\Virtual\Desktop\data\线.shp");
        }

        // 获取要素类
        private IFeatureClass GetFeatureClass(string filePath)
        {
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspaceFactoryLockControl pWorkspaceFactoryLockControl = pWorkspaceFactory as IWorkspaceFactoryLockControl;
            if (pWorkspaceFactoryLockControl.SchemaLockingEnabled)
            {
                pWorkspaceFactoryLockControl.DisableSchemaLocking();
            }
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(filePath));
            return pFeatureClass;
        }

        // 获取Polygon集合
        private List<IPolygon> GetPolygonList(IFeatureClass pFeatureClass)
        {
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            IFeature pFeature = pFeatureCursor.NextFeature();
            if (pFeature == null)
            {
                return null;
            }

            // 遍历游标
            List<IPolygon> list = new List<IPolygon>();
            while (pFeature != null)
            {
                list.Add(pFeature.ShapeCopy as IPolygon);
                pFeature = pFeatureCursor.NextFeature();
            }

            // 返回
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
            return list;
        }

        // 获取Polyline集合
        private List<IPolyline> GetPolylineList(List<IPolygon> polygons)
        {
            List<IPolyline> list = new List<IPolyline>();
            for (int i = 0; i < polygons.Count; i++)
            {
                for (int j = 0; j < polygons.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }

                    IRelationalOperator pRelationalOperator = polygons[i] as IRelationalOperator;
                    if (pRelationalOperator.Touches(polygons[j]))
                    {
                        ITopologicalOperator pTopologicalOperator = polygons[i] as ITopologicalOperator;
                        IGeometry pIntersectGeometry = pTopologicalOperator.Intersect(polygons[j], esriGeometryDimension.esriGeometry1Dimension);
                        list.Add(pIntersectGeometry as IPolyline);
                    }
                }
            }
            return list;
        }

        // 创建要素类
        private IFeatureClass CreateFeatureClass(List<IPolyline> polylines, string filePath)
        {
            // 设置空间参考
            IGeometryDef pGeometryDef = new GeometryDef();
            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
            pGeometryDefEdit.HasM_2 = false;
            pGeometryDefEdit.HasZ_2 = false;
            pGeometryDefEdit.SpatialReference_2 = axMapControl1.SpatialReference;

            // 字段集合
            IFields pFields = new Fields();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            // Shape
            IField pField = new Field();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldEdit.AliasName_2 = "Shape";
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.IsNullable_2 = false;
            pFieldEdit.Required_2 = true;
            pFieldsEdit.AddField(pField);

            // 创建要素类
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(System.IO.Path.GetFileName(filePath), pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

            // 要素游标
            IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
            IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
            for (int i = 0; i < polylines.Count; i++)
            {
                pFeatureBuffer.Shape = polylines[i];
                pFeatureCursor.InsertFeature(pFeatureBuffer);
            }
            pFeatureCursor.Flush();

            // 返回
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureBuffer);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
            return pFeatureClass;
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.

运行结果如下图所示:

ArcEngine提取面要素公共边的实现方法_List_03

5、结语

本文主要介绍了ArcEngine中提取公共边的实现方法。其实对于第二种方法,即:利用空间关系和拓扑工具提取公共边,我个人是不太满意的,因为这是纯粹的暴力解法,数据量一旦较多,效率肯定是个大问题。可惜不知道ESRI是怎么实现的,有了解的同志也可以讲讲这个问题最优的解决方法是什么。