@math.gl/geospatial 5.0.0 → 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/line-simplification.d.ts +7 -0
- package/dist/line-simplification.d.ts.map +1 -1
- package/dist/line-simplification.js +4 -13
- package/dist/line-simplification.js.map +1 -1
- package/dist/linear-referencing.d.ts +3 -1
- package/dist/linear-referencing.d.ts.map +1 -1
- package/dist/linear-referencing.js +21 -33
- package/dist/linear-referencing.js.map +1 -1
- package/dist/measure-referencing.d.ts +16 -0
- package/dist/measure-referencing.d.ts.map +1 -0
- package/dist/measure-referencing.js +143 -0
- package/dist/measure-referencing.js.map +1 -0
- package/dist/polygon-simplification.d.ts +6 -0
- package/dist/polygon-simplification.d.ts.map +1 -0
- package/dist/polygon-simplification.js +111 -0
- package/dist/polygon-simplification.js.map +1 -0
- 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-box-clipping.d.ts +9 -0
- package/dist/spatial-box-clipping.d.ts.map +1 -0
- package/dist/spatial-box-clipping.js +123 -0
- package/dist/spatial-box-clipping.js.map +1 -0
- package/dist/spatial-dwithin.d.ts +19 -0
- package/dist/spatial-dwithin.d.ts.map +1 -0
- package/dist/spatial-dwithin.js +107 -0
- package/dist/spatial-dwithin.js.map +1 -0
- package/dist/spatial-hulls.d.ts +6 -0
- package/dist/spatial-hulls.d.ts.map +1 -0
- package/dist/spatial-hulls.js +176 -0
- package/dist/spatial-hulls.js.map +1 -0
- package/dist/spatial-line-intersection-result.d.ts +4 -0
- package/dist/spatial-line-intersection-result.d.ts.map +1 -0
- package/dist/spatial-line-intersection-result.js +79 -0
- package/dist/spatial-line-intersection-result.js.map +1 -0
- package/dist/spatial-line-polygon-intersection.d.ts +4 -0
- package/dist/spatial-line-polygon-intersection.d.ts.map +1 -0
- package/dist/spatial-line-polygon-intersection.js +130 -0
- package/dist/spatial-line-polygon-intersection.js.map +1 -0
- package/dist/spatial-planar-intersects.d.ts +1 -0
- package/dist/spatial-planar-intersects.d.ts.map +1 -1
- package/dist/spatial-planar-intersects.js +5 -37
- package/dist/spatial-planar-intersects.js.map +1 -1
- package/dist/spatial-planar.d.ts +9 -0
- package/dist/spatial-planar.d.ts.map +1 -1
- package/dist/spatial-planar.js +13 -1
- package/dist/spatial-planar.js.map +1 -1
- package/dist/spatial-polygon-overlay.d.ts +5 -0
- package/dist/spatial-polygon-overlay.d.ts.map +1 -0
- package/dist/spatial-polygon-overlay.js +23 -0
- package/dist/spatial-polygon-overlay.js.map +1 -0
- package/dist/spatial-sql.d.ts +15 -2
- package/dist/spatial-sql.d.ts.map +1 -1
- package/dist/spatial-sql.js +65 -13
- package/dist/spatial-sql.js.map +1 -1
- package/dist/spatial-subdivision.d.ts +9 -0
- package/dist/spatial-subdivision.d.ts.map +1 -0
- package/dist/spatial-subdivision.js +61 -0
- package/dist/spatial-subdivision.js.map +1 -0
- package/dist/spatial-touches.d.ts +11 -0
- package/dist/spatial-touches.d.ts.map +1 -0
- package/dist/spatial-touches.js +178 -0
- package/dist/spatial-touches.js.map +1 -0
- 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 +3279 -554
- package/dist/st-functions.cjs.map +3 -3
- package/dist/st-functions.d.ts +100 -2
- package/dist/st-functions.d.ts.map +1 -1
- package/dist/st-functions.js +239 -22
- package/dist/st-functions.js.map +1 -1
- package/dist/st-geodesic-functions.d.ts +41 -0
- package/dist/st-geodesic-functions.d.ts.map +1 -0
- package/dist/st-geodesic-functions.js +238 -0
- package/dist/st-geodesic-functions.js.map +1 -0
- package/dist/st-hull-functions.cjs +386 -0
- package/dist/st-hull-functions.cjs.map +6 -0
- package/dist/st-hull-functions.d.ts +17 -0
- package/dist/st-hull-functions.d.ts.map +1 -0
- package/dist/st-hull-functions.js +14 -0
- package/dist/st-hull-functions.js.map +1 -0
- package/dist/st-overlay-functions.cjs +668 -0
- package/dist/st-overlay-functions.cjs.map +6 -0
- package/dist/st-overlay-functions.d.ts +46 -0
- package/dist/st-overlay-functions.d.ts.map +1 -0
- package/dist/st-overlay-functions.js +248 -0
- package/dist/st-overlay-functions.js.map +1 -0
- package/dist/st-viewport-functions.cjs +481 -0
- package/dist/st-viewport-functions.cjs.map +6 -0
- package/dist/st-viewport-functions.d.ts +13 -0
- package/dist/st-viewport-functions.d.ts.map +1 -0
- package/dist/st-viewport-functions.js +81 -0
- package/dist/st-viewport-functions.js.map +1 -0
- package/dist/terrain/index.cjs +93 -20
- package/dist/terrain/index.cjs.map +3 -3
- package/dist/terrain/index.d.ts +1 -0
- package/dist/terrain/index.d.ts.map +1 -1
- package/dist/terrain/index.js +1 -0
- package/dist/terrain/index.js.map +1 -1
- package/dist/terrain/regional-watersheds.d.ts +44 -0
- package/dist/terrain/regional-watersheds.d.ts.map +1 -0
- package/dist/terrain/regional-watersheds.js +108 -0
- package/dist/terrain/regional-watersheds.js.map +1 -0
- package/dist/terrain/tile-cutout-update.d.ts.map +1 -1
- package/dist/terrain/tile-cutout-update.js +28 -22
- package/dist/terrain/tile-cutout-update.js.map +1 -1
- package/dist/turf-functions.cjs +16 -9
- package/dist/turf-functions.cjs.map +1 -1
- package/package.json +27 -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/line-simplification.ts +4 -16
- package/src/linear-referencing.ts +27 -40
- package/src/measure-referencing.ts +165 -0
- package/src/polygon-simplification.ts +146 -0
- package/src/route-editing.ts +107 -0
- package/src/spatial-box-clipping.ts +140 -0
- package/src/spatial-dwithin.ts +135 -0
- package/src/spatial-hulls.ts +203 -0
- package/src/spatial-line-intersection-result.ts +93 -0
- package/src/spatial-line-polygon-intersection.ts +152 -0
- package/src/spatial-planar-intersects.ts +10 -51
- package/src/spatial-planar.ts +20 -1
- package/src/spatial-polygon-overlay.ts +30 -0
- package/src/spatial-sql.ts +97 -13
- package/src/spatial-subdivision.ts +75 -0
- package/src/spatial-touches.ts +204 -0
- package/src/st-coverage-functions.ts +88 -0
- package/src/st-functions.ts +345 -26
- package/src/st-geodesic-functions.ts +313 -0
- package/src/st-hull-functions.ts +25 -0
- package/src/st-overlay-functions.ts +313 -0
- package/src/st-viewport-functions.ts +98 -0
- package/src/terrain/index.ts +7 -0
- package/src/terrain/regional-watersheds.ts +153 -0
- package/src/terrain/tile-cutout-update.ts +32 -26
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
+
import triangulate from 'cdt2d';
|
|
7
|
+
import {orient2d} from 'robust-predicates';
|
|
8
|
+
import {visitPaths} from './spatial-planar';
|
|
9
|
+
|
|
10
|
+
/** Finite planar coordinate used by the triangulator. */
|
|
11
|
+
type Site = [number, number];
|
|
12
|
+
|
|
13
|
+
/** Collects distinct XY sites in lexicographic order. */
|
|
14
|
+
function collectSites(geometry: WellKnownGeometry): Site[] {
|
|
15
|
+
const sites: Site[] = [];
|
|
16
|
+
visitPaths(geometry, path => {
|
|
17
|
+
for (const [x, y] of path) {
|
|
18
|
+
if (Number.isFinite(x) && Number.isFinite(y)) sites.push([x!, y!]);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
sites.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
22
|
+
return sites.filter((p, i) => !i || p[0] !== sites[i - 1]![0] || p[1] !== sites[i - 1]![1]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Builds one monotone chain, dropping intermediate collinear points. */
|
|
26
|
+
function buildChain(sites: readonly Site[]): Site[] {
|
|
27
|
+
const chain: Site[] = [];
|
|
28
|
+
for (const point of sites) {
|
|
29
|
+
while (chain.length > 1) {
|
|
30
|
+
const a = chain[chain.length - 2]!;
|
|
31
|
+
const b = chain[chain.length - 1]!;
|
|
32
|
+
// robust-predicates returns a negative value for a Cartesian counterclockwise turn.
|
|
33
|
+
if (orient2d(...a, ...b, ...point) < 0) break;
|
|
34
|
+
chain.pop();
|
|
35
|
+
}
|
|
36
|
+
chain.push(point);
|
|
37
|
+
}
|
|
38
|
+
return chain;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Wraps sorted distinct sites, retaining degenerate point and line results. */
|
|
42
|
+
function hullFromSites(sites: Site[]): WellKnownGeometry {
|
|
43
|
+
if (!sites.length) return {type: 'GeometryCollection', geometries: []};
|
|
44
|
+
if (sites.length === 1) return {type: 'Point', coordinates: sites[0]!};
|
|
45
|
+
const lower = buildChain(sites);
|
|
46
|
+
const upper = buildChain([...sites].reverse());
|
|
47
|
+
const ring = [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
48
|
+
return ring.length === 2
|
|
49
|
+
? {type: 'LineString', coordinates: ring}
|
|
50
|
+
: {type: 'Polygon', coordinates: [[...ring, ring[0]!]]};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Computes the smallest convex geometry enclosing the input vertices. */
|
|
54
|
+
export function convexHull(geometry: WellKnownGeometry): WellKnownGeometry {
|
|
55
|
+
return hullFromSites(collectSites(geometry));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Directed triangle edge and its adjacent triangle, if present. */
|
|
59
|
+
type Edge = {
|
|
60
|
+
/** Start site index in counterclockwise ring order. */
|
|
61
|
+
start: number;
|
|
62
|
+
/** End site index in counterclockwise ring order. */
|
|
63
|
+
end: number;
|
|
64
|
+
/** Triangle on the other side of the edge. */
|
|
65
|
+
neighbor?: Triangle;
|
|
66
|
+
/** Euclidean edge length in coordinate units. */
|
|
67
|
+
length: number;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** A Delaunay triangle with mutable membership in the retained mesh. */
|
|
71
|
+
type Triangle = {
|
|
72
|
+
/** Three directed edges in counterclockwise order. */
|
|
73
|
+
edges: Edge[];
|
|
74
|
+
/** Whether the triangle has been eroded. */
|
|
75
|
+
removed: boolean;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** Candidate for removing a boundary triangle across its current exterior edge. */
|
|
79
|
+
type Candidate = {
|
|
80
|
+
/** Triangle considered for erosion. */
|
|
81
|
+
triangle: Triangle;
|
|
82
|
+
/** Exterior edge replaced by the other two sides. */
|
|
83
|
+
edge: Edge;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Pushes a candidate into a longest-edge-first binary heap. */
|
|
87
|
+
function pushCandidate(heap: Candidate[], candidate: Candidate): void {
|
|
88
|
+
let index = heap.length;
|
|
89
|
+
heap.push(candidate);
|
|
90
|
+
while (index > 0) {
|
|
91
|
+
const parent = (index - 1) >>> 1;
|
|
92
|
+
if (heap[parent]!.edge.length >= candidate.edge.length) break;
|
|
93
|
+
heap[index] = heap[parent]!;
|
|
94
|
+
index = parent;
|
|
95
|
+
}
|
|
96
|
+
heap[index] = candidate;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Removes the longest-edge candidate and restores heap order. */
|
|
100
|
+
function popCandidate(heap: Candidate[]): Candidate {
|
|
101
|
+
const first = heap[0]!;
|
|
102
|
+
const last = heap.pop()!;
|
|
103
|
+
if (!heap.length) return first;
|
|
104
|
+
let index = 0;
|
|
105
|
+
while (index * 2 + 1 < heap.length) {
|
|
106
|
+
const left = index * 2 + 1;
|
|
107
|
+
const right = left + 1;
|
|
108
|
+
const child =
|
|
109
|
+
right < heap.length && heap[right]!.edge.length > heap[left]!.edge.length ? right : left;
|
|
110
|
+
if (heap[child]!.edge.length <= last.edge.length) break;
|
|
111
|
+
heap[index] = heap[child]!;
|
|
112
|
+
index = child;
|
|
113
|
+
}
|
|
114
|
+
heap[index] = last;
|
|
115
|
+
return first;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Connects adjacent triangles and records the Delaunay edge range. */
|
|
119
|
+
function triangulateSites(sites: Site[]) {
|
|
120
|
+
const triangles: Triangle[] = [];
|
|
121
|
+
const edgeOwners = new Map<string, {edge: Edge; triangle: Triangle}>();
|
|
122
|
+
let minimum = Infinity;
|
|
123
|
+
let maximum = 0;
|
|
124
|
+
for (const cell of triangulate(sites, [])) {
|
|
125
|
+
const [a, b, c] = cell;
|
|
126
|
+
const order = orient2d(...sites[a]!, ...sites[b]!, ...sites[c]!) < 0 ? [a, b, c] : [a, c, b];
|
|
127
|
+
const triangle: Triangle = {edges: [], removed: false};
|
|
128
|
+
for (let i = 0; i < 3; i++) {
|
|
129
|
+
const start = order[i]!;
|
|
130
|
+
const end = order[(i + 1) % 3]!;
|
|
131
|
+
const p = sites[start]!;
|
|
132
|
+
const q = sites[end]!;
|
|
133
|
+
const length = Math.hypot(p[0] - q[0], p[1] - q[1]);
|
|
134
|
+
const edge: Edge = {start, end, length};
|
|
135
|
+
const key = start < end ? start + ':' + end : end + ':' + start;
|
|
136
|
+
const owner = edgeOwners.get(key);
|
|
137
|
+
if (owner) {
|
|
138
|
+
owner.edge.neighbor = triangle;
|
|
139
|
+
edge.neighbor = owner.triangle;
|
|
140
|
+
} else {
|
|
141
|
+
edgeOwners.set(key, {edge, triangle});
|
|
142
|
+
minimum = Math.min(minimum, length);
|
|
143
|
+
maximum = Math.max(maximum, length);
|
|
144
|
+
}
|
|
145
|
+
triangle.edges.push(edge);
|
|
146
|
+
}
|
|
147
|
+
triangles.push(triangle);
|
|
148
|
+
}
|
|
149
|
+
return {triangles, minimum, maximum};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Returns the edges with no retained triangle on the other side. */
|
|
153
|
+
function exteriorEdges(triangle: Triangle): Edge[] {
|
|
154
|
+
return triangle.edges.filter(edge => !edge.neighbor || edge.neighbor.removed);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Erodes the exterior without removing a site or introducing a ring pinch. */
|
|
158
|
+
export function concaveHull(geometry: WellKnownGeometry, ratio: number): WellKnownGeometry {
|
|
159
|
+
if (!(ratio >= 0 && ratio <= 1)) throw new Error('ST_ConcaveHull ratio must be between 0 and 1');
|
|
160
|
+
const sites = collectSites(geometry);
|
|
161
|
+
const convex = hullFromSites(sites);
|
|
162
|
+
if (ratio === 1 || convex.type !== 'Polygon') return convex;
|
|
163
|
+
const {triangles, minimum, maximum} = triangulateSites(sites);
|
|
164
|
+
const limit = (1 - ratio) * minimum + ratio * maximum;
|
|
165
|
+
const boundary = new Map<number, number>();
|
|
166
|
+
const heap: Candidate[] = [];
|
|
167
|
+
for (const triangle of triangles) {
|
|
168
|
+
for (const edge of exteriorEdges(triangle)) {
|
|
169
|
+
boundary.set(edge.start, edge.end);
|
|
170
|
+
if (edge.length > limit) pushCandidate(heap, {triangle, edge});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
while (heap.length) {
|
|
174
|
+
const {triangle, edge} = popCandidate(heap);
|
|
175
|
+
if (triangle.removed || exteriorEdges(triangle).length !== 1) continue;
|
|
176
|
+
const first = triangle.edges.find(side => side.start === edge.end)!;
|
|
177
|
+
const tip = first.end;
|
|
178
|
+
if (boundary.has(tip)) continue;
|
|
179
|
+
// Replace start → end with start → tip → end. An interior tip keeps one simple ring.
|
|
180
|
+
triangle.removed = true;
|
|
181
|
+
boundary.set(edge.start, tip);
|
|
182
|
+
boundary.set(tip, edge.end);
|
|
183
|
+
for (const side of triangle.edges) {
|
|
184
|
+
const neighbor = side.neighbor;
|
|
185
|
+
if (!neighbor || neighbor.removed || side.length <= limit) continue;
|
|
186
|
+
const reverse = neighbor.edges.find(candidate => candidate.neighbor === triangle)!;
|
|
187
|
+
pushCandidate(heap, {triangle: neighbor, edge: reverse});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const start = boundary.keys().next().value;
|
|
191
|
+
if (start === undefined) return convex;
|
|
192
|
+
const coordinates = [sites[start]!];
|
|
193
|
+
let site = boundary.get(start)!;
|
|
194
|
+
while (site !== start && coordinates.length < boundary.size) {
|
|
195
|
+
coordinates.push(sites[site]!);
|
|
196
|
+
site = boundary.get(site)!;
|
|
197
|
+
}
|
|
198
|
+
if (site !== start || coordinates.length !== boundary.size) {
|
|
199
|
+
throw new Error('ST_ConcaveHull could not construct a single outer ring');
|
|
200
|
+
}
|
|
201
|
+
coordinates.push(sites[start]!);
|
|
202
|
+
return {type: 'Polygon', coordinates: [coordinates]};
|
|
203
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
GeoArrowBuilder,
|
|
7
|
+
makeArrowVectorFromGeoArrowColumn,
|
|
8
|
+
type GeoArrowDenseUnion,
|
|
9
|
+
type GeoArrowList,
|
|
10
|
+
type WellKnownGeometry
|
|
11
|
+
} from '@math.gl/core/arrow';
|
|
12
|
+
import type {NativeSpatialResult} from './spatial-sql';
|
|
13
|
+
|
|
14
|
+
/** Concrete result children and their canonical XY selectors. */
|
|
15
|
+
const concrete = [
|
|
16
|
+
{type: 'Point', encoding: 'geoarrow.point', id: 1},
|
|
17
|
+
{type: 'LineString', encoding: 'geoarrow.linestring', id: 2},
|
|
18
|
+
{type: 'MultiPoint', encoding: 'geoarrow.multipoint', id: 4},
|
|
19
|
+
{type: 'MultiLineString', encoding: 'geoarrow.multilinestring', id: 5}
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
/** Fixed native mixed-geometry layout shared by every batch. */
|
|
23
|
+
export function lineIntersectionResult(): NativeSpatialResult {
|
|
24
|
+
/** Builds the result without serializing coordinates or dispatching on per-batch schema. */
|
|
25
|
+
const build: NativeSpatialResult['build'] = rows =>
|
|
26
|
+
makeArrowVectorFromGeoArrowColumn({
|
|
27
|
+
encoding: 'geoarrow.geometry',
|
|
28
|
+
dimension: 'xy',
|
|
29
|
+
coordinateLayout: 'interleaved',
|
|
30
|
+
chunks: [union(rows, true)]
|
|
31
|
+
});
|
|
32
|
+
return {encoding: 'geoarrow.geometry', type: build([]).type, build};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Builds concrete children and, at the top level, one collection child. */
|
|
36
|
+
function union(
|
|
37
|
+
rows: readonly (WellKnownGeometry | null)[],
|
|
38
|
+
collections: boolean
|
|
39
|
+
): GeoArrowDenseUnion {
|
|
40
|
+
const groups = concrete.map((): (WellKnownGeometry | null)[] => []);
|
|
41
|
+
const mixed: WellKnownGeometry[] = [];
|
|
42
|
+
const typeIds = new Int8Array(rows.length);
|
|
43
|
+
const valueOffsets = new Int32Array(rows.length);
|
|
44
|
+
for (let row = 0; row < rows.length; row++) {
|
|
45
|
+
const geometry = rows[row] ?? null;
|
|
46
|
+
if (collections && geometry?.type === 'GeometryCollection') {
|
|
47
|
+
typeIds[row] = 7;
|
|
48
|
+
valueOffsets[row] = mixed.length;
|
|
49
|
+
mixed.push(geometry);
|
|
50
|
+
} else {
|
|
51
|
+
const index = geometry ? concrete.findIndex(child => child.type === geometry.type) : 0;
|
|
52
|
+
const group = groups[index];
|
|
53
|
+
if (!group) throw new Error('Unexpected line intersection result geometry');
|
|
54
|
+
typeIds[row] = concrete[index]!.id;
|
|
55
|
+
valueOffsets[row] = group.length;
|
|
56
|
+
group.push(geometry);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const children: GeoArrowDenseUnion['children'][number][] = concrete.map(
|
|
60
|
+
({type, encoding, id}, index) => ({
|
|
61
|
+
name: type,
|
|
62
|
+
encoding,
|
|
63
|
+
typeId: id,
|
|
64
|
+
dimension: 'xy',
|
|
65
|
+
coordinateLayout: 'interleaved',
|
|
66
|
+
data: GeoArrowBuilder.build(groups[index]!, {encoding, dimension: 'xy'}).chunks[0]!
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
if (collections) {
|
|
70
|
+
children.push({
|
|
71
|
+
name: 'GeometryCollection',
|
|
72
|
+
encoding: 'geoarrow.geometrycollection',
|
|
73
|
+
typeId: 7,
|
|
74
|
+
dimension: 'xy',
|
|
75
|
+
coordinateLayout: 'interleaved',
|
|
76
|
+
data: collection(mixed)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return {kind: 'dense-union', length: rows.length, typeIds, valueOffsets, children};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Packs collection children in a fixed inner union. */
|
|
83
|
+
function collection(rows: readonly WellKnownGeometry[]): GeoArrowList {
|
|
84
|
+
const members: WellKnownGeometry[] = [];
|
|
85
|
+
const offsets = new Int32Array(rows.length + 1);
|
|
86
|
+
for (let row = 0; row < rows.length; row++) {
|
|
87
|
+
const geometry = rows[row]!;
|
|
88
|
+
if (geometry.type === 'GeometryCollection')
|
|
89
|
+
for (const child of geometry.geometries) members.push(child);
|
|
90
|
+
offsets[row + 1] = members.length;
|
|
91
|
+
}
|
|
92
|
+
return {kind: 'list', length: rows.length, offsets, child: union(members, false)};
|
|
93
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
+
import {
|
|
7
|
+
endpointContacts,
|
|
8
|
+
locatePointInPolygon,
|
|
9
|
+
pointOnSegment,
|
|
10
|
+
segmentsMeet
|
|
11
|
+
} from './spatial-planar-intersects';
|
|
12
|
+
import {parameter} from './spatial-touches';
|
|
13
|
+
|
|
14
|
+
/** One XY position. */
|
|
15
|
+
type Position = readonly number[];
|
|
16
|
+
/** One line or polygon ring. */
|
|
17
|
+
type Path = readonly Position[];
|
|
18
|
+
/** Endpoints of one segment. */
|
|
19
|
+
type Edge = readonly [Position, Position];
|
|
20
|
+
|
|
21
|
+
/** Tests equality of XY positions. */
|
|
22
|
+
function same(a: Position, b: Position): boolean {
|
|
23
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Interpolates between endpoints without perturbing original vertices. */
|
|
27
|
+
function interpolate(a: Position, b: Position, t: number): Position {
|
|
28
|
+
if (t === 0) return a;
|
|
29
|
+
if (t === 1) return b;
|
|
30
|
+
return [(1 - t) * a[0]! + t * b[0]!, (1 - t) * a[1]! + t * b[1]!];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Returns the position of a proper segment crossing along the first segment. */
|
|
34
|
+
function crossingParameter([a, b]: Edge, [c, d]: Edge): number {
|
|
35
|
+
const dx = b[0]! - a[0]!;
|
|
36
|
+
const dy = b[1]! - a[1]!;
|
|
37
|
+
const ux = d[0]! - c[0]!;
|
|
38
|
+
const uy = d[1]! - c[1]!;
|
|
39
|
+
const x = c[0]! - a[0]!;
|
|
40
|
+
const y = c[1]! - a[1]!;
|
|
41
|
+
const scale = Math.max(
|
|
42
|
+
Math.abs(dx),
|
|
43
|
+
Math.abs(dy),
|
|
44
|
+
Math.abs(ux),
|
|
45
|
+
Math.abs(uy),
|
|
46
|
+
Math.abs(x),
|
|
47
|
+
Math.abs(y)
|
|
48
|
+
);
|
|
49
|
+
return (
|
|
50
|
+
((x / scale) * (uy / scale) - (y / scale) * (ux / scale)) /
|
|
51
|
+
((dx / scale) * (uy / scale) - (dy / scale) * (ux / scale))
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Clips linear components to polygon interiors and boundaries, retaining isolated contacts. */
|
|
56
|
+
export function intersectLinePolygon(
|
|
57
|
+
line: WellKnownGeometry,
|
|
58
|
+
area: WellKnownGeometry
|
|
59
|
+
): WellKnownGeometry {
|
|
60
|
+
if (line.type !== 'LineString' && line.type !== 'MultiLineString')
|
|
61
|
+
throw new Error('Line–polygon intersection requires a linear geometry');
|
|
62
|
+
if (area.type !== 'Polygon' && area.type !== 'MultiPolygon')
|
|
63
|
+
throw new Error('Line–polygon intersection requires a polygonal geometry');
|
|
64
|
+
const polygons = area.type === 'Polygon' ? [area.coordinates] : area.coordinates;
|
|
65
|
+
const boundaries: Edge[] = [];
|
|
66
|
+
for (const ring of polygons.flat()) {
|
|
67
|
+
for (let i = 0; i < ring.length; i++) {
|
|
68
|
+
const a = ring[(i + ring.length - 1) % ring.length]!;
|
|
69
|
+
const b = ring[i]!;
|
|
70
|
+
if (!same(a, b)) boundaries.push([a, b]);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Tests membership in the closed areal geometry. */
|
|
75
|
+
const covered = (point: Position): boolean =>
|
|
76
|
+
polygons.some(rings => locatePointInPolygon(point, rings) >= 0);
|
|
77
|
+
const lines: Position[][] = [];
|
|
78
|
+
const contacts = new Map<string, Position>();
|
|
79
|
+
/** Deduplicates point contacts across adjacent segments and components. */
|
|
80
|
+
const addPoint = (point: Position) => contacts.set(`${point[0]}/${point[1]}`, point);
|
|
81
|
+
for (const path of line.type === 'LineString' ? [line.coordinates] : line.coordinates) {
|
|
82
|
+
if (!path.length) continue;
|
|
83
|
+
let current: Position[] | undefined;
|
|
84
|
+
if (covered(path[0]!)) addPoint(path[0]!);
|
|
85
|
+
for (let index = 1; index < path.length; index++) {
|
|
86
|
+
const a = path[index - 1]!;
|
|
87
|
+
const b = path[index]!;
|
|
88
|
+
if (same(a, b)) continue;
|
|
89
|
+
const segment: Edge = [a, b];
|
|
90
|
+
const cuts = new Map<number, Position>([
|
|
91
|
+
[0, a],
|
|
92
|
+
[1, b]
|
|
93
|
+
]);
|
|
94
|
+
const overlap: [number, number][] = [];
|
|
95
|
+
for (const boundary of boundaries) {
|
|
96
|
+
if (!segmentsMeet(...segment, ...boundary)) continue;
|
|
97
|
+
const endpoints = endpointContacts(segment, boundary);
|
|
98
|
+
if (!endpoints.length) {
|
|
99
|
+
const t = crossingParameter(segment, boundary);
|
|
100
|
+
if (!(t >= 0 && t <= 1)) throw new Error('Unable to locate polygon boundary crossing');
|
|
101
|
+
const [x, y] = interpolate(a, b, t);
|
|
102
|
+
const [c, d] = boundary;
|
|
103
|
+
cuts.set(t, [c[0] === d[0] ? c[0]! : x!, c[1] === d[1] ? c[1]! : y!]);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const parameters = endpoints.map(point => parameter(point, segment));
|
|
107
|
+
for (let contact = 0; contact < endpoints.length; contact++) {
|
|
108
|
+
cuts.set(parameters[contact]!, endpoints[contact]!);
|
|
109
|
+
addPoint(endpoints[contact]!);
|
|
110
|
+
}
|
|
111
|
+
if (parameters.length === 2)
|
|
112
|
+
overlap.push([Math.min(...parameters), Math.max(...parameters)]);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const entries = [...cuts].sort(([x], [y]) => x - y);
|
|
116
|
+
for (let i = 1; i < entries.length; i++) {
|
|
117
|
+
const [from, first] = entries[i - 1]!;
|
|
118
|
+
const [to, last] = entries[i]!;
|
|
119
|
+
if (
|
|
120
|
+
!overlap.some(([start, end]) => start <= from && to <= end) &&
|
|
121
|
+
!covered(interpolate(a, b, (from + to) / 2))
|
|
122
|
+
) {
|
|
123
|
+
current = undefined;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (same(first, last)) continue;
|
|
127
|
+
if (current && same(current[current.length - 1]!, first)) current.push(last);
|
|
128
|
+
else {
|
|
129
|
+
current = [first, last];
|
|
130
|
+
lines.push(current);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const retainedEdges = [...lineEdges(lines)];
|
|
136
|
+
const points = [...contacts.values()].filter(
|
|
137
|
+
point => !retainedEdges.some(([a, b]) => pointOnSegment(point, a, b))
|
|
138
|
+
);
|
|
139
|
+
const parts: WellKnownGeometry[] = [];
|
|
140
|
+
if (lines.length === 1) parts.push({type: 'LineString', coordinates: lines[0]!});
|
|
141
|
+
else if (lines.length) parts.push({type: 'MultiLineString', coordinates: lines});
|
|
142
|
+
if (points.length === 1) parts.push({type: 'Point', coordinates: points[0]!});
|
|
143
|
+
else if (points.length) parts.push({type: 'MultiPoint', coordinates: points});
|
|
144
|
+
return parts.length === 1 ? parts[0]! : {type: 'GeometryCollection', geometries: parts};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Iterates segments of already clipped linear components. */
|
|
148
|
+
function* lineEdges(paths: readonly Path[]): Generator<Edge> {
|
|
149
|
+
for (const path of paths) {
|
|
150
|
+
for (let i = 1; i < path.length; i++) yield [path[i - 1]!, path[i]!];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -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
|
-
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,31 +51,14 @@ function anyEdge(
|
|
|
75
51
|
return false;
|
|
76
52
|
}
|
|
77
53
|
|
|
78
|
-
/**
|
|
54
|
+
/** Tests a finite point against an atomic geometry, including boundaries. */
|
|
79
55
|
function meetsPoint(point: Position, geometry: Primitive): boolean {
|
|
80
56
|
if (!Number.isFinite(point[0]) || !Number.isFinite(point[1])) return false;
|
|
81
57
|
if (geometry.type === 'Point')
|
|
82
58
|
return point[0] === geometry.coordinates[0] && point[1] === geometry.coordinates[1];
|
|
83
59
|
if (geometry.type === 'LineString')
|
|
84
|
-
return anyEdge(
|
|
85
|
-
|
|
86
|
-
false,
|
|
87
|
-
(a, b) => inBounds(point, a, b) && !side(a, b, point)
|
|
88
|
-
);
|
|
89
|
-
let inside = false;
|
|
90
|
-
for (const ring of geometry.coordinates) {
|
|
91
|
-
if (
|
|
92
|
-
anyEdge(ring, true, (a, b) => {
|
|
93
|
-
const orientation = side(a, b, point);
|
|
94
|
-
if (!orientation && inBounds(point, a, b)) return true;
|
|
95
|
-
if (a[1]! > point[1]! !== b[1]! > point[1]! && orientation < 0 === b[1]! > a[1]!)
|
|
96
|
-
inside = !inside;
|
|
97
|
-
return false;
|
|
98
|
-
})
|
|
99
|
-
)
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
return inside;
|
|
60
|
+
return anyEdge(geometry.coordinates, false, (a, b) => pointOnSegment(point, a, b));
|
|
61
|
+
return locatePointInPolygon(point, geometry.coordinates) >= 0;
|
|
103
62
|
}
|
|
104
63
|
|
|
105
64
|
/** Tests boundary intersection and full containment of individual components. */
|
package/src/spatial-planar.ts
CHANGED
|
@@ -4,6 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
6
|
|
|
7
|
+
/** Measures the distance to a finite segment, including its endpoints. */
|
|
8
|
+
export function distanceToPlanarSegment(
|
|
9
|
+
p: readonly number[],
|
|
10
|
+
a: readonly number[],
|
|
11
|
+
b: readonly number[]
|
|
12
|
+
): number {
|
|
13
|
+
const dx = b[0]! - a[0]!,
|
|
14
|
+
dy = b[1]! - a[1]!;
|
|
15
|
+
const length = Math.hypot(dx, dy);
|
|
16
|
+
const px = p[0]! - a[0]!,
|
|
17
|
+
py = p[1]! - a[1]!;
|
|
18
|
+
const t = length
|
|
19
|
+
? Math.max(0, Math.min(1, (px * (dx / length) + py * (dy / length)) / length))
|
|
20
|
+
: 0;
|
|
21
|
+
return Math.hypot(px - t * dx, py - t * dy);
|
|
22
|
+
}
|
|
23
|
+
|
|
7
24
|
/** A position in the input coordinate system. */
|
|
8
25
|
type Position = readonly number[];
|
|
9
26
|
/** The positions of an open or closed path. */
|
|
@@ -36,7 +53,7 @@ function isPresentPoint(position: Position): boolean {
|
|
|
36
53
|
}
|
|
37
54
|
|
|
38
55
|
/** Iterates position sequences in a supported geometry, retaining path boundaries. */
|
|
39
|
-
function visitPaths(geometry: WellKnownGeometry, visit: (path: Path) => void): void {
|
|
56
|
+
export function visitPaths(geometry: WellKnownGeometry, visit: (path: Path) => void): void {
|
|
40
57
|
switch (geometry.type) {
|
|
41
58
|
case 'Point':
|
|
42
59
|
if (isPresentPoint(geometry.coordinates)) visit([geometry.coordinates]);
|
|
@@ -107,6 +124,8 @@ export function planarArea(geometry: WellKnownGeometry): number {
|
|
|
107
124
|
|
|
108
125
|
/** Computes planar line length in coordinate units. */
|
|
109
126
|
export function planarLength(geometry: WellKnownGeometry): number {
|
|
127
|
+
if (geometry.type === 'GeometryCollection')
|
|
128
|
+
return geometry.geometries.reduce((sum, part) => sum + planarLength(part), 0);
|
|
110
129
|
if (geometry.type !== 'LineString' && geometry.type !== 'MultiLineString') return 0;
|
|
111
130
|
let length = 0;
|
|
112
131
|
visitPaths(geometry, path => {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {WellKnownGeometry} from '@math.gl/core/arrow';
|
|
6
|
+
import type {MultiPolygon} from 'polygon-clipping';
|
|
7
|
+
|
|
8
|
+
/** Converts native polygon rings to the existing polygon overlay kernel's input. */
|
|
9
|
+
export function polygonSet(geometry: WellKnownGeometry): MultiPolygon {
|
|
10
|
+
const polygons =
|
|
11
|
+
geometry.type === 'Polygon'
|
|
12
|
+
? [geometry.coordinates]
|
|
13
|
+
: geometry.type === 'MultiPolygon'
|
|
14
|
+
? geometry.coordinates
|
|
15
|
+
: undefined;
|
|
16
|
+
if (!polygons) throw new Error('Polygon overlay requires Polygon or MultiPolygon input');
|
|
17
|
+
return polygons
|
|
18
|
+
.filter(polygon => polygon[0]?.length)
|
|
19
|
+
.map(polygon =>
|
|
20
|
+
polygon
|
|
21
|
+
.filter(ring => ring.length)
|
|
22
|
+
.map(ring =>
|
|
23
|
+
ring.map(([x, y]) => {
|
|
24
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
25
|
+
throw new Error('Polygon overlay requires finite coordinates');
|
|
26
|
+
return [x!, y!];
|
|
27
|
+
})
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
}
|