Skip to content

Commit 3dacee0

Browse files
authored
Merge branch 'master' into mf/nx-cleanup
2 parents bbcff19 + ad8012c commit 3dacee0

File tree

16 files changed

+451
-34
lines changed

16 files changed

+451
-34
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_modules
44

55
# is actually output
66
packages/turf/turf.min.js
7+
packages/turf/test.example.js
78

89
pnpm-lock.yaml
910

packages/turf-clusters-dbscan/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Takes a set of [points][1] and partition them into clusters according to [https:
99
### Parameters
1010

1111
* `points` **[FeatureCollection][3]<[Point][1]>** to be clustered
12-
* `maxDistance` **[number][4]** Maximum Distance between any point of the cluster to generate the clusters (kilometers only)
12+
* `maxDistance` **[number][4]** Maximum Distance between any point of the cluster to generate the clusters (kilometers by default, see options)
1313
* `options` **[Object][5]** Optional parameters (optional, default `{}`)
1414

1515
* `options.units` **[string][6]** in which `maxDistance` is expressed, can be degrees, radians, miles, or kilometers (optional, default `"kilometers"`)

packages/turf-clusters-dbscan/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type IndexedPoint = {
2424
*
2525
* @name clustersDbscan
2626
* @param {FeatureCollection<Point>} points to be clustered
27-
* @param {number} maxDistance Maximum Distance between any point of the cluster to generate the clusters (kilometers only)
27+
* @param {number} maxDistance Maximum Distance between any point of the cluster to generate the clusters (kilometers by default, see options)
2828
* @param {Object} [options={}] Optional parameters
2929
* @param {string} [options.units="kilometers"] in which `maxDistance` is expressed, can be degrees, radians, miles, or kilometers
3030
* @param {boolean} [options.mutate=false] Allows GeoJSON input to be mutated

packages/turf-kinks/index.ts

Lines changed: 120 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
Polygon,
99
} from "geojson";
1010
import { point } from "@turf/helpers";
11-
import { sweeplineIntersections as findIntersections } from "./lib/sweepline-intersections-export.js";
1211

1312
/**
1413
* Takes a {@link LineString|linestring}, {@link MultiLineString|multi-linestring},
@@ -33,29 +32,138 @@ import { sweeplineIntersections as findIntersections } from "./lib/sweepline-int
3332
* var addToMap = [poly, kinks]
3433
*/
3534
function kinks<T extends LineString | MultiLineString | Polygon | MultiPolygon>(
36-
featureIn: Feature<T>
35+
featureIn: Feature<T> | T
3736
): FeatureCollection<Point> {
37+
let coordinates: any;
38+
let feature: any;
3839
const results: FeatureCollection<Point> = {
3940
type: "FeatureCollection",
4041
features: [],
4142
};
42-
if (
43-
featureIn.type === "Feature" &&
44-
((featureIn as Feature).geometry.type === "Point" ||
45-
(featureIn as Feature).geometry.type === "MultiPoint")
46-
) {
43+
if (featureIn.type === "Feature") {
44+
feature = featureIn.geometry;
45+
} else {
46+
feature = featureIn;
47+
}
48+
if (feature.type === "LineString") {
49+
coordinates = [feature.coordinates];
50+
} else if (feature.type === "MultiLineString") {
51+
coordinates = feature.coordinates;
52+
} else if (feature.type === "MultiPolygon") {
53+
coordinates = [].concat(...feature.coordinates);
54+
} else if (feature.type === "Polygon") {
55+
coordinates = feature.coordinates;
56+
} else {
4757
throw new Error(
4858
"Input must be a LineString, MultiLineString, " +
4959
"Polygon, or MultiPolygon Feature or Geometry"
5060
);
5161
}
52-
const intersections = findIntersections(featureIn, false);
53-
for (let i = 0; i < intersections.length; ++i) {
54-
const intersection = intersections[i];
55-
results.features.push(point([intersection[0], intersection[1]]));
56-
}
62+
coordinates.forEach((line1: any) => {
63+
coordinates.forEach((line2: any) => {
64+
for (let i = 0; i < line1.length - 1; i++) {
65+
// start iteration at i, intersections for k < i have already
66+
// been checked in previous outer loop iterations
67+
for (let k = i; k < line2.length - 1; k++) {
68+
if (line1 === line2) {
69+
// segments are adjacent and always share a vertex, not a kink
70+
if (Math.abs(i - k) === 1) {
71+
continue;
72+
}
73+
// first and last segment in a closed lineString or ring always share a vertex, not a kink
74+
if (
75+
// segments are first and last segment of lineString
76+
i === 0 &&
77+
k === line1.length - 2 &&
78+
// lineString is closed
79+
line1[i][0] === line1[line1.length - 1][0] &&
80+
line1[i][1] === line1[line1.length - 1][1]
81+
) {
82+
continue;
83+
}
84+
}
85+
86+
const intersection: any = lineIntersects(
87+
line1[i][0],
88+
line1[i][1],
89+
line1[i + 1][0],
90+
line1[i + 1][1],
91+
line2[k][0],
92+
line2[k][1],
93+
line2[k + 1][0],
94+
line2[k + 1][1]
95+
);
96+
if (intersection) {
97+
results.features.push(point([intersection[0], intersection[1]]));
98+
}
99+
}
100+
}
101+
});
102+
});
57103
return results;
58104
}
59105

106+
// modified from http://jsfiddle.net/justin_c_rounds/Gd2S2/light/
107+
function lineIntersects(
108+
line1StartX: any,
109+
line1StartY: any,
110+
line1EndX: any,
111+
line1EndY: any,
112+
line2StartX: any,
113+
line2StartY: any,
114+
line2EndX: any,
115+
line2EndY: any
116+
) {
117+
// if the lines intersect, the result contains the x and y of the
118+
// intersection (treating the lines as infinite) and booleans for whether
119+
// line segment 1 or line segment 2 contain the point
120+
let denominator;
121+
let a;
122+
let b;
123+
let numerator1;
124+
let numerator2;
125+
const result = {
126+
x: null,
127+
y: null,
128+
onLine1: false,
129+
onLine2: false,
130+
};
131+
denominator =
132+
(line2EndY - line2StartY) * (line1EndX - line1StartX) -
133+
(line2EndX - line2StartX) * (line1EndY - line1StartY);
134+
if (denominator === 0) {
135+
if (result.x !== null && result.y !== null) {
136+
return result;
137+
} else {
138+
return false;
139+
}
140+
}
141+
a = line1StartY - line2StartY;
142+
b = line1StartX - line2StartX;
143+
numerator1 = (line2EndX - line2StartX) * a - (line2EndY - line2StartY) * b;
144+
numerator2 = (line1EndX - line1StartX) * a - (line1EndY - line1StartY) * b;
145+
a = numerator1 / denominator;
146+
b = numerator2 / denominator;
147+
148+
// if we cast these lines infinitely in both directions, they intersect here:
149+
result.x = line1StartX + a * (line1EndX - line1StartX);
150+
result.y = line1StartY + a * (line1EndY - line1StartY);
151+
152+
// if line1 is a segment and line2 is infinite, they intersect if:
153+
if (a >= 0 && a <= 1) {
154+
result.onLine1 = true;
155+
}
156+
// if line2 is a segment and line1 is infinite, they intersect if:
157+
if (b >= 0 && b <= 1) {
158+
result.onLine2 = true;
159+
}
160+
// if line1 and line2 are segments, they intersect if both of the above are true
161+
if (result.onLine1 && result.onLine2) {
162+
return [result.x, result.y];
163+
} else {
164+
return false;
165+
}
166+
}
167+
60168
export { kinks };
61169
export default kinks;

packages/turf-kinks/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"dependencies": {
6767
"@turf/helpers": "workspace:^",
6868
"@types/geojson": "^7946.0.10",
69-
"sweepline-intersections": "^1.5.0",
7069
"tslib": "^2.6.2"
7170
}
7271
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"type": "Polygon",
3+
"coordinates": [
4+
[
5+
[11.032103, 53.905391],
6+
[11.032478, 53.90552],
7+
[11.032784, 53.905631],
8+
[11.033154, 53.905675],
9+
[11.03376, 53.905665],
10+
[11.034415, 53.90571],
11+
[11.034833, 53.905631],
12+
[11.035396, 53.905492],
13+
[11.036094, 53.905583],
14+
[11.03662, 53.90565],
15+
[11.036958, 53.905574],
16+
[11.037118, 53.905526],
17+
[11.037194, 53.905991],
18+
[11.037741, 53.90898],
19+
[11.038202, 53.911439],
20+
[11.038519, 53.913243],
21+
[11.038932, 53.915534],
22+
[11.039125, 53.91658],
23+
[11.039436, 53.918137],
24+
[11.038175, 53.91785],
25+
[11.038368, 53.917654],
26+
[11.037183, 53.917379],
27+
[11.036979, 53.91743],
28+
[11.036662, 53.917695],
29+
[11.036341, 53.918071],
30+
[11.035691, 53.917913],
31+
[11.035332, 53.917869],
32+
[11.032505, 53.917787],
33+
[11.032022, 53.917749],
34+
[11.032135, 53.918662],
35+
[11.031582, 53.918731],
36+
[11.031067, 53.918848],
37+
[11.030268, 53.91912],
38+
[11.028396, 53.919793],
39+
[11.02639, 53.920548],
40+
[11.02573, 53.920772],
41+
[11.025456, 53.920823],
42+
[11.023611, 53.909521],
43+
[11.024276, 53.909518],
44+
[11.025022, 53.909521],
45+
[11.025783, 53.909511],
46+
[11.028165, 53.909508],
47+
[11.028664, 53.909527],
48+
[11.028868, 53.909464],
49+
[11.02904, 53.908601],
50+
[11.029201, 53.907208],
51+
[11.02934, 53.90625],
52+
[11.029839, 53.906275],
53+
[11.030123, 53.90625],
54+
[11.030461, 53.906149],
55+
[11.031008, 53.905963],
56+
[11.031298, 53.905852],
57+
[11.03162, 53.905523],
58+
[11.031824, 53.9054],
59+
[11.032103, 53.905391]
60+
],
61+
[
62+
[11.03228, 53.90643],
63+
[11.032054, 53.906446],
64+
[11.031904, 53.906478],
65+
[11.031711, 53.906655],
66+
[11.031588, 53.906762],
67+
[11.031765, 53.906775],
68+
[11.031985, 53.906699],
69+
[11.032124, 53.906645],
70+
[11.032301, 53.906629],
71+
[11.032435, 53.906509],
72+
[11.03228, 53.90643]
73+
],
74+
[
75+
[11.031631, 53.908491],
76+
[11.031411, 53.908497],
77+
[11.03126, 53.908671],
78+
[11.031368, 53.908845],
79+
[11.0317, 53.908901],
80+
[11.031931, 53.908816],
81+
[11.032012, 53.908699],
82+
[11.031926, 53.908544],
83+
[11.031631, 53.908491]
84+
],
85+
[
86+
[11.033261, 53.909436],
87+
[11.033288, 53.909543],
88+
[11.033422, 53.909502],
89+
[11.033369, 53.909439],
90+
[11.033261, 53.909436]
91+
],
92+
[
93+
[11.031277, 53.911227],
94+
[11.031159, 53.911265],
95+
[11.031089, 53.911325],
96+
[11.031153, 53.911455],
97+
[11.031368, 53.911439],
98+
[11.031421, 53.91129],
99+
[11.031277, 53.911227]
100+
],
101+
[
102+
[11.029549, 53.911297],
103+
[11.029567, 53.911309],
104+
[11.02956, 53.911306],
105+
[11.029458, 53.911433],
106+
[11.029608, 53.911493],
107+
[11.02971, 53.911404],
108+
[11.029567, 53.911309],
109+
[11.029576, 53.911313],
110+
[11.029549, 53.911297]
111+
],
112+
[
113+
[11.02764, 53.911442],
114+
[11.027521, 53.911534],
115+
[11.027709, 53.911597],
116+
[11.027849, 53.911496],
117+
[11.02764, 53.911442]
118+
],
119+
[
120+
[11.034109, 53.912001],
121+
[11.033986, 53.912039],
122+
[11.034066, 53.912166],
123+
[11.034168, 53.912197],
124+
[11.034281, 53.912159],
125+
[11.034302, 53.912077],
126+
[11.034109, 53.912001]
127+
],
128+
[
129+
[11.029367, 53.913382],
130+
[11.029276, 53.913404],
131+
[11.029147, 53.91355],
132+
[11.029142, 53.91367],
133+
[11.02927, 53.913746],
134+
[11.029453, 53.913746],
135+
[11.029501, 53.91348],
136+
[11.029378, 53.913388],
137+
[11.029383, 53.913407],
138+
[11.029367, 53.913382]
139+
],
140+
[
141+
[11.035895, 53.913486],
142+
[11.035783, 53.913496],
143+
[11.03581, 53.913584],
144+
[11.036029, 53.913641],
145+
[11.036137, 53.913572],
146+
[11.035895, 53.913486]
147+
],
148+
[
149+
[11.032178, 53.913496],
150+
[11.03199, 53.913534],
151+
[11.031813, 53.913692],
152+
[11.031491, 53.913869],
153+
[11.031083, 53.913957],
154+
[11.03066, 53.914071],
155+
[11.030349, 53.914169],
156+
[11.029941, 53.914358],
157+
[11.029807, 53.914498],
158+
[11.029801, 53.914769],
159+
[11.029925, 53.914959],
160+
[11.030257, 53.915038],
161+
[11.030654, 53.915013],
162+
[11.031024, 53.91494],
163+
[11.031464, 53.914791],
164+
[11.031738, 53.914592],
165+
[11.032199, 53.914093],
166+
[11.032344, 53.913657],
167+
[11.032178, 53.913496]
168+
],
169+
[
170+
[11.034232, 53.91621],
171+
[11.034109, 53.916333],
172+
[11.034189, 53.916463],
173+
[11.034324, 53.916418],
174+
[11.034318, 53.916292],
175+
[11.034232, 53.91621]
176+
],
177+
[
178+
[11.028745, 53.916801],
179+
[11.028584, 53.916918],
180+
[11.028707, 53.917022],
181+
[11.028863, 53.917117],
182+
[11.0289, 53.917009],
183+
[11.028954, 53.916873],
184+
[11.028745, 53.916801]
185+
],
186+
[
187+
[11.035874, 53.917521],
188+
[11.035761, 53.917578],
189+
[11.036094, 53.917704],
190+
[11.036185, 53.917625],
191+
[11.035879, 53.917527],
192+
[11.035874, 53.917521]
193+
]
194+
]
195+
}

0 commit comments

Comments
 (0)