@turf/boolean-point-in-polygon 6.5.0 → 7.0.0-alpha.1

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 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 resides inside the polygon. The polygon can
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
- **Parameters**
10
+ ### Parameters
11
11
 
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 `{}`)
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
- **Examples**
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
- var pt = getCoord(point);
39
- var geom = getGeom(polygon);
40
- var type = geom.type;
41
- var bbox = polygon.bbox;
42
- var polys = geom.coordinates;
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
- var insidePoly = false;
52
- for (var i = 0; i < polys.length && !insidePoly; i++) {
53
- // check if it is in the outer ring first
54
- if (inRing(pt, polys[i][0], options.ignoreBoundary)) {
55
- var inHole = false;
56
- var k = 1;
57
- // check for the point in any of the holes
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 insidePoly;
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
@@ -1,4 +1,5 @@
1
- import { Coord, Feature, MultiPolygon, Polygon, Properties } from "@turf/helpers";
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 = Properties>(point: Coord, polygon: Feature<G, P> | G, options?: {
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
- var invariant_1 = require("@turf/invariant");
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
- var pt = invariant_1.getCoord(point);
41
- var geom = invariant_1.getGeom(polygon);
42
- var type = geom.type;
43
- var bbox = polygon.bbox;
44
- var polys = geom.coordinates;
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
- var insidePoly = false;
54
- for (var i = 0; i < polys.length && !insidePoly; i++) {
55
- // check if it is in the outer ring first
56
- if (inRing(pt, polys[i][0], options.ignoreBoundary)) {
57
- var inHole = false;
58
- var k = 1;
59
- // check for the point in any of the holes
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 insidePoly;
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": "6.5.0",
3
+ "version": "7.0.0-alpha.1",
4
4
  "description": "turf boolean-point-in-polygon module",
5
5
  "author": "Turf Authors",
6
6
  "license": "MIT",
@@ -29,6 +29,7 @@
29
29
  "exports": {
30
30
  "./package.json": "./package.json",
31
31
  ".": {
32
+ "types": "./dist/js/index.d.ts",
32
33
  "import": "./dist/es/index.js",
33
34
  "require": "./dist/js/index.js"
34
35
  }
@@ -39,26 +40,28 @@
39
40
  "dist"
40
41
  ],
41
42
  "scripts": {
42
- "bench": "ts-node bench.js",
43
+ "bench": "tsx bench.js",
43
44
  "build": "npm-run-all build:*",
44
45
  "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
45
46
  "build:js": "tsc",
46
- "docs": "node ../../scripts/generate-readmes",
47
+ "docs": "tsx ../../scripts/generate-readmes",
47
48
  "test": "npm-run-all test:*",
48
- "test:tape": "ts-node -r esm test.js"
49
+ "test:tape": "tsx test.js"
49
50
  },
50
51
  "devDependencies": {
51
52
  "@types/tape": "*",
52
53
  "benchmark": "*",
53
54
  "npm-run-all": "*",
54
55
  "tape": "*",
55
- "ts-node": "*",
56
56
  "tslint": "*",
57
+ "tsx": "*",
57
58
  "typescript": "*"
58
59
  },
59
60
  "dependencies": {
60
- "@turf/helpers": "^6.5.0",
61
- "@turf/invariant": "^6.5.0"
61
+ "@turf/helpers": "^7.0.0-alpha.1",
62
+ "@turf/invariant": "^7.0.0-alpha.1",
63
+ "point-in-polygon-hao": "^1.1.0",
64
+ "tslib": "^2.3.0"
62
65
  },
63
- "gitHead": "5375941072b90d489389db22b43bfe809d5e451e"
66
+ "gitHead": "cf7a0c507b017ca066acffd0ce23bda5b393fb5a"
64
67
  }