@math.gl/geospatial 5.0.0 → 5.0.1

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.
Files changed (127) hide show
  1. package/dist/line-simplification.d.ts +7 -0
  2. package/dist/line-simplification.d.ts.map +1 -1
  3. package/dist/line-simplification.js +4 -13
  4. package/dist/line-simplification.js.map +1 -1
  5. package/dist/linear-referencing.d.ts +13 -1
  6. package/dist/linear-referencing.d.ts.map +1 -1
  7. package/dist/linear-referencing.js +29 -9
  8. package/dist/linear-referencing.js.map +1 -1
  9. package/dist/measure-referencing.d.ts +16 -0
  10. package/dist/measure-referencing.d.ts.map +1 -0
  11. package/dist/measure-referencing.js +143 -0
  12. package/dist/measure-referencing.js.map +1 -0
  13. package/dist/polygon-simplification.d.ts +6 -0
  14. package/dist/polygon-simplification.d.ts.map +1 -0
  15. package/dist/polygon-simplification.js +141 -0
  16. package/dist/polygon-simplification.js.map +1 -0
  17. package/dist/spatial-box-clipping.d.ts +9 -0
  18. package/dist/spatial-box-clipping.d.ts.map +1 -0
  19. package/dist/spatial-box-clipping.js +123 -0
  20. package/dist/spatial-box-clipping.js.map +1 -0
  21. package/dist/spatial-dwithin.d.ts +19 -0
  22. package/dist/spatial-dwithin.d.ts.map +1 -0
  23. package/dist/spatial-dwithin.js +107 -0
  24. package/dist/spatial-dwithin.js.map +1 -0
  25. package/dist/spatial-hulls.d.ts +6 -0
  26. package/dist/spatial-hulls.d.ts.map +1 -0
  27. package/dist/spatial-hulls.js +176 -0
  28. package/dist/spatial-hulls.js.map +1 -0
  29. package/dist/spatial-line-intersection-result.d.ts +4 -0
  30. package/dist/spatial-line-intersection-result.d.ts.map +1 -0
  31. package/dist/spatial-line-intersection-result.js +79 -0
  32. package/dist/spatial-line-intersection-result.js.map +1 -0
  33. package/dist/spatial-line-polygon-intersection.d.ts +4 -0
  34. package/dist/spatial-line-polygon-intersection.d.ts.map +1 -0
  35. package/dist/spatial-line-polygon-intersection.js +130 -0
  36. package/dist/spatial-line-polygon-intersection.js.map +1 -0
  37. package/dist/spatial-planar-intersects.d.ts +11 -0
  38. package/dist/spatial-planar-intersects.d.ts.map +1 -1
  39. package/dist/spatial-planar-intersects.js +20 -12
  40. package/dist/spatial-planar-intersects.js.map +1 -1
  41. package/dist/spatial-planar.d.ts +9 -0
  42. package/dist/spatial-planar.d.ts.map +1 -1
  43. package/dist/spatial-planar.js +13 -1
  44. package/dist/spatial-planar.js.map +1 -1
  45. package/dist/spatial-polygon-overlay.d.ts +5 -0
  46. package/dist/spatial-polygon-overlay.d.ts.map +1 -0
  47. package/dist/spatial-polygon-overlay.js +23 -0
  48. package/dist/spatial-polygon-overlay.js.map +1 -0
  49. package/dist/spatial-sql.d.ts +13 -2
  50. package/dist/spatial-sql.d.ts.map +1 -1
  51. package/dist/spatial-sql.js +30 -12
  52. package/dist/spatial-sql.js.map +1 -1
  53. package/dist/spatial-subdivision.d.ts +9 -0
  54. package/dist/spatial-subdivision.d.ts.map +1 -0
  55. package/dist/spatial-subdivision.js +61 -0
  56. package/dist/spatial-subdivision.js.map +1 -0
  57. package/dist/spatial-touches.d.ts +13 -0
  58. package/dist/spatial-touches.d.ts.map +1 -0
  59. package/dist/spatial-touches.js +192 -0
  60. package/dist/spatial-touches.js.map +1 -0
  61. package/dist/st-functions.cjs +2411 -112
  62. package/dist/st-functions.cjs.map +3 -3
  63. package/dist/st-functions.d.ts +67 -2
  64. package/dist/st-functions.d.ts.map +1 -1
  65. package/dist/st-functions.js +195 -12
  66. package/dist/st-functions.js.map +1 -1
  67. package/dist/st-geodesic-functions.d.ts +33 -0
  68. package/dist/st-geodesic-functions.d.ts.map +1 -0
  69. package/dist/st-geodesic-functions.js +84 -0
  70. package/dist/st-geodesic-functions.js.map +1 -0
  71. package/dist/st-hull-functions.cjs +386 -0
  72. package/dist/st-hull-functions.cjs.map +6 -0
  73. package/dist/st-hull-functions.d.ts +17 -0
  74. package/dist/st-hull-functions.d.ts.map +1 -0
  75. package/dist/st-hull-functions.js +14 -0
  76. package/dist/st-hull-functions.js.map +1 -0
  77. package/dist/st-overlay-functions.cjs +621 -0
  78. package/dist/st-overlay-functions.cjs.map +6 -0
  79. package/dist/st-overlay-functions.d.ts +36 -0
  80. package/dist/st-overlay-functions.d.ts.map +1 -0
  81. package/dist/st-overlay-functions.js +183 -0
  82. package/dist/st-overlay-functions.js.map +1 -0
  83. package/dist/st-viewport-functions.cjs +481 -0
  84. package/dist/st-viewport-functions.cjs.map +6 -0
  85. package/dist/st-viewport-functions.d.ts +13 -0
  86. package/dist/st-viewport-functions.d.ts.map +1 -0
  87. package/dist/st-viewport-functions.js +81 -0
  88. package/dist/st-viewport-functions.js.map +1 -0
  89. package/dist/terrain/index.cjs +93 -20
  90. package/dist/terrain/index.cjs.map +3 -3
  91. package/dist/terrain/index.d.ts +1 -0
  92. package/dist/terrain/index.d.ts.map +1 -1
  93. package/dist/terrain/index.js +1 -0
  94. package/dist/terrain/index.js.map +1 -1
  95. package/dist/terrain/regional-watersheds.d.ts +44 -0
  96. package/dist/terrain/regional-watersheds.d.ts.map +1 -0
  97. package/dist/terrain/regional-watersheds.js +108 -0
  98. package/dist/terrain/regional-watersheds.js.map +1 -0
  99. package/dist/terrain/tile-cutout-update.d.ts.map +1 -1
  100. package/dist/terrain/tile-cutout-update.js +28 -22
  101. package/dist/terrain/tile-cutout-update.js.map +1 -1
  102. package/dist/turf-functions.cjs +16 -9
  103. package/dist/turf-functions.cjs.map +1 -1
  104. package/package.json +21 -6
  105. package/src/line-simplification.ts +4 -16
  106. package/src/linear-referencing.ts +44 -16
  107. package/src/measure-referencing.ts +165 -0
  108. package/src/polygon-simplification.ts +185 -0
  109. package/src/spatial-box-clipping.ts +140 -0
  110. package/src/spatial-dwithin.ts +135 -0
  111. package/src/spatial-hulls.ts +203 -0
  112. package/src/spatial-line-intersection-result.ts +93 -0
  113. package/src/spatial-line-polygon-intersection.ts +147 -0
  114. package/src/spatial-planar-intersects.ts +21 -15
  115. package/src/spatial-planar.ts +20 -1
  116. package/src/spatial-polygon-overlay.ts +30 -0
  117. package/src/spatial-sql.ts +47 -12
  118. package/src/spatial-subdivision.ts +75 -0
  119. package/src/spatial-touches.ts +218 -0
  120. package/src/st-functions.ts +283 -15
  121. package/src/st-geodesic-functions.ts +121 -0
  122. package/src/st-hull-functions.ts +25 -0
  123. package/src/st-overlay-functions.ts +244 -0
  124. package/src/st-viewport-functions.ts +98 -0
  125. package/src/terrain/index.ts +7 -0
  126. package/src/terrain/regional-watersheds.ts +153 -0
  127. package/src/terrain/tile-cutout-update.ts +32 -26
@@ -16,6 +16,7 @@ import {
16
16
  import type {AggregateFunction, ArrowTableFunction, ScalarFunction} from '@math.gl/sql/arrow';
17
17
  import {
18
18
  getGeoArrowFieldInfo,
19
+ mapGeoArrowVectorCoordinates,
19
20
  getGeoArrowBounds,
20
21
  getGeoArrowRowBounds,
21
22
  getGeoArrowColumnView,
@@ -26,11 +27,15 @@ import {
26
27
  type WellKnownGeometry
27
28
  } from '@math.gl/core/arrow';
28
29
  import {planarIntersects} from './spatial-planar-intersects';
29
- import {lineLocatePoint, lineSubstring} from './linear-referencing';
30
+ import {planarTouches} from './spatial-touches';
31
+ import {linesWithinDistance, pointWithinPolygonDistance} from './spatial-dwithin';
32
+ import {lineLocatePoint, lineSubstring, projectPointToLine} from './linear-referencing';
33
+ import {addMeasure, locateAlong, locateBetween, measuredDimension} from './measure-referencing';
30
34
  import {normalizeCRSIdentifier} from './crs/crs-identifier';
31
35
  import {EPSG3857ToLngLat, EPSG4326ToEPSG3857} from './web-mercator/mercator-meters';
32
36
  import {vertexCount, memberCount, geometryMember} from './geometry-inspection';
33
37
  import {reduceLineVertices} from './line-simplification';
38
+ import {simplifyPolygonTopology} from './polygon-simplification';
34
39
  import {collectPair, makeLine} from './geometry-constructors';
35
40
  import {EPSG3857_HALF_CIRCUMFERENCE} from './web-mercator/mercator-meters';
36
41
  import {
@@ -48,6 +53,7 @@ import {
48
53
  EXTENSION_NAME,
49
54
  geometryTypes,
50
55
  lineKinds,
56
+ nativePolygonResult,
51
57
  planarGeometry,
52
58
  pointCoordinates,
53
59
  pointInPolygon,
@@ -359,20 +365,112 @@ export const ST_DISTANCE = /* @__PURE__ */ spatialFunction(
359
365
  }
360
366
  );
361
367
 
362
- /** Tests an inclusive planar radius in the coordinate units of two points. */
363
- export const ST_DWITHIN = /* @__PURE__ */ spatialFunction(
364
- 'ST_DWithin',
365
- [pointKinds, pointKinds],
366
- /* @__PURE__ */ new Bool(),
367
- ([a, b], radius) => {
368
- if (radius! < 0) throw new Error('ST_DWithin distance must be nonnegative');
369
- const p = pointCoordinates(a!);
370
- const q = pointCoordinates(b!);
371
- return Boolean(p && q && Math.hypot(q[0]! - p[0]!, q[1]! - p[1]!) <= radius!);
372
- },
373
- 1
368
+ /** Geometry arguments supported by the route-snapping functions. */
369
+ const routePointKinds = ['Point', 'LineString'] as const;
370
+
371
+ /** Returns the closest pair in argument order for point/point and point/line inputs. */
372
+ function closestPair(a: WellKnownGeometry, b: WellKnownGeometry) {
373
+ if (a.type === 'LineString' && b.type === 'LineString') {
374
+ throw new Error('Route snapping requires at least one Point');
375
+ }
376
+ if (a.type === 'LineString' || b.type === 'LineString') {
377
+ const lineFirst = a.type === 'LineString';
378
+ const point = lineFirst ? b : a;
379
+ const xy = pointCoordinates(point);
380
+ const projection = projectPointToLine(lineFirst ? a : b, point);
381
+ if (!xy || !projection) return null;
382
+ const {coordinate, distance} = projection;
383
+ return {start: lineFirst ? coordinate : xy, end: lineFirst ? xy : coordinate, distance};
384
+ }
385
+ const start = pointCoordinates(a);
386
+ const end = pointCoordinates(b);
387
+ return start && end
388
+ ? {start, end, distance: Math.hypot(end[0]! - start[0]!, end[1]! - start[1]!)}
389
+ : null;
390
+ }
391
+
392
+ /** Returns the closest point on the first geometry as GeoArrow WKB. */
393
+ export const ST_CLOSESTPOINT = /* @__PURE__ */ spatialFunction(
394
+ 'ST_ClosestPoint',
395
+ [routePointKinds, routePointKinds],
396
+ /* @__PURE__ */ new Binary(),
397
+ ([a, b]) => {
398
+ const pair = closestPair(a!, b!);
399
+ return pair ? writeWKB({type: 'Point', coordinates: pair.start}, 'xy') : null;
400
+ }
374
401
  );
375
402
 
403
+ /** Returns the shortest segment from the first geometry to the second as GeoArrow WKB. */
404
+ export const ST_SHORTESTLINE = /* @__PURE__ */ spatialFunction(
405
+ 'ST_ShortestLine',
406
+ [routePointKinds, routePointKinds],
407
+ /* @__PURE__ */ new Binary(),
408
+ ([a, b]) => {
409
+ const pair = closestPair(a!, b!);
410
+ return pair ? writeWKB({type: 'LineString', coordinates: [pair.start, pair.end]}, 'xy') : null;
411
+ }
412
+ );
413
+
414
+ /** Geometry kinds accepted by the supported proximity pairings. */
415
+ const proximityKinds = [...pointKinds, ...lineKinds, ...polygonKinds];
416
+
417
+ /** Dispatches to the exact planar kernel for the two input geometry types. */
418
+ function withinDistance(a: WellKnownGeometry, b: WellKnownGeometry, radius: number): boolean {
419
+ if (radius < 0) throw new Error('ST_DWithin distance must be nonnegative');
420
+ if (Number.isNaN(radius)) return false;
421
+ if (a.type === 'Point' && (b.type === 'Polygon' || b.type === 'MultiPolygon'))
422
+ return pointWithinPolygonDistance(a, b, radius);
423
+ if (b.type === 'Point' && (a.type === 'Polygon' || a.type === 'MultiPolygon'))
424
+ return pointWithinPolygonDistance(b, a, radius);
425
+ if (
426
+ (a.type === 'LineString' || a.type === 'MultiLineString') &&
427
+ (b.type === 'LineString' || b.type === 'MultiLineString')
428
+ )
429
+ return linesWithinDistance(a, b, radius);
430
+ if (
431
+ (a.type === 'Point' && (b.type === 'Point' || b.type === 'LineString')) ||
432
+ (b.type === 'Point' && a.type === 'LineString')
433
+ ) {
434
+ const pair = closestPair(a, b);
435
+ return pair !== null && pair.distance <= radius;
436
+ }
437
+ throw new Error(`ST_DWithin does not support ${a.type} and ${b.type}`);
438
+ }
439
+
440
+ /** Binds supported combinations when both input encodings identify their geometry kinds. */
441
+ function makeDWithin(): ScalarFunction {
442
+ const spatial = spatialFunction(
443
+ 'ST_DWithin',
444
+ [proximityKinds, proximityKinds],
445
+ new Bool(),
446
+ ([a, b], radius) => withinDistance(a!, b!, radius!),
447
+ 1
448
+ );
449
+ return {
450
+ ...spatial,
451
+ /** Checks concrete native pairs at binding; WKB pairs are checked per row. */
452
+ resolvePrototype(fields) {
453
+ const result = spatial.resolvePrototype!(fields);
454
+ const a = fields[0]?.metadata.get(EXTENSION_NAME);
455
+ const b = fields[1]?.metadata.get(EXTENSION_NAME);
456
+ if (result && a && b && a !== 'geoarrow.wkb' && b !== 'geoarrow.wkb') {
457
+ /** Identifies either concrete native line encoding. */
458
+ const linear = (encoding: string) =>
459
+ encoding === 'geoarrow.linestring' || encoding === 'geoarrow.multilinestring';
460
+ const pointPair =
461
+ (a === 'geoarrow.point' && b !== 'geoarrow.multilinestring') ||
462
+ (b === 'geoarrow.point' && a !== 'geoarrow.multilinestring');
463
+ if (!pointPair && !(linear(a) && linear(b)))
464
+ throw new Error(`ST_DWithin does not support ${a} and ${b}`);
465
+ }
466
+ return result;
467
+ }
468
+ };
469
+ }
470
+
471
+ /** Tests an inclusive planar radius in the coordinate units of the geometries. */
472
+ export const ST_DWITHIN = /* @__PURE__ */ makeDWithin();
473
+
376
474
  /** Tests whether two planar geometries share any point, including their boundaries. */
377
475
  export const ST_INTERSECTS = /* @__PURE__ */ spatialFunction(
378
476
  'ST_Intersects',
@@ -384,6 +482,14 @@ export const ST_INTERSECTS = /* @__PURE__ */ spatialFunction(
384
482
  ([a, b]) => planarIntersects(a!, b!)
385
483
  );
386
484
 
485
+ /** Tests boundary contact while excluding intersections between geometry interiors. */
486
+ export const ST_TOUCHES = /* @__PURE__ */ spatialFunction(
487
+ 'ST_Touches',
488
+ [geometryTypes, geometryTypes],
489
+ /* @__PURE__ */ new Bool(),
490
+ ([a, b]) => planarTouches(a!, b!)
491
+ );
492
+
387
493
  /** Interpolates a point at a fraction of the planar length. */
388
494
  export const ST_LINEINTERPOLATEPOINT = /* @__PURE__ */ spatialFunction(
389
495
  'ST_LineInterpolatePoint',
@@ -414,6 +520,45 @@ export const ST_LINESUBSTRING = /* @__PURE__ */ spatialFunction(
414
520
  2
415
521
  );
416
522
 
523
+ /** Interpolates a new M ordinate by cumulative XY length while retaining Z. */
524
+ export const ST_ADDMEASURE = /* @__PURE__ */ spatialFunction(
525
+ 'ST_AddMeasure',
526
+ [lineKinds],
527
+ /* @__PURE__ */ new Binary(),
528
+ ([line], start, end) => {
529
+ const result = addMeasure(line!, line!.dimension!, start!, end!);
530
+ return writeWKB(result.geometry, result.dimension);
531
+ },
532
+ 2,
533
+ true
534
+ );
535
+
536
+ /** Returns the points on a measured route that match the requested measure. */
537
+ export const ST_LOCATEALONG = /* @__PURE__ */ spatialFunction(
538
+ 'ST_LocateAlong',
539
+ [lineKinds],
540
+ /* @__PURE__ */ new Binary(),
541
+ ([line], measure) => {
542
+ const dimension = measuredDimension(line!.dimension!);
543
+ return writeWKB(locateAlong(line!, dimension, measure!), dimension);
544
+ },
545
+ 1,
546
+ true
547
+ );
548
+
549
+ /** Returns the portions of a measured route within an inclusive measure interval. */
550
+ export const ST_LOCATEBETWEEN = /* @__PURE__ */ spatialFunction(
551
+ 'ST_LocateBetween',
552
+ [lineKinds],
553
+ /* @__PURE__ */ new Binary(),
554
+ ([line], start, end) => {
555
+ const dimension = measuredDimension(line!.dimension!);
556
+ return writeWKB(locateBetween(line!, dimension, start!, end!), dimension);
557
+ },
558
+ 2,
559
+ true
560
+ );
561
+
417
562
  /** Extracts an EPSG code from a GeoArrow authority identifier or a PROJJSON root ID. */
418
563
  function geometrySRID(crs: unknown): number {
419
564
  let identifier = typeof crs === 'string' ? normalizeCRSIdentifier(crs) : '';
@@ -524,6 +669,90 @@ function crsFunction(name: 'ST_SRID' | 'ST_SetSRID' | 'ST_Transform'): ScalarFun
524
669
  };
525
670
  }
526
671
 
672
+ /** A synchronous projection engine with the requested CRS definitions available. */
673
+ export type SpatialSQLProjectionEngine = {
674
+ /** Prepares a transform in GeoArrow XY axis order. */
675
+ createProjection(options: {from: string | object; to: string; enforceAxis: false}): {
676
+ /** Projects one horizontal coordinate. */
677
+ projectSync(coordinate: readonly number[]): readonly number[];
678
+ };
679
+ };
680
+
681
+ /** Creates an ST_Transform overload backed by a supplied CRS projection engine. */
682
+ export function createSTTransform(engine: SpatialSQLProjectionEngine): ScalarFunction {
683
+ return {
684
+ kind: 'scalar',
685
+ name: 'ST_Transform',
686
+ prototypes: [],
687
+ /** Resolves one source and target CRS for the entire projection. */
688
+ resolvePrototype(fields, literals) {
689
+ if (fields.length !== 2 || !fields[0]) return undefined;
690
+ const field = fields[0];
691
+ const target = literals?.[1];
692
+ if (target === undefined || !Number.isSafeInteger(target) || target <= 0 || target > 998999)
693
+ throw new Error('ST_Transform requires a constant positive integer EPSG code');
694
+ const info = getGeoArrowFieldInfo(field);
695
+ if (!info) throw new Error('ST_Transform requires a GeoArrow geometry column');
696
+ if (info.metadata['edges'] === 'spherical')
697
+ throw new Error('ST_Transform requires planar geometry');
698
+ const source = info.metadata['crs'];
699
+ if (
700
+ !(typeof source === 'string'
701
+ ? source.trim()
702
+ : source && typeof source === 'object' && !Array.isArray(source))
703
+ )
704
+ throw new Error('ST_Transform requires source CRS metadata');
705
+ const to = 'EPSG:' + target;
706
+ const transform = engine.createProjection({
707
+ from: source as string | object,
708
+ to,
709
+ enforceAxis: false
710
+ });
711
+ const metadata = JSON.stringify({...info.metadata, crs: to, crs_type: 'authority_code'});
712
+ const targetField = fields[1] ?? new Field('srid', new Int32(), false);
713
+ if (!DataType.isInt(targetField.type) && !DataType.isFloat(targetField.type))
714
+ throw new Error('ST_Transform requires a numeric SRID');
715
+
716
+ /** Projects XY and retains additional ordinates, including the empty-point sentinel. */
717
+ const project = (coordinate: readonly number[]): number[] => {
718
+ if (coordinate.every(Number.isNaN)) return [...coordinate];
719
+ const [x, y] = transform.projectSync([coordinate[0]!, coordinate[1]!]);
720
+ if (!Number.isFinite(x) || !Number.isFinite(y))
721
+ throw new Error('ST_Transform produced nonfinite coordinates');
722
+ return [x!, y!, ...coordinate.slice(2)];
723
+ };
724
+
725
+ if (info.encoding === 'geoarrow.wkb') {
726
+ const prototype = spatialFunction(
727
+ 'ST_Transform',
728
+ [[...geometryTypes, 'GeometryCollection']],
729
+ new Binary(),
730
+ ([geometry]) => writeWKB(projectGeometry(geometry!, project), 'xy'),
731
+ 1
732
+ ).resolvePrototype!(fields)!;
733
+ prototype.result.metadata.set(EXTENSION_METADATA, metadata);
734
+ return prototype;
735
+ }
736
+ if (!geometryTypes.some(kind => 'geoarrow.' + kind.toLowerCase() === info.encoding))
737
+ throw new Error('ST_Transform requires WKB or a specialized native GeoArrow encoding');
738
+ const outputMetadata = new Map(field.metadata);
739
+ outputMetadata.set(EXTENSION_METADATA, metadata);
740
+ return {
741
+ arguments: [field, targetField],
742
+ result: new Field('ST_Transform', field.type, field.nullable, outputMetadata),
743
+ /** Maps native coordinate buffers directly, retaining topology and null rows. */
744
+ evaluate([vector], {activeRows}) {
745
+ return mapGeoArrowVectorCoordinates(
746
+ vector!,
747
+ (coordinate, row) => (activeRows[row] ? project(coordinate) : coordinate),
748
+ {field}
749
+ );
750
+ }
751
+ };
752
+ }
753
+ };
754
+ }
755
+
527
756
  /** Returns the field EPSG code, or zero for an unidentified CRS. */
528
757
  export const ST_SRID = /* @__PURE__ */ crsFunction('ST_SRID');
529
758
  /** Assigns a CRS to a geometry column, preserving its coordinate values. */
@@ -562,6 +791,31 @@ export const ST_REMOVEREPEATEDPOINTS = /* @__PURE__ */ lineReduction(
562
791
  /** Reduces route vertices with Douglas-Peucker simplification. */
563
792
  export const ST_SIMPLIFY = /* @__PURE__ */ lineReduction('ST_Simplify', 'simplify');
564
793
 
794
+ /** Simplifies polygon boundaries while retaining shells, holes, and component topology. */
795
+ export const ST_SIMPLIFYPRESERVETOPOLOGY: ScalarFunction = {
796
+ kind: 'scalar',
797
+ name: 'ST_SimplifyPreserveTopology',
798
+ prototypes: [],
799
+ /** Preserves specialized native storage where the input schema identifies its family. */
800
+ resolvePrototype(fields) {
801
+ const encoding = fields[0]?.metadata.get(EXTENSION_NAME);
802
+ const native = encoding === 'geoarrow.polygon' || encoding === 'geoarrow.multipolygon';
803
+ return spatialFunction(
804
+ 'ST_SimplifyPreserveTopology',
805
+ [polygonKinds],
806
+ native ? nativePolygonResult(encoding) : new Binary(),
807
+ ([geometry], tolerance) => {
808
+ const result = simplifyPolygonTopology(
809
+ geometry as Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>,
810
+ tolerance!
811
+ );
812
+ return native ? result : writeWKB(result, 'xy');
813
+ },
814
+ 1
815
+ ).resolvePrototype!(fields);
816
+ }
817
+ };
818
+
565
819
  /** Returns clockwise azimuth from positive Y in radians. */
566
820
  export const ST_AZIMUTH = /* @__PURE__ */ spatialFunction(
567
821
  'ST_Azimuth',
@@ -587,10 +841,10 @@ export const ST_AREA = /* @__PURE__ */ spatialFunction(
587
841
  ([geometry]) => planarArea(geometry!)
588
842
  );
589
843
 
590
- /** Returns planar linear length, or zero for non-linear input. */
844
+ /** Returns the summed planar length of linear components. */
591
845
  export const ST_LENGTH = /* @__PURE__ */ spatialFunction(
592
846
  'ST_Length',
593
- [geometryTypes],
847
+ [[...geometryTypes, 'GeometryCollection']],
594
848
  /* @__PURE__ */ new Float64(),
595
849
  ([geometry]) => planarLength(geometry!)
596
850
  );
@@ -823,12 +1077,16 @@ export const POSTGIS_SPATIAL_FUNCTIONS: readonly (ArrowTableFunction | Aggregate
823
1077
  ST_MAKEENVELOPE,
824
1078
  ST_DISTANCE,
825
1079
  ST_DWITHIN,
1080
+ ST_CLOSESTPOINT,
1081
+ ST_SHORTESTLINE,
826
1082
  ST_INTERSECTS,
1083
+ ST_TOUCHES,
827
1084
  ST_SRID,
828
1085
  ST_SETSRID,
829
1086
  ST_TRANSFORM,
830
1087
  ST_REMOVEREPEATEDPOINTS,
831
1088
  ST_SIMPLIFY,
1089
+ ST_SIMPLIFYPRESERVETOPOLOGY,
832
1090
  ST_AZIMUTH,
833
1091
  ST_MAKEPOINT,
834
1092
  ST_TILEENVELOPE,
@@ -837,6 +1095,9 @@ export const POSTGIS_SPATIAL_FUNCTIONS: readonly (ArrowTableFunction | Aggregate
837
1095
  ST_LINEINTERPOLATEPOINT,
838
1096
  ST_LINELOCATEPOINT,
839
1097
  ST_LINESUBSTRING,
1098
+ ST_ADDMEASURE,
1099
+ ST_LOCATEALONG,
1100
+ ST_LOCATEBETWEEN,
840
1101
  ST_POINTONSURFACE,
841
1102
  ST_CENTROID,
842
1103
  ST_NPOINTS,
@@ -850,3 +1111,10 @@ export const POSTGIS_SPATIAL_FUNCTIONS: readonly (ArrowTableFunction | Aggregate
850
1111
  ST_ENVELOPE,
851
1112
  ST_EXTENT
852
1113
  ]);
1114
+
1115
+ export {
1116
+ ST_GEODESICDISTANCE,
1117
+ ST_GEODESICLENGTH,
1118
+ ST_GEODESICDWITHIN,
1119
+ GEODESIC_SPATIAL_FUNCTIONS
1120
+ } from './st-geodesic-functions';
@@ -0,0 +1,121 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {Bool, Float64} from 'apache-arrow';
6
+ import type {WellKnownGeometry} from '@math.gl/core/arrow';
7
+ import {WGS84_RADIUS_X, WGS84_RADIUS_Z} from './constants';
8
+ import {normalizeCRSIdentifier} from './crs/crs-identifier';
9
+ import {EllipsoidGeodesic} from './ellipsoid-geodesic';
10
+ import {lineKinds, pointKinds, spatialFunction} from './spatial-sql';
11
+
12
+ /** Ellipsoidal kernel with axes expressed in meters, initialized when first used. */
13
+ let kernel: EllipsoidGeodesic | undefined;
14
+
15
+ /** Measures the shortest WGS84 geodesic between two longitude–latitude positions. */
16
+ function meters(a: readonly number[], b: readonly number[]): number {
17
+ kernel ??= new EllipsoidGeodesic({
18
+ semiMajorAxis: WGS84_RADIUS_X,
19
+ semiMinorAxis: WGS84_RADIUS_Z
20
+ });
21
+ return kernel.inverse(a[1]!, a[0]!, b[1]!, b[0]!).s12;
22
+ }
23
+
24
+ /** Tests the coordinate representation of an empty point. */
25
+ function emptyPoint(coordinate: readonly number[]): boolean {
26
+ return !coordinate.length || coordinate.every(Number.isNaN);
27
+ }
28
+
29
+ /** Measures a point pair, with null for the empty set. */
30
+ function pointDistance(a: WellKnownGeometry, b: WellKnownGeometry): number | null {
31
+ if (a.type !== 'Point' || b.type !== 'Point') throw new Error('Expected geodesic point pair');
32
+ return emptyPoint(a.coordinates) || emptyPoint(b.coordinates)
33
+ ? null
34
+ : meters(a.coordinates, b.coordinates);
35
+ }
36
+
37
+ /** Measures linear components without adding a segment between components. */
38
+ function lineLength(geometry: WellKnownGeometry): number {
39
+ const lines =
40
+ geometry.type === 'LineString'
41
+ ? [geometry.coordinates]
42
+ : geometry.type === 'MultiLineString'
43
+ ? geometry.coordinates
44
+ : [];
45
+ let length = 0;
46
+ for (const path of lines)
47
+ for (let index = 1; index < path.length; index++)
48
+ length += meters(path[index - 1]!, path[index]!);
49
+ return length;
50
+ }
51
+
52
+ /** Recognizes the WGS84 geographic identifiers in GeoArrow field metadata. */
53
+ function isWGS84(crs: unknown): boolean {
54
+ if (typeof crs === 'string') {
55
+ const identifier = normalizeCRSIdentifier(crs);
56
+ return identifier === 'EPSG:4326' || identifier === 'OGC:CRS84';
57
+ }
58
+ if (!crs || typeof crs !== 'object' || !('id' in crs)) return false;
59
+ const {id} = crs;
60
+ return Boolean(
61
+ id &&
62
+ typeof id === 'object' &&
63
+ 'authority' in id &&
64
+ 'code' in id &&
65
+ isWGS84(String(id.authority) + ':' + String(id.code))
66
+ );
67
+ }
68
+
69
+ /** Validates the CRS only when a geodesic function is bound. */
70
+ function validateWGS84(crs: unknown, name: string): void {
71
+ if (!isWGS84(crs))
72
+ throw new Error(`${name} requires WGS84 CRS metadata (EPSG:4326 or OGC:CRS84)`);
73
+ }
74
+
75
+ /** Returns WGS84 point-to-point distance in meters. */
76
+ export const ST_GEODESICDISTANCE = /* @__PURE__ */ spatialFunction(
77
+ 'ST_GeodesicDistance',
78
+ [pointKinds, pointKinds],
79
+ /* @__PURE__ */ new Float64(),
80
+ ([a, b]) => pointDistance(a!, b!),
81
+ 0,
82
+ true,
83
+ false,
84
+ validateWGS84
85
+ );
86
+
87
+ /** Sums WGS84 geodesic segment lengths in meters. */
88
+ export const ST_GEODESICLENGTH = /* @__PURE__ */ spatialFunction(
89
+ 'ST_GeodesicLength',
90
+ [lineKinds],
91
+ /* @__PURE__ */ new Float64(),
92
+ ([line]) => lineLength(line!),
93
+ 0,
94
+ true,
95
+ false,
96
+ validateWGS84
97
+ );
98
+
99
+ /** Checks an inclusive WGS84 point-to-point proximity threshold in meters. */
100
+ export const ST_GEODESICDWITHIN = /* @__PURE__ */ spatialFunction(
101
+ 'ST_GeodesicDWithin',
102
+ [pointKinds, pointKinds],
103
+ /* @__PURE__ */ new Bool(),
104
+ ([a, b], radius) => {
105
+ if (!Number.isFinite(radius) || radius! < 0)
106
+ throw new Error('ST_GeodesicDWithin requires a finite nonnegative radius in meters');
107
+ const distance = pointDistance(a!, b!);
108
+ return distance !== null && distance <= radius!;
109
+ },
110
+ 1,
111
+ true,
112
+ false,
113
+ validateWGS84
114
+ );
115
+
116
+ /** Opt-in SQL package for ellipsoidal distance, length, and proximity. */
117
+ export const GEODESIC_SPATIAL_FUNCTIONS = /* @__PURE__ */ Object.freeze([
118
+ ST_GEODESICDISTANCE,
119
+ ST_GEODESICLENGTH,
120
+ ST_GEODESICDWITHIN
121
+ ]);
@@ -0,0 +1,25 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {Binary} from 'apache-arrow';
6
+ import {writeWKB} from '@math.gl/core/arrow';
7
+ import {concaveHull, convexHull} from './spatial-hulls';
8
+ import {geometryTypes, spatialFunction} from './spatial-sql';
9
+
10
+ /** Returns the smallest convex footprint enclosing the input vertices. */
11
+ export const ST_CONVEXHULL = /* @__PURE__ */ spatialFunction(
12
+ 'ST_ConvexHull',
13
+ [[...geometryTypes, 'GeometryCollection']],
14
+ /* @__PURE__ */ new Binary(),
15
+ ([geometry]) => writeWKB(convexHull(geometry!), 'xy')
16
+ );
17
+
18
+ /** Returns a hole-free concave footprint around a planar point cloud. */
19
+ export const ST_CONCAVEHULL = /* @__PURE__ */ spatialFunction(
20
+ 'ST_ConcaveHull',
21
+ [['Point', 'MultiPoint']],
22
+ /* @__PURE__ */ new Binary(),
23
+ ([geometry], ratio) => writeWKB(concaveHull(geometry!, ratio!), 'xy'),
24
+ 1
25
+ );