mirror of
https://github.com/maxogden/geojson-js-utils.git
synced 2025-12-08 18:19:11 +00:00
Multi Polygon function
This commit is contained in:
parent
058448fb55
commit
5edaf7e12f
@ -105,6 +105,34 @@
|
||||
return insidePoly
|
||||
}
|
||||
|
||||
// support multi (but not donut) polygons
|
||||
gju.pointInMultiPolygon = function (p, poly) {
|
||||
var coords_array = (poly.type == "MultiPolygon") ? [ poly.coordinates ] : poly.coordinates
|
||||
|
||||
var insideBox = false
|
||||
var insidePoly = false
|
||||
for (var i = 0; i < coords_array.length; i++){
|
||||
var coords = coords_array[i];
|
||||
for (var j = 0; j < coords.length; j++) {
|
||||
if (!insideBox){
|
||||
if (gju.pointInBoundingBox(p, boundingBoxAroundPolyCoords(coords[j]))) {
|
||||
insideBox = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!insideBox) return false
|
||||
for (var j = 0; j < coords.length; j++) {
|
||||
if (!insidePoly){
|
||||
if (pnpoly(p.coordinates[1], p.coordinates[0], coords[j])) {
|
||||
insidePoly = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return insidePoly
|
||||
}
|
||||
|
||||
gju.numberToRadius = function (number) {
|
||||
return number * Math.PI / 180;
|
||||
}
|
||||
|
||||
11
test.js
11
test.js
@ -50,4 +50,15 @@ poly = {"type": "Polygon", "coordinates": [[[0, 2], [2, 2], [2,0]]]};
|
||||
|
||||
if (gju.pointInPolygon(point,poly)) throw new Error();
|
||||
|
||||
var singlepoint = {"type": "Point", "coordinates": [-1, -1]};
|
||||
var multipoly = {"type": "MultiPolygon",
|
||||
"coordinates": [
|
||||
[ [ [0,0],[0,10],[10,10],[10,0],[0,0] ] ] ,
|
||||
[ [ [10,10],[10,20],[20,20],[20,10],[10,10] ] ]
|
||||
]
|
||||
};
|
||||
|
||||
if (!gju.pointInMultiPolygon(point,multipoly)) throw new Error();
|
||||
if (gju.pointInMultiPolygon(singlepoint,multipoly)) throw new Error();
|
||||
|
||||
console.log('all passed')
|
||||
Loading…
x
Reference in New Issue
Block a user