【JAVA Geometry 笔记】01 - Geometry 交集、并集、差集

Geometry 取交集

两个图形取交集:
FFFFF,t_0,g_se,x_16)

用图画出来就是这样的,红色的地方就是取出来的交集图形
在这里插入图片描述

Geometry 取并集

两个图形取交集:

在这里插入图片描述

用图画出来就是这样的,红色的地方就是取出来的并集图形

在这里插入图片描述

Geometry 取差集

两个图形取差集:

在这里插入图片描述

用图画出来就是这样的,红色的地方就是取出来的差集图形

在这里插入图片描述





End


以下是示例代码,实现了在 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 方法。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值