Multi Polygon function

This commit is contained in:
abenrob 2014-03-10 23:52:08 -07:00
parent 058448fb55
commit 5edaf7e12f
2 changed files with 39 additions and 0 deletions

View File

@ -105,6 +105,34 @@
return insidePoly 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) { gju.numberToRadius = function (number) {
return number * Math.PI / 180; return number * Math.PI / 180;
} }

11
test.js
View File

@ -50,4 +50,15 @@ poly = {"type": "Polygon", "coordinates": [[[0, 2], [2, 2], [2,0]]]};
if (gju.pointInPolygon(point,poly)) throw new Error(); 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') console.log('all passed')