winfrom 插件DotSpatial 在GIS中添加套索选择地图区域

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 &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pandi18

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值