@turf/line-overlap 4.5.2 → 4.7.3

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
@@ -8,28 +8,14 @@ Takes any LineString or Polygon and returns the overlapping lines between both f
8
8
 
9
9
  - `line1` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** any LineString or Polygon
10
10
  - `line2` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** any LineString or Polygon
11
+ - `tolerance` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** Tolerance distance to match overlapping line segments (in kilometers) (optional, default `0`)
11
12
 
12
13
  **Examples**
13
14
 
14
15
  ```javascript
15
- var line1 = {
16
- "type": "Feature",
17
- "properties": {},
18
- "geometry": {
19
- "type": "LineString",
20
- "coordinates": [[115, -35], [125, -30], [135, -30], [145, -35]
21
- ]
22
- }
23
- }
24
- var line2 = {
25
- "type": "Feature",
26
- "properties": {},
27
- "geometry": {
28
- "type": "LineString",
29
- "coordinates": [[115, -25], [125, -30], [135, -30], [145, -25]
30
- ]
31
- }
32
- }
16
+ var line1 = turf.lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
17
+ var line2 = turf.lineString([[115, -25], [125, -30], [135, -30], [145, -25]]);
18
+
33
19
  var overlapping = turf.lineOverlap(line1, line2);
34
20
 
35
21
  //addToMap
package/index.d.ts CHANGED
@@ -7,6 +7,11 @@ type Feature<Geom extends GeoJSON.GeometryObject> = GeoJSON.Feature<Geom>;
7
7
  /**
8
8
  * http://turfjs.org/docs/#lineoverlap
9
9
  */
10
- declare function lineOverlap<Geom1 extends Geoms, Geom2 extends Geoms>(source: Feature<Geom1> | Geom1, target: Feature<Geom2> | Geom2, precision?: number): LineStrings;
10
+ declare function lineOverlap(
11
+ source: Feature<Geoms> | Geoms,
12
+ target: Feature<Geoms> | Geoms,
13
+ tolerance?: number
14
+ ): LineStrings;
15
+
11
16
  declare namespace lineOverlap {}
12
17
  export = lineOverlap;
package/index.js CHANGED
@@ -1,9 +1,14 @@
1
- var lineSegment = require('@turf/line-segment');
2
- var getCoords = require('@turf/invariant').getCoords;
3
- var rbush = require('geojson-rbush');
1
+ var meta = require('@turf/meta');
4
2
  var equal = require('deep-equal');
3
+ var rbush = require('geojson-rbush');
4
+ var invariant = require('@turf/invariant');
5
+ var lineSegment = require('@turf/line-segment');
6
+ var pointOnLine = require('@turf/point-on-line');
7
+ var booleanPointOnLine = require('@turf/boolean-point-on-line');
5
8
  var featureCollection = require('@turf/helpers').featureCollection;
6
- var featureEach = require('@turf/meta').featureEach;
9
+ var getCoords = invariant.getCoords;
10
+ var featureEach = meta.featureEach;
11
+ var segmentEach = meta.segmentEach;
7
12
 
8
13
  /**
9
14
  * Takes any LineString or Polygon and returns the overlapping lines between both features.
@@ -11,66 +16,76 @@ var featureEach = require('@turf/meta').featureEach;
11
16
  * @name lineOverlap
12
17
  * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
13
18
  * @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
19
+ * @param {number} [tolerance=0] Tolerance distance to match overlapping line segments (in kilometers)
14
20
  * @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
15
21
  * @example
16
- * var line1 = {
17
- * "type": "Feature",
18
- * "properties": {},
19
- * "geometry": {
20
- * "type": "LineString",
21
- * "coordinates": [[115, -35], [125, -30], [135, -30], [145, -35]
22
- * ]
23
- * }
24
- * }
25
- * var line2 = {
26
- * "type": "Feature",
27
- * "properties": {},
28
- * "geometry": {
29
- * "type": "LineString",
30
- * "coordinates": [[115, -25], [125, -30], [135, -30], [145, -25]
31
- * ]
32
- * }
33
- * }
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
+ *
34
25
  * var overlapping = turf.lineOverlap(line1, line2);
35
26
  *
36
27
  * //addToMap
37
28
  * var addToMap = [line1, line2, overlapping]
38
29
  */
39
- module.exports = function (line1, line2) {
40
- var results = [];
30
+ module.exports = function (line1, line2, tolerance) {
31
+ var features = [];
32
+ tolerance = tolerance || 0;
41
33
 
42
34
  // Create Spatial Index
43
35
  var tree = rbush();
44
36
  tree.load(lineSegment(line1));
45
- var overlaps;
37
+ var overlapSegment;
38
+
39
+ // Line Intersection
46
40
 
47
41
  // Iterate over line segments
48
- featureEach(lineSegment(line2), function (segment) {
42
+ segmentEach(line2, function (segment) {
49
43
  var doesOverlaps = false;
44
+
45
+ // Iterate over each segments which falls within the same bounds
50
46
  featureEach(tree.search(segment), function (match) {
51
47
  if (doesOverlaps === false) {
52
- var coords1 = getCoords(segment).sort();
53
- var coords2 = getCoords(match).sort();
48
+ var coordsSegment = getCoords(segment).sort();
49
+ var coordsMatch = getCoords(match).sort();
54
50
 
55
51
  // Segment overlaps feature
56
- if (equal(coords1, coords2)) {
52
+ if (equal(coordsSegment, coordsMatch)) {
57
53
  doesOverlaps = true;
58
54
  // Overlaps already exists - only append last coordinate of segment
59
- if (overlaps) overlaps = concatSegment(overlaps, segment);
60
- else overlaps = segment;
55
+ if (overlapSegment) overlapSegment = concatSegment(overlapSegment, segment);
56
+ else overlapSegment = segment;
57
+ // Match segments which don't share nodes (Issue #901)
58
+ } else if (
59
+ (tolerance === 0) ?
60
+ booleanPointOnLine(coordsSegment[0], match) && booleanPointOnLine(coordsSegment[1], match) :
61
+ pointOnLine(match, coordsSegment[0]).properties.dist <= tolerance &&
62
+ pointOnLine(match, coordsSegment[1]).properties.dist <= tolerance) {
63
+ doesOverlaps = true;
64
+ if (overlapSegment) overlapSegment = concatSegment(overlapSegment, segment);
65
+ else overlapSegment = segment;
66
+ } else if (
67
+ (tolerance === 0) ?
68
+ booleanPointOnLine(coordsMatch[0], segment) && booleanPointOnLine(coordsMatch[1], segment) :
69
+ pointOnLine(segment, coordsMatch[0]).properties.dist <= tolerance &&
70
+ pointOnLine(segment, coordsMatch[1]).properties.dist <= tolerance) {
71
+ // Do not define (doesOverlap = true) since more matches can occur within the same segment
72
+ // doesOverlaps = true;
73
+ if (overlapSegment) overlapSegment = concatSegment(overlapSegment, match);
74
+ else overlapSegment = match;
61
75
  }
62
76
  }
63
77
  });
78
+
64
79
  // Segment doesn't overlap - add overlaps to results & reset
65
- if (doesOverlaps === false && overlaps) {
66
- results.push(overlaps);
67
- overlaps = undefined;
80
+ if (doesOverlaps === false && overlapSegment) {
81
+ features.push(overlapSegment);
82
+ overlapSegment = undefined;
68
83
  }
69
84
  });
70
85
  // Add last segment if exists
71
- if (overlaps) results.push(overlaps);
86
+ if (overlapSegment) features.push(overlapSegment);
72
87
 
73
- return featureCollection(results);
88
+ return featureCollection(features);
74
89
  };
75
90
 
76
91
 
@@ -87,15 +102,11 @@ function concatSegment(line, segment) {
87
102
  var lineCoords = getCoords(line);
88
103
  var start = lineCoords[0];
89
104
  var end = lineCoords[lineCoords.length - 1];
105
+ var geom = line.geometry.coordinates;
90
106
 
91
- if (equal(coords[0], start)) {
92
- line.geometry.coordinates.unshift(coords[1]);
93
- } else if (equal(coords[0], end)) {
94
- line.geometry.coordinates.push(coords[1]);
95
- } else if (equal(coords[1], start)) {
96
- line.geometry.coordinates.unshift(coords[0]);
97
- } else if (equal(coords[1], end)) {
98
- line.geometry.coordinates.push(coords[0]);
99
- }
107
+ if (equal(coords[0], start)) geom.unshift(coords[1]);
108
+ else if (equal(coords[0], end)) geom.push(coords[1]);
109
+ else if (equal(coords[1], start)) geom.unshift(coords[0]);
110
+ else if (equal(coords[1], end)) geom.push(coords[0]);
100
111
  return line;
101
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/line-overlap",
3
- "version": "4.5.2",
3
+ "version": "4.7.3",
4
4
  "description": "turf line-overlap module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -39,10 +39,12 @@
39
39
  "write-json-file": "^2.0.0"
40
40
  },
41
41
  "dependencies": {
42
- "@turf/helpers": "^4.5.2",
43
- "@turf/invariant": "^4.5.2",
44
- "@turf/line-segment": "^4.5.2",
45
- "@turf/meta": "^4.5.2",
42
+ "@turf/boolean-point-on-line": "^4.7.3",
43
+ "@turf/helpers": "^4.7.3",
44
+ "@turf/invariant": "^4.7.3",
45
+ "@turf/line-segment": "^4.7.3",
46
+ "@turf/meta": "^4.7.3",
47
+ "@turf/point-on-line": "^4.7.3",
46
48
  "deep-equal": "^1.0.1",
47
49
  "geojson-rbush": "^1.0.1"
48
50
  }