@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
@@ -0,0 +1,215 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {WellKnownGeometry} from '@math.gl/core/arrow';
6
+ import {BoxIndex} from '@math.gl/geometry/spatial-index';
7
+ import {orient2d} from 'robust-predicates';
8
+ import {distanceToPlanarSegment} from './spatial-planar';
9
+ import {pointOnSegment, segmentsMeet} from './spatial-planar-intersects';
10
+
11
+ /** Polygonal feature in the input coverage. */
12
+ type Area = Extract<WellKnownGeometry, {type: 'Polygon' | 'MultiPolygon'}>;
13
+ /** A planar coordinate. */
14
+ type Point = readonly number[];
15
+ /** One closed polygon ring without its repeated endpoint. */
16
+ type Ring = {
17
+ /** Input feature row. */
18
+ row: number;
19
+ /** Vertices excluding the closing copy. */
20
+ points: readonly Point[];
21
+ /** Selected vertex positions to omit from this ring. */
22
+ removed: Set<number>;
23
+ };
24
+ /** A ring vertex occurrence. */
25
+ type Occurrence = {
26
+ /** Owning ring. */
27
+ ring: Ring;
28
+ /** Index in that ring. */
29
+ index: number;
30
+ };
31
+ /** Two endpoints of a boundary segment. */
32
+ type Edge = readonly [Point, Point];
33
+ /** Candidate removal shared by two feature rings. */
34
+ type Candidate = {
35
+ /** Shared vertex and its neighbors. */
36
+ triangle: readonly [Point, Point, Point];
37
+ /** Occurrences removed together. */
38
+ pair: readonly [Occurrence, Occurrence];
39
+ };
40
+
41
+ /** Keys identical XY locations, including equivalent signed zeros. */
42
+ function key(point: Point): string {
43
+ return `${point[0]}/${point[1]}`;
44
+ }
45
+
46
+ /** Compares the horizontal coordinates. */
47
+ function same(a: Point, b: Point): boolean {
48
+ return a[0] === b[0] && a[1] === b[1];
49
+ }
50
+
51
+ /** Packs one two-dimensional extent for the box index. */
52
+ function bounds(points: readonly Point[]): [number, number, number, number] {
53
+ let x0 = Infinity,
54
+ y0 = Infinity,
55
+ x1 = -Infinity,
56
+ y1 = -Infinity;
57
+ for (const [x, y] of points) {
58
+ x0 = Math.min(x0, x!);
59
+ y0 = Math.min(y0, y!);
60
+ x1 = Math.max(x1, x!);
61
+ y1 = Math.max(y1, y!);
62
+ }
63
+ return [x0, y0, x1, y1];
64
+ }
65
+
66
+ /** Finds segments and edits with overlapping envelopes. */
67
+ function search(index: BoxIndex, points: readonly Point[]): number[] {
68
+ const [x0, y0, x1, y1] = bounds(points);
69
+ return index.search([x0, y0], [x1, y1]);
70
+ }
71
+
72
+ /** Returns the signed orientation of a point relative to an edge. */
73
+ function side(a: Point, b: Point, c: Point): number {
74
+ return Math.sign(orient2d(a[0]!, a[1]!, b[0]!, b[1]!, c[0]!, c[1]!));
75
+ }
76
+
77
+ /** Tests an endpoint inside a nondegenerate edit triangle. */
78
+ function strictlyInside(p: Point, [a, b, c]: Candidate['triangle']): boolean {
79
+ const x = side(a, b, p);
80
+ return x !== 0 && x === side(b, c, p) && x === side(c, a, p);
81
+ }
82
+
83
+ /** Detects segment contact beyond a shared endpoint. */
84
+ function conflicts([a, b]: Edge, [c, d]: Edge): boolean {
85
+ if (!segmentsMeet(a, b, c, d)) return false;
86
+ if (!(same(a, c) || same(a, d) || same(b, c) || same(b, d))) return true;
87
+ return (
88
+ (!same(a, c) && !same(a, d) && pointOnSegment(a, c, d)) ||
89
+ (!same(b, c) && !same(b, d) && pointOnSegment(b, c, d)) ||
90
+ (!same(c, a) && !same(c, b) && pointOnSegment(c, a, b)) ||
91
+ (!same(d, a) && !same(d, b) && pointOnSegment(d, a, b)) ||
92
+ (same(a, c) && same(b, d)) ||
93
+ (same(a, d) && same(b, c))
94
+ );
95
+ }
96
+
97
+ /** Rejects edges entering the swept triangle or touching a replacement chord. */
98
+ function obstructs(edge: Edge, triangle: Candidate['triangle']): boolean {
99
+ const [a, b, c] = triangle;
100
+ const [x, y] = edge;
101
+ if ((same(x, b) && (same(y, a) || same(y, c))) || (same(y, b) && (same(x, a) || same(x, c))))
102
+ return false;
103
+ // A collinear edit sweeps no interior. Test the new chord and retain junctions.
104
+ return (
105
+ strictlyInside(x, triangle) ||
106
+ strictlyInside(y, triangle) ||
107
+ conflicts(edge, [a, b]) ||
108
+ conflicts(edge, [b, c]) ||
109
+ conflicts(edge, [a, c])
110
+ );
111
+ }
112
+
113
+ /** Reads the cyclic neighbor on either side of a ring vertex. */
114
+ function neighbor({ring, index}: Occurrence, delta: number): Point {
115
+ return ring.points[(index + delta + ring.points.length) % ring.points.length]!;
116
+ }
117
+
118
+ /** Finds a shared degree-two boundary vertex eligible for one edit. */
119
+ function candidate(owners: Occurrence[], tolerance: number): Candidate | undefined {
120
+ if (owners.length !== 2) return undefined;
121
+ const [left, right] = owners as [Occurrence, Occurrence];
122
+ if (
123
+ left.ring.row === right.ring.row ||
124
+ left.ring.points.length <= 3 ||
125
+ right.ring.points.length <= 3
126
+ )
127
+ return undefined;
128
+ const a = neighbor(left, -1);
129
+ const b = neighbor(left, 0);
130
+ const c = neighbor(left, 1);
131
+ const x = neighbor(right, -1);
132
+ const y = neighbor(right, 1);
133
+ if (same(a, c) || !((same(a, x) && same(c, y)) || (same(a, y) && same(c, x)))) return undefined;
134
+ if (!(distanceToPlanarSegment(b, a, c) <= tolerance)) return undefined;
135
+ return {triangle: [a, b, c], pair: [left, right]};
136
+ }
137
+
138
+ /** Simplifies matched shared boundaries in a valid, edge-matched planar coverage. */
139
+ export function simplifyCoverage(
140
+ rows: readonly (Area | null)[],
141
+ tolerance: number,
142
+ signal?: AbortSignal
143
+ ): readonly (Area | null)[] {
144
+ if (!tolerance) return rows;
145
+ const occurrences = new Map<string, Occurrence[]>();
146
+ const boundaries: Edge[] = [];
147
+ const shapes = rows.map((geometry, row) => {
148
+ signal?.throwIfAborted();
149
+ if (!geometry) return null;
150
+ const polygons = geometry.type === 'Polygon' ? [geometry.coordinates] : geometry.coordinates;
151
+ return polygons.map(polygon =>
152
+ polygon.map(closed => {
153
+ if (closed.length < 4 || !same(closed[0]!, closed[closed.length - 1]!))
154
+ throw new Error('ST_CoverageSimplify requires closed polygon rings');
155
+ const points = closed.slice(0, -1);
156
+ const ring: Ring = {row, points, removed: new Set()};
157
+ points.forEach((point, index) => {
158
+ if (!Number.isFinite(point[0]) || !Number.isFinite(point[1]))
159
+ throw new Error('ST_CoverageSimplify requires finite coordinates');
160
+ const id = key(point);
161
+ const previous = occurrences.get(id);
162
+ const owner = {ring, index};
163
+ if (previous) previous.push(owner);
164
+ else occurrences.set(id, [owner]);
165
+ boundaries.push([point, points[(index + 1) % points.length]!]);
166
+ });
167
+ return ring;
168
+ })
169
+ );
170
+ });
171
+ const candidates = [...occurrences.values()].flatMap(owners => {
172
+ const edit = candidate(owners, tolerance);
173
+ return edit ? [edit] : [];
174
+ });
175
+ if (!candidates.length) return rows;
176
+
177
+ const original = new BoxIndex({dimension: 2, bounds: boundaries.flatMap(bounds)});
178
+ const edits = new BoxIndex({
179
+ dimension: 2,
180
+ bounds: candidates.flatMap(edit => bounds(edit.triangle))
181
+ });
182
+ const accepted = new Set<number>();
183
+ const protectedVertices = new Set<string>();
184
+ candidates.forEach(({triangle, pair}, index) => {
185
+ signal?.throwIfAborted();
186
+ const [a, b, c] = triangle;
187
+ if (
188
+ protectedVertices.has(key(b)) ||
189
+ pair.some(({ring}) => ring.points.length - ring.removed.size <= 3)
190
+ )
191
+ return;
192
+ if (search(original, triangle).some(hit => obstructs(boundaries[hit]!, triangle))) return;
193
+ // Keep swept regions disjoint so simultaneous edits cannot create a crossing.
194
+ if (search(edits, triangle).some(hit => accepted.has(hit))) return;
195
+ accepted.add(index);
196
+ protectedVertices.add(key(a));
197
+ protectedVertices.add(key(c));
198
+ for (const {ring, index: position} of pair) ring.removed.add(position);
199
+ });
200
+
201
+ if (!accepted.size) return rows;
202
+ return shapes.map((shape, row) => {
203
+ const geometry = rows[row];
204
+ if (!geometry || !shape) return null;
205
+ const coordinates = shape.map(polygon =>
206
+ polygon.map(({points, removed}) => {
207
+ const path = points.filter((_, index) => !removed.has(index));
208
+ return [...path, path[0]!];
209
+ })
210
+ );
211
+ return geometry.type === 'Polygon'
212
+ ? {type: 'Polygon', coordinates: coordinates[0]!}
213
+ : {type: 'MultiPolygon', coordinates};
214
+ });
215
+ }