@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.
- 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,213 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {visitPolylineSpan} from '@math.gl/geometry/polygon';
|
|
6
|
+
import type {LineNetwork, LineNetworkLines} from './build-line-network';
|
|
7
|
+
|
|
8
|
+
/** Predecessor of a source node or an unreachable node (the luma.gl shortest-path sentinel). */
|
|
9
|
+
export const NO_PREDECESSOR = 0xffffffff;
|
|
10
|
+
|
|
11
|
+
/** Compressed sparse row (CSR) adjacency of a line network. */
|
|
12
|
+
export type LineNetworkAdjacency = {
|
|
13
|
+
/** True when each edge is listed only from its source node. */
|
|
14
|
+
directed: boolean;
|
|
15
|
+
/** First slot of each node, followed by the slot count (`nodeCount + 1` values). */
|
|
16
|
+
offsets: Uint32Array;
|
|
17
|
+
/** Node at the other end of the edge in each slot. */
|
|
18
|
+
neighbors: Uint32Array;
|
|
19
|
+
/** Edge of each slot. */
|
|
20
|
+
edges: Uint32Array;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Options of `buildLineNetworkAdjacency`. */
|
|
24
|
+
export type LineNetworkAdjacencyOptions = {
|
|
25
|
+
/** List each edge from its source only. Default: false. */
|
|
26
|
+
directed?: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Builds a CSR adjacency. An undirected adjacency lists each edge from both end nodes; a
|
|
31
|
+
* directed adjacency lists it from its source node only. Slots of a node keep edge order.
|
|
32
|
+
*/
|
|
33
|
+
export function buildLineNetworkAdjacency(
|
|
34
|
+
network: Pick<LineNetwork, 'nodeCount' | 'edgeCount' | 'edgeSource' | 'edgeTarget'>,
|
|
35
|
+
options: LineNetworkAdjacencyOptions = {}
|
|
36
|
+
): LineNetworkAdjacency {
|
|
37
|
+
const directed = options.directed ?? false;
|
|
38
|
+
const {nodeCount, edgeCount, edgeSource, edgeTarget} = network;
|
|
39
|
+
const offsets = new Uint32Array(nodeCount + 1);
|
|
40
|
+
for (let e = 0; e < edgeCount; e++) {
|
|
41
|
+
offsets[edgeSource[e] + 1]++;
|
|
42
|
+
if (!directed) offsets[edgeTarget[e] + 1]++;
|
|
43
|
+
}
|
|
44
|
+
for (let n = 0; n < nodeCount; n++) offsets[n + 1] += offsets[n];
|
|
45
|
+
const cursor = offsets.slice(0, nodeCount);
|
|
46
|
+
const slotCount = directed ? edgeCount : edgeCount * 2;
|
|
47
|
+
const neighbors = new Uint32Array(slotCount);
|
|
48
|
+
const edges = new Uint32Array(slotCount);
|
|
49
|
+
for (let e = 0; e < edgeCount; e++) {
|
|
50
|
+
const s = edgeSource[e];
|
|
51
|
+
const t = edgeTarget[e];
|
|
52
|
+
neighbors[cursor[s]] = t;
|
|
53
|
+
edges[cursor[s]++] = e;
|
|
54
|
+
if (directed) continue;
|
|
55
|
+
neighbors[cursor[t]] = s;
|
|
56
|
+
edges[cursor[t]++] = e;
|
|
57
|
+
}
|
|
58
|
+
return {directed, offsets, neighbors, edges};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Optional predecessor conventions supplied by the solver. */
|
|
62
|
+
export type RoutePredecessorOptions = {
|
|
63
|
+
/** Sentinel for unreachable nodes. Defaults to the luma.gl sentinel. */
|
|
64
|
+
unreachable?: number;
|
|
65
|
+
/** Selected incoming edge ID per node, in the adjacency's edge numbering. */
|
|
66
|
+
predecessorEdges?: ArrayLike<number>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Walks a predecessor array from `target` back to `source`. Returns the edges in travel order,
|
|
71
|
+
* an empty array when the target is the source, or null when the target is unreachable.
|
|
72
|
+
* Between a node and its predecessor it takes the edge of lowest weight (lowest id on a tie),
|
|
73
|
+
* which is the edge that a shortest-path search relaxes.
|
|
74
|
+
*/
|
|
75
|
+
export function getRouteEdges(
|
|
76
|
+
adjacency: LineNetworkAdjacency,
|
|
77
|
+
edgeWeight: ArrayLike<number> | undefined,
|
|
78
|
+
predecessors: ArrayLike<number>,
|
|
79
|
+
source: number,
|
|
80
|
+
target: number,
|
|
81
|
+
options: RoutePredecessorOptions = {}
|
|
82
|
+
): Uint32Array | null {
|
|
83
|
+
const unreachable = options.unreachable ?? NO_PREDECESSOR;
|
|
84
|
+
if (target === source) return new Uint32Array(0);
|
|
85
|
+
if (predecessors[target] === unreachable) return null;
|
|
86
|
+
const route: number[] = [];
|
|
87
|
+
const selectedEdges = options.predecessorEdges;
|
|
88
|
+
let node = target;
|
|
89
|
+
while (node !== source) {
|
|
90
|
+
const previous = predecessors[node];
|
|
91
|
+
if (
|
|
92
|
+
!Number.isInteger(previous) ||
|
|
93
|
+
previous === unreachable ||
|
|
94
|
+
previous < 0 ||
|
|
95
|
+
previous >= predecessors.length ||
|
|
96
|
+
route.length >= predecessors.length
|
|
97
|
+
)
|
|
98
|
+
return null;
|
|
99
|
+
let best = NO_PREDECESSOR;
|
|
100
|
+
// The slots of the predecessor list its out-edges in both a directed and an undirected adjacency.
|
|
101
|
+
for (let slot = adjacency.offsets[previous]; slot < adjacency.offsets[previous + 1]; slot++) {
|
|
102
|
+
if (adjacency.neighbors[slot] !== node) continue;
|
|
103
|
+
const edge = adjacency.edges[slot];
|
|
104
|
+
if (selectedEdges) {
|
|
105
|
+
if (edge !== selectedEdges[node]) continue;
|
|
106
|
+
best = edge;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
best === NO_PREDECESSOR ||
|
|
111
|
+
(edgeWeight?.[edge] ?? 1) < (edgeWeight?.[best] ?? 1) ||
|
|
112
|
+
((edgeWeight?.[edge] ?? 1) === (edgeWeight?.[best] ?? 1) && edge < best)
|
|
113
|
+
) {
|
|
114
|
+
best = edge;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (best === NO_PREDECESSOR) return null;
|
|
118
|
+
route.push(best);
|
|
119
|
+
node = previous;
|
|
120
|
+
}
|
|
121
|
+
return Uint32Array.from(route.reverse());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Number of points in the geometry of an edge. The first and last can be split points. */
|
|
125
|
+
export function getEdgePointCount(network: LineNetwork, edge: number): number {
|
|
126
|
+
const vertices = network.edgeVertexEnd[edge] - network.edgeVertexStart[edge] + 1;
|
|
127
|
+
return Math.max(
|
|
128
|
+
1,
|
|
129
|
+
vertices -
|
|
130
|
+
Number(network.edgeStartFraction[edge] === 1) -
|
|
131
|
+
Number(network.edgeEndFraction[edge] === 0)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Writes the longitude, latitude geometry of an edge, in path direction, into `target` from
|
|
137
|
+
* value index `offset`. The first and last points follow the split fractions.
|
|
138
|
+
* @returns `target`, or a new array of `2 * getEdgePointCount(network, edge)` values.
|
|
139
|
+
*/
|
|
140
|
+
export function getEdgeGeometry(
|
|
141
|
+
network: LineNetwork,
|
|
142
|
+
lines: LineNetworkLines,
|
|
143
|
+
edge: number,
|
|
144
|
+
target: Float64Array = new Float64Array(2 * getEdgePointCount(network, edge)),
|
|
145
|
+
offset: number = 0
|
|
146
|
+
): Float64Array {
|
|
147
|
+
visitEdge(network, lines, edge, false, (x, y) => {
|
|
148
|
+
target[offset++] = x;
|
|
149
|
+
target[offset++] = y;
|
|
150
|
+
});
|
|
151
|
+
return target;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Concatenates the geometry of route edges into one flat longitude, latitude path from `source`.
|
|
156
|
+
* Each edge is reversed as needed. Where two edges meet at the same point, the point appears
|
|
157
|
+
* once. A joined dangling end gives a straight step to the split point of the next edge.
|
|
158
|
+
*/
|
|
159
|
+
export function getRouteGeometry(
|
|
160
|
+
network: LineNetwork,
|
|
161
|
+
lines: LineNetworkLines,
|
|
162
|
+
route: ArrayLike<number>,
|
|
163
|
+
source: number
|
|
164
|
+
): Float64Array {
|
|
165
|
+
let pointTotal = 0;
|
|
166
|
+
for (let i = 0; i < route.length; i++) pointTotal += getEdgePointCount(network, route[i]);
|
|
167
|
+
const path = new Float64Array(pointTotal * 2);
|
|
168
|
+
let at = 0;
|
|
169
|
+
let node = source;
|
|
170
|
+
for (let i = 0; i < route.length; i++) {
|
|
171
|
+
const e = route[i];
|
|
172
|
+
const forward = network.edgeSource[e] === node;
|
|
173
|
+
let first = true;
|
|
174
|
+
visitEdge(network, lines, e, !forward, (x, y) => {
|
|
175
|
+
if (!(first && at > 0 && x === path[at - 2] && y === path[at - 1])) {
|
|
176
|
+
path[at++] = x;
|
|
177
|
+
path[at++] = y;
|
|
178
|
+
}
|
|
179
|
+
first = false;
|
|
180
|
+
});
|
|
181
|
+
node = forward ? network.edgeTarget[e] : network.edgeSource[e];
|
|
182
|
+
}
|
|
183
|
+
return at === path.length ? path : path.slice(0, at);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Visits one edge span in travel order. */
|
|
187
|
+
function visitEdge(
|
|
188
|
+
network: LineNetwork,
|
|
189
|
+
lines: LineNetworkLines,
|
|
190
|
+
edge: number,
|
|
191
|
+
reverse: boolean,
|
|
192
|
+
visit: (x: number, y: number) => void
|
|
193
|
+
): void {
|
|
194
|
+
const {value, size} = lines.positions;
|
|
195
|
+
const startFraction = network.edgeStartFraction[edge];
|
|
196
|
+
const endFraction = network.edgeEndFraction[edge];
|
|
197
|
+
const firstSegment = network.edgeVertexStart[edge] + Number(startFraction === 1);
|
|
198
|
+
const lastSegment = network.edgeVertexEnd[edge] - 1 - Number(endFraction === 0);
|
|
199
|
+
// A split coincident with a vertex is represented on its interior-facing segment.
|
|
200
|
+
if (firstSegment > lastSegment) {
|
|
201
|
+
visit(value[size * firstSegment], value[size * firstSegment + 1]);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
visitPolylineSpan(
|
|
205
|
+
(vertex, ordinate) => value[size * vertex + ordinate],
|
|
206
|
+
firstSegment,
|
|
207
|
+
startFraction === 1 ? 0 : startFraction,
|
|
208
|
+
lastSegment,
|
|
209
|
+
endFraction === 0 ? 1 : endFraction,
|
|
210
|
+
visit,
|
|
211
|
+
reverse
|
|
212
|
+
);
|
|
213
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {LineNetwork, LineNetworkEdgeWeight} from './build-line-network';
|
|
6
|
+
|
|
7
|
+
/** Routing cost policy for a network whose topology has already been built. */
|
|
8
|
+
export type LineNetworkWeightOptions = {
|
|
9
|
+
/** Multiplier for each source path's physical edge lengths. */
|
|
10
|
+
pathWeightFactors?: ArrayLike<number>;
|
|
11
|
+
/** Computes the cost of each edge. Requires includePathFractions at build time. */
|
|
12
|
+
edgeWeight?: LineNetworkEdgeWeight;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Creates Float32 weights indexed by edge ID. Defaults to physical edge length. */
|
|
16
|
+
export function createLineNetworkWeights(
|
|
17
|
+
network: Pick<
|
|
18
|
+
LineNetwork,
|
|
19
|
+
'edgePath' | 'edgeLength' | 'edgePathStartFraction' | 'edgePathEndFraction'
|
|
20
|
+
>,
|
|
21
|
+
{pathWeightFactors, edgeWeight}: LineNetworkWeightOptions = {}
|
|
22
|
+
): Float32Array {
|
|
23
|
+
const {edgePath, edgeLength, edgePathStartFraction, edgePathEndFraction} = network;
|
|
24
|
+
if (pathWeightFactors && edgeWeight)
|
|
25
|
+
throw new Error('Specify pathWeightFactors or edgeWeight, not both');
|
|
26
|
+
if (edgeWeight && (!edgePathStartFraction || !edgePathEndFraction)) {
|
|
27
|
+
throw new Error('A custom edgeWeight requires includePathFractions');
|
|
28
|
+
}
|
|
29
|
+
const weights = new Float32Array(edgeLength.length);
|
|
30
|
+
for (let edge = 0; edge < weights.length; edge++) {
|
|
31
|
+
const path = edgePath[edge]!;
|
|
32
|
+
const length = edgeLength[edge]!;
|
|
33
|
+
const weight = edgeWeight
|
|
34
|
+
? edgeWeight(path, edgePathStartFraction![edge]!, edgePathEndFraction![edge]!, length)
|
|
35
|
+
: length * (pathWeightFactors ? pathWeightFactors[path]! : 1);
|
|
36
|
+
if (!(weight >= 0)) throw new RangeError('Edge weights must be zero or positive');
|
|
37
|
+
weights[edge] = weight;
|
|
38
|
+
}
|
|
39
|
+
return weights;
|
|
40
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export {buildLineNetwork} from './line-network/build-line-network';
|
|
6
|
+
export type {
|
|
7
|
+
BuildLineNetworkOptions,
|
|
8
|
+
LineNetwork,
|
|
9
|
+
LineNetworkDistance,
|
|
10
|
+
LineNetworkEdgeWeight,
|
|
11
|
+
LineNetworkFixedSizeList,
|
|
12
|
+
LineNetworkLines
|
|
13
|
+
} from './line-network/build-line-network';
|
|
14
|
+
export {
|
|
15
|
+
NO_PREDECESSOR,
|
|
16
|
+
buildLineNetworkAdjacency,
|
|
17
|
+
getEdgeGeometry,
|
|
18
|
+
getEdgePointCount,
|
|
19
|
+
getRouteEdges,
|
|
20
|
+
getRouteGeometry
|
|
21
|
+
} from './line-network/line-network-routes';
|
|
22
|
+
export type {
|
|
23
|
+
LineNetworkAdjacency,
|
|
24
|
+
LineNetworkAdjacencyOptions,
|
|
25
|
+
RoutePredecessorOptions
|
|
26
|
+
} from './line-network/line-network-routes';
|
|
27
|
+
|
|
28
|
+
export {createLineNetworkWeights} from './line-network/line-network-weights';
|
|
29
|
+
export type {LineNetworkWeightOptions} from './line-network/line-network-weights';
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
-
import {
|
|
6
|
+
import {projectPointToPolyline, visitPolylineSpan} from '@math.gl/geometry/polygon';
|
|
7
7
|
|
|
8
8
|
/** Ordered planar XY coordinates of a route. */
|
|
9
9
|
|
|
@@ -48,72 +48,31 @@ export function lineSubstring(
|
|
|
48
48
|
const total = distances[distances.length - 1]!;
|
|
49
49
|
const from = start * total,
|
|
50
50
|
to = end * total;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
if (from === to) return {type: 'Point', coordinates: coordinateAt(path, distances, from)};
|
|
52
|
+
const coordinates: number[][] = [];
|
|
53
|
+
let startSegment = 0;
|
|
54
|
+
while (distances[startSegment + 1]! <= from) startSegment++;
|
|
55
|
+
let endSegment = startSegment;
|
|
56
|
+
while (distances[endSegment + 1]! < to) endSegment++;
|
|
57
|
+
visitPolylineSpan(
|
|
58
|
+
(vertex, ordinate) => path[vertex]![ordinate]!,
|
|
59
|
+
startSegment,
|
|
60
|
+
(from - distances[startSegment]!) / (distances[startSegment + 1]! - distances[startSegment]!),
|
|
61
|
+
endSegment,
|
|
62
|
+
(to - distances[endSegment]!) / (distances[endSegment + 1]! - distances[endSegment]!),
|
|
63
|
+
(x, y, vertex) => {
|
|
64
|
+
if (vertex === undefined || distances[vertex] !== distances[vertex - 1])
|
|
65
|
+
coordinates.push([x, y]);
|
|
66
|
+
}
|
|
67
|
+
);
|
|
59
68
|
return {type: 'LineString', coordinates};
|
|
60
69
|
}
|
|
61
70
|
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
distance: number;
|
|
68
|
-
/** Fraction in [0, 1] along the route, or zero for a zero-length route. */
|
|
69
|
-
fraction: number;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
/** Projects a point onto the nearest route segment, taking the first candidate on ties. */
|
|
73
|
-
export function projectPointToLine(
|
|
74
|
-
line: WellKnownGeometry,
|
|
75
|
-
point: WellKnownGeometry
|
|
76
|
-
): LineProjection | null {
|
|
77
|
-
if (
|
|
78
|
-
line.type !== 'LineString' ||
|
|
79
|
-
!line.coordinates.length ||
|
|
80
|
-
point.type !== 'Point' ||
|
|
81
|
-
!Number.isFinite(point.coordinates[0]) ||
|
|
82
|
-
!Number.isFinite(point.coordinates[1])
|
|
83
|
-
)
|
|
84
|
-
return null;
|
|
85
|
-
const path = line.coordinates;
|
|
86
|
-
const x = point.coordinates[0]!;
|
|
87
|
-
const y = point.coordinates[1]!;
|
|
88
|
-
let coordinate = path[0]!;
|
|
89
|
-
let nearest = Math.hypot(x - coordinate[0]!, y - coordinate[1]!);
|
|
90
|
-
let total = 0;
|
|
91
|
-
let position = 0;
|
|
92
|
-
for (let i = 1; i < path.length; i++) {
|
|
93
|
-
const a = path[i - 1]!;
|
|
94
|
-
const b = path[i]!;
|
|
95
|
-
const dx = b[0]! - a[0]!;
|
|
96
|
-
const dy = b[1]! - a[1]!;
|
|
97
|
-
const length = Math.hypot(dx, dy);
|
|
98
|
-
if (!length) continue;
|
|
99
|
-
const ux = dx / length;
|
|
100
|
-
const uy = dy / length;
|
|
101
|
-
const along = Math.max(0, Math.min(length, (x - a[0]!) * ux + (y - a[1]!) * uy));
|
|
102
|
-
let px = along === length ? b[0]! : a[0]! + along * ux;
|
|
103
|
-
let py = along === length ? b[1]! : a[1]! + along * uy;
|
|
104
|
-
if (along > 0 && along < length && orient2d(a[0]!, a[1]!, b[0]!, b[1]!, x, y) === 0) {
|
|
105
|
-
px = x;
|
|
106
|
-
py = y;
|
|
107
|
-
}
|
|
108
|
-
const distance = Math.hypot(x - px, y - py);
|
|
109
|
-
if (distance < nearest) {
|
|
110
|
-
nearest = distance;
|
|
111
|
-
coordinate = [px, py];
|
|
112
|
-
position = total + along;
|
|
113
|
-
}
|
|
114
|
-
total += length;
|
|
115
|
-
}
|
|
116
|
-
return {coordinate, distance: nearest, fraction: total ? position / total : 0};
|
|
71
|
+
/** Projects a SQL point onto a planar route after checking geometry kinds. */
|
|
72
|
+
export function projectPointToLine(line: WellKnownGeometry, point: WellKnownGeometry) {
|
|
73
|
+
return line.type === 'LineString' && point.type === 'Point'
|
|
74
|
+
? projectPointToPolyline(line.coordinates, point.coordinates)
|
|
75
|
+
: null;
|
|
117
76
|
}
|
|
118
77
|
|
|
119
78
|
/** Returns the fraction of the route length at the projected point. */
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
6
|
import {orient2d} from 'robust-predicates';
|
|
7
|
+
import {anyBoundsOverlap, type Bounded} from '@math.gl/geometry/processing';
|
|
7
8
|
import {simplifyLine} from './line-simplification';
|
|
8
9
|
import {planarBounds} from './spatial-planar';
|
|
9
10
|
import {planarIntersects, segmentsMeet} from './spatial-planar-intersects';
|
|
@@ -14,11 +15,6 @@ type Position = readonly number[];
|
|
|
14
15
|
type Ring = readonly Position[];
|
|
15
16
|
/** One component with a shell followed by its holes. */
|
|
16
17
|
type Polygon = readonly Ring[];
|
|
17
|
-
/** Axis-aligned XY bounds. */
|
|
18
|
-
type Bounded = {
|
|
19
|
-
/** Minimum X and Y followed by maximum X and Y. */
|
|
20
|
-
bounds: readonly number[];
|
|
21
|
-
};
|
|
22
18
|
/** An indexed ring segment for the intersection sweep. */
|
|
23
19
|
type Edge = Bounded & {
|
|
24
20
|
/** First endpoint. */
|
|
@@ -31,41 +27,6 @@ type Edge = Bounded & {
|
|
|
31
27
|
index: number;
|
|
32
28
|
};
|
|
33
29
|
|
|
34
|
-
/** Estimates the active interval count relative to the extent of the sweep. */
|
|
35
|
-
function intervalDensity(items: readonly Bounded[], axis: number): number {
|
|
36
|
-
let minimum = Infinity;
|
|
37
|
-
let maximum = -Infinity;
|
|
38
|
-
let length = 0;
|
|
39
|
-
for (const {bounds} of items) {
|
|
40
|
-
const low = bounds[axis]!;
|
|
41
|
-
const high = bounds[axis + 2]!;
|
|
42
|
-
minimum = Math.min(minimum, low);
|
|
43
|
-
maximum = Math.max(maximum, high);
|
|
44
|
-
length += high - low;
|
|
45
|
-
}
|
|
46
|
-
return maximum > minimum ? length / (maximum - minimum) : Infinity;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Visits overlapping bounding boxes along the axis with fewer expected candidates. */
|
|
50
|
-
function anyOverlap<T extends Bounded>(items: T[], reject: (a: T, b: T) => boolean): boolean {
|
|
51
|
-
const axis = intervalDensity(items, 0) > intervalDensity(items, 1) ? 1 : 0;
|
|
52
|
-
const other = 1 - axis;
|
|
53
|
-
items.sort((a, b) => a.bounds[axis]! - b.bounds[axis]!);
|
|
54
|
-
for (let i = 0; i < items.length; i++) {
|
|
55
|
-
const a = items[i]!;
|
|
56
|
-
for (let j = i + 1; j < items.length && items[j]!.bounds[axis]! <= a.bounds[axis + 2]!; j++) {
|
|
57
|
-
const b = items[j]!;
|
|
58
|
-
if (
|
|
59
|
-
b.bounds[other]! <= a.bounds[other + 2]! &&
|
|
60
|
-
a.bounds[other]! <= b.bounds[other + 2]! &&
|
|
61
|
-
reject(a, b)
|
|
62
|
-
)
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
30
|
/** Compares two XY positions. */
|
|
70
31
|
function samePoint(a: Position, b: Position): boolean {
|
|
71
32
|
return a[0] === b[0] && a[1] === b[1];
|
|
@@ -113,7 +74,7 @@ function separatedBoundaries(rings: readonly Ring[]): boolean {
|
|
|
113
74
|
});
|
|
114
75
|
}
|
|
115
76
|
}
|
|
116
|
-
return !
|
|
77
|
+
return !anyBoundsOverlap(edges, (a, b) => {
|
|
117
78
|
if (a.ring === b.ring) {
|
|
118
79
|
const gap = Math.abs(a.index - b.index);
|
|
119
80
|
if (gap === 1 || gap === a.ring.length - 2) return false;
|
|
@@ -137,7 +98,7 @@ function preservesNesting(before: Ring[], after: Ring[]): boolean {
|
|
|
137
98
|
simplified: after[index]!,
|
|
138
99
|
bounds: planarBounds({type: 'LineString', coordinates: original})!
|
|
139
100
|
}));
|
|
140
|
-
return !
|
|
101
|
+
return !anyBoundsOverlap(
|
|
141
102
|
rings,
|
|
142
103
|
(a, b) =>
|
|
143
104
|
contains(a.original, b.original) !== contains(a.simplified, b.simplified) ||
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {getGeoArrowFieldInfo, type WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
+
import {
|
|
7
|
+
mergePolylinePaths,
|
|
8
|
+
segmentizePolylinePaths,
|
|
9
|
+
type PolylineSegmentFactory
|
|
10
|
+
} from '@math.gl/geometry/polygon';
|
|
11
|
+
import type {ScalarFunction} from '@math.gl/sql/arrow';
|
|
12
|
+
import {lineIntersectionResult} from './spatial-line-intersection-result';
|
|
13
|
+
import {lineKinds, nativeGeometryResult, spatialFunction} from './spatial-sql';
|
|
14
|
+
|
|
15
|
+
/** Wraps exact XY endpoint merging in a SQL line result. */
|
|
16
|
+
export function mergeRoute(geometry: WellKnownGeometry): WellKnownGeometry {
|
|
17
|
+
if (geometry.type === 'LineString') return geometry;
|
|
18
|
+
if (geometry.type !== 'MultiLineString') throw new Error('ST_LineMerge requires linear geometry');
|
|
19
|
+
const merged = mergePolylinePaths(geometry.coordinates);
|
|
20
|
+
return merged.length === 1
|
|
21
|
+
? {type: 'LineString', coordinates: merged[0]!}
|
|
22
|
+
: {type: 'MultiLineString', coordinates: merged};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Per-row bound on inserted coordinates, independent of the existing vertex count. */
|
|
26
|
+
const MAX_ADDED_VERTICES = 1_000_000;
|
|
27
|
+
|
|
28
|
+
/** Preserves the SQL line family while using the coordinate-space kernel. */
|
|
29
|
+
function segmentizeRoute(
|
|
30
|
+
geometry: WellKnownGeometry,
|
|
31
|
+
step: number,
|
|
32
|
+
name: string,
|
|
33
|
+
segmentFor: PolylineSegmentFactory | undefined
|
|
34
|
+
): WellKnownGeometry {
|
|
35
|
+
if (!Number.isFinite(step) || step <= 0)
|
|
36
|
+
throw new Error(name + ' requires a finite positive maximum segment length');
|
|
37
|
+
if (geometry.type === 'LineString') {
|
|
38
|
+
const [coordinates] = segmentizePolylinePaths(
|
|
39
|
+
[geometry.coordinates],
|
|
40
|
+
step,
|
|
41
|
+
MAX_ADDED_VERTICES,
|
|
42
|
+
segmentFor
|
|
43
|
+
);
|
|
44
|
+
return {type: 'LineString', coordinates: coordinates!};
|
|
45
|
+
}
|
|
46
|
+
if (geometry.type === 'MultiLineString') {
|
|
47
|
+
return {
|
|
48
|
+
type: 'MultiLineString',
|
|
49
|
+
coordinates: segmentizePolylinePaths(
|
|
50
|
+
geometry.coordinates,
|
|
51
|
+
step,
|
|
52
|
+
MAX_ADDED_VERTICES,
|
|
53
|
+
segmentFor
|
|
54
|
+
)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
throw new Error(name + ' requires linear geometry');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Chooses fixed native line storage where the input guarantees the result family. */
|
|
61
|
+
export function routeEditingFunction(
|
|
62
|
+
name: string,
|
|
63
|
+
preservesLineKind: boolean,
|
|
64
|
+
evaluate: (geometry: WellKnownGeometry, ...numbers: number[]) => WellKnownGeometry,
|
|
65
|
+
numericArguments = 0,
|
|
66
|
+
validateCRS?: (crs: unknown, name: string) => void
|
|
67
|
+
): ScalarFunction {
|
|
68
|
+
return {
|
|
69
|
+
kind: 'scalar',
|
|
70
|
+
name,
|
|
71
|
+
prototypes: [],
|
|
72
|
+
/** Binds a stable native output for specialized, WKB, and mixed inputs. */
|
|
73
|
+
resolvePrototype(fields) {
|
|
74
|
+
const encoding = fields[0] && getGeoArrowFieldInfo(fields[0])?.encoding;
|
|
75
|
+
const output =
|
|
76
|
+
encoding === 'geoarrow.linestring' ||
|
|
77
|
+
(preservesLineKind && encoding === 'geoarrow.multilinestring')
|
|
78
|
+
? nativeGeometryResult(encoding)
|
|
79
|
+
: lineIntersectionResult();
|
|
80
|
+
return spatialFunction(
|
|
81
|
+
name,
|
|
82
|
+
[lineKinds],
|
|
83
|
+
output,
|
|
84
|
+
([geometry], ...numbers) => evaluate(geometry!, ...numbers),
|
|
85
|
+
numericArguments,
|
|
86
|
+
false,
|
|
87
|
+
false,
|
|
88
|
+
validateCRS
|
|
89
|
+
).resolvePrototype!(fields);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Binds segmentization with a planar or ellipsoidal metric. */
|
|
95
|
+
export function segmentizeFunction(
|
|
96
|
+
name: string,
|
|
97
|
+
segmentFor?: PolylineSegmentFactory,
|
|
98
|
+
validateCRS?: (crs: unknown, name: string) => void
|
|
99
|
+
): ScalarFunction {
|
|
100
|
+
return routeEditingFunction(
|
|
101
|
+
name,
|
|
102
|
+
true,
|
|
103
|
+
(geometry, step) => segmentizeRoute(geometry, step!, name, segmentFor),
|
|
104
|
+
1,
|
|
105
|
+
validateCRS
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
endpointContacts,
|
|
8
|
+
locatePointInPolygon,
|
|
9
|
+
pointOnSegment,
|
|
10
|
+
segmentsMeet
|
|
11
|
+
} from './spatial-planar-intersects';
|
|
12
|
+
import {parameter} from './spatial-touches';
|
|
8
13
|
|
|
9
14
|
/** One XY position. */
|
|
10
15
|
type Position = readonly number[];
|
|
@@ -3,7 +3,13 @@
|
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
-
import {
|
|
6
|
+
import {locatePointInPolygon, pointOnSegment, segmentsMeet} from '@math.gl/geometry/polygon';
|
|
7
|
+
export {
|
|
8
|
+
endpointContacts,
|
|
9
|
+
locatePointInPolygon,
|
|
10
|
+
pointOnSegment,
|
|
11
|
+
segmentsMeet
|
|
12
|
+
} from '@math.gl/geometry/polygon';
|
|
7
13
|
import {planarBounds} from './spatial-planar';
|
|
8
14
|
|
|
9
15
|
/** One atomic planar geometry after collection expansion. */
|
|
@@ -33,36 +39,6 @@ function* primitives(geometry: WellKnownGeometry): Generator<Primitive> {
|
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
|
|
36
|
-
/** Reports which side of an oriented edge contains the third point. */
|
|
37
|
-
function side(a: Position, b: Position, c: Position): number {
|
|
38
|
-
return Math.sign(orient2d(a[0]!, a[1]!, b[0]!, b[1]!, c[0]!, c[1]!));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Tests inclusion in the closed bounds of an edge. */
|
|
42
|
-
function inBounds(p: Position, a: Position, b: Position): boolean {
|
|
43
|
-
return (
|
|
44
|
-
p[0]! >= Math.min(a[0]!, b[0]!) &&
|
|
45
|
-
p[0]! <= Math.max(a[0]!, b[0]!) &&
|
|
46
|
-
p[1]! >= Math.min(a[1]!, b[1]!) &&
|
|
47
|
-
p[1]! <= Math.max(a[1]!, b[1]!)
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Detects proper crossings, coincident intervals and endpoint contact. */
|
|
52
|
-
export function segmentsMeet(a: Position, b: Position, c: Position, d: Position): boolean {
|
|
53
|
-
const cSide = side(a, b, c),
|
|
54
|
-
dSide = side(a, b, d);
|
|
55
|
-
const aSide = side(c, d, a),
|
|
56
|
-
bSide = side(c, d, b);
|
|
57
|
-
return (
|
|
58
|
-
(cSide * dSide < 0 && aSide * bSide < 0) ||
|
|
59
|
-
(!cSide && inBounds(c, a, b)) ||
|
|
60
|
-
(!dSide && inBounds(d, a, b)) ||
|
|
61
|
-
(!aSide && inBounds(a, c, d)) ||
|
|
62
|
-
(!bSide && inBounds(b, c, d))
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
42
|
/** Visits path edges until a match, including an implicit ring closure. */
|
|
67
43
|
function anyEdge(
|
|
68
44
|
path: Path,
|
|
@@ -75,29 +51,6 @@ function anyEdge(
|
|
|
75
51
|
return false;
|
|
76
52
|
}
|
|
77
53
|
|
|
78
|
-
/** Tests whether a point is on a segment, including either endpoint. */
|
|
79
|
-
export function pointOnSegment(point: Position, a: Position, b: Position): boolean {
|
|
80
|
-
return inBounds(point, a, b) && side(a, b, point) === 0;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Classifies a point as outside (-1), on the boundary (0), or inside (1). */
|
|
84
|
-
export function locatePointInPolygon(point: Position, rings: readonly Path[]): -1 | 0 | 1 {
|
|
85
|
-
let inside = false;
|
|
86
|
-
for (const ring of rings) {
|
|
87
|
-
if (
|
|
88
|
-
anyEdge(ring, true, (a, b) => {
|
|
89
|
-
const orientation = side(a, b, point);
|
|
90
|
-
if (!orientation && inBounds(point, a, b)) return true;
|
|
91
|
-
if (a[1]! > point[1]! !== b[1]! > point[1]! && orientation < 0 === b[1]! > a[1]!)
|
|
92
|
-
inside = !inside;
|
|
93
|
-
return false;
|
|
94
|
-
})
|
|
95
|
-
)
|
|
96
|
-
return 0;
|
|
97
|
-
}
|
|
98
|
-
return inside ? 1 : -1;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
54
|
/** Tests a finite point against an atomic geometry, including boundaries. */
|
|
102
55
|
function meetsPoint(point: Position, geometry: Primitive): boolean {
|
|
103
56
|
if (!Number.isFinite(point[0]) || !Number.isFinite(point[1])) return false;
|