turf-inside
Advanced tools
Comparing version 1.1.4 to 3.0.0-canary.2f5f7167
var inside = require('./'); | ||
var Benchmark = require('benchmark'); | ||
var fs = require('fs'); | ||
var point = require('turf-point'); | ||
var polygon = require('turf-polygon'); | ||
var point = require('turf-helpers').point; | ||
var polygon = require('turf-helpers').polygon; | ||
@@ -7,0 +7,0 @@ var poly = polygon([[[0,0], [0,100], [100,100], [100,0]]]); |
78
index.js
@@ -0,1 +1,3 @@ | ||
var invariant = require('turf-invariant'); | ||
// http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule | ||
@@ -6,10 +8,9 @@ // modified from: https://github.com/substack/point-in-polygon/blob/master/index.js | ||
/** | ||
* Takes a {@link Point} feature and a {@link Polygon} feature and determines if the Point resides inside the Polygon. The Polygon can | ||
* be convex or concave. The function accepts any valid Polygon or {@link MultiPolygon} | ||
* and accounts for holes. | ||
* Takes a {@link Point} and a {@link Polygon} or {@link MultiPolygon} and determines if the point resides inside the polygon. The polygon can | ||
* be convex or concave. The function accounts for holes. | ||
* | ||
* @module turf/inside | ||
* @name inside | ||
* @category joins | ||
* @param {Point} point a Point feature | ||
* @param {Polygon} polygon a Polygon feature | ||
* @param {Feature<Point>} point input point | ||
* @param {Feature<(Polygon|MultiPolygon)>} polygon input polygon or multipolygon | ||
* @return {Boolean} `true` if the Point is inside the Polygon; `false` if the Point is not inside the Polygon | ||
@@ -65,42 +66,37 @@ * @example | ||
*/ | ||
module.exports = function(point, polygon) { | ||
var polys = polygon.geometry.coordinates; | ||
var pt = [point.geometry.coordinates[0], point.geometry.coordinates[1]]; | ||
// normalize to multipolygon | ||
if(polygon.geometry.type === 'Polygon') polys = [polys]; | ||
module.exports = function input(point, polygon) { | ||
var pt = invariant.getCoord(point); | ||
var polys = polygon.geometry.coordinates; | ||
// normalize to multipolygon | ||
if (polygon.geometry.type === 'Polygon') polys = [polys]; | ||
var insidePoly = false; | ||
var i = 0; | ||
while (i < polys.length && !insidePoly) { | ||
// check if it is in the outer ring first | ||
if(inRing(pt, polys[i][0])) { | ||
var inHole = false; | ||
var k = 1; | ||
// check for the point in any of the holes | ||
while(k < polys[i].length && !inHole) { | ||
if(inRing(pt, polys[i][k])) { | ||
inHole = true; | ||
for (var i = 0, insidePoly = false; i < polys.length && !insidePoly; i++) { | ||
// check if it is in the outer ring first | ||
if (inRing(pt, polys[i][0])) { | ||
var inHole = false; | ||
var k = 1; | ||
// check for the point in any of the holes | ||
while (k < polys[i].length && !inHole) { | ||
if (inRing(pt, polys[i][k])) { | ||
inHole = true; | ||
} | ||
k++; | ||
} | ||
if (!inHole) insidePoly = true; | ||
} | ||
k++; | ||
} | ||
if(!inHole) insidePoly = true; | ||
} | ||
i++; | ||
} | ||
return insidePoly; | ||
} | ||
return insidePoly; | ||
}; | ||
// pt is [x,y] and ring is [[x,y], [x,y],..] | ||
function inRing (pt, ring) { | ||
var isInside = false; | ||
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | ||
var xi = ring[i][0], yi = ring[i][1]; | ||
var xj = ring[j][0], yj = ring[j][1]; | ||
var intersect = ((yi > pt[1]) != (yj > pt[1])) | ||
&& (pt[0] < (xj - xi) * (pt[1] - yi) / (yj - yi) + xi); | ||
if (intersect) isInside = !isInside; | ||
} | ||
return isInside; | ||
function inRing(pt, ring) { | ||
var isInside = false; | ||
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | ||
var xi = ring[i][0], yi = ring[i][1]; | ||
var xj = ring[j][0], yj = ring[j][1]; | ||
var intersect = ((yi > pt[1]) !== (yj > pt[1])) && | ||
(pt[0] < (xj - xi) * (pt[1] - yi) / (yj - yi) + xi); | ||
if (intersect) isInside = !isInside; | ||
} | ||
return isInside; | ||
} | ||
{ | ||
"name": "turf-inside", | ||
"version": "1.1.4", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "turf inside module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "tape test.js" | ||
}, | ||
@@ -28,5 +27,4 @@ "repository": { | ||
"homepage": "https://github.com/Turfjs/turf-inside", | ||
"bin": {}, | ||
"dependencies": { | ||
"minimist": "^1.1.0" | ||
"turf-invariant": "^3.0.0-canary.2f5f7167" | ||
}, | ||
@@ -36,7 +34,4 @@ "devDependencies": { | ||
"tape": "^3.5.0", | ||
"turf-point": "^2.0.0", | ||
"turf-polygon": "^1.0.2", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3" | ||
"turf-helpers": "^3.0.0-canary.2f5f7167" | ||
} | ||
} |
@@ -10,5 +10,4 @@ # turf-inside | ||
Checks to see if a Point is inside of a Polygon. The Polygon can | ||
be convex or concave. The function accepts any valid Polygon or MultiPolygon | ||
and accounts for holes. | ||
Takes a Point and a Polygon or MultiPolygon and determines if the point resides inside the polygon. The polygon can | ||
be convex or concave. The function accounts for holes. | ||
@@ -18,6 +17,6 @@ | ||
| parameter | type | description | | ||
| --------- | ------- | ----------------- | | ||
| `point` | Point | a Point feature | | ||
| `polygon` | Polygon | a Polygon feature | | ||
| parameter | type | description | | ||
| --------- | --------------------------------- | ----------------------------- | | ||
| `point` | Feature\.\<Point\> | input point | | ||
| `polygon` | Feature\.\<Polygon|MultiPolygon\> | input polygon or multipolygon | | ||
@@ -28,13 +27,42 @@ | ||
```js | ||
var pt1 = turf.point([-111.467285, 40.75766], {'marker-color': "#f00"}); | ||
var pt2 = turf.point([-111.873779, 40.647303], {'marker-color': "#0f0" }); | ||
var poly = turf.polygon([[ | ||
[-112.074279, 40.52215], | ||
[-112.074279, 40.853293], | ||
[-111.610107, 40.853293], | ||
[-111.610107, 40.52215], | ||
[-112.074279, 40.52215] | ||
]]); | ||
var features = turf.featurecollection([pt1, pt2, poly]); | ||
var pt1 = { | ||
"type": "Feature", | ||
"properties": { | ||
"marker-color": "#f00" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-111.467285, 40.75766] | ||
} | ||
}; | ||
var pt2 = { | ||
"type": "Feature", | ||
"properties": { | ||
"marker-color": "#0f0" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-111.873779, 40.647303] | ||
} | ||
}; | ||
var poly = { | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [[ | ||
[-112.074279, 40.52215], | ||
[-112.074279, 40.853293], | ||
[-111.610107, 40.853293], | ||
[-111.610107, 40.52215], | ||
[-112.074279, 40.52215] | ||
]] | ||
} | ||
}; | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [pt1, pt2, poly] | ||
}; | ||
//=features | ||
@@ -49,2 +77,5 @@ | ||
**Returns** `Boolean`, `true` if the Point is inside the Polygon; `false` if the Point is not inside the Polygon | ||
## Installation | ||
@@ -64,1 +95,2 @@ | ||
18
test.js
var test = require('tape'); | ||
var inside = require('./'); | ||
var point = require('turf-point'); | ||
var polygon = require('turf-polygon'); | ||
var point = require('turf-helpers').point; | ||
var polygon = require('turf-helpers').polygon; | ||
var fs = require('fs'); | ||
test('bad type', function (t) { | ||
var poly = polygon([[[0,0], [0,100], [100,100], [100,0], [0,0]]]); | ||
t.throws(function() { | ||
inside(poly, poly); | ||
}, /A coordinate, feature, or point geometry is required/); | ||
t.end(); | ||
}); | ||
test('featureCollection', function (t) { | ||
@@ -31,3 +41,3 @@ // test for a simple polygon | ||
var ptOutsidePoly = point([-86.75079345703125, 36.18527313913089]); | ||
var polyHole = JSON.parse(fs.readFileSync(__dirname + '/fixtures/poly-with-hole.geojson')); | ||
var polyHole = JSON.parse(fs.readFileSync(__dirname + '/test/poly-with-hole.geojson')); | ||
@@ -46,3 +56,3 @@ t.false(inside(ptInHole, polyHole)); | ||
var ptOutsidePoly = point([-86.75302505493164, 36.23015046460186]); | ||
var multiPolyHole = JSON.parse(fs.readFileSync(__dirname + '/fixtures/multipoly-with-hole.geojson')); | ||
var multiPolyHole = JSON.parse(fs.readFileSync(__dirname + '/test/multipoly-with-hole.geojson')); | ||
@@ -49,0 +59,0 @@ t.false(inside(ptInHole, multiPolyHole)); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
3
0
92
9387
7
164
1
1
+ Addedturf-invariant@3.0.12(transitive)
- Removedminimist@^1.1.0
- Removedminimist@1.2.8(transitive)