@turf/boolean-point-in-polygon 6.5.0 → 7.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -8
- package/dist/es/index.js +15 -60
- package/dist/js/index.d.ts +3 -2
- package/dist/js/index.js +17 -61
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -4,17 +4,19 @@
|
|
|
4
4
|
|
|
5
5
|
## booleanPointInPolygon
|
|
6
6
|
|
|
7
|
-
Takes a [Point][1] and a [Polygon][2] or [MultiPolygon][3] and determines if the point
|
|
8
|
-
be convex or concave. The function accounts for holes.
|
|
7
|
+
Takes a [Point][1] and a [Polygon][2] or [MultiPolygon][3] and determines if the point
|
|
8
|
+
resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
### Parameters
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- `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`)
|
|
12
|
+
* `point` **[Coord][4]** input point
|
|
13
|
+
* `polygon` **[Feature][5]<([Polygon][6] | [MultiPolygon][7])>** input polygon or multipolygon
|
|
14
|
+
* `options` **[Object][8]** Optional parameters (optional, default `{}`)
|
|
16
15
|
|
|
17
|
-
**
|
|
16
|
+
* `options.ignoreBoundary` **[boolean][9]** True if polygon boundary should be ignored when determining if
|
|
17
|
+
the point is inside the polygon otherwise false. (optional, default `false`)
|
|
18
|
+
|
|
19
|
+
### Examples
|
|
18
20
|
|
|
19
21
|
```javascript
|
|
20
22
|
var pt = turf.point([-77, 44]);
|
package/dist/es/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import pip from "point-in-polygon-hao";
|
|
1
2
|
import { getCoord, getGeom } from "@turf/invariant";
|
|
2
3
|
// http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
|
|
3
4
|
// modified from: https://github.com/substack/point-in-polygon/blob/master/index.js
|
|
@@ -26,8 +27,7 @@ import { getCoord, getGeom } from "@turf/invariant";
|
|
|
26
27
|
* turf.booleanPointInPolygon(pt, poly);
|
|
27
28
|
* //= true
|
|
28
29
|
*/
|
|
29
|
-
export default function booleanPointInPolygon(point, polygon, options) {
|
|
30
|
-
if (options === void 0) { options = {}; }
|
|
30
|
+
export default function booleanPointInPolygon(point, polygon, options = {}) {
|
|
31
31
|
// validation
|
|
32
32
|
if (!point) {
|
|
33
33
|
throw new Error("point is required");
|
|
@@ -35,72 +35,27 @@ export default function booleanPointInPolygon(point, polygon, options) {
|
|
|
35
35
|
if (!polygon) {
|
|
36
36
|
throw new Error("polygon is required");
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
const pt = getCoord(point);
|
|
39
|
+
const geom = getGeom(polygon);
|
|
40
|
+
const type = geom.type;
|
|
41
|
+
const bbox = polygon.bbox;
|
|
42
|
+
let polys = geom.coordinates;
|
|
43
43
|
// Quick elimination if point is not inside bbox
|
|
44
44
|
if (bbox && inBBox(pt, bbox) === false) {
|
|
45
45
|
return false;
|
|
46
46
|
}
|
|
47
|
-
// normalize to multipolygon
|
|
48
47
|
if (type === "Polygon") {
|
|
49
48
|
polys = [polys];
|
|
50
49
|
}
|
|
51
|
-
|
|
52
|
-
for (var i = 0; i < polys.length
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
while (k < polys[i].length && !inHole) {
|
|
59
|
-
if (inRing(pt, polys[i][k], !options.ignoreBoundary)) {
|
|
60
|
-
inHole = true;
|
|
61
|
-
}
|
|
62
|
-
k++;
|
|
63
|
-
}
|
|
64
|
-
if (!inHole) {
|
|
65
|
-
insidePoly = true;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
50
|
+
let result = false;
|
|
51
|
+
for (var i = 0; i < polys.length; ++i) {
|
|
52
|
+
const polyResult = pip(pt, polys[i]);
|
|
53
|
+
if (polyResult === 0)
|
|
54
|
+
return options.ignoreBoundary ? false : true;
|
|
55
|
+
else if (polyResult)
|
|
56
|
+
result = true;
|
|
68
57
|
}
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* inRing
|
|
73
|
-
*
|
|
74
|
-
* @private
|
|
75
|
-
* @param {Array<number>} pt [x,y]
|
|
76
|
-
* @param {Array<Array<number>>} ring [[x,y], [x,y],..]
|
|
77
|
-
* @param {boolean} ignoreBoundary ignoreBoundary
|
|
78
|
-
* @returns {boolean} inRing
|
|
79
|
-
*/
|
|
80
|
-
function inRing(pt, ring, ignoreBoundary) {
|
|
81
|
-
var isInside = false;
|
|
82
|
-
if (ring[0][0] === ring[ring.length - 1][0] &&
|
|
83
|
-
ring[0][1] === ring[ring.length - 1][1]) {
|
|
84
|
-
ring = ring.slice(0, ring.length - 1);
|
|
85
|
-
}
|
|
86
|
-
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
87
|
-
var xi = ring[i][0];
|
|
88
|
-
var yi = ring[i][1];
|
|
89
|
-
var xj = ring[j][0];
|
|
90
|
-
var yj = ring[j][1];
|
|
91
|
-
var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 &&
|
|
92
|
-
(xi - pt[0]) * (xj - pt[0]) <= 0 &&
|
|
93
|
-
(yi - pt[1]) * (yj - pt[1]) <= 0;
|
|
94
|
-
if (onBoundary) {
|
|
95
|
-
return !ignoreBoundary;
|
|
96
|
-
}
|
|
97
|
-
var intersect = yi > pt[1] !== yj > pt[1] &&
|
|
98
|
-
pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi;
|
|
99
|
-
if (intersect) {
|
|
100
|
-
isInside = !isInside;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return isInside;
|
|
58
|
+
return result;
|
|
104
59
|
}
|
|
105
60
|
/**
|
|
106
61
|
* inBBox
|
package/dist/js/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Feature, MultiPolygon, Polygon, GeoJsonProperties } from "geojson";
|
|
2
|
+
import { Coord } from "@turf/helpers";
|
|
2
3
|
/**
|
|
3
4
|
* Takes a {@link Point} and a {@link Polygon} or {@link MultiPolygon} and determines if the point
|
|
4
5
|
* resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
|
|
@@ -23,6 +24,6 @@ import { Coord, Feature, MultiPolygon, Polygon, Properties } from "@turf/helpers
|
|
|
23
24
|
* turf.booleanPointInPolygon(pt, poly);
|
|
24
25
|
* //= true
|
|
25
26
|
*/
|
|
26
|
-
export default function booleanPointInPolygon<G extends Polygon | MultiPolygon, P =
|
|
27
|
+
export default function booleanPointInPolygon<G extends Polygon | MultiPolygon, P = GeoJsonProperties>(point: Coord, polygon: Feature<G, P> | G, options?: {
|
|
27
28
|
ignoreBoundary?: boolean;
|
|
28
29
|
}): boolean;
|
package/dist/js/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const point_in_polygon_hao_1 = tslib_1.__importDefault(require("point-in-polygon-hao"));
|
|
5
|
+
const invariant_1 = require("@turf/invariant");
|
|
4
6
|
// http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
|
|
5
7
|
// modified from: https://github.com/substack/point-in-polygon/blob/master/index.js
|
|
6
8
|
// which was modified from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
|
@@ -28,8 +30,7 @@ var invariant_1 = require("@turf/invariant");
|
|
|
28
30
|
* turf.booleanPointInPolygon(pt, poly);
|
|
29
31
|
* //= true
|
|
30
32
|
*/
|
|
31
|
-
function booleanPointInPolygon(point, polygon, options) {
|
|
32
|
-
if (options === void 0) { options = {}; }
|
|
33
|
+
function booleanPointInPolygon(point, polygon, options = {}) {
|
|
33
34
|
// validation
|
|
34
35
|
if (!point) {
|
|
35
36
|
throw new Error("point is required");
|
|
@@ -37,74 +38,29 @@ function booleanPointInPolygon(point, polygon, options) {
|
|
|
37
38
|
if (!polygon) {
|
|
38
39
|
throw new Error("polygon is required");
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
const pt = invariant_1.getCoord(point);
|
|
42
|
+
const geom = invariant_1.getGeom(polygon);
|
|
43
|
+
const type = geom.type;
|
|
44
|
+
const bbox = polygon.bbox;
|
|
45
|
+
let polys = geom.coordinates;
|
|
45
46
|
// Quick elimination if point is not inside bbox
|
|
46
47
|
if (bbox && inBBox(pt, bbox) === false) {
|
|
47
48
|
return false;
|
|
48
49
|
}
|
|
49
|
-
// normalize to multipolygon
|
|
50
50
|
if (type === "Polygon") {
|
|
51
51
|
polys = [polys];
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
for (var i = 0; i < polys.length
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
while (k < polys[i].length && !inHole) {
|
|
61
|
-
if (inRing(pt, polys[i][k], !options.ignoreBoundary)) {
|
|
62
|
-
inHole = true;
|
|
63
|
-
}
|
|
64
|
-
k++;
|
|
65
|
-
}
|
|
66
|
-
if (!inHole) {
|
|
67
|
-
insidePoly = true;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
53
|
+
let result = false;
|
|
54
|
+
for (var i = 0; i < polys.length; ++i) {
|
|
55
|
+
const polyResult = point_in_polygon_hao_1.default(pt, polys[i]);
|
|
56
|
+
if (polyResult === 0)
|
|
57
|
+
return options.ignoreBoundary ? false : true;
|
|
58
|
+
else if (polyResult)
|
|
59
|
+
result = true;
|
|
70
60
|
}
|
|
71
|
-
return
|
|
61
|
+
return result;
|
|
72
62
|
}
|
|
73
63
|
exports.default = booleanPointInPolygon;
|
|
74
|
-
/**
|
|
75
|
-
* inRing
|
|
76
|
-
*
|
|
77
|
-
* @private
|
|
78
|
-
* @param {Array<number>} pt [x,y]
|
|
79
|
-
* @param {Array<Array<number>>} ring [[x,y], [x,y],..]
|
|
80
|
-
* @param {boolean} ignoreBoundary ignoreBoundary
|
|
81
|
-
* @returns {boolean} inRing
|
|
82
|
-
*/
|
|
83
|
-
function inRing(pt, ring, ignoreBoundary) {
|
|
84
|
-
var isInside = false;
|
|
85
|
-
if (ring[0][0] === ring[ring.length - 1][0] &&
|
|
86
|
-
ring[0][1] === ring[ring.length - 1][1]) {
|
|
87
|
-
ring = ring.slice(0, ring.length - 1);
|
|
88
|
-
}
|
|
89
|
-
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
90
|
-
var xi = ring[i][0];
|
|
91
|
-
var yi = ring[i][1];
|
|
92
|
-
var xj = ring[j][0];
|
|
93
|
-
var yj = ring[j][1];
|
|
94
|
-
var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 &&
|
|
95
|
-
(xi - pt[0]) * (xj - pt[0]) <= 0 &&
|
|
96
|
-
(yi - pt[1]) * (yj - pt[1]) <= 0;
|
|
97
|
-
if (onBoundary) {
|
|
98
|
-
return !ignoreBoundary;
|
|
99
|
-
}
|
|
100
|
-
var intersect = yi > pt[1] !== yj > pt[1] &&
|
|
101
|
-
pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi;
|
|
102
|
-
if (intersect) {
|
|
103
|
-
isInside = !isInside;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return isInside;
|
|
107
|
-
}
|
|
108
64
|
/**
|
|
109
65
|
* inBBox
|
|
110
66
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/boolean-point-in-polygon",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-alpha.0",
|
|
4
4
|
"description": "turf boolean-point-in-polygon module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -57,8 +57,10 @@
|
|
|
57
57
|
"typescript": "*"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@turf/helpers": "^
|
|
61
|
-
"@turf/invariant": "^
|
|
60
|
+
"@turf/helpers": "^7.0.0-alpha.0",
|
|
61
|
+
"@turf/invariant": "^7.0.0-alpha.0",
|
|
62
|
+
"point-in-polygon-hao": "^1.1.0",
|
|
63
|
+
"tslib": "^2.3.0"
|
|
62
64
|
},
|
|
63
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
|
|
64
66
|
}
|