@turf/line-intersect 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 CHANGED
@@ -6,12 +6,12 @@
6
6
 
7
7
  Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
8
8
 
9
- **Parameters**
9
+ ### Parameters
10
10
 
11
- - `line1` **([Geometry][1] \| [FeatureCollection][2] \| [Feature][3]<([LineString][4] \| [MultiLineString][5] \| [Polygon][6] \| [MultiPolygon][7])>)** any LineString or Polygon
12
- - `line2` **([Geometry][1] \| [FeatureCollection][2] \| [Feature][3]<([LineString][4] \| [MultiLineString][5] \| [Polygon][6] \| [MultiPolygon][7])>)** any LineString or Polygon
11
+ * `line1` **[GeoJSON][1]** any LineString or Polygon
12
+ * `line2` **[GeoJSON][1]** any LineString or Polygon
13
13
 
14
- **Examples**
14
+ ### Examples
15
15
 
16
16
  ```javascript
17
17
  var line1 = turf.lineString([[126, -11], [129, -21]]);
@@ -22,23 +22,13 @@ var intersects = turf.lineIntersect(line1, line2);
22
22
  var addToMap = [line1, line2, intersects]
23
23
  ```
24
24
 
25
- Returns **[FeatureCollection][2]<[Point][8]>** point(s) that intersect both
25
+ Returns **[FeatureCollection][2]<[Point][3]>** point(s) that intersect both
26
26
 
27
- [1]: https://tools.ietf.org/html/rfc7946#section-3.1
27
+ [1]: https://tools.ietf.org/html/rfc7946#section-3
28
28
 
29
29
  [2]: https://tools.ietf.org/html/rfc7946#section-3.3
30
30
 
31
- [3]: https://tools.ietf.org/html/rfc7946#section-3.2
32
-
33
- [4]: https://tools.ietf.org/html/rfc7946#section-3.1.4
34
-
35
- [5]: https://tools.ietf.org/html/rfc7946#section-3.1.5
36
-
37
- [6]: https://tools.ietf.org/html/rfc7946#section-3.1.6
38
-
39
- [7]: https://tools.ietf.org/html/rfc7946#section-3.1.7
40
-
41
- [8]: https://tools.ietf.org/html/rfc7946#section-3.1.2
31
+ [3]: https://tools.ietf.org/html/rfc7946#section-3.1.2
42
32
 
43
33
  <!-- This file is automatically generated. Please don't edit it directly:
44
34
  if you find an error, edit the source file (likely index.js), and re-run
package/dist/es/index.js CHANGED
@@ -1,14 +1,14 @@
1
- import { feature, featureCollection, point, } from "@turf/helpers";
2
- import { getCoords } from "@turf/invariant";
3
- import lineSegment from "@turf/line-segment";
4
- import { featureEach } from "@turf/meta";
5
- import rbush from "geojson-rbush";
1
+ import { feature, featureCollection, point } from "@turf/helpers";
2
+ import findIntersections from "sweepline-intersections";
6
3
  /**
7
4
  * Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
8
5
  *
9
6
  * @name lineIntersect
10
7
  * @param {GeoJSON} line1 any LineString or Polygon
11
8
  * @param {GeoJSON} line2 any LineString or Polygon
9
+ * @param {Object} [options={}] Optional parameters
10
+ * @param {boolean} [options.removeDuplicates=true] remove duplicate intersections
11
+ * @param {boolean} [options.ignoreSelfIntersections=false] ignores self-intersections on input features
12
12
  * @returns {FeatureCollection<Point>} point(s) that intersect both
13
13
  * @example
14
14
  * var line1 = turf.lineString([[126, -11], [129, -21]]);
@@ -18,90 +18,44 @@ import rbush from "geojson-rbush";
18
18
  * //addToMap
19
19
  * var addToMap = [line1, line2, intersects]
20
20
  */
21
- function lineIntersect(line1, line2) {
22
- var unique = {};
23
- var results = [];
24
- // First, normalize geometries to features
25
- // Then, handle simple 2-vertex segments
26
- if (line1.type === "LineString") {
27
- line1 = feature(line1);
21
+ function lineIntersect(line1, line2, options = {}) {
22
+ const { removeDuplicates = true, ignoreSelfIntersections = false } = options;
23
+ let features = [];
24
+ if (line1.type === "FeatureCollection")
25
+ features = features.concat(line1.features);
26
+ else if (line1.type === "Feature")
27
+ features.push(line1);
28
+ else if (line1.type === "LineString" ||
29
+ line1.type === "Polygon" ||
30
+ line1.type === "MultiLineString" ||
31
+ line1.type === "MultiPolygon") {
32
+ features.push(feature(line1));
28
33
  }
29
- if (line2.type === "LineString") {
30
- line2 = feature(line2);
34
+ if (line2.type === "FeatureCollection")
35
+ features = features.concat(line2.features);
36
+ else if (line2.type === "Feature")
37
+ features.push(line2);
38
+ else if (line2.type === "LineString" ||
39
+ line2.type === "Polygon" ||
40
+ line2.type === "MultiLineString" ||
41
+ line2.type === "MultiPolygon") {
42
+ features.push(feature(line2));
31
43
  }
32
- if (line1.type === "Feature" &&
33
- line2.type === "Feature" &&
34
- line1.geometry !== null &&
35
- line2.geometry !== null &&
36
- line1.geometry.type === "LineString" &&
37
- line2.geometry.type === "LineString" &&
38
- line1.geometry.coordinates.length === 2 &&
39
- line2.geometry.coordinates.length === 2) {
40
- var intersect = intersects(line1, line2);
41
- if (intersect) {
42
- results.push(intersect);
43
- }
44
- return featureCollection(results);
45
- }
46
- // Handles complex GeoJSON Geometries
47
- var tree = rbush();
48
- tree.load(lineSegment(line2));
49
- featureEach(lineSegment(line1), function (segment) {
50
- featureEach(tree.search(segment), function (match) {
51
- var intersect = intersects(segment, match);
52
- if (intersect) {
53
- // prevent duplicate points https://github.com/Turfjs/turf/issues/688
54
- var key = getCoords(intersect).join(",");
55
- if (!unique[key]) {
56
- unique[key] = true;
57
- results.push(intersect);
58
- }
44
+ const intersections = findIntersections(featureCollection(features), ignoreSelfIntersections);
45
+ let results = [];
46
+ if (removeDuplicates) {
47
+ const unique = {};
48
+ intersections.forEach((intersection) => {
49
+ const key = intersection.join(",");
50
+ if (!unique[key]) {
51
+ unique[key] = true;
52
+ results.push(intersection);
59
53
  }
60
54
  });
61
- });
62
- return featureCollection(results);
63
- }
64
- /**
65
- * Find a point that intersects LineStrings with two coordinates each
66
- *
67
- * @private
68
- * @param {Feature<LineString>} line1 GeoJSON LineString (Must only contain 2 coordinates)
69
- * @param {Feature<LineString>} line2 GeoJSON LineString (Must only contain 2 coordinates)
70
- * @returns {Feature<Point>} intersecting GeoJSON Point
71
- */
72
- function intersects(line1, line2) {
73
- var coords1 = getCoords(line1);
74
- var coords2 = getCoords(line2);
75
- if (coords1.length !== 2) {
76
- throw new Error("<intersects> line1 must only contain 2 coordinates");
77
- }
78
- if (coords2.length !== 2) {
79
- throw new Error("<intersects> line2 must only contain 2 coordinates");
80
- }
81
- var x1 = coords1[0][0];
82
- var y1 = coords1[0][1];
83
- var x2 = coords1[1][0];
84
- var y2 = coords1[1][1];
85
- var x3 = coords2[0][0];
86
- var y3 = coords2[0][1];
87
- var x4 = coords2[1][0];
88
- var y4 = coords2[1][1];
89
- var denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
90
- var numeA = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
91
- var numeB = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
92
- if (denom === 0) {
93
- if (numeA === 0 && numeB === 0) {
94
- return null;
95
- }
96
- return null;
97
55
  }
98
- var uA = numeA / denom;
99
- var uB = numeB / denom;
100
- if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
101
- var x = x1 + uA * (x2 - x1);
102
- var y = y1 + uA * (y2 - y1);
103
- return point([x, y]);
56
+ else {
57
+ results = intersections;
104
58
  }
105
- return null;
59
+ return featureCollection(results.map((r) => point(r)));
106
60
  }
107
61
  export default lineIntersect;
@@ -1,10 +1,13 @@
1
- import { Feature, FeatureCollection, LineString, MultiLineString, MultiPolygon, Point, Polygon } from "@turf/helpers";
1
+ import { Feature, FeatureCollection, LineString, MultiLineString, MultiPolygon, Point, Polygon } from "geojson";
2
2
  /**
3
3
  * Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
4
4
  *
5
5
  * @name lineIntersect
6
6
  * @param {GeoJSON} line1 any LineString or Polygon
7
7
  * @param {GeoJSON} line2 any LineString or Polygon
8
+ * @param {Object} [options={}] Optional parameters
9
+ * @param {boolean} [options.removeDuplicates=true] remove duplicate intersections
10
+ * @param {boolean} [options.ignoreSelfIntersections=false] ignores self-intersections on input features
8
11
  * @returns {FeatureCollection<Point>} point(s) that intersect both
9
12
  * @example
10
13
  * var line1 = turf.lineString([[126, -11], [129, -21]]);
@@ -14,5 +17,8 @@ import { Feature, FeatureCollection, LineString, MultiLineString, MultiPolygon,
14
17
  * //addToMap
15
18
  * var addToMap = [line1, line2, intersects]
16
19
  */
17
- declare function lineIntersect<G1 extends LineString | MultiLineString | Polygon | MultiPolygon, G2 extends LineString | MultiLineString | Polygon | MultiPolygon>(line1: FeatureCollection<G1> | Feature<G1> | G1, line2: FeatureCollection<G2> | Feature<G2> | G2): FeatureCollection<Point>;
20
+ declare function lineIntersect<G1 extends LineString | MultiLineString | Polygon | MultiPolygon, G2 extends LineString | MultiLineString | Polygon | MultiPolygon>(line1: FeatureCollection<G1> | Feature<G1> | G1, line2: FeatureCollection<G2> | Feature<G2> | G2, options?: {
21
+ removeDuplicates?: boolean;
22
+ ignoreSelfIntersections?: boolean;
23
+ }): FeatureCollection<Point>;
18
24
  export default lineIntersect;
package/dist/js/index.js CHANGED
@@ -1,19 +1,17 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var helpers_1 = require("@turf/helpers");
7
- var invariant_1 = require("@turf/invariant");
8
- var line_segment_1 = __importDefault(require("@turf/line-segment"));
9
- var meta_1 = require("@turf/meta");
10
- var geojson_rbush_1 = __importDefault(require("geojson-rbush"));
3
+ const tslib_1 = require("tslib");
4
+ const helpers_1 = require("@turf/helpers");
5
+ const sweepline_intersections_1 = tslib_1.__importDefault(require("sweepline-intersections"));
11
6
  /**
12
7
  * Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
13
8
  *
14
9
  * @name lineIntersect
15
10
  * @param {GeoJSON} line1 any LineString or Polygon
16
11
  * @param {GeoJSON} line2 any LineString or Polygon
12
+ * @param {Object} [options={}] Optional parameters
13
+ * @param {boolean} [options.removeDuplicates=true] remove duplicate intersections
14
+ * @param {boolean} [options.ignoreSelfIntersections=false] ignores self-intersections on input features
17
15
  * @returns {FeatureCollection<Point>} point(s) that intersect both
18
16
  * @example
19
17
  * var line1 = turf.lineString([[126, -11], [129, -21]]);
@@ -23,90 +21,44 @@ var geojson_rbush_1 = __importDefault(require("geojson-rbush"));
23
21
  * //addToMap
24
22
  * var addToMap = [line1, line2, intersects]
25
23
  */
26
- function lineIntersect(line1, line2) {
27
- var unique = {};
28
- var results = [];
29
- // First, normalize geometries to features
30
- // Then, handle simple 2-vertex segments
31
- if (line1.type === "LineString") {
32
- line1 = helpers_1.feature(line1);
24
+ function lineIntersect(line1, line2, options = {}) {
25
+ const { removeDuplicates = true, ignoreSelfIntersections = false } = options;
26
+ let features = [];
27
+ if (line1.type === "FeatureCollection")
28
+ features = features.concat(line1.features);
29
+ else if (line1.type === "Feature")
30
+ features.push(line1);
31
+ else if (line1.type === "LineString" ||
32
+ line1.type === "Polygon" ||
33
+ line1.type === "MultiLineString" ||
34
+ line1.type === "MultiPolygon") {
35
+ features.push(helpers_1.feature(line1));
33
36
  }
34
- if (line2.type === "LineString") {
35
- line2 = helpers_1.feature(line2);
37
+ if (line2.type === "FeatureCollection")
38
+ features = features.concat(line2.features);
39
+ else if (line2.type === "Feature")
40
+ features.push(line2);
41
+ else if (line2.type === "LineString" ||
42
+ line2.type === "Polygon" ||
43
+ line2.type === "MultiLineString" ||
44
+ line2.type === "MultiPolygon") {
45
+ features.push(helpers_1.feature(line2));
36
46
  }
37
- if (line1.type === "Feature" &&
38
- line2.type === "Feature" &&
39
- line1.geometry !== null &&
40
- line2.geometry !== null &&
41
- line1.geometry.type === "LineString" &&
42
- line2.geometry.type === "LineString" &&
43
- line1.geometry.coordinates.length === 2 &&
44
- line2.geometry.coordinates.length === 2) {
45
- var intersect = intersects(line1, line2);
46
- if (intersect) {
47
- results.push(intersect);
48
- }
49
- return helpers_1.featureCollection(results);
50
- }
51
- // Handles complex GeoJSON Geometries
52
- var tree = geojson_rbush_1.default();
53
- tree.load(line_segment_1.default(line2));
54
- meta_1.featureEach(line_segment_1.default(line1), function (segment) {
55
- meta_1.featureEach(tree.search(segment), function (match) {
56
- var intersect = intersects(segment, match);
57
- if (intersect) {
58
- // prevent duplicate points https://github.com/Turfjs/turf/issues/688
59
- var key = invariant_1.getCoords(intersect).join(",");
60
- if (!unique[key]) {
61
- unique[key] = true;
62
- results.push(intersect);
63
- }
47
+ const intersections = sweepline_intersections_1.default(helpers_1.featureCollection(features), ignoreSelfIntersections);
48
+ let results = [];
49
+ if (removeDuplicates) {
50
+ const unique = {};
51
+ intersections.forEach((intersection) => {
52
+ const key = intersection.join(",");
53
+ if (!unique[key]) {
54
+ unique[key] = true;
55
+ results.push(intersection);
64
56
  }
65
57
  });
66
- });
67
- return helpers_1.featureCollection(results);
68
- }
69
- /**
70
- * Find a point that intersects LineStrings with two coordinates each
71
- *
72
- * @private
73
- * @param {Feature<LineString>} line1 GeoJSON LineString (Must only contain 2 coordinates)
74
- * @param {Feature<LineString>} line2 GeoJSON LineString (Must only contain 2 coordinates)
75
- * @returns {Feature<Point>} intersecting GeoJSON Point
76
- */
77
- function intersects(line1, line2) {
78
- var coords1 = invariant_1.getCoords(line1);
79
- var coords2 = invariant_1.getCoords(line2);
80
- if (coords1.length !== 2) {
81
- throw new Error("<intersects> line1 must only contain 2 coordinates");
82
- }
83
- if (coords2.length !== 2) {
84
- throw new Error("<intersects> line2 must only contain 2 coordinates");
85
- }
86
- var x1 = coords1[0][0];
87
- var y1 = coords1[0][1];
88
- var x2 = coords1[1][0];
89
- var y2 = coords1[1][1];
90
- var x3 = coords2[0][0];
91
- var y3 = coords2[0][1];
92
- var x4 = coords2[1][0];
93
- var y4 = coords2[1][1];
94
- var denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
95
- var numeA = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
96
- var numeB = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
97
- if (denom === 0) {
98
- if (numeA === 0 && numeB === 0) {
99
- return null;
100
- }
101
- return null;
102
58
  }
103
- var uA = numeA / denom;
104
- var uB = numeB / denom;
105
- if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
106
- var x = x1 + uA * (x2 - x1);
107
- var y = y1 + uA * (y2 - y1);
108
- return helpers_1.point([x, y]);
59
+ else {
60
+ results = intersections;
109
61
  }
110
- return null;
62
+ return helpers_1.featureCollection(results.map((r) => helpers_1.point(r)));
111
63
  }
112
64
  exports.default = lineIntersect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/line-intersect",
3
- "version": "6.5.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf line-intersect module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -51,7 +51,7 @@
51
51
  "test:tape": "ts-node -r esm test.js"
52
52
  },
53
53
  "devDependencies": {
54
- "@turf/truncate": "^6.5.0",
54
+ "@turf/truncate": "^7.0.0-alpha.0",
55
55
  "@types/tape": "*",
56
56
  "benchmark": "*",
57
57
  "load-json-file": "*",
@@ -63,11 +63,9 @@
63
63
  "write-json-file": "*"
64
64
  },
65
65
  "dependencies": {
66
- "@turf/helpers": "^6.5.0",
67
- "@turf/invariant": "^6.5.0",
68
- "@turf/line-segment": "^6.5.0",
69
- "@turf/meta": "^6.5.0",
70
- "geojson-rbush": "3.x"
66
+ "@turf/helpers": "^7.0.0-alpha.0",
67
+ "sweepline-intersections": "^1.4.0",
68
+ "tslib": "^2.3.0"
71
69
  },
72
- "gitHead": "5375941072b90d489389db22b43bfe809d5e451e"
70
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
73
71
  }