【问题标题】:How to detect if polygons overlap in leaflet如何检测传单中的多边形是否重叠
【发布时间】:2014-11-12 06:47:54
【问题描述】:

我正在使用传单绘制来创建多边形。 我的要求是当用户绘制多边形时,它不应与现有多边形相交/重叠。 我已经使用多边形传单中的点来检测点是否在多边形内并且它正在工作, 但问题是我无法检测一条线是否与另一个多边形相交。在这种情况下,点位于现有多边形之外,但存在重叠。

下面的图片可以提供更好的图片!

【问题讨论】:

    标签: polygon leaflet overlapping


    【解决方案1】:

    在leaflet.draw 中,您可以将选项传递给各个绘图处理程序。其中一个allowIntersection 是“确定线段是否可以交叉”的布尔类型。

    您的选项将如下所示(摘自leaflet.draw example config):

    var options = {
        position: 'topright',
        draw: {
            polygon: {
                allowIntersection: false, // Restricts shapes to simple polygons
                drawError: {
                    color: '#e1e100', // Color the shape will turn when intersects
                    message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect
                },
                shapeOptions: {
                    color: '#bada55'
                }
            },
        },
        edit: {
            featureGroup: editableLayers, //REQUIRED!!
            remove: false
        }
    };
    
    var drawControl = new L.Control.Draw(options);
    map.addControl(drawControl);
    

    【讨论】:

    • 不幸的是,allowIntersection 似乎还不能与 Editable 一起使用。
    • allowIntersection 是一个布尔值,用于控制多边形与其自身的交集,而不是与其他多边形的交集。
    【解决方案2】:

    您也可以使用Turf.js。 Turf 是 Mapbox 开发的一个新的 js GIS 引擎。

    这里有几个例子可以完成这项工作:

    【讨论】:

      【解决方案3】:

      您也可以使用Terraformer Core,这是一个由 ESRI 人员完成的开源项目。你可能会这样做:

      var polygon = new Terraformer.Primitive({ 
        "type": "Polygon", 
        "coordinates": [ [ [ -101.46972656250001, 34.125447565116126 ], 
                           [ -100.85449218750001, 33.760882000869195 ], 
                           [ -101.09619140625, 33.19273094190692 ], 
                           [ -102.12890625000001, 33.137551192346145], 
                           [ -102.19482421875, 33.63291573870479 ], 
                           [ -101.46972656250001, 34.125447565116126 ] ] ] }); 
      var intersects = polygon.intersects({ 
        "type": "Polygon", 
        "coordinates": [ [ [ -103.32539, 30.58608 ], [ -102.32083, 29.87894 ], [ -103.32539, 30.58608 ] ] ] });
      // intersects is a bool.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        • 2021-01-09
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        相关资源
        最近更新 更多