@math.gl/geospatial 5.0.1 → 5.0.2
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/dist/coverage-simplification.d.ts +9 -0
- package/dist/coverage-simplification.d.ts.map +1 -0
- package/dist/coverage-simplification.js +167 -0
- package/dist/coverage-simplification.js.map +1 -0
- package/dist/line-network/build-line-network.d.ts +100 -0
- package/dist/line-network/build-line-network.d.ts.map +1 -0
- package/dist/line-network/build-line-network.js +766 -0
- package/dist/line-network/build-line-network.js.map +1 -0
- package/dist/line-network/line-network-routes.d.ts +53 -0
- package/dist/line-network/line-network-routes.d.ts.map +1 -0
- package/dist/line-network/line-network-routes.js +146 -0
- package/dist/line-network/line-network-routes.js.map +1 -0
- package/dist/line-network/line-network-weights.d.ts +11 -0
- package/dist/line-network/line-network-weights.d.ts.map +1 -0
- package/dist/line-network/line-network-weights.js +25 -0
- package/dist/line-network/line-network-weights.js.map +1 -0
- package/dist/line-network.cjs +850 -0
- package/dist/line-network.cjs.map +6 -0
- package/dist/line-network.d.ts +7 -0
- package/dist/line-network.d.ts.map +1 -0
- package/dist/line-network.js +7 -0
- package/dist/line-network.js.map +1 -0
- package/dist/linear-referencing.d.ts +2 -12
- package/dist/linear-referencing.d.ts.map +1 -1
- package/dist/linear-referencing.js +17 -49
- package/dist/linear-referencing.js.map +1 -1
- package/dist/polygon-simplification.d.ts.map +1 -1
- package/dist/polygon-simplification.js +3 -33
- package/dist/polygon-simplification.js.map +1 -1
- package/dist/route-editing.d.ts +10 -0
- package/dist/route-editing.d.ts.map +1 -0
- package/dist/route-editing.js +58 -0
- package/dist/route-editing.js.map +1 -0
- package/dist/spatial-line-polygon-intersection.d.ts.map +1 -1
- package/dist/spatial-line-polygon-intersection.js +2 -2
- package/dist/spatial-line-polygon-intersection.js.map +1 -1
- package/dist/spatial-planar-intersects.d.ts +1 -11
- package/dist/spatial-planar-intersects.d.ts.map +1 -1
- package/dist/spatial-planar-intersects.js +2 -42
- package/dist/spatial-planar-intersects.js.map +1 -1
- package/dist/spatial-sql.d.ts +6 -4
- package/dist/spatial-sql.d.ts.map +1 -1
- package/dist/spatial-sql.js +38 -4
- package/dist/spatial-sql.js.map +1 -1
- package/dist/spatial-touches.d.ts +0 -2
- package/dist/spatial-touches.d.ts.map +1 -1
- package/dist/spatial-touches.js +1 -15
- package/dist/spatial-touches.js.map +1 -1
- package/dist/st-coverage-functions.d.ts +11 -0
- package/dist/st-coverage-functions.d.ts.map +1 -0
- package/dist/st-coverage-functions.js +57 -0
- package/dist/st-coverage-functions.js.map +1 -0
- package/dist/st-functions.cjs +1201 -775
- package/dist/st-functions.cjs.map +3 -3
- package/dist/st-functions.d.ts +36 -3
- package/dist/st-functions.d.ts.map +1 -1
- package/dist/st-functions.js +53 -19
- package/dist/st-functions.js.map +1 -1
- package/dist/st-geodesic-functions.d.ts +10 -2
- package/dist/st-geodesic-functions.d.ts.map +1 -1
- package/dist/st-geodesic-functions.js +179 -25
- package/dist/st-geodesic-functions.js.map +1 -1
- package/dist/st-hull-functions.cjs.map +1 -1
- package/dist/st-overlay-functions.cjs +114 -67
- package/dist/st-overlay-functions.cjs.map +2 -2
- package/dist/st-overlay-functions.d.ts +11 -1
- package/dist/st-overlay-functions.d.ts.map +1 -1
- package/dist/st-overlay-functions.js +74 -9
- package/dist/st-overlay-functions.js.map +1 -1
- package/dist/st-viewport-functions.cjs.map +1 -1
- package/dist/turf-functions.cjs.map +1 -1
- package/package.json +12 -6
- package/src/coverage-simplification.ts +215 -0
- package/src/line-network/build-line-network.ts +990 -0
- package/src/line-network/line-network-routes.ts +213 -0
- package/src/line-network/line-network-weights.ts +40 -0
- package/src/line-network.ts +29 -0
- package/src/linear-referencing.ts +23 -64
- package/src/polygon-simplification.ts +3 -42
- package/src/route-editing.ts +107 -0
- package/src/spatial-line-polygon-intersection.ts +7 -2
- package/src/spatial-planar-intersects.ts +7 -54
- package/src/spatial-sql.ts +54 -5
- package/src/spatial-touches.ts +1 -15
- package/src/st-coverage-functions.ts +88 -0
- package/src/st-functions.ts +70 -19
- package/src/st-geodesic-functions.ts +218 -26
- package/src/st-overlay-functions.ts +79 -10
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
/** Mean radius of the Earth in metres (IUGG). */
|
|
5
|
+
const EARTH_RADIUS = 6371008.8;
|
|
6
|
+
/** Metres of one degree of latitude on the mean sphere. */
|
|
7
|
+
const METRES_PER_DEGREE = (EARTH_RADIUS * Math.PI) / 180;
|
|
8
|
+
/** Degrees to radians. */
|
|
9
|
+
const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
10
|
+
/** Marks an empty slot, cluster or segment. */
|
|
11
|
+
const EMPTY = -1;
|
|
12
|
+
/** Great-circle distance in metres between two longitude, latitude points (haversine, mean radius). */
|
|
13
|
+
export function haversineDistance(longitude1, latitude1, longitude2, latitude2) {
|
|
14
|
+
const phi1 = latitude1 * DEGREES_TO_RADIANS;
|
|
15
|
+
const phi2 = latitude2 * DEGREES_TO_RADIANS;
|
|
16
|
+
const sinHalfLat = Math.sin((phi2 - phi1) / 2);
|
|
17
|
+
const sinHalfLon = Math.sin(((longitude2 - longitude1) * DEGREES_TO_RADIANS) / 2);
|
|
18
|
+
const h = sinHalfLat * sinHalfLat + Math.cos(phi1) * Math.cos(phi2) * sinHalfLon * sinHalfLon;
|
|
19
|
+
return 2 * EARTH_RADIUS * Math.asin(Math.min(1, Math.sqrt(h)));
|
|
20
|
+
}
|
|
21
|
+
/** Open-addressing slot of an integer cell key. Inserts the key when `insert` is set. */
|
|
22
|
+
function findCellSlot(slotKeys, slotHeads, slotMask, cellX, cellY, insert) {
|
|
23
|
+
let slot = (Math.imul(cellX, 0x9e3779b1) ^ Math.imul(cellY, 0x85ebca77)) & slotMask;
|
|
24
|
+
for (;;) {
|
|
25
|
+
if (slotHeads[slot] === EMPTY) {
|
|
26
|
+
if (!insert)
|
|
27
|
+
return EMPTY;
|
|
28
|
+
slotKeys[slot * 2] = cellX;
|
|
29
|
+
slotKeys[slot * 2 + 1] = cellY;
|
|
30
|
+
return slot;
|
|
31
|
+
}
|
|
32
|
+
if (slotKeys[slot * 2] === cellX && slotKeys[slot * 2 + 1] === cellY)
|
|
33
|
+
return slot;
|
|
34
|
+
slot = (slot + 1) & slotMask;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Smallest power of two, at least 16, that is not less than `count`. */
|
|
38
|
+
function getHashCapacity(count) {
|
|
39
|
+
let capacity = 16;
|
|
40
|
+
while (capacity < count)
|
|
41
|
+
capacity *= 2;
|
|
42
|
+
return capacity;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Snaps points into clusters on a grid in a local metric frame.
|
|
46
|
+
* A point joins the first cluster within the tolerance (own cell first, then the 8 neighbours),
|
|
47
|
+
* so the result is deterministic for a given input order.
|
|
48
|
+
*/
|
|
49
|
+
class PointSnapper {
|
|
50
|
+
/** Makes a snapper for at most `maxPoints` clusters. */
|
|
51
|
+
constructor(maxPoints, tolerance) {
|
|
52
|
+
/** Number of clusters. */
|
|
53
|
+
this.clusterCount = 0;
|
|
54
|
+
this.tolerance = tolerance;
|
|
55
|
+
this.toleranceSquared = tolerance * tolerance;
|
|
56
|
+
this.clusterX = new Float64Array(maxPoints);
|
|
57
|
+
this.clusterY = new Float64Array(maxPoints);
|
|
58
|
+
this.nextInCell = new Int32Array(maxPoints);
|
|
59
|
+
const capacity = getHashCapacity(maxPoints * 2);
|
|
60
|
+
this.slotMask = capacity - 1;
|
|
61
|
+
this.slotKeys = new Int32Array(capacity * 2);
|
|
62
|
+
this.slotHeads = new Int32Array(capacity).fill(EMPTY);
|
|
63
|
+
}
|
|
64
|
+
/** Returns the cluster of the point, and makes a new cluster when none is in range. */
|
|
65
|
+
snap(lon, lat) {
|
|
66
|
+
const cellY = Math.floor((lat * METRES_PER_DEGREE) / this.tolerance);
|
|
67
|
+
let found = EMPTY;
|
|
68
|
+
// Each latitude row has its own longitude scale, so compute the column in every row searched.
|
|
69
|
+
for (let dy = 0; found === EMPTY && dy < 3; dy++) {
|
|
70
|
+
const row = cellY + (dy === 0 ? 0 : dy === 1 ? -1 : 1);
|
|
71
|
+
const column = this.getColumn(lon, row);
|
|
72
|
+
found = this.findInCell(column, row, lon, lat);
|
|
73
|
+
if (found === EMPTY)
|
|
74
|
+
found = this.findInCell(column - 1, row, lon, lat);
|
|
75
|
+
if (found === EMPTY)
|
|
76
|
+
found = this.findInCell(column + 1, row, lon, lat);
|
|
77
|
+
}
|
|
78
|
+
if (found !== EMPTY)
|
|
79
|
+
return found;
|
|
80
|
+
const cluster = this.clusterCount++;
|
|
81
|
+
this.clusterX[cluster] = lon;
|
|
82
|
+
this.clusterY[cluster] = lat;
|
|
83
|
+
const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, this.getColumn(lon, cellY) | 0, cellY | 0, true);
|
|
84
|
+
this.nextInCell[cluster] = this.slotHeads[slot];
|
|
85
|
+
this.slotHeads[slot] = cluster;
|
|
86
|
+
return cluster;
|
|
87
|
+
}
|
|
88
|
+
/** Grid column of a longitude in a row, with the longitude scale at the row centre. */
|
|
89
|
+
getColumn(lon, row) {
|
|
90
|
+
const rowLatitude = ((row + 0.5) * this.tolerance) / METRES_PER_DEGREE;
|
|
91
|
+
const scale = METRES_PER_DEGREE * Math.cos(rowLatitude * DEGREES_TO_RADIANS);
|
|
92
|
+
return Math.floor((lon * scale) / this.tolerance);
|
|
93
|
+
}
|
|
94
|
+
/** First cluster of a cell within the tolerance of the point, or EMPTY. */
|
|
95
|
+
findInCell(cellX, cellY, lon, lat) {
|
|
96
|
+
// Fold large grid keys the same way on insertion and lookup. The distance check
|
|
97
|
+
// below rejects candidates from other cells that fold to the same key.
|
|
98
|
+
const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, cellX | 0, cellY | 0, false);
|
|
99
|
+
if (slot === EMPTY)
|
|
100
|
+
return EMPTY;
|
|
101
|
+
const cosLat = Math.cos(lat * DEGREES_TO_RADIANS);
|
|
102
|
+
for (let c = this.slotHeads[slot]; c !== EMPTY; c = this.nextInCell[c]) {
|
|
103
|
+
const dx = (this.clusterX[c] - lon) * METRES_PER_DEGREE * cosLat;
|
|
104
|
+
const dy = (this.clusterY[c] - lat) * METRES_PER_DEGREE;
|
|
105
|
+
if (dx * dx + dy * dy <= this.toleranceSquared)
|
|
106
|
+
return c;
|
|
107
|
+
}
|
|
108
|
+
return EMPTY;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Grid hash of segments: each segment (named by its first vertex) goes into every cell that it
|
|
113
|
+
* passes through. Rows use the same per-latitude longitude scale as `PointSnapper`, so a 3 x 3
|
|
114
|
+
* search around a point finds every segment within one cell size.
|
|
115
|
+
*/
|
|
116
|
+
class SegmentIndex {
|
|
117
|
+
/** Indexes every segment of `lines`. */
|
|
118
|
+
constructor(lines, cellSize, minLatitude, maxLatitude) {
|
|
119
|
+
/** Number of entries inserted so far. */
|
|
120
|
+
this.entryCount = 0;
|
|
121
|
+
this.cellSize = cellSize;
|
|
122
|
+
// One spare row on each side for the 3 x 3 search.
|
|
123
|
+
this.firstRow = this.row(minLatitude) - 1;
|
|
124
|
+
this.rowScale = new Float64Array(this.row(maxLatitude) - this.firstRow + 2);
|
|
125
|
+
for (let i = 0; i < this.rowScale.length; i++) {
|
|
126
|
+
const rowLatitude = ((this.firstRow + i + 0.5) * cellSize) / METRES_PER_DEGREE;
|
|
127
|
+
this.rowScale[i] = METRES_PER_DEGREE * Math.cos(rowLatitude * DEGREES_TO_RADIANS);
|
|
128
|
+
}
|
|
129
|
+
const pathIndices = lines.pathIndices.value;
|
|
130
|
+
const pathCount = pathIndices.length - 1;
|
|
131
|
+
let entries = 0;
|
|
132
|
+
for (let path = 0; path < pathCount; path++) {
|
|
133
|
+
for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
|
|
134
|
+
entries += this.addSegment(lines.positions, s, false);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const capacity = getHashCapacity(entries * 2);
|
|
138
|
+
this.slotMask = capacity - 1;
|
|
139
|
+
this.slotKeys = new Int32Array(capacity * 2);
|
|
140
|
+
this.slotHeads = new Int32Array(capacity).fill(EMPTY);
|
|
141
|
+
this.entrySegment = new Int32Array(entries);
|
|
142
|
+
this.entryNext = new Int32Array(entries);
|
|
143
|
+
for (let path = 0; path < pathCount; path++) {
|
|
144
|
+
for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
|
|
145
|
+
this.addSegment(lines.positions, s, true);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** Grid row of a latitude. */
|
|
150
|
+
row(lat) {
|
|
151
|
+
return Math.floor((lat * METRES_PER_DEGREE) / this.cellSize);
|
|
152
|
+
}
|
|
153
|
+
/** Metres per degree of longitude in a row (at the row centre). Covers the data and one row on each side. */
|
|
154
|
+
scale(row) {
|
|
155
|
+
return this.rowScale[row - this.firstRow];
|
|
156
|
+
}
|
|
157
|
+
/** Grid column of a longitude in a row. */
|
|
158
|
+
column(lon, row) {
|
|
159
|
+
return Math.floor((lon * this.scale(row)) / this.cellSize);
|
|
160
|
+
}
|
|
161
|
+
/** First entry of a cell, or EMPTY. */
|
|
162
|
+
head(column, row) {
|
|
163
|
+
const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, column, row, false);
|
|
164
|
+
return slot === EMPTY ? EMPTY : this.slotHeads[slot];
|
|
165
|
+
}
|
|
166
|
+
/** Visits the cells that segment `s` (vertex s to s + 1) passes through. Returns the count. */
|
|
167
|
+
addSegment(positions, s, insert) {
|
|
168
|
+
const { value, size } = positions;
|
|
169
|
+
const lon1 = value[size * s];
|
|
170
|
+
const lat1 = value[size * s + 1];
|
|
171
|
+
const lon2 = value[size * s + size];
|
|
172
|
+
const lat2 = value[size * s + size + 1];
|
|
173
|
+
const y1 = (lat1 * METRES_PER_DEGREE) / this.cellSize;
|
|
174
|
+
const y2 = (lat2 * METRES_PER_DEGREE) / this.cellSize;
|
|
175
|
+
const firstRow = Math.floor(Math.min(y1, y2));
|
|
176
|
+
const lastRow = Math.floor(Math.max(y1, y2));
|
|
177
|
+
let count = 0;
|
|
178
|
+
for (let row = firstRow; row <= lastRow; row++) {
|
|
179
|
+
// Clip the segment to the latitude band of the row, then take the column range in that row.
|
|
180
|
+
let ta = 0;
|
|
181
|
+
let tb = 1;
|
|
182
|
+
if (y2 !== y1) {
|
|
183
|
+
const a = (row - y1) / (y2 - y1);
|
|
184
|
+
const b = (row + 1 - y1) / (y2 - y1);
|
|
185
|
+
ta = Math.max(0, Math.min(a, b));
|
|
186
|
+
tb = Math.min(1, Math.max(a, b));
|
|
187
|
+
}
|
|
188
|
+
const lonA = lon1 + ta * (lon2 - lon1);
|
|
189
|
+
const lonB = lon1 + tb * (lon2 - lon1);
|
|
190
|
+
const firstColumn = this.column(Math.min(lonA, lonB), row);
|
|
191
|
+
const lastColumn = this.column(Math.max(lonA, lonB), row);
|
|
192
|
+
for (let column = firstColumn; column <= lastColumn; column++) {
|
|
193
|
+
count++;
|
|
194
|
+
if (!insert)
|
|
195
|
+
continue;
|
|
196
|
+
const slot = findCellSlot(this.slotKeys, this.slotHeads, this.slotMask, column, row, true);
|
|
197
|
+
const entry = this.entryCount++;
|
|
198
|
+
this.entrySegment[entry] = s;
|
|
199
|
+
this.entryNext[entry] = this.slotHeads[slot];
|
|
200
|
+
this.slotHeads[slot] = entry;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return count;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Growable list of cut points inside segments: the segment (first vertex), the fraction and the cluster. */
|
|
207
|
+
class SplitList {
|
|
208
|
+
constructor() {
|
|
209
|
+
/** Segment of each cut. */
|
|
210
|
+
this.vertex = new Int32Array(64);
|
|
211
|
+
/** Fraction of the segment at each cut. */
|
|
212
|
+
this.fraction = new Float64Array(64);
|
|
213
|
+
/** Cluster of each cut. */
|
|
214
|
+
this.cluster = new Int32Array(64);
|
|
215
|
+
/** Number of cuts. */
|
|
216
|
+
this.count = 0;
|
|
217
|
+
}
|
|
218
|
+
/** Adds a cut, and doubles the storage when it is full. */
|
|
219
|
+
push(vertex, fraction, cluster) {
|
|
220
|
+
if (this.count === this.vertex.length) {
|
|
221
|
+
const vertices = new Int32Array(this.count * 2);
|
|
222
|
+
vertices.set(this.vertex);
|
|
223
|
+
this.vertex = vertices;
|
|
224
|
+
const fractions = new Float64Array(this.count * 2);
|
|
225
|
+
fractions.set(this.fraction);
|
|
226
|
+
this.fraction = fractions;
|
|
227
|
+
const clusters = new Int32Array(this.count * 2);
|
|
228
|
+
clusters.set(this.cluster);
|
|
229
|
+
this.cluster = clusters;
|
|
230
|
+
}
|
|
231
|
+
this.vertex[this.count] = vertex;
|
|
232
|
+
this.fraction[this.count] = fraction;
|
|
233
|
+
this.cluster[this.count] = cluster;
|
|
234
|
+
this.count++;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Checks the input and returns the path count. */
|
|
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
|
+
/**
|
|
259
|
+
* Builds a graph from line geometry.
|
|
260
|
+
*
|
|
261
|
+
* Every path with two or more vertices gives one edge between the nodes of its snapped
|
|
262
|
+
* endpoints. `splitAtSharedVertices` also cuts paths at interior vertices that another
|
|
263
|
+
* vertex shares. T-junctions (`junctionTolerance`, on by default) join dangling ends to the
|
|
264
|
+
* interior of nearby paths. `splitAtCrossings` (off by default) adds a node where two segments
|
|
265
|
+
* cross. Paths with fewer than two vertices are skipped. Snapping does not handle the
|
|
266
|
+
* antimeridian.
|
|
267
|
+
*/
|
|
268
|
+
export function buildLineNetwork(lines, options = {}) {
|
|
269
|
+
const tolerance = options.snapTolerance ?? 1;
|
|
270
|
+
if (!(tolerance > 0 && tolerance < Infinity)) {
|
|
271
|
+
throw new RangeError('buildLineNetwork: snapTolerance must be positive and finite');
|
|
272
|
+
}
|
|
273
|
+
const junctionTolerance = options.junctionTolerance ?? tolerance;
|
|
274
|
+
if (!(junctionTolerance >= 0 && junctionTolerance < Infinity)) {
|
|
275
|
+
throw new RangeError('buildLineNetwork: junctionTolerance must be zero or positive and finite');
|
|
276
|
+
}
|
|
277
|
+
const pathCount = validateLines(lines);
|
|
278
|
+
const split = Boolean(options.splitAtSharedVertices);
|
|
279
|
+
const crossings = Boolean(options.splitAtCrossings);
|
|
280
|
+
const distance = options.measureDistance ?? haversineDistance;
|
|
281
|
+
const { value: positions, size } = lines.positions;
|
|
282
|
+
const pathIndices = lines.pathIndices.value;
|
|
283
|
+
const vertexCount = pathCount > 0 ? pathIndices[pathCount] : 0;
|
|
284
|
+
let minLatitude = Infinity;
|
|
285
|
+
let maxLatitude = -Infinity;
|
|
286
|
+
for (let v = 0; v < vertexCount; v++) {
|
|
287
|
+
const lon = positions[size * v];
|
|
288
|
+
const lat = positions[size * v + 1];
|
|
289
|
+
if (!(lat >= -90 && lat <= 90 && Number.isFinite(lon))) {
|
|
290
|
+
throw new RangeError('buildLineNetwork: positions must be finite longitude, latitude degrees');
|
|
291
|
+
}
|
|
292
|
+
if (lat < minLatitude)
|
|
293
|
+
minLatitude = lat;
|
|
294
|
+
if (lat > maxLatitude)
|
|
295
|
+
maxLatitude = lat;
|
|
296
|
+
}
|
|
297
|
+
const snapper = new PointSnapper(split ? vertexCount : pathCount * 2, tolerance);
|
|
298
|
+
// Cluster of each vertex that is a node candidate (EMPTY for the others).
|
|
299
|
+
const vertexCluster = new Int32Array(vertexCount).fill(EMPTY);
|
|
300
|
+
for (let path = 0; path < pathCount; path++) {
|
|
301
|
+
const first = pathIndices[path];
|
|
302
|
+
const last = pathIndices[path + 1] - 1;
|
|
303
|
+
if (last <= first)
|
|
304
|
+
continue;
|
|
305
|
+
if (split) {
|
|
306
|
+
for (let v = first; v <= last; v++) {
|
|
307
|
+
vertexCluster[v] = snapper.snap(positions[size * v], positions[size * v + 1]);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
vertexCluster[first] = snapper.snap(positions[size * first], positions[size * first + 1]);
|
|
312
|
+
vertexCluster[last] = snapper.snap(positions[size * last], positions[size * last + 1]);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// A cluster is a node when it holds an endpoint, or (split mode) two non-consecutive vertices.
|
|
316
|
+
// `clusterUses` counts the visits; an endpoint cluster with one visit is a dangling end.
|
|
317
|
+
const snappedCount = snapper.clusterCount;
|
|
318
|
+
const clusterUses = new Uint32Array(snappedCount);
|
|
319
|
+
const isEndpointCluster = new Uint8Array(snappedCount);
|
|
320
|
+
for (let path = 0; path < pathCount; path++) {
|
|
321
|
+
const first = pathIndices[path];
|
|
322
|
+
const last = pathIndices[path + 1] - 1;
|
|
323
|
+
if (last <= first)
|
|
324
|
+
continue;
|
|
325
|
+
isEndpointCluster[vertexCluster[first]] = 1;
|
|
326
|
+
isEndpointCluster[vertexCluster[last]] = 1;
|
|
327
|
+
for (let v = first; v <= last; v++) {
|
|
328
|
+
const cluster = vertexCluster[v];
|
|
329
|
+
if (cluster !== EMPTY && (v === first || cluster !== vertexCluster[v - 1])) {
|
|
330
|
+
clusterUses[cluster]++;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const isSnappedNode = (c) => isEndpointCluster[c] === 1 || (split && clusterUses[c] >= 2);
|
|
335
|
+
const splits = new SplitList();
|
|
336
|
+
// Union pairs (a dangling end joins an existing node), and the positions of crossing clusters.
|
|
337
|
+
const unions = [];
|
|
338
|
+
const crossingPositions = [];
|
|
339
|
+
const clusterGap = junctionTolerance > 0 ? new Float64Array(snappedCount) : null;
|
|
340
|
+
let dangling = 0;
|
|
341
|
+
if (clusterGap) {
|
|
342
|
+
for (let c = 0; c < snappedCount; c++) {
|
|
343
|
+
if (isEndpointCluster[c] && clusterUses[c] === 1)
|
|
344
|
+
dangling++;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (dangling > 0 || crossings) {
|
|
348
|
+
// Cell size: at least the junction tolerance (so a 3 x 3 search is complete), and about one
|
|
349
|
+
// mean segment length (so a segment covers few cells).
|
|
350
|
+
let lengthSum = 0;
|
|
351
|
+
let segmentCount = 0;
|
|
352
|
+
for (let path = 0; path < pathCount; path++) {
|
|
353
|
+
for (let s = pathIndices[path]; s < pathIndices[path + 1] - 1; s++) {
|
|
354
|
+
const cosLat = Math.cos(positions[size * s + 1] * DEGREES_TO_RADIANS);
|
|
355
|
+
const dx = (positions[size * s + size] - positions[size * s]) * cosLat;
|
|
356
|
+
const dy = positions[size * s + size + 1] - positions[size * s + 1];
|
|
357
|
+
lengthSum += Math.sqrt(dx * dx + dy * dy) * METRES_PER_DEGREE;
|
|
358
|
+
segmentCount++;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const cellSize = Math.max(junctionTolerance, segmentCount ? lengthSum / segmentCount : 0, 1);
|
|
362
|
+
// No segment means no latitude range, and nothing to join or to cross.
|
|
363
|
+
const index = segmentCount > 0 ? new SegmentIndex(lines, cellSize, minLatitude, maxLatitude) : null;
|
|
364
|
+
if (index && clusterGap && dangling > 0) {
|
|
365
|
+
joinDanglingEnds(lines, vertexCluster, clusterUses, isEndpointCluster, index, {
|
|
366
|
+
junctionTolerance,
|
|
367
|
+
distance,
|
|
368
|
+
clusterGap,
|
|
369
|
+
isSnappedNode,
|
|
370
|
+
splits,
|
|
371
|
+
unions
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
if (index && crossings) {
|
|
375
|
+
splitCrossings(lines, vertexCluster, index, snappedCount, splits, crossingPositions);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// Union-find over snapped clusters and crossing clusters. The root is the lowest id.
|
|
379
|
+
const clusterCount = snappedCount + crossingPositions.length / 2;
|
|
380
|
+
const parent = new Int32Array(clusterCount);
|
|
381
|
+
for (let c = 0; c < clusterCount; c++)
|
|
382
|
+
parent[c] = c;
|
|
383
|
+
const find = (x) => {
|
|
384
|
+
while (parent[x] !== x)
|
|
385
|
+
x = parent[x] = parent[parent[x]];
|
|
386
|
+
return x;
|
|
387
|
+
};
|
|
388
|
+
const join = (first, second) => {
|
|
389
|
+
const a = find(first);
|
|
390
|
+
const b = find(second);
|
|
391
|
+
if (a !== b)
|
|
392
|
+
parent[Math.max(a, b)] = Math.min(a, b);
|
|
393
|
+
};
|
|
394
|
+
for (let i = 0; i < unions.length; i += 2)
|
|
395
|
+
join(unions[i], unions[i + 1]);
|
|
396
|
+
// Cut points inside segments, grouped by segment and sorted by fraction.
|
|
397
|
+
const splitOffsets = splits.count ? new Uint32Array(vertexCount + 1) : null;
|
|
398
|
+
const splitFraction = new Float64Array(splits.count);
|
|
399
|
+
const splitNode = new Int32Array(splits.count);
|
|
400
|
+
if (splitOffsets) {
|
|
401
|
+
for (let k = 0; k < splits.count; k++)
|
|
402
|
+
splitOffsets[splits.vertex[k] + 1]++;
|
|
403
|
+
for (let v = 0; v < vertexCount; v++)
|
|
404
|
+
splitOffsets[v + 1] += splitOffsets[v];
|
|
405
|
+
const cursor = splitOffsets.slice(0, vertexCount);
|
|
406
|
+
for (let k = 0; k < splits.count; k++) {
|
|
407
|
+
const at = cursor[splits.vertex[k]]++;
|
|
408
|
+
// Insertion sort inside the bucket of the segment.
|
|
409
|
+
let i = at;
|
|
410
|
+
const start = splitOffsets[splits.vertex[k]];
|
|
411
|
+
while (i > start && splitFraction[i - 1] > splits.fraction[k]) {
|
|
412
|
+
splitFraction[i] = splitFraction[i - 1];
|
|
413
|
+
splitNode[i] = splitNode[i - 1];
|
|
414
|
+
i--;
|
|
415
|
+
}
|
|
416
|
+
splitFraction[i] = splits.fraction[k];
|
|
417
|
+
splitNode[i] = splits.cluster[k];
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (splitOffsets) {
|
|
421
|
+
// Every pair of crossing segments contributes a cut. Join coincident cuts on
|
|
422
|
+
// each segment before assigning node IDs, including three-way intersections.
|
|
423
|
+
for (let v = 0; v < vertexCount; v++) {
|
|
424
|
+
const first = splitOffsets[v];
|
|
425
|
+
const end = splitOffsets[v + 1];
|
|
426
|
+
if (end - first < 2)
|
|
427
|
+
continue;
|
|
428
|
+
const span = Math.max(Math.abs(positions[size * v + size] - positions[size * v]), Math.abs(positions[size * v + size + 1] - positions[size * v + 1]));
|
|
429
|
+
let representative = first;
|
|
430
|
+
for (let k = first + 1; k < end; k++) {
|
|
431
|
+
if ((splitFraction[k] - splitFraction[representative]) * span > 1e-12) {
|
|
432
|
+
representative = k;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
join(splitNode[representative], splitNode[k]);
|
|
436
|
+
splitFraction[k] = splitFraction[representative];
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
const isNode = new Uint8Array(clusterCount);
|
|
441
|
+
for (let c = 0; c < snappedCount; c++)
|
|
442
|
+
if (isSnappedNode(c))
|
|
443
|
+
isNode[find(c)] = 1;
|
|
444
|
+
for (let c = snappedCount; c < clusterCount; c++)
|
|
445
|
+
isNode[c] = 1;
|
|
446
|
+
for (let k = 0; k < splits.count; k++)
|
|
447
|
+
isNode[find(splits.cluster[k])] = 1;
|
|
448
|
+
const clusterNode = new Int32Array(clusterCount).fill(EMPTY);
|
|
449
|
+
let nodeCount = 0;
|
|
450
|
+
for (let c = 0; c < clusterCount; c++) {
|
|
451
|
+
if (find(c) === c && isNode[c])
|
|
452
|
+
clusterNode[c] = nodeCount++;
|
|
453
|
+
}
|
|
454
|
+
const nodePositions = new Float64Array(nodeCount * 2);
|
|
455
|
+
for (let c = 0; c < clusterCount; c++) {
|
|
456
|
+
const root = find(c);
|
|
457
|
+
if (root !== c) {
|
|
458
|
+
clusterNode[c] = clusterNode[root];
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
const node = clusterNode[c];
|
|
462
|
+
if (node === EMPTY)
|
|
463
|
+
continue;
|
|
464
|
+
if (c < snappedCount) {
|
|
465
|
+
nodePositions[2 * node] = snapper.clusterX[c];
|
|
466
|
+
nodePositions[2 * node + 1] = snapper.clusterY[c];
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
nodePositions[2 * node] = crossingPositions[2 * (c - snappedCount)];
|
|
470
|
+
nodePositions[2 * node + 1] = crossingPositions[2 * (c - snappedCount) + 1];
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
for (let k = 0; k < splitNode.length; k++)
|
|
474
|
+
splitNode[k] = clusterNode[splitNode[k]];
|
|
475
|
+
// Count path by path: an empty path (a repeated offset) has no segment, not -1.
|
|
476
|
+
let maxEdges = splits.count;
|
|
477
|
+
for (let path = 0; path < pathCount; path++) {
|
|
478
|
+
const pathVertices = pathIndices[path + 1] - pathIndices[path];
|
|
479
|
+
if (pathVertices > 1)
|
|
480
|
+
maxEdges += split ? pathVertices - 1 : 1;
|
|
481
|
+
}
|
|
482
|
+
const edges = new EdgeColumns(maxEdges, Boolean(options.includePathFractions));
|
|
483
|
+
for (let path = 0; path < pathCount; path++) {
|
|
484
|
+
const first = pathIndices[path];
|
|
485
|
+
const last = pathIndices[path + 1] - 1;
|
|
486
|
+
if (last <= first)
|
|
487
|
+
continue;
|
|
488
|
+
const row = lines.pathRows ? lines.pathRows[path] : path;
|
|
489
|
+
const firstEdge = edges.count;
|
|
490
|
+
let start = first;
|
|
491
|
+
let startFraction = 0;
|
|
492
|
+
let startNode = clusterNode[vertexCluster[first]];
|
|
493
|
+
let previousNode = startNode;
|
|
494
|
+
let length = clusterGap ? clusterGap[vertexCluster[first]] : 0;
|
|
495
|
+
// Metres along the path geometry (no gaps) at the start of the current segment and edge.
|
|
496
|
+
let pathAlong = 0;
|
|
497
|
+
let edgeAlong = 0;
|
|
498
|
+
for (let v = first + 1; v <= last; v++) {
|
|
499
|
+
const segmentLength = distance(positions[size * v - size], positions[size * v - size + 1], positions[size * v], positions[size * v + 1]);
|
|
500
|
+
let along = 0;
|
|
501
|
+
if (splitOffsets) {
|
|
502
|
+
for (let k = splitOffsets[v - 1]; k < splitOffsets[v]; k++) {
|
|
503
|
+
const fraction = splitFraction[k];
|
|
504
|
+
const node = splitNode[k];
|
|
505
|
+
length += (fraction - along) * segmentLength;
|
|
506
|
+
along = fraction;
|
|
507
|
+
previousNode = node;
|
|
508
|
+
if (length === 0 && node === startNode) {
|
|
509
|
+
start = v - 1;
|
|
510
|
+
startFraction = fraction;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
const cutAlong = pathAlong + fraction * segmentLength;
|
|
514
|
+
edges.push(path, row, startNode, node, length, start, v, startFraction, fraction);
|
|
515
|
+
edges.setAlong(edgeAlong, cutAlong);
|
|
516
|
+
start = v - 1;
|
|
517
|
+
startFraction = fraction;
|
|
518
|
+
startNode = node;
|
|
519
|
+
length = 0;
|
|
520
|
+
edgeAlong = cutAlong;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
length += (1 - along) * segmentLength;
|
|
524
|
+
pathAlong += segmentLength;
|
|
525
|
+
const cluster = vertexCluster[v];
|
|
526
|
+
const node = cluster === EMPTY ? EMPTY : clusterNode[cluster];
|
|
527
|
+
const isCut = v === last || (split && node !== EMPTY && node !== previousNode);
|
|
528
|
+
previousNode = node;
|
|
529
|
+
if (!isCut)
|
|
530
|
+
continue;
|
|
531
|
+
if (v === last && clusterGap)
|
|
532
|
+
length += clusterGap[cluster];
|
|
533
|
+
if (length === 0 && node === startNode) {
|
|
534
|
+
start = v;
|
|
535
|
+
startFraction = 0;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
edges.push(path, row, startNode, node, length, start, v, startFraction, 1);
|
|
539
|
+
edges.setAlong(edgeAlong, pathAlong);
|
|
540
|
+
start = v;
|
|
541
|
+
startFraction = 0;
|
|
542
|
+
startNode = node;
|
|
543
|
+
length = 0;
|
|
544
|
+
edgeAlong = pathAlong;
|
|
545
|
+
}
|
|
546
|
+
edges.normalizeAlong(firstEdge, pathAlong);
|
|
547
|
+
}
|
|
548
|
+
return edges.toNetwork(nodeCount, nodePositions);
|
|
549
|
+
}
|
|
550
|
+
/** Edge columns sized for the most edges that a build can make. */
|
|
551
|
+
class EdgeColumns {
|
|
552
|
+
/** Allocates the columns. `withAlong` keeps the along-path positions for routing weights. */
|
|
553
|
+
constructor(maxEdges, withAlong) {
|
|
554
|
+
/** Number of edges written. */
|
|
555
|
+
this.count = 0;
|
|
556
|
+
this.source = new Uint32Array(maxEdges);
|
|
557
|
+
this.target = new Uint32Array(maxEdges);
|
|
558
|
+
this.length = new Float64Array(maxEdges);
|
|
559
|
+
this.path = new Uint32Array(maxEdges);
|
|
560
|
+
this.row = new Uint32Array(maxEdges);
|
|
561
|
+
this.vertexStart = new Uint32Array(maxEdges);
|
|
562
|
+
this.vertexEnd = new Uint32Array(maxEdges);
|
|
563
|
+
this.startFraction = new Float64Array(maxEdges);
|
|
564
|
+
this.endFraction = new Float64Array(maxEdges);
|
|
565
|
+
this.alongStart = withAlong ? new Float64Array(maxEdges) : null;
|
|
566
|
+
this.alongEnd = withAlong ? new Float64Array(maxEdges) : null;
|
|
567
|
+
}
|
|
568
|
+
/** Appends one edge. */
|
|
569
|
+
push(path, row, source, target, length, vertexStart, vertexEnd, startFraction, endFraction) {
|
|
570
|
+
const e = this.count++;
|
|
571
|
+
this.path[e] = path;
|
|
572
|
+
this.row[e] = row;
|
|
573
|
+
this.source[e] = source;
|
|
574
|
+
this.target[e] = target;
|
|
575
|
+
this.length[e] = length;
|
|
576
|
+
this.vertexStart[e] = vertexStart;
|
|
577
|
+
this.vertexEnd[e] = vertexEnd;
|
|
578
|
+
this.startFraction[e] = startFraction;
|
|
579
|
+
this.endFraction[e] = endFraction;
|
|
580
|
+
}
|
|
581
|
+
/** Records where the last edge starts and ends along its path, in metres. */
|
|
582
|
+
setAlong(start, end) {
|
|
583
|
+
if (!this.alongStart || !this.alongEnd)
|
|
584
|
+
return;
|
|
585
|
+
this.alongStart[this.count - 1] = start;
|
|
586
|
+
this.alongEnd[this.count - 1] = end;
|
|
587
|
+
}
|
|
588
|
+
/** Converts retained distances to fractions after the full path has been measured. */
|
|
589
|
+
normalizeAlong(firstEdge, pathLength) {
|
|
590
|
+
if (!this.alongStart || !this.alongEnd || pathLength === 0)
|
|
591
|
+
return;
|
|
592
|
+
for (let edge = firstEdge; edge < this.count; edge++) {
|
|
593
|
+
this.alongStart[edge] /= pathLength;
|
|
594
|
+
this.alongEnd[edge] /= pathLength;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
/** Returns the network with columns trimmed to the edge count. */
|
|
598
|
+
toNetwork(nodeCount, nodePositions) {
|
|
599
|
+
const n = this.count;
|
|
600
|
+
return {
|
|
601
|
+
nodeCount,
|
|
602
|
+
edgeCount: n,
|
|
603
|
+
nodePositions,
|
|
604
|
+
edgeSource: this.source.slice(0, n),
|
|
605
|
+
edgeTarget: this.target.slice(0, n),
|
|
606
|
+
edgeLength: this.length.slice(0, n),
|
|
607
|
+
...(this.alongStart && this.alongEnd
|
|
608
|
+
? {
|
|
609
|
+
edgePathStartFraction: this.alongStart.slice(0, n),
|
|
610
|
+
edgePathEndFraction: this.alongEnd.slice(0, n)
|
|
611
|
+
}
|
|
612
|
+
: {}),
|
|
613
|
+
edgePath: this.path.slice(0, n),
|
|
614
|
+
edgeRow: this.row.slice(0, n),
|
|
615
|
+
edgeVertexStart: this.vertexStart.slice(0, n),
|
|
616
|
+
edgeVertexEnd: this.vertexEnd.slice(0, n),
|
|
617
|
+
edgeStartFraction: this.startFraction.slice(0, n),
|
|
618
|
+
edgeEndFraction: this.endFraction.slice(0, n)
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* For each dangling end, finds the nearest point on a segment of another path. Within the
|
|
624
|
+
* tolerance it records a split there (or a union when the point is an existing node vertex).
|
|
625
|
+
*/
|
|
626
|
+
function joinDanglingEnds(lines, vertexCluster, clusterUses, isEndpointCluster, index, out) {
|
|
627
|
+
const { value: positions, size } = lines.positions;
|
|
628
|
+
const pathIndices = lines.pathIndices.value;
|
|
629
|
+
const pathCount = pathIndices.length - 1;
|
|
630
|
+
const joinedEndpoint = new Int32Array(clusterUses.length).fill(EMPTY);
|
|
631
|
+
const limitSquared = out.junctionTolerance * out.junctionTolerance;
|
|
632
|
+
for (let path = 0; path < pathCount; path++) {
|
|
633
|
+
const first = pathIndices[path];
|
|
634
|
+
const last = pathIndices[path + 1] - 1;
|
|
635
|
+
if (last <= first || vertexCluster[first] === vertexCluster[last])
|
|
636
|
+
continue;
|
|
637
|
+
for (let end = 0; end < 2; end++) {
|
|
638
|
+
const v = end === 0 ? first : last;
|
|
639
|
+
const cluster = vertexCluster[v];
|
|
640
|
+
if (!isEndpointCluster[cluster] || clusterUses[cluster] !== 1)
|
|
641
|
+
continue;
|
|
642
|
+
const lon = positions[size * v];
|
|
643
|
+
const lat = positions[size * v + 1];
|
|
644
|
+
const kx = METRES_PER_DEGREE * Math.cos(lat * DEGREES_TO_RADIANS);
|
|
645
|
+
let bestSquared = Infinity;
|
|
646
|
+
let bestSegment = EMPTY;
|
|
647
|
+
let bestFraction = 0;
|
|
648
|
+
// Search the own cell, and a neighbour cell only when the point is within the tolerance of
|
|
649
|
+
// the border to it.
|
|
650
|
+
const cell = index.cellSize;
|
|
651
|
+
const y = lat * METRES_PER_DEGREE;
|
|
652
|
+
const row = index.row(lat);
|
|
653
|
+
const yLow = y - row * cell < out.junctionTolerance ? -1 : 0;
|
|
654
|
+
const yHigh = (row + 1) * cell - y < out.junctionTolerance ? 1 : 0;
|
|
655
|
+
for (let dy = yLow; dy <= yHigh; dy++) {
|
|
656
|
+
const x = lon * index.scale(row + dy);
|
|
657
|
+
const column = Math.floor(x / cell);
|
|
658
|
+
const xLow = x - column * cell < out.junctionTolerance ? -1 : 0;
|
|
659
|
+
const xHigh = (column + 1) * cell - x < out.junctionTolerance ? 1 : 0;
|
|
660
|
+
for (let dx = xLow; dx <= xHigh; dx++) {
|
|
661
|
+
for (let e = index.head(column + dx, row + dy); e !== EMPTY; e = index.entryNext[e]) {
|
|
662
|
+
const s = index.entrySegment[e];
|
|
663
|
+
// The path of the dangling end itself is not a target.
|
|
664
|
+
if (s >= first && s < last)
|
|
665
|
+
continue;
|
|
666
|
+
const ax = (positions[size * s] - lon) * kx;
|
|
667
|
+
const ay = (positions[size * s + 1] - lat) * METRES_PER_DEGREE;
|
|
668
|
+
const bx = (positions[size * s + size] - lon) * kx;
|
|
669
|
+
const by = (positions[size * s + size + 1] - lat) * METRES_PER_DEGREE;
|
|
670
|
+
const ux = bx - ax;
|
|
671
|
+
const uy = by - ay;
|
|
672
|
+
const lengthSquared = ux * ux + uy * uy;
|
|
673
|
+
let t = lengthSquared > 0 ? -(ax * ux + ay * uy) / lengthSquared : 0;
|
|
674
|
+
t = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
675
|
+
const px = ax + t * ux;
|
|
676
|
+
const py = ay + t * uy;
|
|
677
|
+
const d2 = px * px + py * py;
|
|
678
|
+
if (d2 < bestSquared || (d2 === bestSquared && s < bestSegment)) {
|
|
679
|
+
bestSquared = d2;
|
|
680
|
+
bestSegment = s;
|
|
681
|
+
bestFraction = t;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
if (bestSegment === EMPTY || bestSquared > limitSquared)
|
|
687
|
+
continue;
|
|
688
|
+
const s = bestSegment;
|
|
689
|
+
// The nearest point is a vertex that is already a node: join that node.
|
|
690
|
+
const atVertex = bestFraction === 0 ? s : bestFraction === 1 ? s + 1 : -1;
|
|
691
|
+
const target = atVertex >= 0 ? vertexCluster[atVertex] : EMPTY;
|
|
692
|
+
if (target !== EMPTY && out.isSnappedNode(target)) {
|
|
693
|
+
// A reciprocal target has already charged this physical gap.
|
|
694
|
+
if (joinedEndpoint[target] === cluster)
|
|
695
|
+
continue;
|
|
696
|
+
joinedEndpoint[cluster] = target;
|
|
697
|
+
out.unions.push(cluster, target);
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
out.splits.push(s, bestFraction, cluster);
|
|
701
|
+
}
|
|
702
|
+
out.clusterGap[cluster] = out.distance(lon, lat, positions[size * s] + bestFraction * (positions[size * s + size] - positions[size * s]), positions[size * s + 1] +
|
|
703
|
+
bestFraction * (positions[size * s + size + 1] - positions[size * s + 1]));
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Finds proper crossings (both segment interiors, no shared snapped vertex). Each crossing makes
|
|
709
|
+
* a new cluster and a split on both segments. A pair is counted in the one cell that holds the
|
|
710
|
+
* crossing point, so a pair that shares several cells gives one crossing.
|
|
711
|
+
*/
|
|
712
|
+
function splitCrossings(lines, vertexCluster, index, snappedCount, splits, crossingPositions) {
|
|
713
|
+
const { value: positions, size } = lines.positions;
|
|
714
|
+
const { slotHeads, slotKeys, entryNext, entrySegment } = index;
|
|
715
|
+
for (let slot = 0; slot < slotHeads.length; slot++) {
|
|
716
|
+
const head = slotHeads[slot];
|
|
717
|
+
if (head === EMPTY || entryNext[head] === EMPTY)
|
|
718
|
+
continue;
|
|
719
|
+
const cellX = slotKeys[slot * 2];
|
|
720
|
+
const cellY = slotKeys[slot * 2 + 1];
|
|
721
|
+
for (let i = head; i !== EMPTY; i = entryNext[i]) {
|
|
722
|
+
const a = entrySegment[i];
|
|
723
|
+
const lonA = positions[size * a];
|
|
724
|
+
const latA = positions[size * a + 1];
|
|
725
|
+
const kx = Math.cos(latA * DEGREES_TO_RADIANS);
|
|
726
|
+
const rx = (positions[size * a + size] - lonA) * kx;
|
|
727
|
+
const ry = positions[size * a + size + 1] - latA;
|
|
728
|
+
for (let j = entryNext[i]; j !== EMPTY; j = entryNext[j]) {
|
|
729
|
+
const b = entrySegment[j];
|
|
730
|
+
if (b === a || b === a + 1 || a === b + 1)
|
|
731
|
+
continue;
|
|
732
|
+
if (sharesCluster(vertexCluster, a, b))
|
|
733
|
+
continue;
|
|
734
|
+
const qx = (positions[size * b] - lonA) * kx;
|
|
735
|
+
const qy = positions[size * b + 1] - latA;
|
|
736
|
+
const sx = (positions[size * b + size] - positions[size * b]) * kx;
|
|
737
|
+
const sy = positions[size * b + size + 1] - positions[size * b + 1];
|
|
738
|
+
const denominator = rx * sy - ry * sx;
|
|
739
|
+
if (denominator === 0)
|
|
740
|
+
continue;
|
|
741
|
+
const t = (qx * sy - qy * sx) / denominator;
|
|
742
|
+
const u = (qx * ry - qy * rx) / denominator;
|
|
743
|
+
if (!(t > 0 && t < 1 && u > 0 && u < 1))
|
|
744
|
+
continue;
|
|
745
|
+
const lon = lonA + t * (positions[size * a + size] - lonA);
|
|
746
|
+
const lat = latA + t * (positions[size * a + size + 1] - latA);
|
|
747
|
+
const row = index.row(lat);
|
|
748
|
+
if (row !== cellY || index.column(lon, row) !== cellX)
|
|
749
|
+
continue;
|
|
750
|
+
const cluster = snappedCount + crossingPositions.length / 2;
|
|
751
|
+
crossingPositions.push(lon, lat);
|
|
752
|
+
splits.push(a, t, cluster);
|
|
753
|
+
splits.push(b, u, cluster);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
/** True when segments `a` and `b` share a snapped vertex. */
|
|
759
|
+
function sharesCluster(vertexCluster, a, b) {
|
|
760
|
+
const a0 = vertexCluster[a];
|
|
761
|
+
const a1 = vertexCluster[a + 1];
|
|
762
|
+
const b0 = vertexCluster[b];
|
|
763
|
+
const b1 = vertexCluster[b + 1];
|
|
764
|
+
return (a0 !== EMPTY && (a0 === b0 || a0 === b1)) || (a1 !== EMPTY && (a1 === b0 || a1 === b1));
|
|
765
|
+
}
|
|
766
|
+
//# sourceMappingURL=build-line-network.js.map
|