@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
package/src/spatial-sql.ts
CHANGED
|
@@ -7,16 +7,20 @@ import {
|
|
|
7
7
|
DataType,
|
|
8
8
|
Field,
|
|
9
9
|
Float64,
|
|
10
|
+
FixedSizeList,
|
|
11
|
+
makeData,
|
|
12
|
+
makeVector,
|
|
10
13
|
vectorFromArray,
|
|
11
14
|
type Bool,
|
|
12
|
-
type FixedSizeList,
|
|
13
15
|
type Int32,
|
|
14
16
|
type Utf8,
|
|
15
17
|
type Vector
|
|
16
18
|
} from 'apache-arrow';
|
|
17
19
|
import type {ScalarFunction} from '@math.gl/sql/arrow';
|
|
18
20
|
import {
|
|
21
|
+
GeoArrowBuilder,
|
|
19
22
|
getGeoArrowFieldInfo,
|
|
23
|
+
makeArrowVectorFromGeoArrowColumn,
|
|
20
24
|
isGeoArrowValueValid,
|
|
21
25
|
makeGeoArrowColumnFromArrowVector,
|
|
22
26
|
makeGeoArrowColumnFromGeometryRows,
|
|
@@ -53,6 +57,74 @@ export const polygonKinds = ['Polygon', 'MultiPolygon'] as const;
|
|
|
53
57
|
|
|
54
58
|
/** Supported Arrow result storage for a spatial function. */
|
|
55
59
|
type SpatialResult = Int32 | Float64 | Bool | Binary | FixedSizeList<Float64> | Utf8;
|
|
60
|
+
/** Native geometry result storage and its row-aligned Arrow vector builder. */
|
|
61
|
+
export type NativeSpatialResult = {
|
|
62
|
+
/** Extension encoding declared on the bound result field. */
|
|
63
|
+
encoding:
|
|
64
|
+
| 'geoarrow.point'
|
|
65
|
+
| 'geoarrow.linestring'
|
|
66
|
+
| 'geoarrow.multilinestring'
|
|
67
|
+
| 'geoarrow.polygon'
|
|
68
|
+
| 'geoarrow.multipolygon'
|
|
69
|
+
| 'geoarrow.geometry';
|
|
70
|
+
/** Fixed physical Arrow schema for all batches. */
|
|
71
|
+
type: DataType;
|
|
72
|
+
/** Packs decoded geometry rows into native coordinate buffers. */
|
|
73
|
+
build: (rows: readonly (WellKnownGeometry | null)[]) => Vector;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Wraps already-packed point coordinates and row validity without copying either buffer. */
|
|
77
|
+
export function nativePointVector(
|
|
78
|
+
type: FixedSizeList<Float64>,
|
|
79
|
+
data: Float64Array,
|
|
80
|
+
nullBitmap: Uint8Array,
|
|
81
|
+
nullCount: number
|
|
82
|
+
): Vector {
|
|
83
|
+
const child = makeData({type: type.valueType, data, length: data.length});
|
|
84
|
+
return makeVector(
|
|
85
|
+
makeData({type, length: data.length / type.listSize, nullCount, nullBitmap, child})
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Declares interleaved XY point storage and packs each output row once. */
|
|
90
|
+
function nativeXYPointResult(): NativeSpatialResult {
|
|
91
|
+
const type = new FixedSizeList(2, new Field('xy', new Float64(), true));
|
|
92
|
+
return {
|
|
93
|
+
encoding: 'geoarrow.point',
|
|
94
|
+
type,
|
|
95
|
+
/** Writes empty points as NaN coordinates and uses a bitmap for null rows. */
|
|
96
|
+
build(rows) {
|
|
97
|
+
const values = new Float64Array(rows.length * 2);
|
|
98
|
+
const validity = new Uint8Array(Math.ceil(rows.length / 8));
|
|
99
|
+
let nullCount = 0;
|
|
100
|
+
for (let row = 0; row < rows.length; row++) {
|
|
101
|
+
const point = rows[row];
|
|
102
|
+
if (!point) {
|
|
103
|
+
nullCount++;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (point.type !== 'Point') throw new Error('Expected a Point result');
|
|
107
|
+
validity[row >> 3]! |= 1 << (row & 7);
|
|
108
|
+
values[2 * row] = point.coordinates[0] ?? NaN;
|
|
109
|
+
values[2 * row + 1] = point.coordinates[1] ?? NaN;
|
|
110
|
+
}
|
|
111
|
+
return nativePointVector(type, values, validity, nullCount);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Declares a fixed native interleaved XY layout, including for empty or all-null batches. */
|
|
117
|
+
export function nativeGeometryResult(
|
|
118
|
+
encoding: Exclude<NativeSpatialResult['encoding'], 'geoarrow.geometry'>
|
|
119
|
+
): NativeSpatialResult {
|
|
120
|
+
if (encoding === 'geoarrow.point') return nativeXYPointResult();
|
|
121
|
+
const options = {encoding, dimension: 'xy', coordinateLayout: 'interleaved'} as const;
|
|
122
|
+
/** Packs the output into native coordinate buffers. */
|
|
123
|
+
const build: NativeSpatialResult['build'] = rows =>
|
|
124
|
+
makeArrowVectorFromGeoArrowColumn(GeoArrowBuilder.build(rows, options));
|
|
125
|
+
return {encoding, type: build([]).type, build};
|
|
126
|
+
}
|
|
127
|
+
|
|
56
128
|
/** A nullable result value passed to the Arrow vector builder. */
|
|
57
129
|
type SpatialValue = string | number | boolean | number[] | Uint8Array | null;
|
|
58
130
|
|
|
@@ -135,15 +207,17 @@ export function planarGeometry(
|
|
|
135
207
|
}
|
|
136
208
|
|
|
137
209
|
/** Declares a row-preserving spatial overload using the input columns' GeoArrow types. */
|
|
138
|
-
export function spatialFunction(
|
|
210
|
+
export function spatialFunction<Value extends SpatialValue | WellKnownGeometry>(
|
|
139
211
|
name: string,
|
|
140
212
|
signatures: readonly (readonly GeometryKind[])[],
|
|
141
|
-
|
|
142
|
-
evaluate: (geometries: readonly GeometryRow[], ...numbers: number[]) =>
|
|
213
|
+
output: SpatialResult | NativeSpatialResult,
|
|
214
|
+
evaluate: (geometries: readonly GeometryRow[], ...numbers: number[]) => Value,
|
|
143
215
|
numericArguments = 0,
|
|
144
216
|
allDimensions = false,
|
|
145
|
-
skipNullGeometries = false
|
|
217
|
+
skipNullGeometries = false,
|
|
218
|
+
validateCRS?: (crs: unknown, name: string) => void
|
|
146
219
|
): ScalarFunction {
|
|
220
|
+
const type = 'encoding' in output ? output.type : output;
|
|
147
221
|
return {
|
|
148
222
|
kind: 'scalar',
|
|
149
223
|
name,
|
|
@@ -167,13 +241,17 @@ export function spatialFunction(
|
|
|
167
241
|
(DataType.isBinary(field.type) || DataType.isLargeBinary(field.type))
|
|
168
242
|
) {
|
|
169
243
|
// WKB reveals its geometry kind and dimensionality per row.
|
|
244
|
+
} else if (encoding === 'geoarrow.geometry' && extension.dimension === 'xy') {
|
|
245
|
+
// Native XY union rows are checked against the input signature during evaluation.
|
|
170
246
|
} else if (kinds.some(kind => encoding === 'geoarrow.' + kind.toLowerCase())) {
|
|
171
247
|
if (!allDimensions && extension?.dimension !== 'xy')
|
|
172
248
|
throw new Error(`${name} requires 2D geometry`);
|
|
173
249
|
} else {
|
|
174
250
|
throw new Error(`${name} argument ${index + 1} requires GeoArrow ${kinds.join(' or ')}`);
|
|
175
251
|
}
|
|
252
|
+
validateCRS?.(extension?.metadata['crs'], name);
|
|
176
253
|
if (
|
|
254
|
+
!validateCRS &&
|
|
177
255
|
name.startsWith('ST_') &&
|
|
178
256
|
name !== 'ST_SRID' &&
|
|
179
257
|
name !== 'ST_SetSRID' &&
|
|
@@ -186,18 +264,22 @@ export function spatialFunction(
|
|
|
186
264
|
.map(extension => extension?.metadata['crs'])
|
|
187
265
|
.filter(crs => crs !== undefined && crs !== null);
|
|
188
266
|
if (
|
|
267
|
+
!validateCRS &&
|
|
189
268
|
name.startsWith('ST_') &&
|
|
190
269
|
crsValues.some(crs => JSON.stringify(crs) !== JSON.stringify(crsValues[0]))
|
|
191
270
|
) {
|
|
192
271
|
throw new Error(`${name} requires arguments in the same CRS`);
|
|
193
272
|
}
|
|
194
273
|
const metadata = new Map<string, string>();
|
|
195
|
-
if (DataType.isBinary(type)) {
|
|
196
|
-
metadata.set(EXTENSION_NAME, 'geoarrow.wkb');
|
|
197
|
-
const inputMetadata =
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
274
|
+
if ('encoding' in output || DataType.isBinary(type)) {
|
|
275
|
+
metadata.set(EXTENSION_NAME, 'encoding' in output ? output.encoding : 'geoarrow.wkb');
|
|
276
|
+
const inputMetadata = extensions[0]?.metadata;
|
|
277
|
+
const crs = inputMetadata?.['crs'];
|
|
278
|
+
const crsType = crs === undefined ? undefined : inputMetadata?.['crs_type'];
|
|
279
|
+
const edges = inputMetadata?.['edges'];
|
|
280
|
+
if (crs !== undefined || edges !== undefined) {
|
|
281
|
+
metadata.set(EXTENSION_METADATA, JSON.stringify({crs, crs_type: crsType, edges}));
|
|
282
|
+
}
|
|
201
283
|
}
|
|
202
284
|
const arguments_ = [
|
|
203
285
|
...geometryFields.map(field => field ?? new Field('geometry', new Binary(), true)),
|
|
@@ -223,7 +305,7 @@ export function spatialFunction(
|
|
|
223
305
|
name === 'ST_GeometryN'
|
|
224
306
|
)
|
|
225
307
|
);
|
|
226
|
-
const values = new Array<
|
|
308
|
+
const values = new Array<Value | null>(rowCount).fill(null);
|
|
227
309
|
for (let row = 0; row < rowCount; row++) {
|
|
228
310
|
if (!activeRows[row]) continue;
|
|
229
311
|
const geometries: WellKnownGeometry[] = [];
|
|
@@ -238,7 +320,9 @@ export function spatialFunction(
|
|
|
238
320
|
}
|
|
239
321
|
values[row] = evaluate(geometries, ...numbers.map(vector => Number(vector.get(row))));
|
|
240
322
|
}
|
|
241
|
-
return
|
|
323
|
+
return 'encoding' in output
|
|
324
|
+
? output.build(values as (WellKnownGeometry | null)[])
|
|
325
|
+
: vectorFromArray(values as SpatialValue[], output);
|
|
242
326
|
}
|
|
243
327
|
};
|
|
244
328
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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 {vertexCount} from './geometry-inspection';
|
|
7
|
+
import {clipPolygons, type Box} from './spatial-box-clipping';
|
|
8
|
+
import {planarBounds} from './spatial-planar';
|
|
9
|
+
|
|
10
|
+
/** Default vertex ceiling for ST_Subdivide, including polygon closures. */
|
|
11
|
+
const MAX_VERTICES = 256;
|
|
12
|
+
|
|
13
|
+
/** Maximum bisection depth before reporting an unrepresentable split. */
|
|
14
|
+
const MAX_DEPTH = 64;
|
|
15
|
+
|
|
16
|
+
/** Individual polygon produced by rectilinear subdivision. */
|
|
17
|
+
type Polygon = Extract<WellKnownGeometry, {type: 'Polygon'}>;
|
|
18
|
+
|
|
19
|
+
/** Splits a rectangle at the midpoint of its wider axis. */
|
|
20
|
+
function splitBox(box: Box): [Box, Box] {
|
|
21
|
+
const [minX, minY, maxX, maxY] = box;
|
|
22
|
+
if (maxX - minX >= maxY - minY) {
|
|
23
|
+
const mid = minX / 2 + maxX / 2;
|
|
24
|
+
if (mid > minX && mid < maxX)
|
|
25
|
+
return [
|
|
26
|
+
[minX, minY, mid, maxY],
|
|
27
|
+
[mid, minY, maxX, maxY]
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
const mid = minY / 2 + maxY / 2;
|
|
31
|
+
if (mid > minY && mid < maxY)
|
|
32
|
+
return [
|
|
33
|
+
[minX, minY, maxX, mid],
|
|
34
|
+
[minX, mid, maxX, maxY]
|
|
35
|
+
];
|
|
36
|
+
throw new Error('ST_Subdivide cannot bisect the polygon at the available coordinate precision');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Emits bounded polygon parts with rectilinear cuts, retaining holes through each split. */
|
|
40
|
+
export function* subdividePolygons(
|
|
41
|
+
geometry: WellKnownGeometry,
|
|
42
|
+
signal?: AbortSignal
|
|
43
|
+
): Generator<Polygon> {
|
|
44
|
+
if (geometry.type !== 'Polygon' && geometry.type !== 'MultiPolygon') {
|
|
45
|
+
throw new Error('ST_Subdivide requires a Polygon or MultiPolygon');
|
|
46
|
+
}
|
|
47
|
+
const polygons = geometry.type === 'Polygon' ? [geometry.coordinates] : geometry.coordinates;
|
|
48
|
+
for (const coordinates of polygons) {
|
|
49
|
+
yield* subdividePolygon({type: 'Polygon', coordinates}, 0, signal);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Recursively subdivides a single polygon until each piece fits the vertex budget. */
|
|
54
|
+
function* subdividePolygon(
|
|
55
|
+
polygon: Polygon,
|
|
56
|
+
depth: number,
|
|
57
|
+
signal?: AbortSignal
|
|
58
|
+
): Generator<Polygon> {
|
|
59
|
+
signal?.throwIfAborted();
|
|
60
|
+
const count = vertexCount(polygon);
|
|
61
|
+
if (!count) return;
|
|
62
|
+
if (count <= MAX_VERTICES) {
|
|
63
|
+
yield polygon;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (depth >= MAX_DEPTH) throw new Error('ST_Subdivide could not reach the vertex limit');
|
|
67
|
+
const bounds = planarBounds(polygon);
|
|
68
|
+
if (!bounds?.every(Number.isFinite))
|
|
69
|
+
throw new Error('ST_Subdivide requires finite polygon bounds');
|
|
70
|
+
for (const box of splitBox(bounds as Box)) {
|
|
71
|
+
for (const coordinates of clipPolygons([polygon.coordinates], box)) {
|
|
72
|
+
yield* subdividePolygon({type: 'Polygon', coordinates}, depth + 1, signal);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
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 polygonClipping from 'polygon-clipping';
|
|
7
|
+
import {
|
|
8
|
+
endpointContacts,
|
|
9
|
+
locatePointInPolygon,
|
|
10
|
+
planarIntersects,
|
|
11
|
+
pointOnSegment,
|
|
12
|
+
segmentsMeet
|
|
13
|
+
} from './spatial-planar-intersects';
|
|
14
|
+
import {polygonSet} from './spatial-polygon-overlay';
|
|
15
|
+
|
|
16
|
+
/** XY position and ordered path. */
|
|
17
|
+
type Position = readonly number[];
|
|
18
|
+
/** Ordered vertices of a line component or ring. */
|
|
19
|
+
type Path = readonly Position[];
|
|
20
|
+
/** Endpoints of one planar segment. */
|
|
21
|
+
type Edge = readonly [Position, Position];
|
|
22
|
+
/** Point and multipoint geometry. */
|
|
23
|
+
type Points = Extract<WellKnownGeometry, {type: 'Point' | 'MultiPoint'}>;
|
|
24
|
+
/** Line and multiline geometry. */
|
|
25
|
+
type Lines = Extract<WellKnownGeometry, {type: 'LineString' | 'MultiLineString'}>;
|
|
26
|
+
/** Polygon and multipolygon geometry. */
|
|
27
|
+
type Area = Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>;
|
|
28
|
+
|
|
29
|
+
/** Narrows the geometry types containing only points. */
|
|
30
|
+
function isPointLike(geometry: WellKnownGeometry): geometry is Points {
|
|
31
|
+
return geometry.type === 'Point' || geometry.type === 'MultiPoint';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Narrows line and multiline geometries. */
|
|
35
|
+
function isLinear(geometry: WellKnownGeometry): geometry is Lines {
|
|
36
|
+
return geometry.type === 'LineString' || geometry.type === 'MultiLineString';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Narrows polygon and multipolygon geometries. */
|
|
40
|
+
function isAreal(geometry: WellKnownGeometry): geometry is Area {
|
|
41
|
+
return geometry.type === 'Polygon' || geometry.type === 'MultiPolygon';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Returns independent line components. */
|
|
45
|
+
function paths(geometry: Lines): readonly Path[] {
|
|
46
|
+
return geometry.type === 'LineString' ? [geometry.coordinates] : geometry.coordinates;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Returns independent areal components with their interior rings. */
|
|
50
|
+
function polygons(geometry: Area): readonly (readonly Path[])[] {
|
|
51
|
+
return geometry.type === 'Polygon' ? [geometry.coordinates] : geometry.coordinates;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Compares planar coordinates without imposing a distance tolerance. */
|
|
55
|
+
function samePoint(a: Position, b: Position): boolean {
|
|
56
|
+
return a[0] === b[0] && a[1] === b[1];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Keys an XY location, including equivalent positive and negative zero. */
|
|
60
|
+
function pointKey(point: Position): string {
|
|
61
|
+
return `${point[0]}/${point[1]}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Iterates nonzero segments and implicitly closes rings. */
|
|
65
|
+
function* edges(components: readonly Path[], closed = false): Generator<Edge> {
|
|
66
|
+
for (const path of components) {
|
|
67
|
+
for (let i = closed ? 0 : 1; i < path.length; i++) {
|
|
68
|
+
const a = path[i ? i - 1 : path.length - 1]!;
|
|
69
|
+
const b = path[i]!;
|
|
70
|
+
if (!samePoint(a, b)) yield [a, b];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Computes the line boundary using odd endpoint incidence across all components. */
|
|
76
|
+
function lineBoundary(components: readonly Path[]): ReadonlyMap<string, Position> {
|
|
77
|
+
const boundary = new Map<string, Position>();
|
|
78
|
+
for (const component of components) {
|
|
79
|
+
if (!component.length) continue;
|
|
80
|
+
for (const point of [component[0]!, component[component.length - 1]!]) {
|
|
81
|
+
const key = pointKey(point);
|
|
82
|
+
if (!boundary.delete(key)) boundary.set(key, point);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return boundary;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Tests whether any odd endpoint is also on both segments of a proper crossing. */
|
|
89
|
+
function hasBoundaryCrossing(boundary: ReadonlyMap<string, Position>, a: Edge, b: Edge): boolean {
|
|
90
|
+
for (const point of boundary.values()) {
|
|
91
|
+
if (pointOnSegment(point, ...a) && pointOnSegment(point, ...b)) return true;
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Detects line interior overlap, accounting for the whole multiline boundary. */
|
|
97
|
+
function lineInteriorsMeet(a: Lines, b: Lines): boolean {
|
|
98
|
+
const aPaths = paths(a);
|
|
99
|
+
const bPaths = paths(b);
|
|
100
|
+
const aBoundary = lineBoundary(aPaths);
|
|
101
|
+
const bBoundary = lineBoundary(bPaths);
|
|
102
|
+
const bEdges = [...edges(bPaths)];
|
|
103
|
+
for (const left of edges(aPaths)) {
|
|
104
|
+
for (const right of bEdges) {
|
|
105
|
+
if (!segmentsMeet(...left, ...right)) continue;
|
|
106
|
+
const contacts = endpointContacts(left, right);
|
|
107
|
+
if (contacts.length > 1) return true;
|
|
108
|
+
if (contacts.length) {
|
|
109
|
+
const key = pointKey(contacts[0]!);
|
|
110
|
+
if (!aBoundary.has(key) && !bBoundary.has(key)) return true;
|
|
111
|
+
} else if (
|
|
112
|
+
!hasBoundaryCrossing(aBoundary, left, right) &&
|
|
113
|
+
!hasBoundaryCrossing(bBoundary, left, right)
|
|
114
|
+
) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Tests a point against the interior of any areal component. */
|
|
123
|
+
function insideArea(point: Position, areas: readonly (readonly Path[])[]): boolean {
|
|
124
|
+
return areas.some(rings => locatePointInPolygon(point, rings) === 1);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Finds a segment parameter on its axis with the least relative rounding error. */
|
|
128
|
+
export function parameter(point: Position, [a, b]: Edge): number {
|
|
129
|
+
const dx = b[0]! - a[0]!;
|
|
130
|
+
const dy = b[1]! - a[1]!;
|
|
131
|
+
return Math.abs(dx) >= Math.abs(dy) ? (point[0]! - a[0]!) / dx : (point[1]! - a[1]!) / dy;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Tests intervals between area boundary contacts, excluding coincident boundary segments. */
|
|
135
|
+
function segmentEntersArea(
|
|
136
|
+
edge: Edge,
|
|
137
|
+
boundaries: readonly Edge[],
|
|
138
|
+
areas: readonly (readonly Path[])[]
|
|
139
|
+
): boolean {
|
|
140
|
+
const [a, b] = edge;
|
|
141
|
+
if (insideArea(a, areas) || insideArea(b, areas)) return true;
|
|
142
|
+
const cuts = new Set([0, 1]);
|
|
143
|
+
const coincident: [number, number][] = [];
|
|
144
|
+
for (const boundary of boundaries) {
|
|
145
|
+
if (!segmentsMeet(...edge, ...boundary)) continue;
|
|
146
|
+
const contacts = endpointContacts(edge, boundary).map(point => parameter(point, edge));
|
|
147
|
+
if (!contacts.length) return true;
|
|
148
|
+
for (const contact of contacts) cuts.add(contact);
|
|
149
|
+
if (contacts.length === 2) coincident.push([Math.min(...contacts), Math.max(...contacts)]);
|
|
150
|
+
}
|
|
151
|
+
const sorted = [...cuts].sort((left, right) => left - right);
|
|
152
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
153
|
+
const from = sorted[i - 1]!;
|
|
154
|
+
const to = sorted[i]!;
|
|
155
|
+
if (coincident.some(([start, end]) => start <= from && to <= end)) continue;
|
|
156
|
+
const t = (from + to) / 2;
|
|
157
|
+
if (insideArea([(1 - t) * a[0]! + t * b[0]!, (1 - t) * a[1]! + t * b[1]!], areas)) return true;
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Finds any line segment that enters an areal interior. */
|
|
163
|
+
function lineEntersArea(line: Lines, area: Area): boolean {
|
|
164
|
+
const areas = polygons(area);
|
|
165
|
+
const boundaries = [...edges(areas.flat(), true)];
|
|
166
|
+
for (const edge of edges(paths(line))) {
|
|
167
|
+
if (segmentEntersArea(edge, boundaries, areas)) return true;
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Checks point locations against the other geometry's interior. */
|
|
173
|
+
function pointsMeetInterior(points: Points, other: Lines | Area): boolean {
|
|
174
|
+
const positions = points.type === 'Point' ? [points.coordinates] : points.coordinates;
|
|
175
|
+
if (isAreal(other)) {
|
|
176
|
+
const areas = polygons(other);
|
|
177
|
+
return positions.some(
|
|
178
|
+
point => Number.isFinite(point[0]) && Number.isFinite(point[1]) && insideArea(point, areas)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
const linePaths = paths(other);
|
|
182
|
+
const boundary = lineBoundary(linePaths);
|
|
183
|
+
const segments = [...edges(linePaths)];
|
|
184
|
+
return positions.some(
|
|
185
|
+
point => !boundary.has(pointKey(point)) && segments.some(edge => pointOnSegment(point, ...edge))
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Reports any geometric contact without intersection between the two interiors. */
|
|
190
|
+
export function planarTouches(a: WellKnownGeometry, b: WellKnownGeometry): boolean {
|
|
191
|
+
if (
|
|
192
|
+
(!isPointLike(a) && !isLinear(a) && !isAreal(a)) ||
|
|
193
|
+
(!isPointLike(b) && !isLinear(b) && !isAreal(b))
|
|
194
|
+
)
|
|
195
|
+
throw new Error('ST_Touches requires point, line or polygon geometry');
|
|
196
|
+
if (isPointLike(a) && isPointLike(b)) return false;
|
|
197
|
+
if (!planarIntersects(a, b)) return false;
|
|
198
|
+
if (isPointLike(a) && !isPointLike(b)) return !pointsMeetInterior(a, b);
|
|
199
|
+
if (isPointLike(b) && !isPointLike(a)) return !pointsMeetInterior(b, a);
|
|
200
|
+
if (isLinear(a) && isLinear(b)) return !lineInteriorsMeet(a, b);
|
|
201
|
+
if (isLinear(a) && isAreal(b)) return !lineEntersArea(a, b);
|
|
202
|
+
if (isAreal(a) && isLinear(b)) return !lineEntersArea(b, a);
|
|
203
|
+
return !polygonClipping.intersection(polygonSet(a), polygonSet(b)).length;
|
|
204
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Data, Schema, Struct, Table} from 'apache-arrow';
|
|
6
|
+
import {
|
|
7
|
+
createRecordBatchWithMetadata,
|
|
8
|
+
getRecordBatchMetadata,
|
|
9
|
+
getGeoArrowFieldInfo,
|
|
10
|
+
makeGeoArrowColumnFromArrowVector,
|
|
11
|
+
materializeGeoArrowRows,
|
|
12
|
+
type WellKnownGeometry
|
|
13
|
+
} from '@math.gl/core/arrow';
|
|
14
|
+
import type {TableFunction} from '@math.gl/sql/arrow';
|
|
15
|
+
import {simplifyCoverage} from './coverage-simplification';
|
|
16
|
+
import {nativeGeometryResult} from './spatial-sql';
|
|
17
|
+
|
|
18
|
+
/** Settings for a whole-relation coverage simplification. */
|
|
19
|
+
export type CoverageSimplifyOptions = {
|
|
20
|
+
/** Maximum vertex displacement in planar coordinate units. */
|
|
21
|
+
tolerance: number;
|
|
22
|
+
/** Name of the native Polygon or MultiPolygon column. */
|
|
23
|
+
geometry: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Creates an opted-in ST_CoverageSimplify table function with fixed tolerance. */
|
|
27
|
+
export function createSTCoverageSimplify({
|
|
28
|
+
geometry,
|
|
29
|
+
tolerance
|
|
30
|
+
}: CoverageSimplifyOptions): TableFunction {
|
|
31
|
+
if (!Number.isFinite(tolerance) || tolerance < 0)
|
|
32
|
+
throw new Error('ST_CoverageSimplify requires a finite nonnegative tolerance');
|
|
33
|
+
return {
|
|
34
|
+
kind: 'table',
|
|
35
|
+
name: 'ST_CoverageSimplify',
|
|
36
|
+
/** Binds one native 2D polygon field and a stable output schema. */
|
|
37
|
+
bind(inputSchema) {
|
|
38
|
+
const index = inputSchema.fields.findIndex(field => field.name === geometry);
|
|
39
|
+
const field = inputSchema.fields[index];
|
|
40
|
+
const info = field && getGeoArrowFieldInfo(field);
|
|
41
|
+
const encoding = info?.encoding;
|
|
42
|
+
if (
|
|
43
|
+
!field ||
|
|
44
|
+
(encoding !== 'geoarrow.polygon' && encoding !== 'geoarrow.multipolygon') ||
|
|
45
|
+
info.dimension !== 'xy' ||
|
|
46
|
+
info.metadata['edges'] === 'spherical'
|
|
47
|
+
)
|
|
48
|
+
throw new Error(
|
|
49
|
+
'ST_CoverageSimplify requires a native planar XY Polygon or MultiPolygon column'
|
|
50
|
+
);
|
|
51
|
+
const output = nativeGeometryResult(encoding);
|
|
52
|
+
const fields = inputSchema.fields.slice();
|
|
53
|
+
fields[index] = field.clone({type: output.type});
|
|
54
|
+
const outputSchema = new Schema(
|
|
55
|
+
fields,
|
|
56
|
+
inputSchema.metadata,
|
|
57
|
+
undefined,
|
|
58
|
+
inputSchema.metadataVersion
|
|
59
|
+
);
|
|
60
|
+
return {
|
|
61
|
+
outputSchema,
|
|
62
|
+
/** Simplifies shared boundaries across batches while preserving row alignment. */
|
|
63
|
+
evaluate(input, {signal}) {
|
|
64
|
+
signal?.throwIfAborted();
|
|
65
|
+
if (!input.numRows) return new Table(outputSchema, []);
|
|
66
|
+
const vector = input.getChildAt(index)!;
|
|
67
|
+
const column = makeGeoArrowColumnFromArrowVector(vector, {field});
|
|
68
|
+
const rows = materializeGeoArrowRows(column) as (Extract<
|
|
69
|
+
WellKnownGeometry,
|
|
70
|
+
{type: 'Polygon' | 'MultiPolygon'}
|
|
71
|
+
> | null)[];
|
|
72
|
+
const result = simplifyCoverage(rows, tolerance, signal);
|
|
73
|
+
let first = 0;
|
|
74
|
+
const type = new Struct(fields);
|
|
75
|
+
const batches = input.batches.map(batch => {
|
|
76
|
+
signal?.throwIfAborted();
|
|
77
|
+
const children = batch.data.children.slice();
|
|
78
|
+
children[index] = output.build(result.slice(first, first + batch.numRows)).data[0]!;
|
|
79
|
+
first += batch.numRows;
|
|
80
|
+
const data = new Data(type, 0, batch.numRows, 0, undefined, children);
|
|
81
|
+
return createRecordBatchWithMetadata(outputSchema, data, getRecordBatchMetadata(batch));
|
|
82
|
+
});
|
|
83
|
+
return new Table(outputSchema, batches);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|