Skip to content

Commit

Permalink
Multi Polygon function
Browse files Browse the repository at this point in the history
  • Loading branch information
abenrob committed Mar 11, 2014
1 parent 058448f commit 5edaf7e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions geojson-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 5edaf7e

Please sign in to comment.