using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using Point = System.Drawing.Point;
using SelectionMode = DotSpatial.Symbology.SelectionMode;
namespace DotSpatial.Controls
{
/// <summary>
/// 使用Extension扩展map,并拷贝部分源码,以达到不侵入性修改源代码的作用,还有一种,使用partial class的方式将修改的源码保留在此文件中,这里暂只支持polygon,其余Geometry另外支持
/// </summary>
public static class MapSelectableExtension
{
public static bool SelectedByPolygon(this IMap map, Polygon polygon, SelectionMode mode, out IEnvelope affectedArea)
{
affectedArea = new Envelope();
if (map.MapFrame == null) return false;
return map.MapFrame.SelectedByPolygon(polygon, mode, out affectedArea);
}
public static bool SelectedByPolygon(this IMapFrame mapframe, Polygon polygon, SelectionMode mode, out IEnvelope affectedArea)
{
affectedArea = new Envelope();
if (!mapframe.SelectionEnabled) return false;
bool somethingChanged = false;
mapframe.MapFrame.SuspendEvents();
foreach (var s in mapframe.GetLayers()
.Reverse()
.Where(_ => _.SelectionEnabled && _.IsVisible))
{
//这里只能识别featurelayer图层,其它图层不能处理
if (s is FeatureLayer)
{
IEnvelope layerArea;
if (((FeatureLayer)s).SelectedByPolygon(polygon, mode, out layerArea))
somethingChanged = true;
affectedArea.ExpandToInclude(layerArea);
}
}
mapframe.MapFrame.ResumeEvents();
return somethingChanged;
}
}
/// <summary>
/// 多边形选择的扩展
/// </summary>
public static class FeatureLayerSelectByPolygonExtension
{
public static bool SelectedByPolygon(this FeatureLayer lyr, Polygon polygon, SelectionMode mode, out IEnvelope affectedArea)
{
if (!lyr.DrawnStatesNeeded && !lyr.EditMode)
lyr.AssignFastDrawnStates();
var tolerant = polygon.Envelope;
var strict = polygon.Envelope;
IEnvelope region = tolerant;
if (lyr.DataSet.FeatureType == FeatureType.Polygon)
region = strict;
affectedArea = lyr.Selection.Envelope;
bool changed &