@turf/kinks 6.4.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
@@ -4,53 +4,46 @@
4
4
 
5
5
  ## kinks
6
6
 
7
- Takes a [linestring][1], [multi-linestring][2], [multi-polygon][3], or [polygon][4] and returns [points][5] at all self-intersections.
7
+ Takes a [linestring][1], [multi-linestring][2],
8
+ [multi-polygon][3] or [polygon][4] and
9
+ returns [points][5] at all self-intersections.
8
10
 
9
- **Parameters**
11
+ ### Parameters
10
12
 
11
- - `featureIn` **[Feature][6]<([LineString][7] \| [MultiLineString][8] \| [MultiPolygon][9] \| [Polygon][10])>** input feature
13
+ - `featureIn` **[Feature][6]<([LineString][7] | [MultiLineString][8] | [MultiPolygon][9] | [Polygon][10])>** input feature
12
14
 
13
- **Examples**
15
+ ### Examples
14
16
 
15
17
  ```javascript
16
- var poly = turf.polygon([[
17
- [-12.034835, 8.901183],
18
- [-12.060413, 8.899826],
19
- [-12.03638, 8.873199],
20
- [-12.059383, 8.871418],
21
- [-12.034835, 8.901183]
22
- ]]);
18
+ var poly = turf.polygon([
19
+ [
20
+ [-12.034835, 8.901183],
21
+ [-12.060413, 8.899826],
22
+ [-12.03638, 8.873199],
23
+ [-12.059383, 8.871418],
24
+ [-12.034835, 8.901183],
25
+ ],
26
+ ]);
23
27
 
24
28
  var kinks = turf.kinks(poly);
25
29
 
26
30
  //addToMap
27
- var addToMap = [poly, kinks]
31
+ var addToMap = [poly, kinks];
28
32
  ```
29
33
 
30
- Returns **[FeatureCollection][11]&lt;[Point][12]>** self-intersections
34
+ Returns **[FeatureCollection][11]<[Point][12]>** self-intersections
31
35
 
32
36
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.4
33
-
34
37
  [2]: https://tools.ietf.org/html/rfc7946#section-3.1.5
35
-
36
38
  [3]: https://tools.ietf.org/html/rfc7946#section-3.1.7
37
-
38
39
  [4]: https://tools.ietf.org/html/rfc7946#section-3.1.6
39
-
40
40
  [5]: https://tools.ietf.org/html/rfc7946#section-3.1.2
41
-
42
41
  [6]: https://tools.ietf.org/html/rfc7946#section-3.2
43
-
44
42
  [7]: https://tools.ietf.org/html/rfc7946#section-3.1.4
45
-
46
43
  [8]: https://tools.ietf.org/html/rfc7946#section-3.1.5
47
-
48
44
  [9]: https://tools.ietf.org/html/rfc7946#section-3.1.7
49
-
50
45
  [10]: https://tools.ietf.org/html/rfc7946#section-3.1.6
51
-
52
46
  [11]: https://tools.ietf.org/html/rfc7946#section-3.3
53
-
54
47
  [12]: https://tools.ietf.org/html/rfc7946#section-3.1.2
55
48
 
56
49
  <!-- This file is automatically generated. Please don't edit it directly:
package/dist/es/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import findIntersections from "sweepline-intersections";
1
2
  import { point } from "@turf/helpers";
2
3
  /**
3
4
  * Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring},
@@ -22,115 +23,20 @@ import { point } from "@turf/helpers";
22
23
  * var addToMap = [poly, kinks]
23
24
  */
24
25
  export default function kinks(featureIn) {
25
- var coordinates;
26
- var feature;
27
- var results = {
26
+ const results = {
28
27
  type: "FeatureCollection",
29
28
  features: [],
30
29
  };
31
- if (featureIn.type === "Feature") {
32
- feature = featureIn.geometry;
33
- }
34
- else {
35
- feature = featureIn;
36
- }
37
- if (feature.type === "LineString") {
38
- coordinates = [feature.coordinates];
39
- }
40
- else if (feature.type === "MultiLineString") {
41
- coordinates = feature.coordinates;
42
- }
43
- else if (feature.type === "MultiPolygon") {
44
- coordinates = [].concat.apply([], feature.coordinates);
45
- }
46
- else if (feature.type === "Polygon") {
47
- coordinates = feature.coordinates;
48
- }
49
- else {
30
+ if (featureIn.type === "Feature" &&
31
+ (featureIn.geometry.type === "Point" ||
32
+ featureIn.geometry.type === "MultiPoint")) {
50
33
  throw new Error("Input must be a LineString, MultiLineString, " +
51
34
  "Polygon, or MultiPolygon Feature or Geometry");
52
35
  }
53
- coordinates.forEach(function (line1) {
54
- coordinates.forEach(function (line2) {
55
- for (var i = 0; i < line1.length - 1; i++) {
56
- // start iteration at i, intersections for k < i have already
57
- // been checked in previous outer loop iterations
58
- for (var k = i; k < line2.length - 1; k++) {
59
- if (line1 === line2) {
60
- // segments are adjacent and always share a vertex, not a kink
61
- if (Math.abs(i - k) === 1) {
62
- continue;
63
- }
64
- // first and last segment in a closed lineString or ring always share a vertex, not a kink
65
- if (
66
- // segments are first and last segment of lineString
67
- i === 0 &&
68
- k === line1.length - 2 &&
69
- // lineString is closed
70
- line1[i][0] === line1[line1.length - 1][0] &&
71
- line1[i][1] === line1[line1.length - 1][1]) {
72
- continue;
73
- }
74
- }
75
- var intersection = lineIntersects(line1[i][0], line1[i][1], line1[i + 1][0], line1[i + 1][1], line2[k][0], line2[k][1], line2[k + 1][0], line2[k + 1][1]);
76
- if (intersection) {
77
- results.features.push(point([intersection[0], intersection[1]]));
78
- }
79
- }
80
- }
81
- });
82
- });
83
- return results;
84
- }
85
- // modified from http://jsfiddle.net/justin_c_rounds/Gd2S2/light/
86
- function lineIntersects(line1StartX, line1StartY, line1EndX, line1EndY, line2StartX, line2StartY, line2EndX, line2EndY) {
87
- // if the lines intersect, the result contains the x and y of the
88
- // intersection (treating the lines as infinite) and booleans for whether
89
- // line segment 1 or line segment 2 contain the point
90
- var denominator;
91
- var a;
92
- var b;
93
- var numerator1;
94
- var numerator2;
95
- var result = {
96
- x: null,
97
- y: null,
98
- onLine1: false,
99
- onLine2: false,
100
- };
101
- denominator =
102
- (line2EndY - line2StartY) * (line1EndX - line1StartX) -
103
- (line2EndX - line2StartX) * (line1EndY - line1StartY);
104
- if (denominator === 0) {
105
- if (result.x !== null && result.y !== null) {
106
- return result;
107
- }
108
- else {
109
- return false;
110
- }
111
- }
112
- a = line1StartY - line2StartY;
113
- b = line1StartX - line2StartX;
114
- numerator1 = (line2EndX - line2StartX) * a - (line2EndY - line2StartY) * b;
115
- numerator2 = (line1EndX - line1StartX) * a - (line1EndY - line1StartY) * b;
116
- a = numerator1 / denominator;
117
- b = numerator2 / denominator;
118
- // if we cast these lines infinitely in both directions, they intersect here:
119
- result.x = line1StartX + a * (line1EndX - line1StartX);
120
- result.y = line1StartY + a * (line1EndY - line1StartY);
121
- // if line1 is a segment and line2 is infinite, they intersect if:
122
- if (a >= 0 && a <= 1) {
123
- result.onLine1 = true;
124
- }
125
- // if line2 is a segment and line1 is infinite, they intersect if:
126
- if (b >= 0 && b <= 1) {
127
- result.onLine2 = true;
128
- }
129
- // if line1 and line2 are segments, they intersect if both of the above are true
130
- if (result.onLine1 && result.onLine2) {
131
- return [result.x, result.y];
132
- }
133
- else {
134
- return false;
36
+ const intersections = findIntersections(featureIn, false);
37
+ for (let i = 0; i < intersections.length; ++i) {
38
+ const intersection = intersections[i];
39
+ results.features.push(point([intersection[0], intersection[1]]));
135
40
  }
41
+ return results;
136
42
  }
@@ -1,4 +1,4 @@
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 a {@link LineString|linestring}, {@link MultiLineString|multi-linestring},
4
4
  * {@link MultiPolygon|multi-polygon} or {@link Polygon|polygon} and
@@ -21,4 +21,4 @@ import { Feature, FeatureCollection, LineString, MultiLineString, MultiPolygon,
21
21
  * //addToMap
22
22
  * var addToMap = [poly, kinks]
23
23
  */
24
- export default function kinks<T extends LineString | MultiLineString | Polygon | MultiPolygon>(featureIn: Feature<T> | T): FeatureCollection<Point>;
24
+ export default function kinks<T extends LineString | MultiLineString | Polygon | MultiPolygon>(featureIn: Feature<T>): FeatureCollection<Point>;
package/dist/js/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("@turf/helpers");
3
+ const tslib_1 = require("tslib");
4
+ const sweepline_intersections_1 = tslib_1.__importDefault(require("sweepline-intersections"));
5
+ const helpers_1 = require("@turf/helpers");
4
6
  /**
5
7
  * Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring},
6
8
  * {@link MultiPolygon|multi-polygon} or {@link Polygon|polygon} and
@@ -24,116 +26,21 @@ var helpers_1 = require("@turf/helpers");
24
26
  * var addToMap = [poly, kinks]
25
27
  */
26
28
  function kinks(featureIn) {
27
- var coordinates;
28
- var feature;
29
- var results = {
29
+ const results = {
30
30
  type: "FeatureCollection",
31
31
  features: [],
32
32
  };
33
- if (featureIn.type === "Feature") {
34
- feature = featureIn.geometry;
35
- }
36
- else {
37
- feature = featureIn;
38
- }
39
- if (feature.type === "LineString") {
40
- coordinates = [feature.coordinates];
41
- }
42
- else if (feature.type === "MultiLineString") {
43
- coordinates = feature.coordinates;
44
- }
45
- else if (feature.type === "MultiPolygon") {
46
- coordinates = [].concat.apply([], feature.coordinates);
47
- }
48
- else if (feature.type === "Polygon") {
49
- coordinates = feature.coordinates;
50
- }
51
- else {
33
+ if (featureIn.type === "Feature" &&
34
+ (featureIn.geometry.type === "Point" ||
35
+ featureIn.geometry.type === "MultiPoint")) {
52
36
  throw new Error("Input must be a LineString, MultiLineString, " +
53
37
  "Polygon, or MultiPolygon Feature or Geometry");
54
38
  }
55
- coordinates.forEach(function (line1) {
56
- coordinates.forEach(function (line2) {
57
- for (var i = 0; i < line1.length - 1; i++) {
58
- // start iteration at i, intersections for k < i have already
59
- // been checked in previous outer loop iterations
60
- for (var k = i; k < line2.length - 1; k++) {
61
- if (line1 === line2) {
62
- // segments are adjacent and always share a vertex, not a kink
63
- if (Math.abs(i - k) === 1) {
64
- continue;
65
- }
66
- // first and last segment in a closed lineString or ring always share a vertex, not a kink
67
- if (
68
- // segments are first and last segment of lineString
69
- i === 0 &&
70
- k === line1.length - 2 &&
71
- // lineString is closed
72
- line1[i][0] === line1[line1.length - 1][0] &&
73
- line1[i][1] === line1[line1.length - 1][1]) {
74
- continue;
75
- }
76
- }
77
- var intersection = lineIntersects(line1[i][0], line1[i][1], line1[i + 1][0], line1[i + 1][1], line2[k][0], line2[k][1], line2[k + 1][0], line2[k + 1][1]);
78
- if (intersection) {
79
- results.features.push(helpers_1.point([intersection[0], intersection[1]]));
80
- }
81
- }
82
- }
83
- });
84
- });
39
+ const intersections = sweepline_intersections_1.default(featureIn, false);
40
+ for (let i = 0; i < intersections.length; ++i) {
41
+ const intersection = intersections[i];
42
+ results.features.push(helpers_1.point([intersection[0], intersection[1]]));
43
+ }
85
44
  return results;
86
45
  }
87
46
  exports.default = kinks;
88
- // modified from http://jsfiddle.net/justin_c_rounds/Gd2S2/light/
89
- function lineIntersects(line1StartX, line1StartY, line1EndX, line1EndY, line2StartX, line2StartY, line2EndX, line2EndY) {
90
- // if the lines intersect, the result contains the x and y of the
91
- // intersection (treating the lines as infinite) and booleans for whether
92
- // line segment 1 or line segment 2 contain the point
93
- var denominator;
94
- var a;
95
- var b;
96
- var numerator1;
97
- var numerator2;
98
- var result = {
99
- x: null,
100
- y: null,
101
- onLine1: false,
102
- onLine2: false,
103
- };
104
- denominator =
105
- (line2EndY - line2StartY) * (line1EndX - line1StartX) -
106
- (line2EndX - line2StartX) * (line1EndY - line1StartY);
107
- if (denominator === 0) {
108
- if (result.x !== null && result.y !== null) {
109
- return result;
110
- }
111
- else {
112
- return false;
113
- }
114
- }
115
- a = line1StartY - line2StartY;
116
- b = line1StartX - line2StartX;
117
- numerator1 = (line2EndX - line2StartX) * a - (line2EndY - line2StartY) * b;
118
- numerator2 = (line1EndX - line1StartX) * a - (line1EndY - line1StartY) * b;
119
- a = numerator1 / denominator;
120
- b = numerator2 / denominator;
121
- // if we cast these lines infinitely in both directions, they intersect here:
122
- result.x = line1StartX + a * (line1EndX - line1StartX);
123
- result.y = line1StartY + a * (line1EndY - line1StartY);
124
- // if line1 is a segment and line2 is infinite, they intersect if:
125
- if (a >= 0 && a <= 1) {
126
- result.onLine1 = true;
127
- }
128
- // if line2 is a segment and line1 is infinite, they intersect if:
129
- if (b >= 0 && b <= 1) {
130
- result.onLine2 = true;
131
- }
132
- // if line1 and line2 are segments, they intersect if both of the above are true
133
- if (result.onLine1 && result.onLine2) {
134
- return [result.x, result.y];
135
- }
136
- else {
137
- return false;
138
- }
139
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/kinks",
3
- "version": "6.4.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf kinks module",
5
5
  "author": "Turf Authors",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "type": "git",
13
13
  "url": "git://github.com/Turfjs/turf.git"
14
14
  },
15
+ "funding": "https://opencollective.com/turf",
15
16
  "publishConfig": {
16
17
  "access": "public"
17
18
  },
@@ -42,10 +43,10 @@
42
43
  "docs": "node ../../scripts/generate-readmes",
43
44
  "test": "npm-run-all test:*",
44
45
  "test:tape": "ts-node -r esm test.js",
45
- "test:types": "tsc --esModuleInterop --noEmit types.ts"
46
+ "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
46
47
  },
47
48
  "devDependencies": {
48
- "@turf/meta": "^6.4.0",
49
+ "@turf/meta": "^7.0.0-alpha.0",
49
50
  "benchmark": "*",
50
51
  "load-json-file": "*",
51
52
  "npm-run-all": "*",
@@ -55,7 +56,9 @@
55
56
  "write-json-file": "*"
56
57
  },
57
58
  "dependencies": {
58
- "@turf/helpers": "^6.4.0"
59
+ "@turf/helpers": "^7.0.0-alpha.0",
60
+ "sweepline-intersections": "^1.5.0",
61
+ "tslib": "^2.3.0"
59
62
  },
60
- "gitHead": "1e62773cfc88c627cca8effcb5c14cfb65a905ac"
63
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
61
64
  }