接着上一篇博客,这篇还是讲线要素的打断~线要素有时会出现线段自相交的情况,如下图所示,该shp文件中有3条要素自相交。
那么如何快速找到自相交的要素呢?方法一基本上学过ArcGIS的都能想到,那就是导入要素数据集,进行相应的拓扑检查。另一个方法就是利用ITopologicalOperator3接口进行检查,看下面这段代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
}
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
SelectSelfIntersectionsFeatures(pFeatureLayer);
}
private void SelectSelfIntersectionsFeatures(IFeatureLayer pFeatureLayer)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return;
}
// 遍历线要素
StringBuilder builder = new StringBuilder();
while (pFeature != null)
{
ITopologicalOperator3 pTopologicalOperator3 = pFeature.ShapeCopy as ITopologicalOperator3;
pTopologicalOperator3.IsKnownSimple_2 = false;
// 检查自相交
esriNonSimpleReasonEnum reason = esriNonSimpleReasonEnum.esriNonSimpleOK;
if (!pTopologicalOperator3.get_IsSimpleEx(out reason))
{
if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)
{
builder.Append("FID=" + pFeature.OID.ToString() + " or ");
}
}
pFeature = pFeatureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
builder.Remove(builder.Length - 3, 3);
// 创建属性过滤器
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.AddField("FID");
pQueryFilter.WhereClause = builder.ToString();
// 查询自相交要素
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
}
}
这段代码的核心就是IsKnownSimple_2和get_IsSimpleEx,前者设置为false,就是假设当前要素不是一个简单要素,然后调用get_IsSimpleEx,判断当前要素是否为自相交要素,运行结果如下所示,通过这段代码就能查询出自相交的要素。
在找出自相交的要素后如何在其自相交处对线进行打断呢?前一篇博客介绍了IFeatureEdit接口,但在这种情况下并不适用,因此我们考虑使用IGeometryCollection接口来做,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
}
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(1) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
List<IPolyline> list = GetSelfIntersectionsPolyline(axMapControl1.get_Layer(0) as IFeatureLayer);
for (int i = 0; i < list.Count; i++)
{
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = list[i];
pFeature.Store();
}
}
private List<IPolyline> GetSelfIntersectionsPolyline(IFeatureLayer pFeatureLayer)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return null;
}
// 遍历线要素
List<IPolyline> list = new List<IPolyline>();
object missing = Type.Missing;
while (pFeature != null)
{
ITopologicalOperator3 pTopologicalOperator3 = pFeature.ShapeCopy as ITopologicalOperator3;
pTopologicalOperator3.IsKnownSimple_2 = false;
// 检查自相交
esriNonSimpleReasonEnum reason = esriNonSimpleReasonEnum.esriNonSimpleOK;
if (!pTopologicalOperator3.get_IsSimpleEx(out reason))
{
if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)
{
pTopologicalOperator3.Simplify();
IGeometryCollection pGeometryCollection = pTopologicalOperator3 as IGeometryCollection;
for (int i = 0; i < pGeometryCollection.GeometryCount; i++)
{
IGeometryCollection pPolyline = new Polyline() as IGeometryCollection;
pPolyline.AddGeometry(pGeometryCollection.get_Geometry(i), ref missing, ref missing);
list.Add(pPolyline as IPolyline);
}
}
}
pFeature = pFeatureCursor.NextFeature();
}
// 返回结果
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
return list;
}
}
}
结果如下: