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

1、前言

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

2、ArcMap的实现方法

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

在这里插入图片描述

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

在这里插入图片描述

结果如下图所示:

在这里插入图片描述

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);
        }
    }
}

运行结果如下图所示:

在这里插入图片描述

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;
        }
    }
}

运行结果如下图所示:

在这里插入图片描述

5、结语

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值