@turf/line-overlap 7.0.0-alpha.2 → 7.0.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
@@ -46,26 +46,21 @@ Returns **[FeatureCollection][9]<[LineString][3]>** lines(s) that are overlappin
46
46
 
47
47
  [9]: https://tools.ietf.org/html/rfc7946#section-3.3
48
48
 
49
- <!-- This file is automatically generated. Please don't edit it directly:
50
- if you find an error, edit the source file (likely index.js), and re-run
51
- ./scripts/generate-readmes in the turf project. -->
49
+ <!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
52
50
 
53
51
  ---
54
52
 
55
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
56
- module collection dedicated to geographic algorithms. It is maintained in the
57
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
58
- PRs and issues.
53
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
59
54
 
60
55
  ### Installation
61
56
 
62
- Install this module individually:
57
+ Install this single module individually:
63
58
 
64
59
  ```sh
65
60
  $ npm install @turf/line-overlap
66
61
  ```
67
62
 
68
- Or install the Turf module that includes it as a function:
63
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
69
64
 
70
65
  ```sh
71
66
  $ npm install @turf/turf
@@ -0,0 +1,96 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ var _geojsonrbush = require('@turf/geojson-rbush');
6
+ var _linesegment = require('@turf/line-segment');
7
+ var _nearestpointonline = require('@turf/nearest-point-on-line');
8
+ var _booleanpointonline = require('@turf/boolean-point-on-line');
9
+ var _invariant = require('@turf/invariant');
10
+ var _meta = require('@turf/meta');
11
+ var _helpers = require('@turf/helpers');
12
+ var _deepequal = require('deep-equal'); var _deepequal2 = _interopRequireDefault(_deepequal);
13
+ function lineOverlap(line1, line2, options = {}) {
14
+ options = options || {};
15
+ if (!_helpers.isObject.call(void 0, options))
16
+ throw new Error("options is invalid");
17
+ var tolerance = options.tolerance || 0;
18
+ var features = [];
19
+ var tree = _geojsonrbush.geojsonRbush.call(void 0, );
20
+ const line = _linesegment.lineSegment.call(void 0, line1);
21
+ tree.load(line);
22
+ var overlapSegment;
23
+ let additionalSegments = [];
24
+ _meta.segmentEach.call(void 0, line2, function(segment) {
25
+ var doesOverlaps = false;
26
+ if (!segment) {
27
+ return;
28
+ }
29
+ _meta.featureEach.call(void 0, tree.search(segment), function(match) {
30
+ if (doesOverlaps === false) {
31
+ var coordsSegment = _invariant.getCoords.call(void 0, segment).sort();
32
+ var coordsMatch = _invariant.getCoords.call(void 0, match).sort();
33
+ if (_deepequal2.default.call(void 0, coordsSegment, coordsMatch)) {
34
+ doesOverlaps = true;
35
+ if (overlapSegment) {
36
+ overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
37
+ } else
38
+ overlapSegment = segment;
39
+ } else if (tolerance === 0 ? _booleanpointonline.booleanPointOnLine.call(void 0, coordsSegment[0], match) && _booleanpointonline.booleanPointOnLine.call(void 0, coordsSegment[1], match) : _nearestpointonline.nearestPointOnLine.call(void 0, match, coordsSegment[0]).properties.dist <= tolerance && _nearestpointonline.nearestPointOnLine.call(void 0, match, coordsSegment[1]).properties.dist <= tolerance) {
40
+ doesOverlaps = true;
41
+ if (overlapSegment) {
42
+ overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
43
+ } else
44
+ overlapSegment = segment;
45
+ } else if (tolerance === 0 ? _booleanpointonline.booleanPointOnLine.call(void 0, coordsMatch[0], segment) && _booleanpointonline.booleanPointOnLine.call(void 0, coordsMatch[1], segment) : _nearestpointonline.nearestPointOnLine.call(void 0, segment, coordsMatch[0]).properties.dist <= tolerance && _nearestpointonline.nearestPointOnLine.call(void 0, segment, coordsMatch[1]).properties.dist <= tolerance) {
46
+ if (overlapSegment) {
47
+ const combinedSegment = concatSegment(overlapSegment, match);
48
+ if (combinedSegment) {
49
+ overlapSegment = combinedSegment;
50
+ } else {
51
+ additionalSegments.push(match);
52
+ }
53
+ } else
54
+ overlapSegment = match;
55
+ }
56
+ }
57
+ });
58
+ if (doesOverlaps === false && overlapSegment) {
59
+ features.push(overlapSegment);
60
+ if (additionalSegments.length) {
61
+ features = features.concat(additionalSegments);
62
+ additionalSegments = [];
63
+ }
64
+ overlapSegment = void 0;
65
+ }
66
+ });
67
+ if (overlapSegment)
68
+ features.push(overlapSegment);
69
+ return _helpers.featureCollection.call(void 0, features);
70
+ }
71
+ __name(lineOverlap, "lineOverlap");
72
+ function concatSegment(line, segment) {
73
+ var coords = _invariant.getCoords.call(void 0, segment);
74
+ var lineCoords = _invariant.getCoords.call(void 0, line);
75
+ var start = lineCoords[0];
76
+ var end = lineCoords[lineCoords.length - 1];
77
+ var geom = line.geometry.coordinates;
78
+ if (_deepequal2.default.call(void 0, coords[0], start))
79
+ geom.unshift(coords[1]);
80
+ else if (_deepequal2.default.call(void 0, coords[0], end))
81
+ geom.push(coords[1]);
82
+ else if (_deepequal2.default.call(void 0, coords[1], start))
83
+ geom.unshift(coords[0]);
84
+ else if (_deepequal2.default.call(void 0, coords[1], end))
85
+ geom.push(coords[0]);
86
+ else
87
+ return;
88
+ return line;
89
+ }
90
+ __name(concatSegment, "concatSegment");
91
+ var turf_line_overlap_default = lineOverlap;
92
+
93
+
94
+
95
+ exports.default = turf_line_overlap_default; exports.lineOverlap = lineOverlap;
96
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB,aAAa;AACtC,SAAS,mBAAmB;AAC5B,SAAS,0BAA0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from \"@turf/geojson-rbush\";\nimport { lineSegment } from \"@turf/line-segment\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { booleanPointOnLine } from \"@turf/boolean-point-on-line\";\nimport { getCoords } from \"@turf/invariant\";\nimport { featureEach, segmentEach } from \"@turf/meta\";\nimport {\n FeatureCollection,\n Feature,\n LineString,\n MultiLineString,\n Polygon,\n MultiPolygon,\n GeoJsonProperties,\n} from \"geojson\";\nimport { featureCollection, isObject } from \"@turf/helpers\";\nimport equal from \"deep-equal\";\n\n/**\n * Takes any LineString or Polygon and returns the overlapping lines between both features.\n *\n * @name lineOverlap\n * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon\n * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)\n * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features\n * @example\n * var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);\n * var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);\n *\n * var overlapping = turf.lineOverlap(line1, line2);\n *\n * //addToMap\n * var addToMap = [line1, line2, overlapping]\n */\nfunction lineOverlap<\n G1 extends LineString | MultiLineString | Polygon | MultiPolygon,\n G2 extends LineString | MultiLineString | Polygon | MultiPolygon,\n>(\n line1: Feature<G1> | G1,\n line2: Feature<G2> | G2,\n options: { tolerance?: number } = {}\n): FeatureCollection<LineString> {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var tolerance = options.tolerance || 0;\n\n // Containers\n var features: Feature<LineString, GeoJsonProperties>[] = [];\n\n // Create Spatial Index\n var tree = rbush<LineString>();\n\n // To-Do -- HACK way to support typescript\n const line: any = lineSegment(line1);\n tree.load(line);\n var overlapSegment: Feature<LineString> | undefined;\n let additionalSegments: Feature<LineString>[] = [];\n\n // Line Intersection\n\n // Iterate over line segments\n segmentEach(line2, function (segment) {\n var doesOverlaps = false;\n\n if (!segment) {\n return;\n }\n\n // Iterate over each segments which falls within the same bounds\n featureEach(tree.search(segment), function (match) {\n if (doesOverlaps === false) {\n var coordsSegment = getCoords(segment).sort();\n var coordsMatch: any = getCoords(match).sort();\n\n // Segment overlaps feature\n if (equal(coordsSegment, coordsMatch)) {\n doesOverlaps = true;\n // Overlaps already exists - only append last coordinate of segment\n if (overlapSegment) {\n overlapSegment =\n concatSegment(overlapSegment, segment) || overlapSegment;\n } else overlapSegment = segment;\n // Match segments which don't share nodes (Issue #901)\n } else if (\n tolerance === 0\n ? booleanPointOnLine(coordsSegment[0], match) &&\n booleanPointOnLine(coordsSegment[1], match)\n : nearestPointOnLine(match, coordsSegment[0]).properties.dist! <=\n tolerance &&\n nearestPointOnLine(match, coordsSegment[1]).properties.dist! <=\n tolerance\n ) {\n doesOverlaps = true;\n if (overlapSegment) {\n overlapSegment =\n concatSegment(overlapSegment, segment) || overlapSegment;\n } else overlapSegment = segment;\n } else if (\n tolerance === 0\n ? booleanPointOnLine(coordsMatch[0], segment) &&\n booleanPointOnLine(coordsMatch[1], segment)\n : nearestPointOnLine(segment, coordsMatch[0]).properties.dist! <=\n tolerance &&\n nearestPointOnLine(segment, coordsMatch[1]).properties.dist! <=\n tolerance\n ) {\n // Do not define (doesOverlap = true) since more matches can occur within the same segment\n // doesOverlaps = true;\n if (overlapSegment) {\n const combinedSegment = concatSegment(overlapSegment, match);\n if (combinedSegment) {\n overlapSegment = combinedSegment;\n } else {\n additionalSegments.push(match);\n }\n } else overlapSegment = match;\n }\n }\n });\n\n // Segment doesn't overlap - add overlaps to results & reset\n if (doesOverlaps === false && overlapSegment) {\n features.push(overlapSegment);\n if (additionalSegments.length) {\n features = features.concat(additionalSegments);\n additionalSegments = [];\n }\n overlapSegment = undefined;\n }\n });\n // Add last segment if exists\n if (overlapSegment) features.push(overlapSegment);\n\n return featureCollection(features);\n}\n\n/**\n * Concat Segment\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {Feature<LineString>} segment 2-vertex LineString\n * @returns {Feature<LineString>} concat linestring\n */\nfunction concatSegment(\n line: Feature<LineString>,\n segment: Feature<LineString>\n) {\n var coords = getCoords(segment);\n var lineCoords = getCoords(line);\n var start = lineCoords[0];\n var end = lineCoords[lineCoords.length - 1];\n var geom = line.geometry.coordinates;\n\n if (equal(coords[0], start)) geom.unshift(coords[1]);\n else if (equal(coords[0], end)) geom.push(coords[1]);\n else if (equal(coords[1], start)) geom.unshift(coords[0]);\n else if (equal(coords[1], end)) geom.push(coords[0]);\n else return; // If the overlap leaves the segment unchanged, return undefined so that this can be identified.\n\n // Otherwise return the mutated line.\n return line;\n}\n\nexport { lineOverlap };\nexport default lineOverlap;\n"]}
@@ -0,0 +1,25 @@
1
+ import { LineString, MultiLineString, Polygon, MultiPolygon, Feature, FeatureCollection } from 'geojson';
2
+
3
+ /**
4
+ * Takes any LineString or Polygon and returns the overlapping lines between both features.
5
+ *
6
+ * @name lineOverlap
7
+ * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
8
+ * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
9
+ * @param {Object} [options={}] Optional parameters
10
+ * @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)
11
+ * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
12
+ * @example
13
+ * var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
14
+ * var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);
15
+ *
16
+ * var overlapping = turf.lineOverlap(line1, line2);
17
+ *
18
+ * //addToMap
19
+ * var addToMap = [line1, line2, overlapping]
20
+ */
21
+ declare function lineOverlap<G1 extends LineString | MultiLineString | Polygon | MultiPolygon, G2 extends LineString | MultiLineString | Polygon | MultiPolygon>(line1: Feature<G1> | G1, line2: Feature<G2> | G2, options?: {
22
+ tolerance?: number;
23
+ }): FeatureCollection<LineString>;
24
+
25
+ export { lineOverlap as default, lineOverlap };
@@ -1,4 +1,5 @@
1
- import { FeatureCollection, Feature, LineString, MultiLineString, Polygon, MultiPolygon } from "geojson";
1
+ import { LineString, MultiLineString, Polygon, MultiPolygon, Feature, FeatureCollection } from 'geojson';
2
+
2
3
  /**
3
4
  * Takes any LineString or Polygon and returns the overlapping lines between both features.
4
5
  *
@@ -20,4 +21,5 @@ import { FeatureCollection, Feature, LineString, MultiLineString, Polygon, Multi
20
21
  declare function lineOverlap<G1 extends LineString | MultiLineString | Polygon | MultiPolygon, G2 extends LineString | MultiLineString | Polygon | MultiPolygon>(line1: Feature<G1> | G1, line2: Feature<G2> | G2, options?: {
21
22
  tolerance?: number;
22
23
  }): FeatureCollection<LineString>;
23
- export default lineOverlap;
24
+
25
+ export { lineOverlap as default, lineOverlap };
@@ -0,0 +1,96 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { geojsonRbush as rbush } from "@turf/geojson-rbush";
6
+ import { lineSegment } from "@turf/line-segment";
7
+ import { nearestPointOnLine } from "@turf/nearest-point-on-line";
8
+ import { booleanPointOnLine } from "@turf/boolean-point-on-line";
9
+ import { getCoords } from "@turf/invariant";
10
+ import { featureEach, segmentEach } from "@turf/meta";
11
+ import { featureCollection, isObject } from "@turf/helpers";
12
+ import equal from "deep-equal";
13
+ function lineOverlap(line1, line2, options = {}) {
14
+ options = options || {};
15
+ if (!isObject(options))
16
+ throw new Error("options is invalid");
17
+ var tolerance = options.tolerance || 0;
18
+ var features = [];
19
+ var tree = rbush();
20
+ const line = lineSegment(line1);
21
+ tree.load(line);
22
+ var overlapSegment;
23
+ let additionalSegments = [];
24
+ segmentEach(line2, function(segment) {
25
+ var doesOverlaps = false;
26
+ if (!segment) {
27
+ return;
28
+ }
29
+ featureEach(tree.search(segment), function(match) {
30
+ if (doesOverlaps === false) {
31
+ var coordsSegment = getCoords(segment).sort();
32
+ var coordsMatch = getCoords(match).sort();
33
+ if (equal(coordsSegment, coordsMatch)) {
34
+ doesOverlaps = true;
35
+ if (overlapSegment) {
36
+ overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
37
+ } else
38
+ overlapSegment = segment;
39
+ } else if (tolerance === 0 ? booleanPointOnLine(coordsSegment[0], match) && booleanPointOnLine(coordsSegment[1], match) : nearestPointOnLine(match, coordsSegment[0]).properties.dist <= tolerance && nearestPointOnLine(match, coordsSegment[1]).properties.dist <= tolerance) {
40
+ doesOverlaps = true;
41
+ if (overlapSegment) {
42
+ overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
43
+ } else
44
+ overlapSegment = segment;
45
+ } else if (tolerance === 0 ? booleanPointOnLine(coordsMatch[0], segment) && booleanPointOnLine(coordsMatch[1], segment) : nearestPointOnLine(segment, coordsMatch[0]).properties.dist <= tolerance && nearestPointOnLine(segment, coordsMatch[1]).properties.dist <= tolerance) {
46
+ if (overlapSegment) {
47
+ const combinedSegment = concatSegment(overlapSegment, match);
48
+ if (combinedSegment) {
49
+ overlapSegment = combinedSegment;
50
+ } else {
51
+ additionalSegments.push(match);
52
+ }
53
+ } else
54
+ overlapSegment = match;
55
+ }
56
+ }
57
+ });
58
+ if (doesOverlaps === false && overlapSegment) {
59
+ features.push(overlapSegment);
60
+ if (additionalSegments.length) {
61
+ features = features.concat(additionalSegments);
62
+ additionalSegments = [];
63
+ }
64
+ overlapSegment = void 0;
65
+ }
66
+ });
67
+ if (overlapSegment)
68
+ features.push(overlapSegment);
69
+ return featureCollection(features);
70
+ }
71
+ __name(lineOverlap, "lineOverlap");
72
+ function concatSegment(line, segment) {
73
+ var coords = getCoords(segment);
74
+ var lineCoords = getCoords(line);
75
+ var start = lineCoords[0];
76
+ var end = lineCoords[lineCoords.length - 1];
77
+ var geom = line.geometry.coordinates;
78
+ if (equal(coords[0], start))
79
+ geom.unshift(coords[1]);
80
+ else if (equal(coords[0], end))
81
+ geom.push(coords[1]);
82
+ else if (equal(coords[1], start))
83
+ geom.unshift(coords[0]);
84
+ else if (equal(coords[1], end))
85
+ geom.push(coords[0]);
86
+ else
87
+ return;
88
+ return line;
89
+ }
90
+ __name(concatSegment, "concatSegment");
91
+ var turf_line_overlap_default = lineOverlap;
92
+ export {
93
+ turf_line_overlap_default as default,
94
+ lineOverlap
95
+ };
96
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { geojsonRbush as rbush } from \"@turf/geojson-rbush\";\nimport { lineSegment } from \"@turf/line-segment\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { booleanPointOnLine } from \"@turf/boolean-point-on-line\";\nimport { getCoords } from \"@turf/invariant\";\nimport { featureEach, segmentEach } from \"@turf/meta\";\nimport {\n FeatureCollection,\n Feature,\n LineString,\n MultiLineString,\n Polygon,\n MultiPolygon,\n GeoJsonProperties,\n} from \"geojson\";\nimport { featureCollection, isObject } from \"@turf/helpers\";\nimport equal from \"deep-equal\";\n\n/**\n * Takes any LineString or Polygon and returns the overlapping lines between both features.\n *\n * @name lineOverlap\n * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon\n * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)\n * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features\n * @example\n * var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);\n * var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);\n *\n * var overlapping = turf.lineOverlap(line1, line2);\n *\n * //addToMap\n * var addToMap = [line1, line2, overlapping]\n */\nfunction lineOverlap<\n G1 extends LineString | MultiLineString | Polygon | MultiPolygon,\n G2 extends LineString | MultiLineString | Polygon | MultiPolygon,\n>(\n line1: Feature<G1> | G1,\n line2: Feature<G2> | G2,\n options: { tolerance?: number } = {}\n): FeatureCollection<LineString> {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n var tolerance = options.tolerance || 0;\n\n // Containers\n var features: Feature<LineString, GeoJsonProperties>[] = [];\n\n // Create Spatial Index\n var tree = rbush<LineString>();\n\n // To-Do -- HACK way to support typescript\n const line: any = lineSegment(line1);\n tree.load(line);\n var overlapSegment: Feature<LineString> | undefined;\n let additionalSegments: Feature<LineString>[] = [];\n\n // Line Intersection\n\n // Iterate over line segments\n segmentEach(line2, function (segment) {\n var doesOverlaps = false;\n\n if (!segment) {\n return;\n }\n\n // Iterate over each segments which falls within the same bounds\n featureEach(tree.search(segment), function (match) {\n if (doesOverlaps === false) {\n var coordsSegment = getCoords(segment).sort();\n var coordsMatch: any = getCoords(match).sort();\n\n // Segment overlaps feature\n if (equal(coordsSegment, coordsMatch)) {\n doesOverlaps = true;\n // Overlaps already exists - only append last coordinate of segment\n if (overlapSegment) {\n overlapSegment =\n concatSegment(overlapSegment, segment) || overlapSegment;\n } else overlapSegment = segment;\n // Match segments which don't share nodes (Issue #901)\n } else if (\n tolerance === 0\n ? booleanPointOnLine(coordsSegment[0], match) &&\n booleanPointOnLine(coordsSegment[1], match)\n : nearestPointOnLine(match, coordsSegment[0]).properties.dist! <=\n tolerance &&\n nearestPointOnLine(match, coordsSegment[1]).properties.dist! <=\n tolerance\n ) {\n doesOverlaps = true;\n if (overlapSegment) {\n overlapSegment =\n concatSegment(overlapSegment, segment) || overlapSegment;\n } else overlapSegment = segment;\n } else if (\n tolerance === 0\n ? booleanPointOnLine(coordsMatch[0], segment) &&\n booleanPointOnLine(coordsMatch[1], segment)\n : nearestPointOnLine(segment, coordsMatch[0]).properties.dist! <=\n tolerance &&\n nearestPointOnLine(segment, coordsMatch[1]).properties.dist! <=\n tolerance\n ) {\n // Do not define (doesOverlap = true) since more matches can occur within the same segment\n // doesOverlaps = true;\n if (overlapSegment) {\n const combinedSegment = concatSegment(overlapSegment, match);\n if (combinedSegment) {\n overlapSegment = combinedSegment;\n } else {\n additionalSegments.push(match);\n }\n } else overlapSegment = match;\n }\n }\n });\n\n // Segment doesn't overlap - add overlaps to results & reset\n if (doesOverlaps === false && overlapSegment) {\n features.push(overlapSegment);\n if (additionalSegments.length) {\n features = features.concat(additionalSegments);\n additionalSegments = [];\n }\n overlapSegment = undefined;\n }\n });\n // Add last segment if exists\n if (overlapSegment) features.push(overlapSegment);\n\n return featureCollection(features);\n}\n\n/**\n * Concat Segment\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {Feature<LineString>} segment 2-vertex LineString\n * @returns {Feature<LineString>} concat linestring\n */\nfunction concatSegment(\n line: Feature<LineString>,\n segment: Feature<LineString>\n) {\n var coords = getCoords(segment);\n var lineCoords = getCoords(line);\n var start = lineCoords[0];\n var end = lineCoords[lineCoords.length - 1];\n var geom = line.geometry.coordinates;\n\n if (equal(coords[0], start)) geom.unshift(coords[1]);\n else if (equal(coords[0], end)) geom.push(coords[1]);\n else if (equal(coords[1], start)) geom.unshift(coords[0]);\n else if (equal(coords[1], end)) geom.push(coords[0]);\n else return; // If the overlap leaves the segment unchanged, return undefined so that this can be identified.\n\n // Otherwise return the mutated line.\n return line;\n}\n\nexport { lineOverlap };\nexport default lineOverlap;\n"],"mappings":";;;;AAAA,SAAS,gBAAgB,aAAa;AACtC,SAAS,mBAAmB;AAC5B,SAAS,0BAA0B;AACnC,SAAS,0BAA0B;AACnC,SAAS,iBAAiB;AAC1B,SAAS,aAAa,mBAAmB;AAUzC,SAAS,mBAAmB,gBAAgB;AAC5C,OAAO,WAAW;AAoBlB,SAAS,YAIP,OACA,OACA,UAAkC,CAAC,GACJ;AAE/B,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO;AAAG,UAAM,IAAI,MAAM,oBAAoB;AAC5D,M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AAK,KAAK;AAAA,YAC/B;AAAA,UACF;AAAO,6BAAiB;AAAA,QAC1B;AAAA,MACF;AAAA,IACF,CAAC;AAGD,QAAI,iBAAiB,SAAS,gBAAgB;AAC5C,eAAS,KAAK,cAAc;AAC5B,UAAI,mBAAmB,QAAQ;AAC7B,mBAAW,SAAS,OAAO,kBAAkB;AAC7C,6BAAqB,CAAC;AAAA,MACxB;AACA,uBAAiB;AAAA,IACnB;AAAA,EACF,CAAC;AAED,MAAI;AAAgB,aAAS,KAAK,cAAc;AAEhD,SAAO,kBAAkB,QAAQ;AACnC;AArGS;AA+GT,SAAS,cACP,MACA,SACA;AACA,MAAI,SAAS,UAAU,OAAO;AAC9B,MAAI,aAAa,UAAU,IAAI;AAC/B,MAAI,QAAQ,WAAW,CAAC;AACxB,MAAI,MAAM,WAAW,WAAW,SAAS,CAAC;AAC1C,MAAI,OAAO,KAAK,SAAS;AAEzB,MAAI,MAAM,OAAO,CAAC,GAAG,KAAK;AAAG,SAAK,QAAQ,OAAO,CAAC,CAAC;AAAA,WAC1C,MAAM,OAAO,CAAC,GAAG,GAAG;AAAG,SAAK,KAAK,OAAO,CAAC,CAAC;AAAA,WAC1C,MAAM,OAAO,CAAC,GAAG,KAAK;AAAG,SAAK,QAAQ,OAAO,CAAC,CAAC;AAAA,WAC/C,MAAM,OAAO,CAAC,GAAG,GAAG;AAAG,SAAK,KAAK,OAAO,CAAC,CAAC;AAAA;AAC9C;AAGL,SAAO;AACT;AAlBS;AAqBT,IAAO,4BAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/line-overlap",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.0.0",
4
4
  "description": "turf line-overlap module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -26,53 +26,58 @@
26
26
  "line",
27
27
  "overlap"
28
28
  ],
29
- "main": "dist/js/index.js",
30
- "module": "dist/es/index.js",
29
+ "type": "module",
30
+ "main": "dist/cjs/index.cjs",
31
+ "module": "dist/esm/index.js",
32
+ "types": "dist/esm/index.d.ts",
31
33
  "exports": {
32
34
  "./package.json": "./package.json",
33
35
  ".": {
34
- "types": "./dist/js/index.d.ts",
35
- "import": "./dist/es/index.js",
36
- "require": "./dist/js/index.js"
36
+ "import": {
37
+ "types": "./dist/esm/index.d.ts",
38
+ "default": "./dist/esm/index.js"
39
+ },
40
+ "require": {
41
+ "types": "./dist/cjs/index.d.cts",
42
+ "default": "./dist/cjs/index.cjs"
43
+ }
37
44
  }
38
45
  },
39
- "types": "dist/js/index.d.ts",
40
46
  "sideEffects": false,
41
47
  "files": [
42
48
  "dist"
43
49
  ],
44
50
  "scripts": {
45
- "bench": "tsx bench.js",
46
- "build": "npm-run-all build:*",
47
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
48
- "build:js": "tsc",
49
- "docs": "tsx ../../scripts/generate-readmes",
50
- "test": "npm-run-all test:*",
51
- "test:tape": "tsx test.js",
52
- "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
51
+ "bench": "tsx bench.ts",
52
+ "build": "tsup --config ../../tsup.config.ts",
53
+ "docs": "tsx ../../scripts/generate-readmes.ts",
54
+ "test": "npm-run-all --npm-path npm test:*",
55
+ "test:tape": "tsx test.ts",
56
+ "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
53
57
  },
54
58
  "devDependencies": {
55
- "@types/deep-equal": "^1.0.1",
56
- "@types/tape": "*",
57
- "benchmark": "*",
58
- "load-json-file": "*",
59
- "npm-run-all": "*",
60
- "tape": "*",
61
- "tslint": "*",
62
- "tsx": "*",
63
- "typescript": "*",
64
- "write-json-file": "*"
59
+ "@types/benchmark": "^2.1.5",
60
+ "@types/deep-equal": "^1.0.4",
61
+ "@types/tape": "^4.2.32",
62
+ "benchmark": "^2.1.4",
63
+ "load-json-file": "^7.0.1",
64
+ "npm-run-all": "^4.1.5",
65
+ "tape": "^5.7.2",
66
+ "tsup": "^8.0.1",
67
+ "tsx": "^4.6.2",
68
+ "typescript": "^5.2.2",
69
+ "write-json-file": "^5.0.0"
65
70
  },
66
71
  "dependencies": {
67
- "@turf/boolean-point-on-line": "^7.0.0-alpha.2",
68
- "@turf/geojson-rbush": "^3.2.0",
69
- "@turf/helpers": "^7.0.0-alpha.2",
70
- "@turf/invariant": "^7.0.0-alpha.2",
71
- "@turf/line-segment": "^7.0.0-alpha.2",
72
- "@turf/meta": "^7.0.0-alpha.2",
73
- "@turf/nearest-point-on-line": "^7.0.0-alpha.2",
74
- "deep-equal": "1.x",
75
- "tslib": "^2.3.0"
72
+ "@turf/boolean-point-on-line": "^7.0.0",
73
+ "@turf/geojson-rbush": "^7.0.0",
74
+ "@turf/helpers": "^7.0.0",
75
+ "@turf/invariant": "^7.0.0",
76
+ "@turf/line-segment": "^7.0.0",
77
+ "@turf/meta": "^7.0.0",
78
+ "@turf/nearest-point-on-line": "^7.0.0",
79
+ "deep-equal": "^2.2.3",
80
+ "tslib": "^2.6.2"
76
81
  },
77
- "gitHead": "dd35b52725945b4fa29a98d9a550733e06cc222e"
82
+ "gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
78
83
  }
package/dist/es/index.js DELETED
@@ -1,146 +0,0 @@
1
- import rbush from "@turf/geojson-rbush";
2
- import lineSegment from "@turf/line-segment";
3
- import nearestPointOnLine from "@turf/nearest-point-on-line";
4
- import booleanPointOnLine from "@turf/boolean-point-on-line";
5
- import { getCoords } from "@turf/invariant";
6
- import { featureEach, segmentEach } from "@turf/meta";
7
- import { featureCollection, isObject } from "@turf/helpers";
8
- import equal from "deep-equal";
9
- /**
10
- * Takes any LineString or Polygon and returns the overlapping lines between both features.
11
- *
12
- * @name lineOverlap
13
- * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
14
- * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
15
- * @param {Object} [options={}] Optional parameters
16
- * @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)
17
- * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
18
- * @example
19
- * var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
20
- * var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);
21
- *
22
- * var overlapping = turf.lineOverlap(line1, line2);
23
- *
24
- * //addToMap
25
- * var addToMap = [line1, line2, overlapping]
26
- */
27
- function lineOverlap(line1, line2, options = {}) {
28
- // Optional parameters
29
- options = options || {};
30
- if (!isObject(options))
31
- throw new Error("options is invalid");
32
- var tolerance = options.tolerance || 0;
33
- // Containers
34
- var features = [];
35
- // Create Spatial Index
36
- var tree = rbush();
37
- // To-Do -- HACK way to support typescript
38
- const line = lineSegment(line1);
39
- tree.load(line);
40
- var overlapSegment;
41
- let additionalSegments = [];
42
- // Line Intersection
43
- // Iterate over line segments
44
- segmentEach(line2, function (segment) {
45
- var doesOverlaps = false;
46
- if (!segment) {
47
- return;
48
- }
49
- // Iterate over each segments which falls within the same bounds
50
- featureEach(tree.search(segment), function (match) {
51
- if (doesOverlaps === false) {
52
- var coordsSegment = getCoords(segment).sort();
53
- var coordsMatch = getCoords(match).sort();
54
- // Segment overlaps feature
55
- if (equal(coordsSegment, coordsMatch)) {
56
- doesOverlaps = true;
57
- // Overlaps already exists - only append last coordinate of segment
58
- if (overlapSegment) {
59
- overlapSegment =
60
- concatSegment(overlapSegment, segment) || overlapSegment;
61
- }
62
- else
63
- overlapSegment = segment;
64
- // Match segments which don't share nodes (Issue #901)
65
- }
66
- else if (tolerance === 0
67
- ? booleanPointOnLine(coordsSegment[0], match) &&
68
- booleanPointOnLine(coordsSegment[1], match)
69
- : nearestPointOnLine(match, coordsSegment[0]).properties.dist <=
70
- tolerance &&
71
- nearestPointOnLine(match, coordsSegment[1]).properties.dist <=
72
- tolerance) {
73
- doesOverlaps = true;
74
- if (overlapSegment) {
75
- overlapSegment =
76
- concatSegment(overlapSegment, segment) || overlapSegment;
77
- }
78
- else
79
- overlapSegment = segment;
80
- }
81
- else if (tolerance === 0
82
- ? booleanPointOnLine(coordsMatch[0], segment) &&
83
- booleanPointOnLine(coordsMatch[1], segment)
84
- : nearestPointOnLine(segment, coordsMatch[0]).properties.dist <=
85
- tolerance &&
86
- nearestPointOnLine(segment, coordsMatch[1]).properties.dist <=
87
- tolerance) {
88
- // Do not define (doesOverlap = true) since more matches can occur within the same segment
89
- // doesOverlaps = true;
90
- if (overlapSegment) {
91
- const combinedSegment = concatSegment(overlapSegment, match);
92
- if (combinedSegment) {
93
- overlapSegment = combinedSegment;
94
- }
95
- else {
96
- additionalSegments.push(match);
97
- }
98
- }
99
- else
100
- overlapSegment = match;
101
- }
102
- }
103
- });
104
- // Segment doesn't overlap - add overlaps to results & reset
105
- if (doesOverlaps === false && overlapSegment) {
106
- features.push(overlapSegment);
107
- if (additionalSegments.length) {
108
- features = features.concat(additionalSegments);
109
- additionalSegments = [];
110
- }
111
- overlapSegment = undefined;
112
- }
113
- });
114
- // Add last segment if exists
115
- if (overlapSegment)
116
- features.push(overlapSegment);
117
- return featureCollection(features);
118
- }
119
- /**
120
- * Concat Segment
121
- *
122
- * @private
123
- * @param {Feature<LineString>} line LineString
124
- * @param {Feature<LineString>} segment 2-vertex LineString
125
- * @returns {Feature<LineString>} concat linestring
126
- */
127
- function concatSegment(line, segment) {
128
- var coords = getCoords(segment);
129
- var lineCoords = getCoords(line);
130
- var start = lineCoords[0];
131
- var end = lineCoords[lineCoords.length - 1];
132
- var geom = line.geometry.coordinates;
133
- if (equal(coords[0], start))
134
- geom.unshift(coords[1]);
135
- else if (equal(coords[0], end))
136
- geom.push(coords[1]);
137
- else if (equal(coords[1], start))
138
- geom.unshift(coords[0]);
139
- else if (equal(coords[1], end))
140
- geom.push(coords[0]);
141
- else
142
- return; // If the overlap leaves the segment unchanged, return undefined so that this can be identified.
143
- // Otherwise return the mutated line.
144
- return line;
145
- }
146
- export default lineOverlap;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,149 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const geojson_rbush_1 = tslib_1.__importDefault(require("@turf/geojson-rbush"));
5
- const line_segment_1 = tslib_1.__importDefault(require("@turf/line-segment"));
6
- const nearest_point_on_line_1 = tslib_1.__importDefault(require("@turf/nearest-point-on-line"));
7
- const boolean_point_on_line_1 = tslib_1.__importDefault(require("@turf/boolean-point-on-line"));
8
- const invariant_1 = require("@turf/invariant");
9
- const meta_1 = require("@turf/meta");
10
- const helpers_1 = require("@turf/helpers");
11
- const deep_equal_1 = tslib_1.__importDefault(require("deep-equal"));
12
- /**
13
- * Takes any LineString or Polygon and returns the overlapping lines between both features.
14
- *
15
- * @name lineOverlap
16
- * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
17
- * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
18
- * @param {Object} [options={}] Optional parameters
19
- * @param {number} [options.tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)
20
- * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
21
- * @example
22
- * var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
23
- * var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);
24
- *
25
- * var overlapping = turf.lineOverlap(line1, line2);
26
- *
27
- * //addToMap
28
- * var addToMap = [line1, line2, overlapping]
29
- */
30
- function lineOverlap(line1, line2, options = {}) {
31
- // Optional parameters
32
- options = options || {};
33
- if (!helpers_1.isObject(options))
34
- throw new Error("options is invalid");
35
- var tolerance = options.tolerance || 0;
36
- // Containers
37
- var features = [];
38
- // Create Spatial Index
39
- var tree = geojson_rbush_1.default();
40
- // To-Do -- HACK way to support typescript
41
- const line = line_segment_1.default(line1);
42
- tree.load(line);
43
- var overlapSegment;
44
- let additionalSegments = [];
45
- // Line Intersection
46
- // Iterate over line segments
47
- meta_1.segmentEach(line2, function (segment) {
48
- var doesOverlaps = false;
49
- if (!segment) {
50
- return;
51
- }
52
- // Iterate over each segments which falls within the same bounds
53
- meta_1.featureEach(tree.search(segment), function (match) {
54
- if (doesOverlaps === false) {
55
- var coordsSegment = invariant_1.getCoords(segment).sort();
56
- var coordsMatch = invariant_1.getCoords(match).sort();
57
- // Segment overlaps feature
58
- if (deep_equal_1.default(coordsSegment, coordsMatch)) {
59
- doesOverlaps = true;
60
- // Overlaps already exists - only append last coordinate of segment
61
- if (overlapSegment) {
62
- overlapSegment =
63
- concatSegment(overlapSegment, segment) || overlapSegment;
64
- }
65
- else
66
- overlapSegment = segment;
67
- // Match segments which don't share nodes (Issue #901)
68
- }
69
- else if (tolerance === 0
70
- ? boolean_point_on_line_1.default(coordsSegment[0], match) &&
71
- boolean_point_on_line_1.default(coordsSegment[1], match)
72
- : nearest_point_on_line_1.default(match, coordsSegment[0]).properties.dist <=
73
- tolerance &&
74
- nearest_point_on_line_1.default(match, coordsSegment[1]).properties.dist <=
75
- tolerance) {
76
- doesOverlaps = true;
77
- if (overlapSegment) {
78
- overlapSegment =
79
- concatSegment(overlapSegment, segment) || overlapSegment;
80
- }
81
- else
82
- overlapSegment = segment;
83
- }
84
- else if (tolerance === 0
85
- ? boolean_point_on_line_1.default(coordsMatch[0], segment) &&
86
- boolean_point_on_line_1.default(coordsMatch[1], segment)
87
- : nearest_point_on_line_1.default(segment, coordsMatch[0]).properties.dist <=
88
- tolerance &&
89
- nearest_point_on_line_1.default(segment, coordsMatch[1]).properties.dist <=
90
- tolerance) {
91
- // Do not define (doesOverlap = true) since more matches can occur within the same segment
92
- // doesOverlaps = true;
93
- if (overlapSegment) {
94
- const combinedSegment = concatSegment(overlapSegment, match);
95
- if (combinedSegment) {
96
- overlapSegment = combinedSegment;
97
- }
98
- else {
99
- additionalSegments.push(match);
100
- }
101
- }
102
- else
103
- overlapSegment = match;
104
- }
105
- }
106
- });
107
- // Segment doesn't overlap - add overlaps to results & reset
108
- if (doesOverlaps === false && overlapSegment) {
109
- features.push(overlapSegment);
110
- if (additionalSegments.length) {
111
- features = features.concat(additionalSegments);
112
- additionalSegments = [];
113
- }
114
- overlapSegment = undefined;
115
- }
116
- });
117
- // Add last segment if exists
118
- if (overlapSegment)
119
- features.push(overlapSegment);
120
- return helpers_1.featureCollection(features);
121
- }
122
- /**
123
- * Concat Segment
124
- *
125
- * @private
126
- * @param {Feature<LineString>} line LineString
127
- * @param {Feature<LineString>} segment 2-vertex LineString
128
- * @returns {Feature<LineString>} concat linestring
129
- */
130
- function concatSegment(line, segment) {
131
- var coords = invariant_1.getCoords(segment);
132
- var lineCoords = invariant_1.getCoords(line);
133
- var start = lineCoords[0];
134
- var end = lineCoords[lineCoords.length - 1];
135
- var geom = line.geometry.coordinates;
136
- if (deep_equal_1.default(coords[0], start))
137
- geom.unshift(coords[1]);
138
- else if (deep_equal_1.default(coords[0], end))
139
- geom.push(coords[1]);
140
- else if (deep_equal_1.default(coords[1], start))
141
- geom.unshift(coords[0]);
142
- else if (deep_equal_1.default(coords[1], end))
143
- geom.push(coords[0]);
144
- else
145
- return; // If the overlap leaves the segment unchanged, return undefined so that this can be identified.
146
- // Otherwise return the mutated line.
147
- return line;
148
- }
149
- exports.default = lineOverlap;