private void toolStripButton45_Click(object sender, EventArgs e) { //存在Bug,合并丢失现象 if (mapMain.Layers.Count == 0) { return; } //查看与选中要素重叠的要素 PolygonLayer pLayer = Lzq_LayerManager.getLayerByName(layerNamePolygon, mapMain) as PolygonLayer; if (pLayer == null) { MessageBox.Show(@"未找到具体图层:" + layerNamePolygon, @"系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } FeatureSet fs = null; fs = (FeatureSet)pLayer.DataSet; ; if (pLayer.Selection.Count <2) { MessageBox.Show(@"图层至少要选中两个要素:" + pLayer.Selection.Count, @"系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //防止有合并后有悬挂线,先将面图元进行正值缓冲,合并后,将图元负值缓冲 double buffer = 0.001; //容差 ////获取第一个被选中要素 IFeature pFeature = (Feature)pLayer.Selection.ToFeatureList()[0]; NetTopologySuite.Geometries.Geometry gm1 = (NetTopologySuite.Geometries.Geometry)pFeature.Geometry; Geometry g1 = (Geometry)pFeature.Geometry.Clone(); Geometry g1Buff = (Geometry)g1.Buffer(buffer); //遍历选择级,找到重叠要素 for (int j = 1; j < pLayer.Selection.Count; j++) { IFeature pFeature2 = (Feature)pLayer.Selection.ToFeatureList()[j]; Geometry gm2 = (Geometry)pFeature2.Geometry.Clone(); CoordinateArrays.RemoveRepeatedPoints(gm2.Coordinates); Geometry gm2Buff = (Geometry)gm2.Buffer(buffer); Geometry p = (Geometry)g1Buff.Union(gm2Buff); Console.WriteLine(p.IsSimple.ToString()); if (p.IsSimple) { CoordinateArrays.RemoveRepeatedPoints(p.Coordinates); p.Normalize(); pFeature.Geometry = p.Buffer(-1 * buffer); fs.Features.Remove(pFeature2); ClassBase.IndexError(pLayer, mapMain); } } fs.Save(); } public static void IndexError(DotSpatial.Symbology.IFeatureLayer fLayer, DotSpatial.Controls.Map _map) { fLayer.DataSet.UpdateExtent(); fLayer.DataSet.InitializeVertices(); if (fLayer.LabelLayer != null) { fLayer.LabelLayer.CreateLabels(); } fLayer.AssignFastDrawnStates(); if (_map != null) { _map.Refresh(); _map.ResetBuffer(); } } public static Layer getLayerByName(string layerName, Map mainMap) { Layer layer = null; foreach (FeatureLayer featureLayer in mainMap.Layers) { if (featureLayer.Name == layerName) { layer = featureLayer; } } return layer; }
选中两个以上元素,只能合并两个,目前没解决此处bug,应该是
ClassBase.IndexError(pLayer, mapMain);操作后将selection 清空导致,有研究过的帮忙 解答一下,谢谢!
原文地址:https://www.cnblogs.com/kogame/p/12503582.html
时间: 2024-10-10 17:24:16