Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added function for type MultiPolygon #18

Merged
merged 2 commits into from

2 participants

@abenrob

Added new function, added new test.
Doesn't attempt to deal with donut polygons...

@maxogden maxogden merged commit b16514d into maxogden:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2014
  1. @abenrob

    Merge pull request #1 from maxogden/master

    abenrob authored
    updating fork
  2. @abenrob

    Multi Polygon function

    abenrob authored
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 0 deletions.
  1. +28 −0 geojson-utils.js
  2. +11 −0 test.js
View
28 geojson-utils.js
@@ -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;
}
View
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')
Something went wrong with that request. Please try again.