@math.gl/geospatial 5.0.1 → 5.0.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.
Files changed (88) hide show
  1. package/dist/coverage-simplification.d.ts +9 -0
  2. package/dist/coverage-simplification.d.ts.map +1 -0
  3. package/dist/coverage-simplification.js +167 -0
  4. package/dist/coverage-simplification.js.map +1 -0
  5. package/dist/line-network/build-line-network.d.ts +100 -0
  6. package/dist/line-network/build-line-network.d.ts.map +1 -0
  7. package/dist/line-network/build-line-network.js +766 -0
  8. package/dist/line-network/build-line-network.js.map +1 -0
  9. package/dist/line-network/line-network-routes.d.ts +53 -0
  10. package/dist/line-network/line-network-routes.d.ts.map +1 -0
  11. package/dist/line-network/line-network-routes.js +146 -0
  12. package/dist/line-network/line-network-routes.js.map +1 -0
  13. package/dist/line-network/line-network-weights.d.ts +11 -0
  14. package/dist/line-network/line-network-weights.d.ts.map +1 -0
  15. package/dist/line-network/line-network-weights.js +25 -0
  16. package/dist/line-network/line-network-weights.js.map +1 -0
  17. package/dist/line-network.cjs +850 -0
  18. package/dist/line-network.cjs.map +6 -0
  19. package/dist/line-network.d.ts +7 -0
  20. package/dist/line-network.d.ts.map +1 -0
  21. package/dist/line-network.js +7 -0
  22. package/dist/line-network.js.map +1 -0
  23. package/dist/linear-referencing.d.ts +2 -12
  24. package/dist/linear-referencing.d.ts.map +1 -1
  25. package/dist/linear-referencing.js +17 -49
  26. package/dist/linear-referencing.js.map +1 -1
  27. package/dist/polygon-simplification.d.ts.map +1 -1
  28. package/dist/polygon-simplification.js +3 -33
  29. package/dist/polygon-simplification.js.map +1 -1
  30. package/dist/route-editing.d.ts +10 -0
  31. package/dist/route-editing.d.ts.map +1 -0
  32. package/dist/route-editing.js +58 -0
  33. package/dist/route-editing.js.map +1 -0
  34. package/dist/spatial-line-polygon-intersection.d.ts.map +1 -1
  35. package/dist/spatial-line-polygon-intersection.js +2 -2
  36. package/dist/spatial-line-polygon-intersection.js.map +1 -1
  37. package/dist/spatial-planar-intersects.d.ts +1 -11
  38. package/dist/spatial-planar-intersects.d.ts.map +1 -1
  39. package/dist/spatial-planar-intersects.js +2 -42
  40. package/dist/spatial-planar-intersects.js.map +1 -1
  41. package/dist/spatial-sql.d.ts +6 -4
  42. package/dist/spatial-sql.d.ts.map +1 -1
  43. package/dist/spatial-sql.js +38 -4
  44. package/dist/spatial-sql.js.map +1 -1
  45. package/dist/spatial-touches.d.ts +0 -2
  46. package/dist/spatial-touches.d.ts.map +1 -1
  47. package/dist/spatial-touches.js +1 -15
  48. package/dist/spatial-touches.js.map +1 -1
  49. package/dist/st-coverage-functions.d.ts +11 -0
  50. package/dist/st-coverage-functions.d.ts.map +1 -0
  51. package/dist/st-coverage-functions.js +57 -0
  52. package/dist/st-coverage-functions.js.map +1 -0
  53. package/dist/st-functions.cjs +1201 -775
  54. package/dist/st-functions.cjs.map +3 -3
  55. package/dist/st-functions.d.ts +36 -3
  56. package/dist/st-functions.d.ts.map +1 -1
  57. package/dist/st-functions.js +53 -19
  58. package/dist/st-functions.js.map +1 -1
  59. package/dist/st-geodesic-functions.d.ts +10 -2
  60. package/dist/st-geodesic-functions.d.ts.map +1 -1
  61. package/dist/st-geodesic-functions.js +179 -25
  62. package/dist/st-geodesic-functions.js.map +1 -1
  63. package/dist/st-hull-functions.cjs.map +1 -1
  64. package/dist/st-overlay-functions.cjs +114 -67
  65. package/dist/st-overlay-functions.cjs.map +2 -2
  66. package/dist/st-overlay-functions.d.ts +11 -1
  67. package/dist/st-overlay-functions.d.ts.map +1 -1
  68. package/dist/st-overlay-functions.js +74 -9
  69. package/dist/st-overlay-functions.js.map +1 -1
  70. package/dist/st-viewport-functions.cjs.map +1 -1
  71. package/dist/turf-functions.cjs.map +1 -1
  72. package/package.json +12 -6
  73. package/src/coverage-simplification.ts +215 -0
  74. package/src/line-network/build-line-network.ts +990 -0
  75. package/src/line-network/line-network-routes.ts +213 -0
  76. package/src/line-network/line-network-weights.ts +40 -0
  77. package/src/line-network.ts +29 -0
  78. package/src/linear-referencing.ts +23 -64
  79. package/src/polygon-simplification.ts +3 -42
  80. package/src/route-editing.ts +107 -0
  81. package/src/spatial-line-polygon-intersection.ts +7 -2
  82. package/src/spatial-planar-intersects.ts +7 -54
  83. package/src/spatial-sql.ts +54 -5
  84. package/src/spatial-touches.ts +1 -15
  85. package/src/st-coverage-functions.ts +88 -0
  86. package/src/st-functions.ts +70 -19
  87. package/src/st-geodesic-functions.ts +218 -26
  88. package/src/st-overlay-functions.ts +79 -10
@@ -0,0 +1,850 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // dist/line-network.js
20
+ var line_network_exports = {};
21
+ __export(line_network_exports, {
22
+ NO_PREDECESSOR: () => NO_PREDECESSOR,
23
+ buildLineNetwork: () => buildLineNetwork,
24
+ buildLineNetworkAdjacency: () => buildLineNetworkAdjacency,
25
+ createLineNetworkWeights: () => createLineNetworkWeights,
26
+ getEdgeGeometry: () => getEdgeGeometry,
27
+ getEdgePointCount: () => getEdgePointCount,
28
+ getRouteEdges: () => getRouteEdges,
29
+ getRouteGeometry: () => getRouteGeometry
30
+ });
31
+ module.exports = __toCommonJS(line_network_exports);
32
+
33
+ // dist/line-network/build-line-network.js
34
+ var EARTH_RADIUS = 63710088e-1;
35
+ var METRES_PER_DEGREE = EARTH_RADIUS * Math.PI / 180;
36
+ var DEGREES_TO_RADIANS = Math.PI / 180;
37
+ var EMPTY = -1;
38
+ function haversineDistance(longitude1, latitude1, longitude2, latitude2) {
39
+ const phi1 = latitude1 * DEGREES_TO_RADIANS;
40
+ const phi2 = latitude2 * DEGREES_TO_RADIANS;
41
+ const sinHalfLat = Math.sin((phi2 - phi1) / 2);
42
+ const sinHalfLon = Math.sin((longitude2 - longitude1) * DEGREES_TO_RADIANS / 2);
43
+ const h = sinHalfLat * sinHalfLat + Math.cos(phi1) * Math.cos(phi2) * sinHalfLon * sinHalfLon;
44
+ return 2 * EARTH_RADIUS * Math.asin(Math.min(1, Math.sqrt(h)));
45
+ }
46
+ function findCellSlot(slotKeys, slotHeads, slotMask, cellX, cellY, insert) {
47
+ let slot = (Math.imul(cellX, 2654435761) ^ Math.imul(cellY, 2246822519)) & slotMask;
48
+ for (; ; ) {
49
+ if (slotHeads[slot] === EMPTY) {
50
+ if (!insert)
51
+ return EMPTY;
52
+ slotKeys[slot * 2] = cellX;
53
+ slotKeys[slot * 2 + 1] = cellY;
54
+ return slot;
55
+ }
56
+ if (slotKeys[slot * 2] === cellX && slotKeys[slot * 2 + 1] === cellY)
57
+ return slot;
58
+ slot = slot + 1 & slotMask;
59
+ }
60
+ }
61
+ function getHashCapacity(count) {
62
+ let capacity = 16;
63
+ while (capacity < count)
64
+ capacity *= 2;
65
+ return capacity;
66
+ }
67
+ var PointSnapper = class {
68
+ /** Makes a snapper for at most `maxPoints` clusters. */
69
+ constructor(maxPoints, tolerance) {
70
+ this.clusterCount = 0;
71
+ this.tolerance = tolerance;
72
+ this.toleranceSquared = tolerance * tolerance;
73
+ this.clusterX = new Float64Array(maxPoints);
74
+ this.clusterY = new Float64Array(maxPoints);
75
+ this.nextInCell = new Int32Array(maxPoints);
76
+ const capacity = getHashCapacity(maxPoints * 2);
77
+ this.slotMask = capacity - 1;
78
+ this.slotKeys = new Int32Array(capacity * 2);
79
+ this.slotHeads = new Int32Array(capacity).fill(EMPTY);
80
+ }
81
+ /** Returns the cluster of the point, and makes a new cluster when none is in range. */
82
+ snap(lon, lat) {
83
+ const cellY = Math.floor(lat * METRES_PER_DEGREE / this.tolerance);
84
+ let found = EMPTY;
85
+ for (let dy = 0; found === EMPTY && dy < 3; dy++) {
86
+ const row = cellY + (dy === 0 ? 0 : dy === 1 ? -1 : 1);
87
+ const column = this.getColumn(lon, row);
88
+ found = this.findInCell(column, row, lon, lat);
89
+ if (found === EMPTY)
90
+ found = this.findInCell(column - 1, row, lon, lat);
91
+ if (found === EMPTY)
92
+ found = this.findInCell(column + 1, row, lon, lat);
93
+ }
94
+ if (found !== EMPTY)
95
+ return found;
96
+ const cluster = this.clusterCount++;
97
+ this.clusterX[cluster] = lon;
98
+ this.clusterY[cluster] = lat;
99
+ const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, this.getColumn(lon, cellY) | 0, cellY | 0, true);
100
+ this.nextInCell[cluster] = this.slotHeads[slot];
101
+ this.slotHeads[slot] = cluster;
102
+ return cluster;
103
+ }
104
+ /** Grid column of a longitude in a row, with the longitude scale at the row centre. */
105
+ getColumn(lon, row) {
106
+ const rowLatitude = (row + 0.5) * this.tolerance / METRES_PER_DEGREE;
107
+ const scale = METRES_PER_DEGREE * Math.cos(rowLatitude * DEGREES_TO_RADIANS);
108
+ return Math.floor(lon * scale / this.tolerance);
109
+ }
110
+ /** First cluster of a cell within the tolerance of the point, or EMPTY. */
111
+ findInCell(cellX, cellY, lon, lat) {
112
+ const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, cellX | 0, cellY | 0, false);
113
+ if (slot === EMPTY)
114
+ return EMPTY;
115
+ const cosLat = Math.cos(lat * DEGREES_TO_RADIANS);
116
+ for (let c = this.slotHeads[slot]; c !== EMPTY; c = this.nextInCell[c]) {
117
+ const dx = (this.clusterX[c] - lon) * METRES_PER_DEGREE * cosLat;
118
+ const dy = (this.clusterY[c] - lat) * METRES_PER_DEGREE;
119
+ if (dx * dx + dy * dy <= this.toleranceSquared)
120
+ return c;
121
+ }
122
+ return EMPTY;
123
+ }
124
+ };
125
+ var SegmentIndex = class {
126
+ /** Indexes every segment of `lines`. */
127
+ constructor(lines, cellSize, minLatitude, maxLatitude) {
128
+ this.entryCount = 0;
129
+ this.cellSize = cellSize;
130
+ this.firstRow = this.row(minLatitude) - 1;
131
+ this.rowScale = new Float64Array(this.row(maxLatitude) - this.firstRow + 2);
132
+ for (let i = 0; i < this.rowScale.length; i++) {
133
+ const rowLatitude = (this.firstRow + i + 0.5) * cellSize / METRES_PER_DEGREE;
134
+ this.rowScale[i] = METRES_PER_DEGREE * Math.cos(rowLatitude * DEGREES_TO_RADIANS);
135
+ }
136
+ const pathIndices = lines.pathIndices.value;
137
+ const pathCount = pathIndices.length - 1;
138
+ let entries = 0;
139
+ for (let path = 0; path < pathCount; path++) {
140
+ for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
141
+ entries += this.addSegment(lines.positions, s, false);
142
+ }
143
+ }
144
+ const capacity = getHashCapacity(entries * 2);
145
+ this.slotMask = capacity - 1;
146
+ this.slotKeys = new Int32Array(capacity * 2);
147
+ this.slotHeads = new Int32Array(capacity).fill(EMPTY);
148
+ this.entrySegment = new Int32Array(entries);
149
+ this.entryNext = new Int32Array(entries);
150
+ for (let path = 0; path < pathCount; path++) {
151
+ for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
152
+ this.addSegment(lines.positions, s, true);
153
+ }
154
+ }
155
+ }
156
+ /** Grid row of a latitude. */
157
+ row(lat) {
158
+ return Math.floor(lat * METRES_PER_DEGREE / this.cellSize);
159
+ }
160
+ /** Metres per degree of longitude in a row (at the row centre). Covers the data and one row on each side. */
161
+ scale(row) {
162
+ return this.rowScale[row - this.firstRow];
163
+ }
164
+ /** Grid column of a longitude in a row. */
165
+ column(lon, row) {
166
+ return Math.floor(lon * this.scale(row) / this.cellSize);
167
+ }
168
+ /** First entry of a cell, or EMPTY. */
169
+ head(column, row) {
170
+ const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, column, row, false);
171
+ return slot === EMPTY ? EMPTY : this.slotHeads[slot];
172
+ }
173
+ /** Visits the cells that segment `s` (vertex s to s + 1) passes through. Returns the count. */
174
+ addSegment(positions, s, insert) {
175
+ const { value, size } = positions;
176
+ const lon1 = value[size * s];
177
+ const lat1 = value[size * s + 1];
178
+ const lon2 = value[size * s + size];
179
+ const lat2 = value[size * s + size + 1];
180
+ const y1 = lat1 * METRES_PER_DEGREE / this.cellSize;
181
+ const y2 = lat2 * METRES_PER_DEGREE / this.cellSize;
182
+ const firstRow = Math.floor(Math.min(y1, y2));
183
+ const lastRow = Math.floor(Math.max(y1, y2));
184
+ let count = 0;
185
+ for (let row = firstRow; row <= lastRow; row++) {
186
+ let ta = 0;
187
+ let tb = 1;
188
+ if (y2 !== y1) {
189
+ const a = (row - y1) / (y2 - y1);
190
+ const b = (row + 1 - y1) / (y2 - y1);
191
+ ta = Math.max(0, Math.min(a, b));
192
+ tb = Math.min(1, Math.max(a, b));
193
+ }
194
+ const lonA = lon1 + ta * (lon2 - lon1);
195
+ const lonB = lon1 + tb * (lon2 - lon1);
196
+ const firstColumn = this.column(Math.min(lonA, lonB), row);
197
+ const lastColumn = this.column(Math.max(lonA, lonB), row);
198
+ for (let column = firstColumn; column <= lastColumn; column++) {
199
+ count++;
200
+ if (!insert)
201
+ continue;
202
+ const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, column, row, true);
203
+ const entry = this.entryCount++;
204
+ this.entrySegment[entry] = s;
205
+ this.entryNext[entry] = this.slotHeads[slot];
206
+ this.slotHeads[slot] = entry;
207
+ }
208
+ }
209
+ return count;
210
+ }
211
+ };
212
+ var SplitList = class {
213
+ constructor() {
214
+ this.vertex = new Int32Array(64);
215
+ this.fraction = new Float64Array(64);
216
+ this.cluster = new Int32Array(64);
217
+ this.count = 0;
218
+ }
219
+ /** Adds a cut, and doubles the storage when it is full. */
220
+ push(vertex, fraction, cluster) {
221
+ if (this.count === this.vertex.length) {
222
+ const vertices = new Int32Array(this.count * 2);
223
+ vertices.set(this.vertex);
224
+ this.vertex = vertices;
225
+ const fractions = new Float64Array(this.count * 2);
226
+ fractions.set(this.fraction);
227
+ this.fraction = fractions;
228
+ const clusters = new Int32Array(this.count * 2);
229
+ clusters.set(this.cluster);
230
+ this.cluster = clusters;
231
+ }
232
+ this.vertex[this.count] = vertex;
233
+ this.fraction[this.count] = fraction;
234
+ this.cluster[this.count] = cluster;
235
+ this.count++;
236
+ }
237
+ };
238
+ function validateLines(lines) {
239
+ const { positions, pathIndices, pathRows } = lines;
240
+ if (!(Number.isInteger(positions.size) && positions.size >= 2)) {
241
+ throw new RangeError("buildLineNetwork: positions.size must be an integer of 2 or more");
242
+ }
243
+ const offsets = pathIndices.value;
244
+ const pathCount = Math.max(offsets.length - 1, 0);
245
+ const vertexCount = positions.value.length / positions.size;
246
+ for (let path = 0; path <= pathCount && pathCount > 0; path++) {
247
+ const at = offsets[path];
248
+ const previous = path === 0 ? 0 : offsets[path - 1];
249
+ if (!(Number.isInteger(at) && at >= previous && at <= vertexCount)) {
250
+ throw new RangeError("buildLineNetwork: pathIndices must be ascending vertex indices");
251
+ }
252
+ }
253
+ if (pathRows && pathRows.length < pathCount) {
254
+ throw new RangeError("buildLineNetwork: pathRows needs one value for each path");
255
+ }
256
+ return pathCount;
257
+ }
258
+ function buildLineNetwork(lines, options = {}) {
259
+ const tolerance = options.snapTolerance ?? 1;
260
+ if (!(tolerance > 0 && tolerance < Infinity)) {
261
+ throw new RangeError("buildLineNetwork: snapTolerance must be positive and finite");
262
+ }
263
+ const junctionTolerance = options.junctionTolerance ?? tolerance;
264
+ if (!(junctionTolerance >= 0 && junctionTolerance < Infinity)) {
265
+ throw new RangeError("buildLineNetwork: junctionTolerance must be zero or positive and finite");
266
+ }
267
+ const pathCount = validateLines(lines);
268
+ const split = Boolean(options.splitAtSharedVertices);
269
+ const crossings = Boolean(options.splitAtCrossings);
270
+ const distance = options.measureDistance ?? haversineDistance;
271
+ const { value: positions, size } = lines.positions;
272
+ const pathIndices = lines.pathIndices.value;
273
+ const vertexCount = pathCount > 0 ? pathIndices[pathCount] : 0;
274
+ let minLatitude = Infinity;
275
+ let maxLatitude = -Infinity;
276
+ for (let v = 0; v < vertexCount; v++) {
277
+ const lon = positions[size * v];
278
+ const lat = positions[size * v + 1];
279
+ if (!(lat >= -90 && lat <= 90 && Number.isFinite(lon))) {
280
+ throw new RangeError("buildLineNetwork: positions must be finite longitude, latitude degrees");
281
+ }
282
+ if (lat < minLatitude)
283
+ minLatitude = lat;
284
+ if (lat > maxLatitude)
285
+ maxLatitude = lat;
286
+ }
287
+ const snapper = new PointSnapper(split ? vertexCount : pathCount * 2, tolerance);
288
+ const vertexCluster = new Int32Array(vertexCount).fill(EMPTY);
289
+ for (let path = 0; path < pathCount; path++) {
290
+ const first = pathIndices[path];
291
+ const last = pathIndices[path + 1] - 1;
292
+ if (last <= first)
293
+ continue;
294
+ if (split) {
295
+ for (let v = first; v <= last; v++) {
296
+ vertexCluster[v] = snapper.snap(positions[size * v], positions[size * v + 1]);
297
+ }
298
+ } else {
299
+ vertexCluster[first] = snapper.snap(positions[size * first], positions[size * first + 1]);
300
+ vertexCluster[last] = snapper.snap(positions[size * last], positions[size * last + 1]);
301
+ }
302
+ }
303
+ const snappedCount = snapper.clusterCount;
304
+ const clusterUses = new Uint32Array(snappedCount);
305
+ const isEndpointCluster = new Uint8Array(snappedCount);
306
+ for (let path = 0; path < pathCount; path++) {
307
+ const first = pathIndices[path];
308
+ const last = pathIndices[path + 1] - 1;
309
+ if (last <= first)
310
+ continue;
311
+ isEndpointCluster[vertexCluster[first]] = 1;
312
+ isEndpointCluster[vertexCluster[last]] = 1;
313
+ for (let v = first; v <= last; v++) {
314
+ const cluster = vertexCluster[v];
315
+ if (cluster !== EMPTY && (v === first || cluster !== vertexCluster[v - 1])) {
316
+ clusterUses[cluster]++;
317
+ }
318
+ }
319
+ }
320
+ const isSnappedNode = (c) => isEndpointCluster[c] === 1 || split && clusterUses[c] >= 2;
321
+ const splits = new SplitList();
322
+ const unions = [];
323
+ const crossingPositions = [];
324
+ const clusterGap = junctionTolerance > 0 ? new Float64Array(snappedCount) : null;
325
+ let dangling = 0;
326
+ if (clusterGap) {
327
+ for (let c = 0; c < snappedCount; c++) {
328
+ if (isEndpointCluster[c] && clusterUses[c] === 1)
329
+ dangling++;
330
+ }
331
+ }
332
+ if (dangling > 0 || crossings) {
333
+ let lengthSum = 0;
334
+ let segmentCount = 0;
335
+ for (let path = 0; path < pathCount; path++) {
336
+ for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
337
+ const cosLat = Math.cos(positions[size * s + 1] * DEGREES_TO_RADIANS);
338
+ const dx = (positions[size * s + size] - positions[size * s]) * cosLat;
339
+ const dy = positions[size * s + size + 1] - positions[size * s + 1];
340
+ lengthSum += Math.sqrt(dx * dx + dy * dy) * METRES_PER_DEGREE;
341
+ segmentCount++;
342
+ }
343
+ }
344
+ const cellSize = Math.max(junctionTolerance, segmentCount ? lengthSum / segmentCount : 0, 1);
345
+ const index = segmentCount > 0 ? new SegmentIndex(lines, cellSize, minLatitude, maxLatitude) : null;
346
+ if (index && clusterGap && dangling > 0) {
347
+ joinDanglingEnds(lines, vertexCluster, clusterUses, isEndpointCluster, index, {
348
+ junctionTolerance,
349
+ distance,
350
+ clusterGap,
351
+ isSnappedNode,
352
+ splits,
353
+ unions
354
+ });
355
+ }
356
+ if (index && crossings) {
357
+ splitCrossings(lines, vertexCluster, index, snappedCount, splits, crossingPositions);
358
+ }
359
+ }
360
+ const clusterCount = snappedCount + crossingPositions.length / 2;
361
+ const parent = new Int32Array(clusterCount);
362
+ for (let c = 0; c < clusterCount; c++)
363
+ parent[c] = c;
364
+ const find = (x) => {
365
+ while (parent[x] !== x)
366
+ x = parent[x] = parent[parent[x]];
367
+ return x;
368
+ };
369
+ const join = (first, second) => {
370
+ const a = find(first);
371
+ const b = find(second);
372
+ if (a !== b)
373
+ parent[Math.max(a, b)] = Math.min(a, b);
374
+ };
375
+ for (let i = 0; i < unions.length; i += 2)
376
+ join(unions[i], unions[i + 1]);
377
+ const splitOffsets = splits.count ? new Uint32Array(vertexCount + 1) : null;
378
+ const splitFraction = new Float64Array(splits.count);
379
+ const splitNode = new Int32Array(splits.count);
380
+ if (splitOffsets) {
381
+ for (let k = 0; k < splits.count; k++)
382
+ splitOffsets[splits.vertex[k] + 1]++;
383
+ for (let v = 0; v < vertexCount; v++)
384
+ splitOffsets[v + 1] += splitOffsets[v];
385
+ const cursor = splitOffsets.slice(0, vertexCount);
386
+ for (let k = 0; k < splits.count; k++) {
387
+ const at = cursor[splits.vertex[k]]++;
388
+ let i = at;
389
+ const start = splitOffsets[splits.vertex[k]];
390
+ while (i > start && splitFraction[i - 1] > splits.fraction[k]) {
391
+ splitFraction[i] = splitFraction[i - 1];
392
+ splitNode[i] = splitNode[i - 1];
393
+ i--;
394
+ }
395
+ splitFraction[i] = splits.fraction[k];
396
+ splitNode[i] = splits.cluster[k];
397
+ }
398
+ }
399
+ if (splitOffsets) {
400
+ for (let v = 0; v < vertexCount; v++) {
401
+ const first = splitOffsets[v];
402
+ const end = splitOffsets[v + 1];
403
+ if (end - first < 2)
404
+ continue;
405
+ const span = Math.max(Math.abs(positions[size * v + size] - positions[size * v]), Math.abs(positions[size * v + size + 1] - positions[size * v + 1]));
406
+ let representative = first;
407
+ for (let k = first + 1; k < end; k++) {
408
+ if ((splitFraction[k] - splitFraction[representative]) * span > 1e-12) {
409
+ representative = k;
410
+ continue;
411
+ }
412
+ join(splitNode[representative], splitNode[k]);
413
+ splitFraction[k] = splitFraction[representative];
414
+ }
415
+ }
416
+ }
417
+ const isNode = new Uint8Array(clusterCount);
418
+ for (let c = 0; c < snappedCount; c++)
419
+ if (isSnappedNode(c))
420
+ isNode[find(c)] = 1;
421
+ for (let c = snappedCount; c < clusterCount; c++)
422
+ isNode[c] = 1;
423
+ for (let k = 0; k < splits.count; k++)
424
+ isNode[find(splits.cluster[k])] = 1;
425
+ const clusterNode = new Int32Array(clusterCount).fill(EMPTY);
426
+ let nodeCount = 0;
427
+ for (let c = 0; c < clusterCount; c++) {
428
+ if (find(c) === c && isNode[c])
429
+ clusterNode[c] = nodeCount++;
430
+ }
431
+ const nodePositions = new Float64Array(nodeCount * 2);
432
+ for (let c = 0; c < clusterCount; c++) {
433
+ const root = find(c);
434
+ if (root !== c) {
435
+ clusterNode[c] = clusterNode[root];
436
+ continue;
437
+ }
438
+ const node = clusterNode[c];
439
+ if (node === EMPTY)
440
+ continue;
441
+ if (c < snappedCount) {
442
+ nodePositions[2 * node] = snapper.clusterX[c];
443
+ nodePositions[2 * node + 1] = snapper.clusterY[c];
444
+ } else {
445
+ nodePositions[2 * node] = crossingPositions[2 * (c - snappedCount)];
446
+ nodePositions[2 * node + 1] = crossingPositions[2 * (c - snappedCount) + 1];
447
+ }
448
+ }
449
+ for (let k = 0; k < splitNode.length; k++)
450
+ splitNode[k] = clusterNode[splitNode[k]];
451
+ let maxEdges = splits.count;
452
+ for (let path = 0; path < pathCount; path++) {
453
+ const pathVertices = pathIndices[path + 1] - pathIndices[path];
454
+ if (pathVertices > 1)
455
+ maxEdges += split ? pathVertices - 1 : 1;
456
+ }
457
+ const edges = new EdgeColumns(maxEdges, Boolean(options.includePathFractions));
458
+ for (let path = 0; path < pathCount; path++) {
459
+ const first = pathIndices[path];
460
+ const last = pathIndices[path + 1] - 1;
461
+ if (last <= first)
462
+ continue;
463
+ const row = lines.pathRows ? lines.pathRows[path] : path;
464
+ const firstEdge = edges.count;
465
+ let start = first;
466
+ let startFraction = 0;
467
+ let startNode = clusterNode[vertexCluster[first]];
468
+ let previousNode = startNode;
469
+ let length = clusterGap ? clusterGap[vertexCluster[first]] : 0;
470
+ let pathAlong = 0;
471
+ let edgeAlong = 0;
472
+ for (let v = first + 1; v <= last; v++) {
473
+ const segmentLength = distance(positions[size * v - size], positions[size * v - size + 1], positions[size * v], positions[size * v + 1]);
474
+ let along = 0;
475
+ if (splitOffsets) {
476
+ for (let k = splitOffsets[v - 1]; k < splitOffsets[v]; k++) {
477
+ const fraction = splitFraction[k];
478
+ const node2 = splitNode[k];
479
+ length += (fraction - along) * segmentLength;
480
+ along = fraction;
481
+ previousNode = node2;
482
+ if (length === 0 && node2 === startNode) {
483
+ start = v - 1;
484
+ startFraction = fraction;
485
+ continue;
486
+ }
487
+ const cutAlong = pathAlong + fraction * segmentLength;
488
+ edges.push(path, row, startNode, node2, length, start, v, startFraction, fraction);
489
+ edges.setAlong(edgeAlong, cutAlong);
490
+ start = v - 1;
491
+ startFraction = fraction;
492
+ startNode = node2;
493
+ length = 0;
494
+ edgeAlong = cutAlong;
495
+ }
496
+ }
497
+ length += (1 - along) * segmentLength;
498
+ pathAlong += segmentLength;
499
+ const cluster = vertexCluster[v];
500
+ const node = cluster === EMPTY ? EMPTY : clusterNode[cluster];
501
+ const isCut = v === last || split && node !== EMPTY && node !== previousNode;
502
+ previousNode = node;
503
+ if (!isCut)
504
+ continue;
505
+ if (v === last && clusterGap)
506
+ length += clusterGap[cluster];
507
+ if (length === 0 && node === startNode) {
508
+ start = v;
509
+ startFraction = 0;
510
+ continue;
511
+ }
512
+ edges.push(path, row, startNode, node, length, start, v, startFraction, 1);
513
+ edges.setAlong(edgeAlong, pathAlong);
514
+ start = v;
515
+ startFraction = 0;
516
+ startNode = node;
517
+ length = 0;
518
+ edgeAlong = pathAlong;
519
+ }
520
+ edges.normalizeAlong(firstEdge, pathAlong);
521
+ }
522
+ return edges.toNetwork(nodeCount, nodePositions);
523
+ }
524
+ var EdgeColumns = class {
525
+ /** Allocates the columns. `withAlong` keeps the along-path positions for routing weights. */
526
+ constructor(maxEdges, withAlong) {
527
+ this.count = 0;
528
+ this.source = new Uint32Array(maxEdges);
529
+ this.target = new Uint32Array(maxEdges);
530
+ this.length = new Float64Array(maxEdges);
531
+ this.path = new Uint32Array(maxEdges);
532
+ this.row = new Uint32Array(maxEdges);
533
+ this.vertexStart = new Uint32Array(maxEdges);
534
+ this.vertexEnd = new Uint32Array(maxEdges);
535
+ this.startFraction = new Float64Array(maxEdges);
536
+ this.endFraction = new Float64Array(maxEdges);
537
+ this.alongStart = withAlong ? new Float64Array(maxEdges) : null;
538
+ this.alongEnd = withAlong ? new Float64Array(maxEdges) : null;
539
+ }
540
+ /** Appends one edge. */
541
+ push(path, row, source, target, length, vertexStart, vertexEnd, startFraction, endFraction) {
542
+ const e = this.count++;
543
+ this.path[e] = path;
544
+ this.row[e] = row;
545
+ this.source[e] = source;
546
+ this.target[e] = target;
547
+ this.length[e] = length;
548
+ this.vertexStart[e] = vertexStart;
549
+ this.vertexEnd[e] = vertexEnd;
550
+ this.startFraction[e] = startFraction;
551
+ this.endFraction[e] = endFraction;
552
+ }
553
+ /** Records where the last edge starts and ends along its path, in metres. */
554
+ setAlong(start, end) {
555
+ if (!this.alongStart || !this.alongEnd)
556
+ return;
557
+ this.alongStart[this.count - 1] = start;
558
+ this.alongEnd[this.count - 1] = end;
559
+ }
560
+ /** Converts retained distances to fractions after the full path has been measured. */
561
+ normalizeAlong(firstEdge, pathLength) {
562
+ if (!this.alongStart || !this.alongEnd || pathLength === 0)
563
+ return;
564
+ for (let edge = firstEdge; edge < this.count; edge++) {
565
+ this.alongStart[edge] /= pathLength;
566
+ this.alongEnd[edge] /= pathLength;
567
+ }
568
+ }
569
+ /** Returns the network with columns trimmed to the edge count. */
570
+ toNetwork(nodeCount, nodePositions) {
571
+ const n = this.count;
572
+ return {
573
+ nodeCount,
574
+ edgeCount: n,
575
+ nodePositions,
576
+ edgeSource: this.source.slice(0, n),
577
+ edgeTarget: this.target.slice(0, n),
578
+ edgeLength: this.length.slice(0, n),
579
+ ...this.alongStart && this.alongEnd ? {
580
+ edgePathStartFraction: this.alongStart.slice(0, n),
581
+ edgePathEndFraction: this.alongEnd.slice(0, n)
582
+ } : {},
583
+ edgePath: this.path.slice(0, n),
584
+ edgeRow: this.row.slice(0, n),
585
+ edgeVertexStart: this.vertexStart.slice(0, n),
586
+ edgeVertexEnd: this.vertexEnd.slice(0, n),
587
+ edgeStartFraction: this.startFraction.slice(0, n),
588
+ edgeEndFraction: this.endFraction.slice(0, n)
589
+ };
590
+ }
591
+ };
592
+ function joinDanglingEnds(lines, vertexCluster, clusterUses, isEndpointCluster, index, out) {
593
+ const { value: positions, size } = lines.positions;
594
+ const pathIndices = lines.pathIndices.value;
595
+ const pathCount = pathIndices.length - 1;
596
+ const joinedEndpoint = new Int32Array(clusterUses.length).fill(EMPTY);
597
+ const limitSquared = out.junctionTolerance * out.junctionTolerance;
598
+ for (let path = 0; path < pathCount; path++) {
599
+ const first = pathIndices[path];
600
+ const last = pathIndices[path + 1] - 1;
601
+ if (last <= first || vertexCluster[first] === vertexCluster[last])
602
+ continue;
603
+ for (let end = 0; end < 2; end++) {
604
+ const v = end === 0 ? first : last;
605
+ const cluster = vertexCluster[v];
606
+ if (!isEndpointCluster[cluster] || clusterUses[cluster] !== 1)
607
+ continue;
608
+ const lon = positions[size * v];
609
+ const lat = positions[size * v + 1];
610
+ const kx = METRES_PER_DEGREE * Math.cos(lat * DEGREES_TO_RADIANS);
611
+ let bestSquared = Infinity;
612
+ let bestSegment = EMPTY;
613
+ let bestFraction = 0;
614
+ const cell = index.cellSize;
615
+ const y = lat * METRES_PER_DEGREE;
616
+ const row = index.row(lat);
617
+ const yLow = y - row * cell < out.junctionTolerance ? -1 : 0;
618
+ const yHigh = (row + 1) * cell - y < out.junctionTolerance ? 1 : 0;
619
+ for (let dy = yLow; dy <= yHigh; dy++) {
620
+ const x = lon * index.scale(row + dy);
621
+ const column = Math.floor(x / cell);
622
+ const xLow = x - column * cell < out.junctionTolerance ? -1 : 0;
623
+ const xHigh = (column + 1) * cell - x < out.junctionTolerance ? 1 : 0;
624
+ for (let dx = xLow; dx <= xHigh; dx++) {
625
+ for (let e = index.head(column + dx, row + dy); e !== EMPTY; e = index.entryNext[e]) {
626
+ const s2 = index.entrySegment[e];
627
+ if (s2 >= first && s2 < last)
628
+ continue;
629
+ const ax = (positions[size * s2] - lon) * kx;
630
+ const ay = (positions[size * s2 + 1] - lat) * METRES_PER_DEGREE;
631
+ const bx = (positions[size * s2 + size] - lon) * kx;
632
+ const by = (positions[size * s2 + size + 1] - lat) * METRES_PER_DEGREE;
633
+ const ux = bx - ax;
634
+ const uy = by - ay;
635
+ const lengthSquared = ux * ux + uy * uy;
636
+ let t = lengthSquared > 0 ? -(ax * ux + ay * uy) / lengthSquared : 0;
637
+ t = t < 0 ? 0 : t > 1 ? 1 : t;
638
+ const px = ax + t * ux;
639
+ const py = ay + t * uy;
640
+ const d2 = px * px + py * py;
641
+ if (d2 < bestSquared || d2 === bestSquared && s2 < bestSegment) {
642
+ bestSquared = d2;
643
+ bestSegment = s2;
644
+ bestFraction = t;
645
+ }
646
+ }
647
+ }
648
+ }
649
+ if (bestSegment === EMPTY || bestSquared > limitSquared)
650
+ continue;
651
+ const s = bestSegment;
652
+ const atVertex = bestFraction === 0 ? s : bestFraction === 1 ? s + 1 : -1;
653
+ const target = atVertex >= 0 ? vertexCluster[atVertex] : EMPTY;
654
+ if (target !== EMPTY && out.isSnappedNode(target)) {
655
+ if (joinedEndpoint[target] === cluster)
656
+ continue;
657
+ joinedEndpoint[cluster] = target;
658
+ out.unions.push(cluster, target);
659
+ } else {
660
+ out.splits.push(s, bestFraction, cluster);
661
+ }
662
+ out.clusterGap[cluster] = out.distance(lon, lat, positions[size * s] + bestFraction * (positions[size * s + size] - positions[size * s]), positions[size * s + 1] + bestFraction * (positions[size * s + size + 1] - positions[size * s + 1]));
663
+ }
664
+ }
665
+ }
666
+ function splitCrossings(lines, vertexCluster, index, snappedCount, splits, crossingPositions) {
667
+ const { value: positions, size } = lines.positions;
668
+ const { slotHeads, slotKeys, entryNext, entrySegment } = index;
669
+ for (let slot = 0; slot < slotHeads.length; slot++) {
670
+ const head = slotHeads[slot];
671
+ if (head === EMPTY || entryNext[head] === EMPTY)
672
+ continue;
673
+ const cellX = slotKeys[slot * 2];
674
+ const cellY = slotKeys[slot * 2 + 1];
675
+ for (let i = head; i !== EMPTY; i = entryNext[i]) {
676
+ const a = entrySegment[i];
677
+ const lonA = positions[size * a];
678
+ const latA = positions[size * a + 1];
679
+ const kx = Math.cos(latA * DEGREES_TO_RADIANS);
680
+ const rx = (positions[size * a + size] - lonA) * kx;
681
+ const ry = positions[size * a + size + 1] - latA;
682
+ for (let j = entryNext[i]; j !== EMPTY; j = entryNext[j]) {
683
+ const b = entrySegment[j];
684
+ if (b === a || b === a + 1 || a === b + 1)
685
+ continue;
686
+ if (sharesCluster(vertexCluster, a, b))
687
+ continue;
688
+ const qx = (positions[size * b] - lonA) * kx;
689
+ const qy = positions[size * b + 1] - latA;
690
+ const sx = (positions[size * b + size] - positions[size * b]) * kx;
691
+ const sy = positions[size * b + size + 1] - positions[size * b + 1];
692
+ const denominator = rx * sy - ry * sx;
693
+ if (denominator === 0)
694
+ continue;
695
+ const t = (qx * sy - qy * sx) / denominator;
696
+ const u = (qx * ry - qy * rx) / denominator;
697
+ if (!(t > 0 && t < 1 && u > 0 && u < 1))
698
+ continue;
699
+ const lon = lonA + t * (positions[size * a + size] - lonA);
700
+ const lat = latA + t * (positions[size * a + size + 1] - latA);
701
+ const row = index.row(lat);
702
+ if (row !== cellY || index.column(lon, row) !== cellX)
703
+ continue;
704
+ const cluster = snappedCount + crossingPositions.length / 2;
705
+ crossingPositions.push(lon, lat);
706
+ splits.push(a, t, cluster);
707
+ splits.push(b, u, cluster);
708
+ }
709
+ }
710
+ }
711
+ }
712
+ function sharesCluster(vertexCluster, a, b) {
713
+ const a0 = vertexCluster[a];
714
+ const a1 = vertexCluster[a + 1];
715
+ const b0 = vertexCluster[b];
716
+ const b1 = vertexCluster[b + 1];
717
+ return a0 !== EMPTY && (a0 === b0 || a0 === b1) || a1 !== EMPTY && (a1 === b0 || a1 === b1);
718
+ }
719
+
720
+ // dist/line-network/line-network-routes.js
721
+ var import_polygon = require("@math.gl/geometry/polygon");
722
+ var NO_PREDECESSOR = 4294967295;
723
+ function buildLineNetworkAdjacency(network, options = {}) {
724
+ const directed = options.directed ?? false;
725
+ const { nodeCount, edgeCount, edgeSource, edgeTarget } = network;
726
+ const offsets = new Uint32Array(nodeCount + 1);
727
+ for (let e = 0; e < edgeCount; e++) {
728
+ offsets[edgeSource[e] + 1]++;
729
+ if (!directed)
730
+ offsets[edgeTarget[e] + 1]++;
731
+ }
732
+ for (let n = 0; n < nodeCount; n++)
733
+ offsets[n + 1] += offsets[n];
734
+ const cursor = offsets.slice(0, nodeCount);
735
+ const slotCount = directed ? edgeCount : edgeCount * 2;
736
+ const neighbors = new Uint32Array(slotCount);
737
+ const edges = new Uint32Array(slotCount);
738
+ for (let e = 0; e < edgeCount; e++) {
739
+ const s = edgeSource[e];
740
+ const t = edgeTarget[e];
741
+ neighbors[cursor[s]] = t;
742
+ edges[cursor[s]++] = e;
743
+ if (directed)
744
+ continue;
745
+ neighbors[cursor[t]] = s;
746
+ edges[cursor[t]++] = e;
747
+ }
748
+ return { directed, offsets, neighbors, edges };
749
+ }
750
+ function getRouteEdges(adjacency, edgeWeight, predecessors, source, target, options = {}) {
751
+ const unreachable = options.unreachable ?? NO_PREDECESSOR;
752
+ if (target === source)
753
+ return new Uint32Array(0);
754
+ if (predecessors[target] === unreachable)
755
+ return null;
756
+ const route = [];
757
+ const selectedEdges = options.predecessorEdges;
758
+ let node = target;
759
+ while (node !== source) {
760
+ const previous = predecessors[node];
761
+ if (!Number.isInteger(previous) || previous === unreachable || previous < 0 || previous >= predecessors.length || route.length >= predecessors.length)
762
+ return null;
763
+ let best = NO_PREDECESSOR;
764
+ for (let slot = adjacency.offsets[previous]; slot < adjacency.offsets[previous + 1]; slot++) {
765
+ if (adjacency.neighbors[slot] !== node)
766
+ continue;
767
+ const edge = adjacency.edges[slot];
768
+ if (selectedEdges) {
769
+ if (edge !== selectedEdges[node])
770
+ continue;
771
+ best = edge;
772
+ break;
773
+ }
774
+ if (best === NO_PREDECESSOR || ((edgeWeight == null ? void 0 : edgeWeight[edge]) ?? 1) < ((edgeWeight == null ? void 0 : edgeWeight[best]) ?? 1) || ((edgeWeight == null ? void 0 : edgeWeight[edge]) ?? 1) === ((edgeWeight == null ? void 0 : edgeWeight[best]) ?? 1) && edge < best) {
775
+ best = edge;
776
+ }
777
+ }
778
+ if (best === NO_PREDECESSOR)
779
+ return null;
780
+ route.push(best);
781
+ node = previous;
782
+ }
783
+ return Uint32Array.from(route.reverse());
784
+ }
785
+ function getEdgePointCount(network, edge) {
786
+ const vertices = network.edgeVertexEnd[edge] - network.edgeVertexStart[edge] + 1;
787
+ return Math.max(1, vertices - Number(network.edgeStartFraction[edge] === 1) - Number(network.edgeEndFraction[edge] === 0));
788
+ }
789
+ function getEdgeGeometry(network, lines, edge, target = new Float64Array(2 * getEdgePointCount(network, edge)), offset = 0) {
790
+ visitEdge(network, lines, edge, false, (x, y) => {
791
+ target[offset++] = x;
792
+ target[offset++] = y;
793
+ });
794
+ return target;
795
+ }
796
+ function getRouteGeometry(network, lines, route, source) {
797
+ let pointTotal = 0;
798
+ for (let i = 0; i < route.length; i++)
799
+ pointTotal += getEdgePointCount(network, route[i]);
800
+ const path = new Float64Array(pointTotal * 2);
801
+ let at = 0;
802
+ let node = source;
803
+ for (let i = 0; i < route.length; i++) {
804
+ const e = route[i];
805
+ const forward = network.edgeSource[e] === node;
806
+ let first = true;
807
+ visitEdge(network, lines, e, !forward, (x, y) => {
808
+ if (!(first && at > 0 && x === path[at - 2] && y === path[at - 1])) {
809
+ path[at++] = x;
810
+ path[at++] = y;
811
+ }
812
+ first = false;
813
+ });
814
+ node = forward ? network.edgeTarget[e] : network.edgeSource[e];
815
+ }
816
+ return at === path.length ? path : path.slice(0, at);
817
+ }
818
+ function visitEdge(network, lines, edge, reverse, visit) {
819
+ const { value, size } = lines.positions;
820
+ const startFraction = network.edgeStartFraction[edge];
821
+ const endFraction = network.edgeEndFraction[edge];
822
+ const firstSegment = network.edgeVertexStart[edge] + Number(startFraction === 1);
823
+ const lastSegment = network.edgeVertexEnd[edge] - 1 - Number(endFraction === 0);
824
+ if (firstSegment > lastSegment) {
825
+ visit(value[size * firstSegment], value[size * firstSegment + 1]);
826
+ return;
827
+ }
828
+ (0, import_polygon.visitPolylineSpan)((vertex, ordinate) => value[size * vertex + ordinate], firstSegment, startFraction === 1 ? 0 : startFraction, lastSegment, endFraction === 0 ? 1 : endFraction, visit, reverse);
829
+ }
830
+
831
+ // dist/line-network/line-network-weights.js
832
+ function createLineNetworkWeights(network, { pathWeightFactors, edgeWeight } = {}) {
833
+ const { edgePath, edgeLength, edgePathStartFraction, edgePathEndFraction } = network;
834
+ if (pathWeightFactors && edgeWeight)
835
+ throw new Error("Specify pathWeightFactors or edgeWeight, not both");
836
+ if (edgeWeight && (!edgePathStartFraction || !edgePathEndFraction)) {
837
+ throw new Error("A custom edgeWeight requires includePathFractions");
838
+ }
839
+ const weights = new Float32Array(edgeLength.length);
840
+ for (let edge = 0; edge < weights.length; edge++) {
841
+ const path = edgePath[edge];
842
+ const length = edgeLength[edge];
843
+ const weight = edgeWeight ? edgeWeight(path, edgePathStartFraction[edge], edgePathEndFraction[edge], length) : length * (pathWeightFactors ? pathWeightFactors[path] : 1);
844
+ if (!(weight >= 0))
845
+ throw new RangeError("Edge weights must be zero or positive");
846
+ weights[edge] = weight;
847
+ }
848
+ return weights;
849
+ }
850
+ //# sourceMappingURL=line-network.cjs.map