9

我有兩套從單張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 
    ] 
] 

孩子是父母區域內繪製圖片所示: enter image description here

使用Ray Casting algorithm,以確定是否點位於內多邊形我「M無法確定,因爲我得到的結果是錯誤的。 請讓我知道我在做什麼錯或任何其他方式來確定解決方案。謝謝

+0

注意:對於內部多邊形折線其工作正常。不爲內多邊形(如圖所示)多邊形工作 – forgottofly

回答

4

我試着用你的算法和另一個發現這裏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)); 
} 

羅塞塔碼算法中

//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)); 
    } 
} 
4

我有Turf的良好經驗。它運作良好,有很好的文檔記錄,並且已經用小冊子展示了這些例子。

對於你的問題,你可以使用turf.withinparentCoordinatesturf.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的小提琴例子。

1

您可以嘗試傳單的API - contains。您可以使用LatLngBounds創建一個父多邊形,然後再創建一個子多邊形。

parentPolygon.contains(childPolygon) 
+0

的LatLngBounds建立一個矩形;海報需要一般的多邊形。 Downvoted。 –

相關問題