@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.
Files changed (164) hide show
  1. package/dist/coverage-simplification.d.ts +9 -0
  2. package/dist/coverage-simplification.d.ts.map +1 -0
  3. package/dist/coverage-simplification.js +167 -0
  4. package/dist/coverage-simplification.js.map +1 -0
  5. package/dist/line-network/build-line-network.d.ts +100 -0
  6. package/dist/line-network/build-line-network.d.ts.map +1 -0
  7. package/dist/line-network/build-line-network.js +766 -0
  8. package/dist/line-network/build-line-network.js.map +1 -0
  9. package/dist/line-network/line-network-routes.d.ts +53 -0
  10. package/dist/line-network/line-network-routes.d.ts.map +1 -0
  11. package/dist/line-network/line-network-routes.js +146 -0
  12. package/dist/line-network/line-network-routes.js.map +1 -0
  13. package/dist/line-network/line-network-weights.d.ts +11 -0
  14. package/dist/line-network/line-network-weights.d.ts.map +1 -0
  15. package/dist/line-network/line-network-weights.js +25 -0
  16. package/dist/line-network/line-network-weights.js.map +1 -0
  17. package/dist/line-network.cjs +850 -0
  18. package/dist/line-network.cjs.map +6 -0
  19. package/dist/line-network.d.ts +7 -0
  20. package/dist/line-network.d.ts.map +1 -0
  21. package/dist/line-network.js +7 -0
  22. package/dist/line-network.js.map +1 -0
  23. package/dist/line-simplification.d.ts +7 -0
  24. package/dist/line-simplification.d.ts.map +1 -1
  25. package/dist/line-simplification.js +4 -13
  26. package/dist/line-simplification.js.map +1 -1
  27. package/dist/linear-referencing.d.ts +3 -1
  28. package/dist/linear-referencing.d.ts.map +1 -1
  29. package/dist/linear-referencing.js +21 -33
  30. package/dist/linear-referencing.js.map +1 -1
  31. package/dist/measure-referencing.d.ts +16 -0
  32. package/dist/measure-referencing.d.ts.map +1 -0
  33. package/dist/measure-referencing.js +143 -0
  34. package/dist/measure-referencing.js.map +1 -0
  35. package/dist/polygon-simplification.d.ts +6 -0
  36. package/dist/polygon-simplification.d.ts.map +1 -0
  37. package/dist/polygon-simplification.js +111 -0
  38. package/dist/polygon-simplification.js.map +1 -0
  39. package/dist/route-editing.d.ts +10 -0
  40. package/dist/route-editing.d.ts.map +1 -0
  41. package/dist/route-editing.js +58 -0
  42. package/dist/route-editing.js.map +1 -0
  43. package/dist/spatial-box-clipping.d.ts +9 -0
  44. package/dist/spatial-box-clipping.d.ts.map +1 -0
  45. package/dist/spatial-box-clipping.js +123 -0
  46. package/dist/spatial-box-clipping.js.map +1 -0
  47. package/dist/spatial-dwithin.d.ts +19 -0
  48. package/dist/spatial-dwithin.d.ts.map +1 -0
  49. package/dist/spatial-dwithin.js +107 -0
  50. package/dist/spatial-dwithin.js.map +1 -0
  51. package/dist/spatial-hulls.d.ts +6 -0
  52. package/dist/spatial-hulls.d.ts.map +1 -0
  53. package/dist/spatial-hulls.js +176 -0
  54. package/dist/spatial-hulls.js.map +1 -0
  55. package/dist/spatial-line-intersection-result.d.ts +4 -0
  56. package/dist/spatial-line-intersection-result.d.ts.map +1 -0
  57. package/dist/spatial-line-intersection-result.js +79 -0
  58. package/dist/spatial-line-intersection-result.js.map +1 -0
  59. package/dist/spatial-line-polygon-intersection.d.ts +4 -0
  60. package/dist/spatial-line-polygon-intersection.d.ts.map +1 -0
  61. package/dist/spatial-line-polygon-intersection.js +130 -0
  62. package/dist/spatial-line-polygon-intersection.js.map +1 -0
  63. package/dist/spatial-planar-intersects.d.ts +1 -0
  64. package/dist/spatial-planar-intersects.d.ts.map +1 -1
  65. package/dist/spatial-planar-intersects.js +5 -37
  66. package/dist/spatial-planar-intersects.js.map +1 -1
  67. package/dist/spatial-planar.d.ts +9 -0
  68. package/dist/spatial-planar.d.ts.map +1 -1
  69. package/dist/spatial-planar.js +13 -1
  70. package/dist/spatial-planar.js.map +1 -1
  71. package/dist/spatial-polygon-overlay.d.ts +5 -0
  72. package/dist/spatial-polygon-overlay.d.ts.map +1 -0
  73. package/dist/spatial-polygon-overlay.js +23 -0
  74. package/dist/spatial-polygon-overlay.js.map +1 -0
  75. package/dist/spatial-sql.d.ts +15 -2
  76. package/dist/spatial-sql.d.ts.map +1 -1
  77. package/dist/spatial-sql.js +65 -13
  78. package/dist/spatial-sql.js.map +1 -1
  79. package/dist/spatial-subdivision.d.ts +9 -0
  80. package/dist/spatial-subdivision.d.ts.map +1 -0
  81. package/dist/spatial-subdivision.js +61 -0
  82. package/dist/spatial-subdivision.js.map +1 -0
  83. package/dist/spatial-touches.d.ts +11 -0
  84. package/dist/spatial-touches.d.ts.map +1 -0
  85. package/dist/spatial-touches.js +178 -0
  86. package/dist/spatial-touches.js.map +1 -0
  87. package/dist/st-coverage-functions.d.ts +11 -0
  88. package/dist/st-coverage-functions.d.ts.map +1 -0
  89. package/dist/st-coverage-functions.js +57 -0
  90. package/dist/st-coverage-functions.js.map +1 -0
  91. package/dist/st-functions.cjs +3279 -554
  92. package/dist/st-functions.cjs.map +3 -3
  93. package/dist/st-functions.d.ts +100 -2
  94. package/dist/st-functions.d.ts.map +1 -1
  95. package/dist/st-functions.js +239 -22
  96. package/dist/st-functions.js.map +1 -1
  97. package/dist/st-geodesic-functions.d.ts +41 -0
  98. package/dist/st-geodesic-functions.d.ts.map +1 -0
  99. package/dist/st-geodesic-functions.js +238 -0
  100. package/dist/st-geodesic-functions.js.map +1 -0
  101. package/dist/st-hull-functions.cjs +386 -0
  102. package/dist/st-hull-functions.cjs.map +6 -0
  103. package/dist/st-hull-functions.d.ts +17 -0
  104. package/dist/st-hull-functions.d.ts.map +1 -0
  105. package/dist/st-hull-functions.js +14 -0
  106. package/dist/st-hull-functions.js.map +1 -0
  107. package/dist/st-overlay-functions.cjs +668 -0
  108. package/dist/st-overlay-functions.cjs.map +6 -0
  109. package/dist/st-overlay-functions.d.ts +46 -0
  110. package/dist/st-overlay-functions.d.ts.map +1 -0
  111. package/dist/st-overlay-functions.js +248 -0
  112. package/dist/st-overlay-functions.js.map +1 -0
  113. package/dist/st-viewport-functions.cjs +481 -0
  114. package/dist/st-viewport-functions.cjs.map +6 -0
  115. package/dist/st-viewport-functions.d.ts +13 -0
  116. package/dist/st-viewport-functions.d.ts.map +1 -0
  117. package/dist/st-viewport-functions.js +81 -0
  118. package/dist/st-viewport-functions.js.map +1 -0
  119. package/dist/terrain/index.cjs +93 -20
  120. package/dist/terrain/index.cjs.map +3 -3
  121. package/dist/terrain/index.d.ts +1 -0
  122. package/dist/terrain/index.d.ts.map +1 -1
  123. package/dist/terrain/index.js +1 -0
  124. package/dist/terrain/index.js.map +1 -1
  125. package/dist/terrain/regional-watersheds.d.ts +44 -0
  126. package/dist/terrain/regional-watersheds.d.ts.map +1 -0
  127. package/dist/terrain/regional-watersheds.js +108 -0
  128. package/dist/terrain/regional-watersheds.js.map +1 -0
  129. package/dist/terrain/tile-cutout-update.d.ts.map +1 -1
  130. package/dist/terrain/tile-cutout-update.js +28 -22
  131. package/dist/terrain/tile-cutout-update.js.map +1 -1
  132. package/dist/turf-functions.cjs +16 -9
  133. package/dist/turf-functions.cjs.map +1 -1
  134. package/package.json +27 -6
  135. package/src/coverage-simplification.ts +215 -0
  136. package/src/line-network/build-line-network.ts +990 -0
  137. package/src/line-network/line-network-routes.ts +213 -0
  138. package/src/line-network/line-network-weights.ts +40 -0
  139. package/src/line-network.ts +29 -0
  140. package/src/line-simplification.ts +4 -16
  141. package/src/linear-referencing.ts +27 -40
  142. package/src/measure-referencing.ts +165 -0
  143. package/src/polygon-simplification.ts +146 -0
  144. package/src/route-editing.ts +107 -0
  145. package/src/spatial-box-clipping.ts +140 -0
  146. package/src/spatial-dwithin.ts +135 -0
  147. package/src/spatial-hulls.ts +203 -0
  148. package/src/spatial-line-intersection-result.ts +93 -0
  149. package/src/spatial-line-polygon-intersection.ts +152 -0
  150. package/src/spatial-planar-intersects.ts +10 -51
  151. package/src/spatial-planar.ts +20 -1
  152. package/src/spatial-polygon-overlay.ts +30 -0
  153. package/src/spatial-sql.ts +97 -13
  154. package/src/spatial-subdivision.ts +75 -0
  155. package/src/spatial-touches.ts +204 -0
  156. package/src/st-coverage-functions.ts +88 -0
  157. package/src/st-functions.ts +345 -26
  158. package/src/st-geodesic-functions.ts +313 -0
  159. package/src/st-hull-functions.ts +25 -0
  160. package/src/st-overlay-functions.ts +313 -0
  161. package/src/st-viewport-functions.ts +98 -0
  162. package/src/terrain/index.ts +7 -0
  163. package/src/terrain/regional-watersheds.ts +153 -0
  164. 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,
@@ -25,12 +26,18 @@ import {
25
26
  writeWKB,
26
27
  type WellKnownGeometry
27
28
  } from '@math.gl/core/arrow';
29
+ import {getPolygonValidityReason} from '@math.gl/geometry/polygon';
28
30
  import {planarIntersects} from './spatial-planar-intersects';
29
- import {lineLocatePoint, lineSubstring} from './linear-referencing';
31
+ import {planarTouches} from './spatial-touches';
32
+ import {linesWithinDistance, pointWithinPolygonDistance} from './spatial-dwithin';
33
+ import {mergeRoute, routeEditingFunction, segmentizeFunction} from './route-editing';
34
+ import {lineLocatePoint, lineSubstring, projectPointToLine} from './linear-referencing';
35
+ import {addMeasure, locateAlong, locateBetween, measuredDimension} from './measure-referencing';
30
36
  import {normalizeCRSIdentifier} from './crs/crs-identifier';
31
37
  import {EPSG3857ToLngLat, EPSG4326ToEPSG3857} from './web-mercator/mercator-meters';
32
38
  import {vertexCount, memberCount, geometryMember} from './geometry-inspection';
33
39
  import {reduceLineVertices} from './line-simplification';
40
+ import {simplifyPolygonTopology} from './polygon-simplification';
34
41
  import {collectPair, makeLine} from './geometry-constructors';
35
42
  import {EPSG3857_HALF_CIRCUMFERENCE} from './web-mercator/mercator-meters';
36
43
  import {
@@ -48,6 +55,8 @@ import {
48
55
  EXTENSION_NAME,
49
56
  geometryTypes,
50
57
  lineKinds,
58
+ nativeGeometryResult,
59
+ nativePointVector,
51
60
  planarGeometry,
52
61
  pointCoordinates,
53
62
  pointInPolygon,
@@ -63,7 +72,7 @@ export const ST_MAKEPOINT: ScalarFunction = {
63
72
  kind: 'scalar',
64
73
  name: 'ST_MakePoint',
65
74
  prototypes: [],
66
- /** Binds two to four numeric ordinate fields and declares a WKB result. */
75
+ /** Binds two to four numeric ordinates to a native GeoArrow point. */
67
76
  resolvePrototype(fields) {
68
77
  if (fields.length < 2 || fields.length > 4) return undefined;
69
78
  const arguments_ = fields.map(field => {
@@ -71,20 +80,29 @@ export const ST_MAKEPOINT: ScalarFunction = {
71
80
  throw new Error('ST_MakePoint requires numeric coordinates');
72
81
  return field ?? new Field('coordinate', new Float64(), true);
73
82
  });
74
- const type = new Binary();
83
+ const dimension = fields.length === 2 ? 'xy' : fields.length === 3 ? 'xyz' : 'xyzm';
84
+ const coordinateType = new Float64();
85
+ const size = fields.length;
86
+ const type = new FixedSizeList(size, new Field(dimension, coordinateType, true));
75
87
  return {
76
88
  arguments: arguments_,
77
- result: new Field('ST_MakePoint', type, true, new Map([[EXTENSION_NAME, 'geoarrow.wkb']])),
78
- /** Writes one point per active row, retaining nulls in all other rows. */
89
+ result: new Field('ST_MakePoint', type, true, new Map([[EXTENSION_NAME, 'geoarrow.point']])),
90
+ /** Writes active rows directly to the interleaved coordinate buffer. */
79
91
  evaluate(vectors, {activeRows, length}) {
80
- const values = new Array<Uint8Array | null>(length).fill(null);
92
+ const data = new Float64Array(length * size);
93
+ const nullBitmap = new Uint8Array(Math.ceil(length / 8));
94
+ let nullCount = 0;
81
95
  for (let row = 0; row < length; row++) {
82
- if (activeRows[row]) {
83
- const coordinates = vectors.map(vector => Number(vector.get(row)));
84
- values[row] = writeWKB({type: 'Point', coordinates});
96
+ if (!activeRows[row]) {
97
+ nullCount++;
98
+ continue;
99
+ }
100
+ nullBitmap[row >> 3]! |= 1 << (row & 7);
101
+ for (let coordinate = 0; coordinate < size; coordinate++) {
102
+ data[row * size + coordinate] = Number(vectors[coordinate]!.get(row));
85
103
  }
86
104
  }
87
- return vectorFromArray(values, type);
105
+ return nativePointVector(type, data, nullBitmap, nullCount);
88
106
  }
89
107
  };
90
108
  }
@@ -359,20 +377,112 @@ export const ST_DISTANCE = /* @__PURE__ */ spatialFunction(
359
377
  }
360
378
  );
361
379
 
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
380
+ /** Geometry arguments supported by the route-snapping functions. */
381
+ const routePointKinds = ['Point', 'LineString'] as const;
382
+
383
+ /** Returns the closest pair in argument order for point/point and point/line inputs. */
384
+ function closestPair(a: WellKnownGeometry, b: WellKnownGeometry) {
385
+ if (a.type === 'LineString' && b.type === 'LineString') {
386
+ throw new Error('Route snapping requires at least one Point');
387
+ }
388
+ if (a.type === 'LineString' || b.type === 'LineString') {
389
+ const lineFirst = a.type === 'LineString';
390
+ const point = lineFirst ? b : a;
391
+ const xy = pointCoordinates(point);
392
+ const projection = projectPointToLine(lineFirst ? a : b, point);
393
+ if (!xy || !projection) return null;
394
+ const {coordinate, distance} = projection;
395
+ return {start: lineFirst ? coordinate : xy, end: lineFirst ? xy : coordinate, distance};
396
+ }
397
+ const start = pointCoordinates(a);
398
+ const end = pointCoordinates(b);
399
+ return start && end
400
+ ? {start, end, distance: Math.hypot(end[0]! - start[0]!, end[1]! - start[1]!)}
401
+ : null;
402
+ }
403
+
404
+ /** Returns the closest point on the first geometry as a native GeoArrow point. */
405
+ export const ST_CLOSESTPOINT = /* @__PURE__ */ spatialFunction(
406
+ 'ST_ClosestPoint',
407
+ [routePointKinds, routePointKinds],
408
+ /* @__PURE__ */ nativeGeometryResult('geoarrow.point'),
409
+ ([a, b]) => {
410
+ const pair = closestPair(a!, b!);
411
+ return pair ? {type: 'Point', coordinates: pair.start} : null;
412
+ }
374
413
  );
375
414
 
415
+ /** Returns the shortest segment from the first geometry to the second as a native GeoArrow line. */
416
+ export const ST_SHORTESTLINE = /* @__PURE__ */ spatialFunction(
417
+ 'ST_ShortestLine',
418
+ [routePointKinds, routePointKinds],
419
+ /* @__PURE__ */ nativeGeometryResult('geoarrow.linestring'),
420
+ ([a, b]) => {
421
+ const pair = closestPair(a!, b!);
422
+ return pair ? {type: 'LineString', coordinates: [pair.start, pair.end]} : null;
423
+ }
424
+ );
425
+
426
+ /** Geometry kinds accepted by the supported proximity pairings. */
427
+ const proximityKinds = [...pointKinds, ...lineKinds, ...polygonKinds];
428
+
429
+ /** Dispatches to the exact planar kernel for the two input geometry types. */
430
+ function withinDistance(a: WellKnownGeometry, b: WellKnownGeometry, radius: number): boolean {
431
+ if (radius < 0) throw new Error('ST_DWithin distance must be nonnegative');
432
+ if (Number.isNaN(radius)) return false;
433
+ if (a.type === 'Point' && (b.type === 'Polygon' || b.type === 'MultiPolygon'))
434
+ return pointWithinPolygonDistance(a, b, radius);
435
+ if (b.type === 'Point' && (a.type === 'Polygon' || a.type === 'MultiPolygon'))
436
+ return pointWithinPolygonDistance(b, a, radius);
437
+ if (
438
+ (a.type === 'LineString' || a.type === 'MultiLineString') &&
439
+ (b.type === 'LineString' || b.type === 'MultiLineString')
440
+ )
441
+ return linesWithinDistance(a, b, radius);
442
+ if (
443
+ (a.type === 'Point' && (b.type === 'Point' || b.type === 'LineString')) ||
444
+ (b.type === 'Point' && a.type === 'LineString')
445
+ ) {
446
+ const pair = closestPair(a, b);
447
+ return pair !== null && pair.distance <= radius;
448
+ }
449
+ throw new Error(`ST_DWithin does not support ${a.type} and ${b.type}`);
450
+ }
451
+
452
+ /** Binds supported combinations when both input encodings identify their geometry kinds. */
453
+ function makeDWithin(): ScalarFunction {
454
+ const spatial = spatialFunction(
455
+ 'ST_DWithin',
456
+ [proximityKinds, proximityKinds],
457
+ new Bool(),
458
+ ([a, b], radius) => withinDistance(a!, b!, radius!),
459
+ 1
460
+ );
461
+ return {
462
+ ...spatial,
463
+ /** Checks concrete native pairs at binding; WKB pairs are checked per row. */
464
+ resolvePrototype(fields) {
465
+ const result = spatial.resolvePrototype!(fields);
466
+ const a = fields[0]?.metadata.get(EXTENSION_NAME);
467
+ const b = fields[1]?.metadata.get(EXTENSION_NAME);
468
+ if (result && a && b && a !== 'geoarrow.wkb' && b !== 'geoarrow.wkb') {
469
+ /** Identifies either concrete native line encoding. */
470
+ const linear = (encoding: string) =>
471
+ encoding === 'geoarrow.linestring' || encoding === 'geoarrow.multilinestring';
472
+ const pointPair =
473
+ (a === 'geoarrow.point' && b !== 'geoarrow.multilinestring') ||
474
+ (b === 'geoarrow.point' && a !== 'geoarrow.multilinestring');
475
+ if (!pointPair && !(linear(a) && linear(b)))
476
+ throw new Error(`ST_DWithin does not support ${a} and ${b}`);
477
+ }
478
+ return result;
479
+ }
480
+ };
481
+ }
482
+
483
+ /** Tests an inclusive planar radius in the coordinate units of the geometries. */
484
+ export const ST_DWITHIN = /* @__PURE__ */ makeDWithin();
485
+
376
486
  /** Tests whether two planar geometries share any point, including their boundaries. */
377
487
  export const ST_INTERSECTS = /* @__PURE__ */ spatialFunction(
378
488
  'ST_Intersects',
@@ -384,6 +494,20 @@ export const ST_INTERSECTS = /* @__PURE__ */ spatialFunction(
384
494
  ([a, b]) => planarIntersects(a!, b!)
385
495
  );
386
496
 
497
+ /** Tests boundary contact while excluding intersections between geometry interiors. */
498
+ export const ST_TOUCHES = /* @__PURE__ */ spatialFunction(
499
+ 'ST_Touches',
500
+ [geometryTypes, geometryTypes],
501
+ /* @__PURE__ */ new Bool(),
502
+ ([a, b]) => planarTouches(a!, b!)
503
+ );
504
+
505
+ /** Joins planar line components through exact degree-two endpoints. */
506
+ export const ST_LINEMERGE = /* @__PURE__ */ routeEditingFunction('ST_LineMerge', false, mergeRoute);
507
+
508
+ /** Splits planar segments evenly to meet the maximum length in CRS units. */
509
+ export const ST_SEGMENTIZE = /* @__PURE__ */ segmentizeFunction('ST_Segmentize');
510
+
387
511
  /** Interpolates a point at a fraction of the planar length. */
388
512
  export const ST_LINEINTERPOLATEPOINT = /* @__PURE__ */ spatialFunction(
389
513
  'ST_LineInterpolatePoint',
@@ -414,6 +538,45 @@ export const ST_LINESUBSTRING = /* @__PURE__ */ spatialFunction(
414
538
  2
415
539
  );
416
540
 
541
+ /** Interpolates a new M ordinate by cumulative XY length while retaining Z. */
542
+ export const ST_ADDMEASURE = /* @__PURE__ */ spatialFunction(
543
+ 'ST_AddMeasure',
544
+ [lineKinds],
545
+ /* @__PURE__ */ new Binary(),
546
+ ([line], start, end) => {
547
+ const result = addMeasure(line!, line!.dimension!, start!, end!);
548
+ return writeWKB(result.geometry, result.dimension);
549
+ },
550
+ 2,
551
+ true
552
+ );
553
+
554
+ /** Returns the points on a measured route that match the requested measure. */
555
+ export const ST_LOCATEALONG = /* @__PURE__ */ spatialFunction(
556
+ 'ST_LocateAlong',
557
+ [lineKinds],
558
+ /* @__PURE__ */ new Binary(),
559
+ ([line], measure) => {
560
+ const dimension = measuredDimension(line!.dimension!);
561
+ return writeWKB(locateAlong(line!, dimension, measure!), dimension);
562
+ },
563
+ 1,
564
+ true
565
+ );
566
+
567
+ /** Returns the portions of a measured route within an inclusive measure interval. */
568
+ export const ST_LOCATEBETWEEN = /* @__PURE__ */ spatialFunction(
569
+ 'ST_LocateBetween',
570
+ [lineKinds],
571
+ /* @__PURE__ */ new Binary(),
572
+ ([line], start, end) => {
573
+ const dimension = measuredDimension(line!.dimension!);
574
+ return writeWKB(locateBetween(line!, dimension, start!, end!), dimension);
575
+ },
576
+ 2,
577
+ true
578
+ );
579
+
417
580
  /** Extracts an EPSG code from a GeoArrow authority identifier or a PROJJSON root ID. */
418
581
  function geometrySRID(crs: unknown): number {
419
582
  let identifier = typeof crs === 'string' ? normalizeCRSIdentifier(crs) : '';
@@ -524,6 +687,90 @@ function crsFunction(name: 'ST_SRID' | 'ST_SetSRID' | 'ST_Transform'): ScalarFun
524
687
  };
525
688
  }
526
689
 
690
+ /** A synchronous projection engine with the requested CRS definitions available. */
691
+ export type SpatialSQLProjectionEngine = {
692
+ /** Prepares a transform in GeoArrow XY axis order. */
693
+ createProjection(options: {from: string | object; to: string; enforceAxis: false}): {
694
+ /** Projects one horizontal coordinate. */
695
+ projectSync(coordinate: readonly number[]): readonly number[];
696
+ };
697
+ };
698
+
699
+ /** Creates an ST_Transform overload backed by a supplied CRS projection engine. */
700
+ export function createSTTransform(engine: SpatialSQLProjectionEngine): ScalarFunction {
701
+ return {
702
+ kind: 'scalar',
703
+ name: 'ST_Transform',
704
+ prototypes: [],
705
+ /** Resolves one source and target CRS for the entire projection. */
706
+ resolvePrototype(fields, literals) {
707
+ if (fields.length !== 2 || !fields[0]) return undefined;
708
+ const field = fields[0];
709
+ const target = literals?.[1];
710
+ if (target === undefined || !Number.isSafeInteger(target) || target <= 0 || target > 998999)
711
+ throw new Error('ST_Transform requires a constant positive integer EPSG code');
712
+ const info = getGeoArrowFieldInfo(field);
713
+ if (!info) throw new Error('ST_Transform requires a GeoArrow geometry column');
714
+ if (info.metadata['edges'] === 'spherical')
715
+ throw new Error('ST_Transform requires planar geometry');
716
+ const source = info.metadata['crs'];
717
+ if (
718
+ !(typeof source === 'string'
719
+ ? source.trim()
720
+ : source && typeof source === 'object' && !Array.isArray(source))
721
+ )
722
+ throw new Error('ST_Transform requires source CRS metadata');
723
+ const to = 'EPSG:' + target;
724
+ const transform = engine.createProjection({
725
+ from: source as string | object,
726
+ to,
727
+ enforceAxis: false
728
+ });
729
+ const metadata = JSON.stringify({...info.metadata, crs: to, crs_type: 'authority_code'});
730
+ const targetField = fields[1] ?? new Field('srid', new Int32(), false);
731
+ if (!DataType.isInt(targetField.type) && !DataType.isFloat(targetField.type))
732
+ throw new Error('ST_Transform requires a numeric SRID');
733
+
734
+ /** Projects XY and retains additional ordinates, including the empty-point sentinel. */
735
+ const project = (coordinate: readonly number[]): number[] => {
736
+ if (coordinate.every(Number.isNaN)) return [...coordinate];
737
+ const [x, y] = transform.projectSync([coordinate[0]!, coordinate[1]!]);
738
+ if (!Number.isFinite(x) || !Number.isFinite(y))
739
+ throw new Error('ST_Transform produced nonfinite coordinates');
740
+ return [x!, y!, ...coordinate.slice(2)];
741
+ };
742
+
743
+ if (info.encoding === 'geoarrow.wkb') {
744
+ const prototype = spatialFunction(
745
+ 'ST_Transform',
746
+ [[...geometryTypes, 'GeometryCollection']],
747
+ new Binary(),
748
+ ([geometry]) => writeWKB(projectGeometry(geometry!, project), 'xy'),
749
+ 1
750
+ ).resolvePrototype!(fields)!;
751
+ prototype.result.metadata.set(EXTENSION_METADATA, metadata);
752
+ return prototype;
753
+ }
754
+ if (!geometryTypes.some(kind => 'geoarrow.' + kind.toLowerCase() === info.encoding))
755
+ throw new Error('ST_Transform requires WKB or a specialized native GeoArrow encoding');
756
+ const outputMetadata = new Map(field.metadata);
757
+ outputMetadata.set(EXTENSION_METADATA, metadata);
758
+ return {
759
+ arguments: [field, targetField],
760
+ result: new Field('ST_Transform', field.type, field.nullable, outputMetadata),
761
+ /** Maps native coordinate buffers directly, retaining topology and null rows. */
762
+ evaluate([vector], {activeRows}) {
763
+ return mapGeoArrowVectorCoordinates(
764
+ vector!,
765
+ (coordinate, row) => (activeRows[row] ? project(coordinate) : coordinate),
766
+ {field}
767
+ );
768
+ }
769
+ };
770
+ }
771
+ };
772
+ }
773
+
527
774
  /** Returns the field EPSG code, or zero for an unidentified CRS. */
528
775
  export const ST_SRID = /* @__PURE__ */ crsFunction('ST_SRID');
529
776
  /** Assigns a CRS to a geometry column, preserving its coordinate values. */
@@ -562,6 +809,31 @@ export const ST_REMOVEREPEATEDPOINTS = /* @__PURE__ */ lineReduction(
562
809
  /** Reduces route vertices with Douglas-Peucker simplification. */
563
810
  export const ST_SIMPLIFY = /* @__PURE__ */ lineReduction('ST_Simplify', 'simplify');
564
811
 
812
+ /** Simplifies polygon boundaries while retaining shells, holes, and component topology. */
813
+ export const ST_SIMPLIFYPRESERVETOPOLOGY: ScalarFunction = {
814
+ kind: 'scalar',
815
+ name: 'ST_SimplifyPreserveTopology',
816
+ prototypes: [],
817
+ /** Preserves specialized native storage where the input schema identifies its family. */
818
+ resolvePrototype(fields) {
819
+ const encoding = fields[0]?.metadata.get(EXTENSION_NAME);
820
+ const native = encoding === 'geoarrow.polygon' || encoding === 'geoarrow.multipolygon';
821
+ return spatialFunction(
822
+ 'ST_SimplifyPreserveTopology',
823
+ [polygonKinds],
824
+ native ? nativeGeometryResult(encoding) : new Binary(),
825
+ ([geometry], tolerance) => {
826
+ const result = simplifyPolygonTopology(
827
+ geometry as Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>,
828
+ tolerance!
829
+ );
830
+ return native ? result : writeWKB(result, 'xy');
831
+ },
832
+ 1
833
+ ).resolvePrototype!(fields);
834
+ }
835
+ };
836
+
565
837
  /** Returns clockwise azimuth from positive Y in radians. */
566
838
  export const ST_AZIMUTH = /* @__PURE__ */ spatialFunction(
567
839
  'ST_Azimuth',
@@ -587,10 +859,10 @@ export const ST_AREA = /* @__PURE__ */ spatialFunction(
587
859
  ([geometry]) => planarArea(geometry!)
588
860
  );
589
861
 
590
- /** Returns planar linear length, or zero for non-linear input. */
862
+ /** Returns the summed planar length of linear components. */
591
863
  export const ST_LENGTH = /* @__PURE__ */ spatialFunction(
592
864
  'ST_Length',
593
- [geometryTypes],
865
+ [[...geometryTypes, 'GeometryCollection']],
594
866
  /* @__PURE__ */ new Float64(),
595
867
  ([geometry]) => planarLength(geometry!)
596
868
  );
@@ -694,12 +966,39 @@ export const ST_COLLECT = /* @__PURE__ */ spatialFunction(
694
966
  true
695
967
  );
696
968
 
969
+ /** Adapts a SQL polygon value to the coordinate-space validity kernel. */
970
+ function polygonValidityReason(geometry: WellKnownGeometry): string | undefined {
971
+ if (geometry.type === 'Polygon') return getPolygonValidityReason([geometry.coordinates]);
972
+ if (geometry.type === 'MultiPolygon') return getPolygonValidityReason(geometry.coordinates);
973
+ throw new Error('Expected polygonal geometry');
974
+ }
975
+
976
+ /** Checks Polygon or MultiPolygon validity in XY. */
977
+ export const ST_ISVALID = /* @__PURE__ */ spatialFunction(
978
+ 'ST_IsValid',
979
+ [polygonKinds],
980
+ /* @__PURE__ */ new Bool(),
981
+ ([polygon]) => polygonValidityReason(polygon!) === undefined,
982
+ 0,
983
+ true
984
+ );
985
+
986
+ /** Returns the first XY polygon validity diagnostic, or Valid Geometry. */
987
+ export const ST_ISVALIDREASON = /* @__PURE__ */ spatialFunction(
988
+ 'ST_IsValidReason',
989
+ [polygonKinds],
990
+ /* @__PURE__ */ new Utf8(),
991
+ ([polygon]) => polygonValidityReason(polygon!) ?? 'Valid Geometry',
992
+ 0,
993
+ true
994
+ );
995
+
697
996
  /** Returns an interior label point for a planar polygonal surface. */
698
997
  export const ST_POINTONSURFACE = /* @__PURE__ */ spatialFunction(
699
998
  'ST_PointOnSurface',
700
999
  [polygonKinds],
701
- /* @__PURE__ */ new Binary(),
702
- ([polygon]) => writeWKB(planarPolygonPointOnSurface(polygon!), 'xy')
1000
+ /* @__PURE__ */ nativeGeometryResult('geoarrow.point'),
1001
+ ([polygon]) => planarPolygonPointOnSurface(polygon!)
703
1002
  );
704
1003
 
705
1004
  /** Binds the WKB fallback and native row-bounds implementation. */
@@ -823,21 +1122,32 @@ export const POSTGIS_SPATIAL_FUNCTIONS: readonly (ArrowTableFunction | Aggregate
823
1122
  ST_MAKEENVELOPE,
824
1123
  ST_DISTANCE,
825
1124
  ST_DWITHIN,
1125
+ ST_CLOSESTPOINT,
1126
+ ST_SHORTESTLINE,
826
1127
  ST_INTERSECTS,
1128
+ ST_TOUCHES,
827
1129
  ST_SRID,
828
1130
  ST_SETSRID,
829
1131
  ST_TRANSFORM,
830
1132
  ST_REMOVEREPEATEDPOINTS,
831
1133
  ST_SIMPLIFY,
1134
+ ST_SIMPLIFYPRESERVETOPOLOGY,
832
1135
  ST_AZIMUTH,
833
1136
  ST_MAKEPOINT,
834
1137
  ST_TILEENVELOPE,
835
1138
  ST_AREA,
836
1139
  ST_LENGTH,
1140
+ ST_LINEMERGE,
1141
+ ST_SEGMENTIZE,
837
1142
  ST_LINEINTERPOLATEPOINT,
838
1143
  ST_LINELOCATEPOINT,
839
1144
  ST_LINESUBSTRING,
1145
+ ST_ADDMEASURE,
1146
+ ST_LOCATEALONG,
1147
+ ST_LOCATEBETWEEN,
840
1148
  ST_POINTONSURFACE,
1149
+ ST_ISVALID,
1150
+ ST_ISVALIDREASON,
841
1151
  ST_CENTROID,
842
1152
  ST_NPOINTS,
843
1153
  ST_NUMGEOMETRIES,
@@ -850,3 +1160,12 @@ export const POSTGIS_SPATIAL_FUNCTIONS: readonly (ArrowTableFunction | Aggregate
850
1160
  ST_ENVELOPE,
851
1161
  ST_EXTENT
852
1162
  ]);
1163
+
1164
+ export {
1165
+ ST_GEODESICDISTANCE,
1166
+ ST_GEODESICLENGTH,
1167
+ ST_GEODESICDWITHIN,
1168
+ ST_GEODESICSEGMENTIZE,
1169
+ GEODESIC_SPATIAL_FUNCTIONS
1170
+ } from './st-geodesic-functions';
1171
+ export {createSTCoverageSimplify, type CoverageSimplifyOptions} from './st-coverage-functions';