1、前言
在ArcEngine
中,线段的分割主要使用IFeatureEdit
接口的Split
方法实现。需要注意的是:该方法只能将1
条线段按距离或按比例分割成 2
条线段,关于如何将1
条线段分成n
条线段(n >= 2)
的方法我会在下一篇博客介绍,下面给出实现代码。
2、线段分割
2.1、主界面代码
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Windows.Forms;
using WindowsFormsApplication1.Command;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
btnSelect.Enabled = false;
btnSplitByDistance.Enabled = false;
btnSplitByRatio.Enabled = false;
}
// 开始编辑
private void btnStartEditing_Click(object sender, EventArgs e)
{
ICommand command = new ControlsEditingStartCommand();
command.OnCreate(axMapControl1.Object);
command.OnClick();
btnStartEditing.Enabled = false;
btnStopEditing.Enabled = true;
btnSelect.Enabled = true;
btnSplitByDistance.Enabled = true;
btnSplitByRatio.Enabled = true;
}
// 结束编辑
private void btnStopEditing_Click(object sender, EventArgs e)
{
ICommand saveCommand = new ControlsEditingSaveCommand();
saveCommand.OnCreate(axMapControl1.Object);
saveCommand.OnClick();
ICommand clearSelectionCommand = new ControlsClearSelectionCommand();
clearSelectionCommand.OnCreate(axMapControl1.Object);
clearSelectionCommand.OnClick();
btnStartEditing.Enabled = true;
btnStopEditing.Enabled = false;
btnSelect.Enabled = false;
btnSplitByDistance.Enabled = false;
btnSplitByRatio.Enabled = false;
}
// 选择要素
private void btnSelect_Click(object sender, EventArgs e)
{
ICommand command = new ControlsSelectFeaturesTool();
command.OnCreate(axMapControl1.Object);
axMapControl1.CurrentTool = command as ITool;
}
// 距离分割
private void btnSplitByDistance_Click(object sender, EventArgs e)
{
ICommand command = new SplitByDistanceCommand();
command.OnCreate(axMapControl1.Object);
command.OnClick();
}
// 比例分割
private void btnSplitByRatio_Click(object sender, EventArgs e)
{
ICommand command = new SplitByRatioCommand();
command.OnCreate(axMapControl1.Object);
command.OnClick();
}
}
}
2.2、按距离分割
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1.Command
{
/// <summary>
/// Summary description for SplitByDistanceCommand.
/// </summary>
[Guid("b8a71064-25d0-4816-9b9a-0f3738ddf981")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Command.SplitByDistanceCommand")]
public sealed class SplitByDistanceCommand : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
public SplitByDistanceCommand()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = ""; //localizable text
base.m_message = ""; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
if (m_hookHelper.FocusMap.SelectionCount != 1)
{
MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取被选择的要素
IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
pEnumFeature.Reset();
IFeature pFeature = pEnumFeature.Next();
if (pFeature.Shape is IPoint || pFeature.Shape is IPolygon)
{
MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取分割点
double distance = 50;
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
pPolyline.QueryPoint(esriSegmentExtension.esriNoExtension, distance, false, pPoint);
// 分割要素
IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
pFeatureEdit.Split(pPoint);
m_hookHelper.ActiveView.Refresh();
}
#endregion
}
}
上面的代码按照50米
的距离对线段进行分割,运行结果如下图所示:
2.2、按比例分割
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1.Command
{
/// <summary>
/// Summary description for SplitByRatioCommand.
/// </summary>
[Guid("d4624838-4abc-458e-b6cf-fb5d78562e96")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("WindowsFormsApplication1.Command.SplitByRatioCommand")]
public sealed class SplitByRatioCommand : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
#endregion
#endregion
private IHookHelper m_hookHelper;
public SplitByRatioCommand()
{
//
// TODO: Define values for the public properties
//
base.m_category = ""; //localizable text
base.m_caption = ""; //localizable text
base.m_message = ""; //localizable text
base.m_toolTip = ""; //localizable text
base.m_name = ""; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
if (m_hookHelper.FocusMap.SelectionCount != 1)
{
MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取被选择的要素
IEnumFeature pEnumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature;
pEnumFeature.Reset();
IFeature pFeature = pEnumFeature.Next();
if (pFeature.Shape is IPoint || pFeature.Shape is IPolygon)
{
MessageBox.Show("请选择1条线要素进行分割", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取分割点
double ratio = 0.3;
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
IPolyline pPolyline = pFeature.ShapeCopy as IPolyline;
pPolyline.QueryPoint(esriSegmentExtension.esriNoExtension, ratio, true, pPoint);
// 分割要素
IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
pFeatureEdit.Split(pPoint);
m_hookHelper.ActiveView.Refresh();
}
#endregion
}
}
上面的代码按照30%
的比例对线段进行分割,运行结果如下图所示: