在一般的数据编辑流程中,有些时候可能会涉及到多边形内部的挖洞操作。在ArcEngine中,一般可以通过两种方式实现挖洞操作。一种是叠置分析,这主要是针对图层与图层之间的操作,我会在后续的博客中进行介绍,另一种则是用户自己绘制多边形实现挖洞操作,这主要是利用ITopologicalOperator
接口进行实现。先来看一下效果图:
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
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 MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"E:\Users\dsf\Desktop\无标题.mxd");
}
// OnMouseDown事件
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
IFeatureClass pFeatureClass = (axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;
IGeometry pGeometry = axMapControl1.TrackPolygon();
// 空间过滤器
ISpatialFilter pSpatialFilter = new SpatialFilter();
pSpatialFilter.AddField("Shape");
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
// 要素游标
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return;
}
// 遍历游标
while (pFeature != null)
{
ITopologicalOperator pTopologicalOperator = pFeature.ShapeCopy as ITopologicalOperator;
pFeature.Shape = pTopologicalOperator.Difference(pGeometry);
pFeature.Store();
pFeature = pFeatureCursor.NextFeature();
}
// 刷新地图
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
axMapControl1.ActiveView.Refresh();
}
}
}