@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,146 @@
|
|
|
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 {orient2d} from 'robust-predicates';
|
|
7
|
+
import {anyBoundsOverlap, type Bounded} from '@math.gl/geometry/processing';
|
|
8
|
+
import {simplifyLine} from './line-simplification';
|
|
9
|
+
import {planarBounds} from './spatial-planar';
|
|
10
|
+
import {planarIntersects, segmentsMeet} from './spatial-planar-intersects';
|
|
11
|
+
|
|
12
|
+
/** One planar vertex. */
|
|
13
|
+
type Position = readonly number[];
|
|
14
|
+
/** Vertices of a closed ring, including its repeated endpoint. */
|
|
15
|
+
type Ring = readonly Position[];
|
|
16
|
+
/** One component with a shell followed by its holes. */
|
|
17
|
+
type Polygon = readonly Ring[];
|
|
18
|
+
/** An indexed ring segment for the intersection sweep. */
|
|
19
|
+
type Edge = Bounded & {
|
|
20
|
+
/** First endpoint. */
|
|
21
|
+
a: Position;
|
|
22
|
+
/** Second endpoint. */
|
|
23
|
+
b: Position;
|
|
24
|
+
/** Identity of the containing ring. */
|
|
25
|
+
ring: Ring;
|
|
26
|
+
/** Index of the first endpoint in that ring. */
|
|
27
|
+
index: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Compares two XY positions. */
|
|
31
|
+
function samePoint(a: Position, b: Position): boolean {
|
|
32
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Computes translated signed area for the winding and collapse check. */
|
|
36
|
+
function signedArea(ring: Ring): number {
|
|
37
|
+
const [x, y] = ring[0]!;
|
|
38
|
+
let area = 0;
|
|
39
|
+
for (let i = 1; i + 1 < ring.length; i++) {
|
|
40
|
+
const a = ring[i]!;
|
|
41
|
+
const b = ring[i + 1]!;
|
|
42
|
+
area += (a[0]! - x!) * (b[1]! - y!) - (a[1]! - y!) * (b[0]! - x!);
|
|
43
|
+
}
|
|
44
|
+
return area;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Rejects invalid, self-touching, or mutually touching boundaries conservatively. */
|
|
48
|
+
function separatedBoundaries(rings: readonly Ring[]): boolean {
|
|
49
|
+
const edges: Edge[] = [];
|
|
50
|
+
for (const ring of rings) {
|
|
51
|
+
const size = ring.length - 1;
|
|
52
|
+
if (size < 3 || !samePoint(ring[0]!, ring[size]!)) return false;
|
|
53
|
+
for (let i = 0; i < size; i++) {
|
|
54
|
+
const a = ring[i]!;
|
|
55
|
+
const b = ring[i + 1]!;
|
|
56
|
+
const c = ring[(i + 2) % size]!;
|
|
57
|
+
if (!Number.isFinite(a[0]) || !Number.isFinite(a[1]) || samePoint(a, b)) return false;
|
|
58
|
+
if (
|
|
59
|
+
orient2d(a[0]!, a[1]!, b[0]!, b[1]!, c[0]!, c[1]!) === 0 &&
|
|
60
|
+
(a[0]! - b[0]!) * (c[0]! - b[0]!) + (a[1]! - b[1]!) * (c[1]! - b[1]!) > 0
|
|
61
|
+
)
|
|
62
|
+
return false;
|
|
63
|
+
edges.push({
|
|
64
|
+
a,
|
|
65
|
+
b,
|
|
66
|
+
ring,
|
|
67
|
+
index: i,
|
|
68
|
+
bounds: [
|
|
69
|
+
Math.min(a[0]!, b[0]!),
|
|
70
|
+
Math.min(a[1]!, b[1]!),
|
|
71
|
+
Math.max(a[0]!, b[0]!),
|
|
72
|
+
Math.max(a[1]!, b[1]!)
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return !anyBoundsOverlap(edges, (a, b) => {
|
|
78
|
+
if (a.ring === b.ring) {
|
|
79
|
+
const gap = Math.abs(a.index - b.index);
|
|
80
|
+
if (gap === 1 || gap === a.ring.length - 2) return false;
|
|
81
|
+
}
|
|
82
|
+
return segmentsMeet(a.a, a.b, b.a, b.b);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Checks containment of one ring by another after ruling out boundary contact. */
|
|
87
|
+
function contains(outer: Ring, inner: Ring): boolean {
|
|
88
|
+
return planarIntersects(
|
|
89
|
+
{type: 'Polygon', coordinates: [outer]},
|
|
90
|
+
{type: 'Point', coordinates: inner[0]!}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Checks that all shell, hole, and island containment relationships remain the same. */
|
|
95
|
+
function preservesNesting(before: Ring[], after: Ring[]): boolean {
|
|
96
|
+
const rings = before.map((original, index) => ({
|
|
97
|
+
original,
|
|
98
|
+
simplified: after[index]!,
|
|
99
|
+
bounds: planarBounds({type: 'LineString', coordinates: original})!
|
|
100
|
+
}));
|
|
101
|
+
return !anyBoundsOverlap(
|
|
102
|
+
rings,
|
|
103
|
+
(a, b) =>
|
|
104
|
+
contains(a.original, b.original) !== contains(a.simplified, b.simplified) ||
|
|
105
|
+
contains(b.original, a.original) !== contains(b.simplified, a.simplified)
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Keeps the original ring if simplification would collapse it or reverse its winding. */
|
|
110
|
+
function simplifyRing(ring: Ring, tolerance: number): Ring {
|
|
111
|
+
const reduced = simplifyLine(ring, tolerance);
|
|
112
|
+
if (reduced.length < 4 || reduced.length === ring.length) return ring;
|
|
113
|
+
const before = signedArea(ring);
|
|
114
|
+
const after = signedArea(reduced);
|
|
115
|
+
return Number.isFinite(before) &&
|
|
116
|
+
Number.isFinite(after) &&
|
|
117
|
+
after &&
|
|
118
|
+
Math.sign(before) === Math.sign(after)
|
|
119
|
+
? reduced
|
|
120
|
+
: ring;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Simplifies closed rings only when boundary and nesting checks prove topology is preserved. */
|
|
124
|
+
export function simplifyPolygonTopology(
|
|
125
|
+
geometry: Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>,
|
|
126
|
+
tolerance: number
|
|
127
|
+
): typeof geometry {
|
|
128
|
+
if (!Number.isFinite(tolerance) || tolerance < 0)
|
|
129
|
+
throw new Error('Expected a finite nonnegative tolerance');
|
|
130
|
+
if (!tolerance) return geometry;
|
|
131
|
+
const polygons: readonly Polygon[] =
|
|
132
|
+
geometry.type === 'Polygon' ? [geometry.coordinates] : geometry.coordinates;
|
|
133
|
+
const before = polygons.flat();
|
|
134
|
+
const reduced = polygons.map(polygon => polygon.map(ring => simplifyRing(ring, tolerance)));
|
|
135
|
+
const after = reduced.flat();
|
|
136
|
+
if (before.every((ring, index) => ring === after[index])) return geometry;
|
|
137
|
+
if (
|
|
138
|
+
!separatedBoundaries(before) ||
|
|
139
|
+
!separatedBoundaries(after) ||
|
|
140
|
+
!preservesNesting(before, after)
|
|
141
|
+
)
|
|
142
|
+
return geometry;
|
|
143
|
+
return geometry.type === 'Polygon'
|
|
144
|
+
? {type: 'Polygon', coordinates: reduced[0]!}
|
|
145
|
+
: {type: 'MultiPolygon', coordinates: reduced};
|
|
146
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
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 {clipPolyline} from '@math.gl/geometry/polygon';
|
|
7
|
+
import polygonClipping from 'polygon-clipping';
|
|
8
|
+
import {vertexCount} from './geometry-inspection';
|
|
9
|
+
import {planarBounds} from './spatial-planar';
|
|
10
|
+
|
|
11
|
+
/** Axis-aligned rectangle in planar XY coordinates. */
|
|
12
|
+
export type Box = [number, number, number, number];
|
|
13
|
+
|
|
14
|
+
/** Tests whether a point lies in the interior of the clipping rectangle. */
|
|
15
|
+
function containsPoint(point: readonly number[], [minX, minY, maxX, maxY]: Box): boolean {
|
|
16
|
+
const [x, y] = point;
|
|
17
|
+
return x! > minX && x! < maxX && y! > minY && y! < maxY;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Clips a path without joining fragments across an excursion outside the rectangle. */
|
|
21
|
+
function clipPath(path: readonly (readonly number[])[], box: Box): number[][][] {
|
|
22
|
+
if (path.some(point => !Number.isFinite(point[0]) || !Number.isFinite(point[1]))) {
|
|
23
|
+
throw new Error('ST_ClipByBox2D requires finite line coordinates');
|
|
24
|
+
}
|
|
25
|
+
if (path.every(point => containsPoint(point, box))) return [path.map(point => [...point])];
|
|
26
|
+
const [minX, minY, maxX, maxY] = box;
|
|
27
|
+
const paths: number[][][] = [];
|
|
28
|
+
for (const part of clipPolyline(
|
|
29
|
+
path.flatMap(point => [point[0]!, point[1]!]),
|
|
30
|
+
box
|
|
31
|
+
)) {
|
|
32
|
+
let vertices: number[][] = [];
|
|
33
|
+
for (let i = 2; i < part.length; i += 2) {
|
|
34
|
+
const ax = part[i - 2]!;
|
|
35
|
+
const ay = part[i - 1]!;
|
|
36
|
+
const bx = part[i]!;
|
|
37
|
+
const by = part[i + 1]!;
|
|
38
|
+
if (ax === bx && ay === by) continue;
|
|
39
|
+
const boundary =
|
|
40
|
+
(ax === bx && (ax === minX || ax === maxX)) || (ay === by && (ay === minY || ay === maxY));
|
|
41
|
+
if (boundary) {
|
|
42
|
+
if (vertices.length) paths.push(vertices);
|
|
43
|
+
vertices = [];
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (!vertices.length) vertices.push([ax, ay]);
|
|
47
|
+
vertices.push([bx, by]);
|
|
48
|
+
}
|
|
49
|
+
if (vertices.length) paths.push(vertices);
|
|
50
|
+
}
|
|
51
|
+
return paths;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Clips polygon shells and holes together so disconnected pieces retain their topology. */
|
|
55
|
+
export function clipPolygons(
|
|
56
|
+
polygons: readonly (readonly (readonly (readonly number[])[])[])[],
|
|
57
|
+
[minX, minY, maxX, maxY]: Box
|
|
58
|
+
): polygonClipping.MultiPolygon {
|
|
59
|
+
const subject: polygonClipping.MultiPolygon = polygons
|
|
60
|
+
.filter(polygon => polygon[0]?.length)
|
|
61
|
+
.map(rings => rings.filter(ring => ring.length).map(ring => ring.map(([x, y]) => [x!, y!])));
|
|
62
|
+
if (!subject.length) return [];
|
|
63
|
+
const rectangle: polygonClipping.Polygon = [
|
|
64
|
+
[
|
|
65
|
+
[minX, minY],
|
|
66
|
+
[maxX, minY],
|
|
67
|
+
[maxX, maxY],
|
|
68
|
+
[minX, maxY],
|
|
69
|
+
[minX, minY]
|
|
70
|
+
]
|
|
71
|
+
];
|
|
72
|
+
return polygonClipping.intersection(subject, rectangle);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Individual geometry types emitted by rectangle clipping. */
|
|
76
|
+
type ClipPart = Extract<WellKnownGeometry, {type: 'Point' | 'LineString' | 'Polygon'}>;
|
|
77
|
+
|
|
78
|
+
/** Clips a geometry to atomic parts so the result can be recombined by geometry type. */
|
|
79
|
+
function clipParts(geometry: WellKnownGeometry, box: Box): ClipPart[] {
|
|
80
|
+
switch (geometry.type) {
|
|
81
|
+
case 'Point':
|
|
82
|
+
return containsPoint(geometry.coordinates, box) ? [geometry] : [];
|
|
83
|
+
case 'MultiPoint':
|
|
84
|
+
return geometry.coordinates.flatMap(coordinates =>
|
|
85
|
+
containsPoint(coordinates, box) ? [{type: 'Point', coordinates}] : []
|
|
86
|
+
);
|
|
87
|
+
case 'LineString':
|
|
88
|
+
return geometry.coordinates.length
|
|
89
|
+
? clipPath(geometry.coordinates, box).map(coordinates => ({
|
|
90
|
+
type: 'LineString',
|
|
91
|
+
coordinates
|
|
92
|
+
}))
|
|
93
|
+
: [];
|
|
94
|
+
case 'MultiLineString':
|
|
95
|
+
return geometry.coordinates.flatMap(path =>
|
|
96
|
+
path.length
|
|
97
|
+
? clipPath(path, box).map(coordinates => ({type: 'LineString', coordinates}))
|
|
98
|
+
: []
|
|
99
|
+
);
|
|
100
|
+
case 'Polygon':
|
|
101
|
+
case 'MultiPolygon': {
|
|
102
|
+
const polygons = geometry.type === 'Polygon' ? [geometry.coordinates] : geometry.coordinates;
|
|
103
|
+
return clipPolygons(polygons, box).map(coordinates => ({type: 'Polygon', coordinates}));
|
|
104
|
+
}
|
|
105
|
+
case 'GeometryCollection':
|
|
106
|
+
return geometry.geometries.flatMap(child => clipParts(child, box));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Returns a single geometry, a homogeneous multipart, or a mixed collection. */
|
|
111
|
+
function combineParts(parts: ClipPart[]): WellKnownGeometry {
|
|
112
|
+
if (parts.length === 1) return parts[0]!;
|
|
113
|
+
if (parts.length) {
|
|
114
|
+
if (parts.every(part => part.type === 'Point')) {
|
|
115
|
+
return {type: 'MultiPoint', coordinates: parts.map(part => part.coordinates)};
|
|
116
|
+
}
|
|
117
|
+
if (parts.every(part => part.type === 'LineString')) {
|
|
118
|
+
return {type: 'MultiLineString', coordinates: parts.map(part => part.coordinates)};
|
|
119
|
+
}
|
|
120
|
+
if (parts.every(part => part.type === 'Polygon')) {
|
|
121
|
+
return {type: 'MultiPolygon', coordinates: parts.map(part => part.coordinates)};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {type: 'GeometryCollection', geometries: parts};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Derives the rectangle from a bounds geometry and clips a planar feature. */
|
|
128
|
+
export function clipGeometryByBox(
|
|
129
|
+
geometry: WellKnownGeometry,
|
|
130
|
+
boundsGeometry: WellKnownGeometry
|
|
131
|
+
): WellKnownGeometry | null {
|
|
132
|
+
const bounds = planarBounds(boundsGeometry);
|
|
133
|
+
if (!bounds) return null;
|
|
134
|
+
if (!bounds.every(Number.isFinite)) throw new Error('ST_ClipByBox2D requires finite bounds');
|
|
135
|
+
if (!vertexCount(geometry)) return geometry;
|
|
136
|
+
if (bounds[0] === bounds[2] || bounds[1] === bounds[3]) {
|
|
137
|
+
throw new Error('ST_ClipByBox2D requires a rectangle with positive width and height');
|
|
138
|
+
}
|
|
139
|
+
return combineParts(clipParts(geometry, bounds as Box));
|
|
140
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
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 {BoxIndex} from '@math.gl/geometry/spatial-index';
|
|
7
|
+
import {distanceToPlanarSegment, planarBounds} from './spatial-planar';
|
|
8
|
+
import {planarIntersects, segmentsMeet} from './spatial-planar-intersects';
|
|
9
|
+
|
|
10
|
+
/** One planar coordinate. */
|
|
11
|
+
type Position = readonly number[];
|
|
12
|
+
/** One polyline component. */
|
|
13
|
+
type Path = readonly Position[];
|
|
14
|
+
/** A segment and its two-dimensional bounds. */
|
|
15
|
+
type Segment = {
|
|
16
|
+
/** First endpoint. */
|
|
17
|
+
a: Position;
|
|
18
|
+
/** Second endpoint. */
|
|
19
|
+
b: Position;
|
|
20
|
+
/** Minimum X and Y followed by maximum X and Y. */
|
|
21
|
+
bounds: readonly [number, number, number, number];
|
|
22
|
+
};
|
|
23
|
+
/** 2D geometries with linear components. */
|
|
24
|
+
type Linear = Extract<WellKnownGeometry, {type: 'LineString' | 'MultiLineString'}>;
|
|
25
|
+
/** 2D geometries with polygonal components. */
|
|
26
|
+
type Areal = Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>;
|
|
27
|
+
/** A single point geometry. */
|
|
28
|
+
type Point = Extract<WellKnownGeometry, {type: 'Point'}>;
|
|
29
|
+
|
|
30
|
+
/** Candidate-pair cutoff above which a spatial index can avoid a full scan. */
|
|
31
|
+
const directPairLimit = 1024;
|
|
32
|
+
|
|
33
|
+
/** Tests a point against the filled area, hole boundaries, and exterior boundaries. */
|
|
34
|
+
export function pointWithinPolygonDistance(point: Point, polygon: Areal, radius: number): boolean {
|
|
35
|
+
const [x, y] = point.coordinates;
|
|
36
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) return false;
|
|
37
|
+
const bounds = planarBounds(polygon);
|
|
38
|
+
if (!bounds || !boxesWithin([x!, y!, x!, y!], bounds, radius)) return false;
|
|
39
|
+
if (planarIntersects(point, polygon)) return true;
|
|
40
|
+
if (radius === 0) return false;
|
|
41
|
+
const areas = polygon.type === 'Polygon' ? [polygon.coordinates] : polygon.coordinates;
|
|
42
|
+
for (const area of areas) {
|
|
43
|
+
for (const ring of area) {
|
|
44
|
+
for (let i = 0; i < ring.length; i++) {
|
|
45
|
+
const a = ring[i === 0 ? ring.length - 1 : i - 1]!;
|
|
46
|
+
const b = ring[i]!;
|
|
47
|
+
if (distanceToPlanarSegment(point.coordinates, a, b) <= radius) return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Keeps multiline components separate, including degenerate point-like segments. */
|
|
55
|
+
function lineSegments(geometry: Linear): Segment[] {
|
|
56
|
+
const paths: readonly Path[] =
|
|
57
|
+
geometry.type === 'LineString' ? [geometry.coordinates] : geometry.coordinates;
|
|
58
|
+
const segments: Segment[] = [];
|
|
59
|
+
for (const path of paths) {
|
|
60
|
+
if (path.some(point => !Number.isFinite(point[0]) || !Number.isFinite(point[1])))
|
|
61
|
+
throw new Error('ST_DWithin requires finite line coordinates');
|
|
62
|
+
if (!path.length) continue;
|
|
63
|
+
for (let i = 0; i < Math.max(1, path.length - 1); i++) {
|
|
64
|
+
const a = path[i]!;
|
|
65
|
+
const b = path[i + 1] ?? a;
|
|
66
|
+
segments.push({
|
|
67
|
+
a,
|
|
68
|
+
b,
|
|
69
|
+
bounds: [
|
|
70
|
+
Math.min(a[0]!, b[0]!),
|
|
71
|
+
Math.min(a[1]!, b[1]!),
|
|
72
|
+
Math.max(a[0]!, b[0]!),
|
|
73
|
+
Math.max(a[1]!, b[1]!)
|
|
74
|
+
]
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return segments;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Uses the largest axis gap as a conservative lower bound on Euclidean distance. */
|
|
82
|
+
function boxesWithin(a: readonly number[], b: readonly number[], radius: number): boolean {
|
|
83
|
+
return Math.max(a[0]! - b[2]!, b[0]! - a[2]!, a[1]! - b[3]!, b[1]! - a[3]!) <= radius;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Intersecting segments have zero gap; otherwise a closest pair includes an endpoint. */
|
|
87
|
+
function segmentsWithin(left: Segment, right: Segment, radius: number): boolean {
|
|
88
|
+
if (!boxesWithin(left.bounds, right.bounds, radius)) return false;
|
|
89
|
+
const {a, b} = left;
|
|
90
|
+
const {a: c, b: d} = right;
|
|
91
|
+
return (
|
|
92
|
+
segmentsMeet(a, b, c, d) ||
|
|
93
|
+
(radius > 0 &&
|
|
94
|
+
(distanceToPlanarSegment(a, c, d) <= radius ||
|
|
95
|
+
distanceToPlanarSegment(b, c, d) <= radius ||
|
|
96
|
+
distanceToPlanarSegment(c, a, b) <= radius ||
|
|
97
|
+
distanceToPlanarSegment(d, a, b) <= radius))
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Builds finite search bounds with enough padding to round outward. */
|
|
102
|
+
function expandBound(value: number, radius: number, direction: -1 | 1): number {
|
|
103
|
+
const padding = Math.max(Math.abs(value), radius) * Number.EPSILON;
|
|
104
|
+
return Math.max(
|
|
105
|
+
-Number.MAX_VALUE,
|
|
106
|
+
Math.min(Number.MAX_VALUE, value + direction * (radius + padding))
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Tests line pairs by exact segment refinement after rejecting distant bounds. */
|
|
111
|
+
export function linesWithinDistance(left: Linear, right: Linear, radius: number): boolean {
|
|
112
|
+
const leftBounds = planarBounds(left);
|
|
113
|
+
const rightBounds = planarBounds(right);
|
|
114
|
+
if (!leftBounds || !rightBounds || !boxesWithin(leftBounds, rightBounds, radius)) return false;
|
|
115
|
+
const a = lineSegments(left);
|
|
116
|
+
const b = lineSegments(right);
|
|
117
|
+
if (!a.length || !b.length) return false;
|
|
118
|
+
if (radius === Infinity) return true;
|
|
119
|
+
if (a.length * b.length <= directPairLimit || Math.min(a.length, b.length) < 8) {
|
|
120
|
+
return a.some(segment => b.some(candidate => segmentsWithin(segment, candidate, radius)));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const [indexed, queries] = a.length <= b.length ? [a, b] : [b, a];
|
|
124
|
+
const index = new BoxIndex({dimension: 2, bounds: indexed.flatMap(segment => segment.bounds)});
|
|
125
|
+
for (const segment of queries) {
|
|
126
|
+
const [x0, y0, x1, y1] = segment.bounds;
|
|
127
|
+
const {result} = index.searchQuery(
|
|
128
|
+
[expandBound(x0, radius, -1), expandBound(y0, radius, -1)],
|
|
129
|
+
[expandBound(x1, radius, 1), expandBound(y1, radius, 1)],
|
|
130
|
+
{filter: candidate => segmentsWithin(segment, indexed[candidate]!, radius), maxResults: 1}
|
|
131
|
+
);
|
|
132
|
+
if (result.length) return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|