【问题标题】:Check if a polygon point is inside another in leaflet检查一个多边形点是否在传单中的另一个内部
【发布时间】:2017-04-29 12:21:33
【问题描述】:

我从传单 geoJSON 地图中选择了两组多边形坐标。 父子坐标是坐标是:

var parentCoordinates=[
    [
        32.05898221582174,
        -28.31004731142091
    ],
    [
        32.05898221582174,
        -28.308044824292978
    ],
    [
        32.06134255975485,
        -28.308044824292978
    ],
    [
        32.06134255975485,
        -28.31004731142091
    ],
    [
        32.05898221582174,
        -28.31004731142091
    ]
]
var childCoordinates=[
  [
    32.059904895722866,
    -28.30970726909422
  ],
  [
    32.059904895722866,
    -28.308743809931784
  ],
  [
    32.06089194864035,
    -28.308743809931784
  ],
  [
    32.06089194864035,
    -28.30970726909422
  ],
  [
    32.059904895722866,
    -28.30970726909422
  ]
]

child在父区域内绘制如图:

使用Ray Casting algorithm 来确定该点是否位于多边形内,我无法确定,因为我得到的结果是错误的。 请让我知道我做错了什么或任何其他方式来确定解决方案。谢谢

【问题讨论】:

  • 注意:对于多边形内的折线,其工作正常。不适用于多边形内的多边形(如图所示)

标签: javascript angularjs leaflet polygon angular-leaflet-directive


【解决方案1】:

我对@9​​87654321@ 有很好的体验。它运作良好,有据可查,并且示例已经与传单一起显示。

对于您的问题,您可以使用turf.withinparentCoordinates 作为turf.polygonchildCoordinates 作为turf.point 的数组:

var parentPolygon = turf.polygon([parentCoordinates]);

var inside = true;
childCoordinates.forEach(function(coordinates) {
    point = turf.point(coordinates);
    if (!turf.inside(point, parentPolygon)){
      alert("Oh no! "+ coordinates + " isn't in polygon");
      inside = false;
    }
});

alert("Child polygon inside parent polygon ? " + inside);

Here 是一个 Fiddle 示例。

【讨论】:

    【解决方案2】:

    我尝试了你的算法和另一个在这里找到的算法 https://rosettacode.org/wiki/Ray-casting_algorithm 并且都返回了正确的值。

    也许这个小提琴可以帮助你实现:

    https://jsfiddle.net/4psL2hoo/1/

    您的算法

    // Data
    var parentCoordinates=[
        [
            32.05898221582174,
            -28.31004731142091
        ],
        [
            32.05898221582174,
            -28.308044824292978
        ],
        [
            32.06134255975485,
            -28.308044824292978
        ],
        [
            32.06134255975485,
            -28.31004731142091
        ],
        [
            32.05898221582174,
            -28.31004731142091
        ]
    ]
    var childCoordinates=[
      [
        32.059904895722866,
        -28.30970726909422
      ],
      [
        32.059904895722866,
        -28.308743809931784
      ],
      [
        32.06089194864035,
        -28.308743809931784
      ],
      [
        32.06089194864035,
        -28.30970726909422
      ],
      [
        32.059904895722866,
        -28.30970726909422
      ]
    ]
    
    // Other algo
    function test(point, vs) {
        // ray-casting algorithm based on
        // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
    
        var x = point[0], y = point[1];
    
        var inside = false;
        for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
            var xi = vs[i][0], yi = vs[i][1];
            var xj = vs[j][0], yj = vs[j][1];
    
            var intersect = ((yi > y) != (yj > y))
                && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) inside = !inside;
        }
    
        return inside;
    };
    
    for (var i = 0; i < childCoordinates.length; i++) {
         var testPoint = childCoordinates[i];
         console.log(JSON.stringify(testPoint) + '\tin parentCoordinate\t' + test(testPoint, parentCoordinates));
    }
    

    Rosetta 代码算法

    //https://rosettacode.org/wiki/Ray-casting_algorithm
    function contains(bounds, lat, lng) {
        //https://rosettacode.org/wiki/Ray-casting_algorithm
        var count = 0;
        for (var b = 0; b < bounds.length; b++) {
            var vertex1 = bounds[b];
            var vertex2 = bounds[(b + 1) % bounds.length];
            if (west(vertex1, vertex2, lng, lat))
                ++count;
        }
        return count % 2;
    
        /**
         * @return {boolean} true if (x,y) is west of the line segment connecting A and B
         */
        function west(A, B, x, y) {
            if (A.y <= B.y) {
                if (y <= A.y || y > B.y ||
                    x >= A.x && x >= B.x) {
                    return false;
                } else if (x < A.x && x < B.x) {
                    return true;
                } else {
                    return (y - A.y) / (x - A.x) > (B.y - A.y) / (B.x - A.x);
                }
            } else {
                return west(B, A, x, y);
            }
        }
    }
    
    var square = {name: 'square', bounds: [{x: 32.05898221582174, y: -28.31004731142091}, {x: 32.05898221582174, y: -28.308044824292978}, {x: 32.06134255975485, y: -28.308044824292978}, {x: 32.06134255975485, y: -28.31004731142091}]};
    
    var shapes = [square];
    var testPoints = [{lng: 32.059904895722866, lat: -28.30970726909422}, {lng: 32.059904895722866, lat: -28.308743809931784}, {lng: 32.06089194864035, lat: -28.308743809931784},
        {lng: 32.06089194864035, lat: -28.30970726909422}];
    
    for (var s = 0; s < shapes.length; s++) {
        var shape = shapes[s];
        for (var tp = 0; tp < testPoints.length; tp++) {
            var testPoint = testPoints[tp];
            console.log(JSON.stringify(testPoint) + '\tin ' + shape.name + '\t' + contains(shape.bounds, testPoint.lat, testPoint.lng));
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用 Leaflet 的 api - contains 。您使用 LatLngBounds 创建一个父多边形,然后也创建一个子多边形。

      parentPolygon.contains(childPolygon)
      

      【讨论】:

      • LatLngBounds 创建一个矩形;海报需要一般的多边形。投反对票。
      猜你喜欢
      • 2019-08-23
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2013-03-29
      • 2015-10-25
      相关资源
      最近更新 更多