arcgis engine在地图上任意画两个多边形,分别实现两个多边形的逻辑运算(交集,并集,差集)代码
时间: 2024-04-22 22:23:29 浏览: 138
以下是示例代码,实现了在 ArcGIS Engine 上任意画两个多边形,并对它们进行交集、并集、差集运算:
```csharp
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PolygonOperations
{
public partial class MainForm : Form
{
private IToolbarMenu m_menu;
private IGraphicsContainer m_graphicsContainer;
private IActiveView m_activeView;
private IPolygon m_polygon1;
private IPolygon m_polygon2;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 初始化 ArcGIS Engine 控件
axMapControl1.LoadMxFile(@"C:\data\map.mxd");
m_graphicsContainer = axMapControl1.ActiveView.GraphicsContainer;
m_activeView = axMapControl1.ActiveView;
// 创建一个右键菜单,包含三个子菜单:交集、并集、差集
m_menu = new ToolbarMenuClass();
m_menu.AddItem(new IntersectMenuItem(this));
m_menu.AddItem(new UnionMenuItem(this));
m_menu.AddItem(new DifferenceMenuItem(this));
}
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
// 鼠标左键单击画多边形
if (e.button == 1)
{
if (m_polygon1 == null)
{
m_polygon1 = DrawPolygon();
}
else if (m_polygon2 == null)
{
m_polygon2 = DrawPolygon();
}
}
// 鼠标右键单击打开右键菜单
else if (e.button == 2)
{
m_menu.PopupMenu(e.x, e.y, axMapControl1.hWnd);
}
}
private IPolygon DrawPolygon()
{
// 在地图上画多边形
IGeometry geometry = axMapControl1.TrackPolygon();
if (geometry == null)
return null;
// 设置多边形的符号
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = GetRandomColor();
ISymbol symbol = fillSymbol as ISymbol;
symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
// 把多边形添加到图形容器中
IElement element = new PolygonElementClass();
element.Geometry = geometry;
element.Symbol = symbol;
m_graphicsContainer.AddElement(element, 0);
// 刷新地图显示
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
return geometry as IPolygon;
}
private Color GetRandomColor()
{
// 获取随机颜色
Random rand = new Random();
return Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
}
public void Intersect()
{
// 计算两个多边形的交集
if (m_polygon1 == null || m_polygon2 == null)
return;
ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator;
IGeometry result = topoOp.Intersect(m_polygon2, esriGeometryDimension.esriGeometry2Dimension);
ShowResult(result);
}
public void Union()
{
// 计算两个多边形的并集
if (m_polygon1 == null || m_polygon2 == null)
return;
ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator;
IGeometry result = topoOp.Union(m_polygon2);
ShowResult(result);
}
public void Difference()
{
// 计算两个多边形的差集
if (m_polygon1 == null || m_polygon2 == null)
return;
ITopologicalOperator topoOp = m_polygon1 as ITopologicalOperator;
IGeometry result = topoOp.Difference(m_polygon2);
ShowResult(result);
}
private void ShowResult(IGeometry geometry)
{
// 显示运算结果
if (geometry == null)
return;
IElement element = new PolygonElementClass();
element.Geometry = geometry;
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
fillSymbol.Color = Color.Yellow;
ISymbol symbol = fillSymbol as ISymbol;
symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
element.Symbol = symbol;
m_graphicsContainer.AddElement(element, 0);
m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
public class IntersectMenuItem : IToolbarMenuEvent
{
private MainForm m_form;
public IntersectMenuItem(MainForm form)
{
m_form = form;
}
public void OnItemClick()
{
m_form.Intersect();
}
}
public class UnionMenuItem : IToolbarMenuEvent
{
private MainForm m_form;
public UnionMenuItem(MainForm form)
{
m_form = form;
}
public void OnItemClick()
{
m_form.Union();
}
}
public class DifferenceMenuItem : IToolbarMenuEvent
{
private MainForm m_form;
public DifferenceMenuItem(MainForm form)
{
m_form = form;
}
public void OnItemClick()
{
m_form.Difference();
}
}
}
```
代码中定义了一个 MainForm 类,包含一个 ArcGIS Engine 控件和一个右键菜单,用于实现多边形运算。在 MainForm_Load 方法中初始化 ArcGIS Engine 控件和右键菜单,在 axMapControl1_OnMouseDown 方法中实现画多边形的功能,在 Intersect、Union、Difference 方法中分别计算多边形的交集、并集、差集,在 ShowResult 方法中显示运算结果。
在 IntersectMenuItem、UnionMenuItem、DifferenceMenuItem 类中分别实现右键菜单中的子菜单,用于调用 MainForm 中的 Intersect、Union、Difference 方法。
阅读全文
相关推荐

















