@turf/boolean-point-in-polygon
Advanced tools
Comparing version 6.5.0 to 7.0.0-alpha.0
@@ -0,1 +1,2 @@ | ||
import pip from "point-in-polygon-hao"; | ||
import { getCoord, getGeom } from "@turf/invariant"; | ||
@@ -29,4 +30,3 @@ // http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule | ||
*/ | ||
export default function booleanPointInPolygon(point, polygon, options) { | ||
if (options === void 0) { options = {}; } | ||
export default function booleanPointInPolygon(point, polygon, options = {}) { | ||
// validation | ||
@@ -39,7 +39,7 @@ if (!point) { | ||
} | ||
var pt = getCoord(point); | ||
var geom = getGeom(polygon); | ||
var type = geom.type; | ||
var bbox = polygon.bbox; | ||
var polys = geom.coordinates; | ||
const pt = getCoord(point); | ||
const geom = getGeom(polygon); | ||
const type = geom.type; | ||
const bbox = polygon.bbox; | ||
let polys = geom.coordinates; | ||
// Quick elimination if point is not inside bbox | ||
@@ -49,61 +49,16 @@ if (bbox && inBBox(pt, bbox) === false) { | ||
} | ||
// normalize to multipolygon | ||
if (type === "Polygon") { | ||
polys = [polys]; | ||
} | ||
var insidePoly = false; | ||
for (var i = 0; i < polys.length && !insidePoly; i++) { | ||
// check if it is in the outer ring first | ||
if (inRing(pt, polys[i][0], options.ignoreBoundary)) { | ||
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], !options.ignoreBoundary)) { | ||
inHole = true; | ||
} | ||
k++; | ||
} | ||
if (!inHole) { | ||
insidePoly = true; | ||
} | ||
} | ||
let result = false; | ||
for (var i = 0; i < polys.length; ++i) { | ||
const polyResult = pip(pt, polys[i]); | ||
if (polyResult === 0) | ||
return options.ignoreBoundary ? false : true; | ||
else if (polyResult) | ||
result = true; | ||
} | ||
return insidePoly; | ||
return result; | ||
} | ||
/** | ||
* inRing | ||
* | ||
* @private | ||
* @param {Array<number>} pt [x,y] | ||
* @param {Array<Array<number>>} ring [[x,y], [x,y],..] | ||
* @param {boolean} ignoreBoundary ignoreBoundary | ||
* @returns {boolean} inRing | ||
*/ | ||
function inRing(pt, ring, ignoreBoundary) { | ||
var isInside = false; | ||
if (ring[0][0] === ring[ring.length - 1][0] && | ||
ring[0][1] === ring[ring.length - 1][1]) { | ||
ring = ring.slice(0, ring.length - 1); | ||
} | ||
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | ||
var xi = ring[i][0]; | ||
var yi = ring[i][1]; | ||
var xj = ring[j][0]; | ||
var yj = ring[j][1]; | ||
var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 && | ||
(xi - pt[0]) * (xj - pt[0]) <= 0 && | ||
(yi - pt[1]) * (yj - pt[1]) <= 0; | ||
if (onBoundary) { | ||
return !ignoreBoundary; | ||
} | ||
var intersect = yi > pt[1] !== yj > pt[1] && | ||
pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi; | ||
if (intersect) { | ||
isInside = !isInside; | ||
} | ||
} | ||
return isInside; | ||
} | ||
/** | ||
* inBBox | ||
@@ -110,0 +65,0 @@ * |
@@ -1,2 +0,3 @@ | ||
import { Coord, Feature, MultiPolygon, Polygon, Properties } from "@turf/helpers"; | ||
import { Feature, MultiPolygon, Polygon, GeoJsonProperties } from "geojson"; | ||
import { Coord } from "@turf/helpers"; | ||
/** | ||
@@ -26,4 +27,4 @@ * Takes a {@link Point} and a {@link Polygon} or {@link MultiPolygon} and determines if the point | ||
*/ | ||
export default function booleanPointInPolygon<G extends Polygon | MultiPolygon, P = Properties>(point: Coord, polygon: Feature<G, P> | G, options?: { | ||
export default function booleanPointInPolygon<G extends Polygon | MultiPolygon, P = GeoJsonProperties>(point: Coord, polygon: Feature<G, P> | G, options?: { | ||
ignoreBoundary?: boolean; | ||
}): boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var invariant_1 = require("@turf/invariant"); | ||
const tslib_1 = require("tslib"); | ||
const point_in_polygon_hao_1 = tslib_1.__importDefault(require("point-in-polygon-hao")); | ||
const invariant_1 = require("@turf/invariant"); | ||
// http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule | ||
@@ -31,4 +33,3 @@ // modified from: https://github.com/substack/point-in-polygon/blob/master/index.js | ||
*/ | ||
function booleanPointInPolygon(point, polygon, options) { | ||
if (options === void 0) { options = {}; } | ||
function booleanPointInPolygon(point, polygon, options = {}) { | ||
// validation | ||
@@ -41,7 +42,7 @@ if (!point) { | ||
} | ||
var pt = invariant_1.getCoord(point); | ||
var geom = invariant_1.getGeom(polygon); | ||
var type = geom.type; | ||
var bbox = polygon.bbox; | ||
var polys = geom.coordinates; | ||
const pt = invariant_1.getCoord(point); | ||
const geom = invariant_1.getGeom(polygon); | ||
const type = geom.type; | ||
const bbox = polygon.bbox; | ||
let polys = geom.coordinates; | ||
// Quick elimination if point is not inside bbox | ||
@@ -51,62 +52,17 @@ if (bbox && inBBox(pt, bbox) === false) { | ||
} | ||
// normalize to multipolygon | ||
if (type === "Polygon") { | ||
polys = [polys]; | ||
} | ||
var insidePoly = false; | ||
for (var i = 0; i < polys.length && !insidePoly; i++) { | ||
// check if it is in the outer ring first | ||
if (inRing(pt, polys[i][0], options.ignoreBoundary)) { | ||
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], !options.ignoreBoundary)) { | ||
inHole = true; | ||
} | ||
k++; | ||
} | ||
if (!inHole) { | ||
insidePoly = true; | ||
} | ||
} | ||
let result = false; | ||
for (var i = 0; i < polys.length; ++i) { | ||
const polyResult = point_in_polygon_hao_1.default(pt, polys[i]); | ||
if (polyResult === 0) | ||
return options.ignoreBoundary ? false : true; | ||
else if (polyResult) | ||
result = true; | ||
} | ||
return insidePoly; | ||
return result; | ||
} | ||
exports.default = booleanPointInPolygon; | ||
/** | ||
* inRing | ||
* | ||
* @private | ||
* @param {Array<number>} pt [x,y] | ||
* @param {Array<Array<number>>} ring [[x,y], [x,y],..] | ||
* @param {boolean} ignoreBoundary ignoreBoundary | ||
* @returns {boolean} inRing | ||
*/ | ||
function inRing(pt, ring, ignoreBoundary) { | ||
var isInside = false; | ||
if (ring[0][0] === ring[ring.length - 1][0] && | ||
ring[0][1] === ring[ring.length - 1][1]) { | ||
ring = ring.slice(0, ring.length - 1); | ||
} | ||
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | ||
var xi = ring[i][0]; | ||
var yi = ring[i][1]; | ||
var xj = ring[j][0]; | ||
var yj = ring[j][1]; | ||
var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 && | ||
(xi - pt[0]) * (xj - pt[0]) <= 0 && | ||
(yi - pt[1]) * (yj - pt[1]) <= 0; | ||
if (onBoundary) { | ||
return !ignoreBoundary; | ||
} | ||
var intersect = yi > pt[1] !== yj > pt[1] && | ||
pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi; | ||
if (intersect) { | ||
isInside = !isInside; | ||
} | ||
} | ||
return isInside; | ||
} | ||
/** | ||
* inBBox | ||
@@ -113,0 +69,0 @@ * |
{ | ||
"name": "@turf/boolean-point-in-polygon", | ||
"version": "6.5.0", | ||
"version": "7.0.0-alpha.0", | ||
"description": "turf boolean-point-in-polygon module", | ||
@@ -60,6 +60,8 @@ "author": "Turf Authors", | ||
"dependencies": { | ||
"@turf/helpers": "^6.5.0", | ||
"@turf/invariant": "^6.5.0" | ||
"@turf/helpers": "^7.0.0-alpha.0", | ||
"@turf/invariant": "^7.0.0-alpha.0", | ||
"point-in-polygon-hao": "^1.1.0", | ||
"tslib": "^2.3.0" | ||
}, | ||
"gitHead": "5375941072b90d489389db22b43bfe809d5e451e" | ||
"gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189" | ||
} |
@@ -7,14 +7,16 @@ # @turf/boolean-point-in-polygon | ||
Takes a [Point][1] and a [Polygon][2] or [MultiPolygon][3] and determines if the point resides inside the polygon. The polygon can | ||
be convex or concave. The function accounts for holes. | ||
Takes a [Point][1] and a [Polygon][2] or [MultiPolygon][3] and determines if the point | ||
resides inside the polygon. The polygon can be convex or concave. The function accounts for holes. | ||
**Parameters** | ||
### Parameters | ||
- `point` **[Coord][4]** input point | ||
- `polygon` **[Feature][5]<([Polygon][6] \| [MultiPolygon][7])>** input polygon or multipolygon | ||
- `options` **[Object][8]** Optional parameters (optional, default `{}`) | ||
- `options.ignoreBoundary` **[boolean][9]** True if polygon boundary should be ignored when determining if the point is inside the polygon otherwise false. (optional, default `false`) | ||
* `point` **[Coord][4]** input point | ||
* `polygon` **[Feature][5]<([Polygon][6] | [MultiPolygon][7])>** input polygon or multipolygon | ||
* `options` **[Object][8]** Optional parameters (optional, default `{}`) | ||
**Examples** | ||
* `options.ignoreBoundary` **[boolean][9]** True if polygon boundary should be ignored when determining if | ||
the point is inside the polygon otherwise false. (optional, default `false`) | ||
### Examples | ||
```javascript | ||
@@ -21,0 +23,0 @@ var pt = turf.point([-77, 44]); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79
11350
4
173
1
+ Addedpoint-in-polygon-hao@^1.1.0
+ Addedtslib@^2.3.0
+ Added@turf/helpers@7.1.0(transitive)
+ Added@turf/invariant@7.1.0(transitive)
+ Added@types/geojson@7946.0.14(transitive)
+ Addedpoint-in-polygon-hao@1.1.0(transitive)
+ Addedtslib@2.7.0(transitive)
- Removed@turf/helpers@6.5.0(transitive)
- Removed@turf/invariant@6.5.0(transitive)
Updated@turf/helpers@^7.0.0-alpha.0