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、方法二:根据空间关系及拓扑工具提取公共边
获取两个面之间的公共边分以下两步:
- 利用
IRelationalOperator
判断两个Polygon
是否为Touches
关系? - 如果是
Touches
关系,利用ITopologicalOperator
的Intersect
方法提取相交部分即可
代码如下:
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
是怎么实现的,有了解的同志也可以讲讲这个问题最优的解决方法是什么。