@mailwoman/spatial 10.0.0 → 10.1.0

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 (145) hide show
  1. package/README.md +2 -2
  2. package/lib/bbox.ts +41 -34
  3. package/lib/coordinate/bounds.ts +2 -2
  4. package/lib/coordinate/formats.ts +10 -27
  5. package/lib/distance.ts +53 -27
  6. package/lib/feature.ts +2 -2
  7. package/lib/geometries/point.ts +41 -33
  8. package/lib/geometries/polygon.ts +24 -128
  9. package/lib/geometries/ring-blob.ts +33 -130
  10. package/lib/google/place-id.ts +4 -2
  11. package/lib/h3/cell.ts +21 -76
  12. package/lib/h3/coverage.ts +11 -7
  13. package/lib/h3/interior-cells.ts +13 -34
  14. package/lib/h3/polygon-cells.ts +59 -165
  15. package/lib/{h3/index.ts → h3.ts} +1 -1
  16. package/lib/index.ts +5 -5
  17. package/lib/objects.ts +6 -6
  18. package/lib/open-location-code.ts +11 -42
  19. package/lib/osgb36.ts +28 -187
  20. package/lib/polyline.ts +5 -3
  21. package/lib/position.ts +18 -2
  22. package/lib/projection/transform.ts +39 -40
  23. package/lib/{projection/index.ts → projection.ts} +1 -1
  24. package/lib/well-known-text.ts +8 -8
  25. package/out/bbox.d.ts +17 -14
  26. package/out/bbox.d.ts.map +1 -1
  27. package/out/bbox.js +32 -23
  28. package/out/bbox.js.map +1 -1
  29. package/out/coordinate/bounds.d.ts +2 -2
  30. package/out/coordinate/bounds.js +2 -2
  31. package/out/coordinate/formats.d.ts +8 -11
  32. package/out/coordinate/formats.d.ts.map +1 -1
  33. package/out/coordinate/formats.js +10 -27
  34. package/out/coordinate/formats.js.map +1 -1
  35. package/out/distance.d.ts +48 -25
  36. package/out/distance.d.ts.map +1 -1
  37. package/out/distance.js +53 -27
  38. package/out/distance.js.map +1 -1
  39. package/out/feature.d.ts +2 -2
  40. package/out/feature.d.ts.map +1 -1
  41. package/out/geometries/point.d.ts +30 -22
  42. package/out/geometries/point.d.ts.map +1 -1
  43. package/out/geometries/point.js +35 -29
  44. package/out/geometries/point.js.map +1 -1
  45. package/out/geometries/polygon.d.ts +23 -122
  46. package/out/geometries/polygon.d.ts.map +1 -1
  47. package/out/geometries/polygon.js +13 -41
  48. package/out/geometries/polygon.js.map +1 -1
  49. package/out/geometries/ring-blob.d.ts +23 -109
  50. package/out/geometries/ring-blob.d.ts.map +1 -1
  51. package/out/geometries/ring-blob.js +29 -115
  52. package/out/geometries/ring-blob.js.map +1 -1
  53. package/out/{geometries/index.js → geometries.d.ts} +1 -1
  54. package/out/geometries.d.ts.map +1 -0
  55. package/out/{geometries/index.d.ts → geometries.js} +1 -1
  56. package/out/geometries.js.map +1 -0
  57. package/out/google/place-id.d.ts +4 -2
  58. package/out/google/place-id.d.ts.map +1 -1
  59. package/out/google/place-id.js.map +1 -1
  60. package/out/{google/index.d.ts → google.d.ts} +1 -1
  61. package/out/google.d.ts.map +1 -0
  62. package/out/{google/index.js → google.js} +1 -1
  63. package/out/google.js.map +1 -0
  64. package/out/h3/cell.d.ts +19 -73
  65. package/out/h3/cell.d.ts.map +1 -1
  66. package/out/h3/cell.js +20 -63
  67. package/out/h3/cell.js.map +1 -1
  68. package/out/h3/coverage.d.ts +11 -7
  69. package/out/h3/coverage.d.ts.map +1 -1
  70. package/out/h3/coverage.js +11 -7
  71. package/out/h3/coverage.js.map +1 -1
  72. package/out/h3/interior-cells.d.ts +9 -28
  73. package/out/h3/interior-cells.d.ts.map +1 -1
  74. package/out/h3/interior-cells.js +13 -32
  75. package/out/h3/interior-cells.js.map +1 -1
  76. package/out/h3/polygon-cells.d.ts +28 -120
  77. package/out/h3/polygon-cells.d.ts.map +1 -1
  78. package/out/h3/polygon-cells.js +48 -146
  79. package/out/h3/polygon-cells.js.map +1 -1
  80. package/out/{h3/index.d.ts → h3.d.ts} +2 -2
  81. package/out/h3.d.ts.map +1 -0
  82. package/out/{h3/index.js → h3.js} +2 -2
  83. package/out/h3.js.map +1 -0
  84. package/out/index.d.ts +5 -5
  85. package/out/index.d.ts.map +1 -1
  86. package/out/index.js +5 -5
  87. package/out/index.js.map +1 -1
  88. package/out/objects.d.ts +6 -6
  89. package/out/open-location-code.d.ts +11 -22
  90. package/out/open-location-code.d.ts.map +1 -1
  91. package/out/open-location-code.js +9 -40
  92. package/out/open-location-code.js.map +1 -1
  93. package/out/osgb36.d.ts +21 -97
  94. package/out/osgb36.d.ts.map +1 -1
  95. package/out/osgb36.js +12 -165
  96. package/out/osgb36.js.map +1 -1
  97. package/out/polyline.d.ts +5 -3
  98. package/out/polyline.d.ts.map +1 -1
  99. package/out/polyline.js +5 -3
  100. package/out/polyline.js.map +1 -1
  101. package/out/position.d.ts +16 -2
  102. package/out/position.d.ts.map +1 -1
  103. package/out/position.js.map +1 -1
  104. package/out/projection/transform.d.ts +30 -27
  105. package/out/projection/transform.d.ts.map +1 -1
  106. package/out/projection/transform.js +14 -16
  107. package/out/projection/transform.js.map +1 -1
  108. package/out/{projection/index.d.ts → projection.d.ts} +2 -2
  109. package/out/projection.d.ts.map +1 -0
  110. package/out/{projection/index.js → projection.js} +2 -2
  111. package/out/projection.js.map +1 -0
  112. package/out/{regions/index.js → regions.d.ts} +1 -1
  113. package/out/regions.d.ts.map +1 -0
  114. package/out/{regions/index.d.ts → regions.js} +1 -1
  115. package/out/regions.js.map +1 -0
  116. package/out/tools/ogr/stream.d.ts +21 -11
  117. package/out/tools/ogr/stream.d.ts.map +1 -1
  118. package/out/tools/ogr/stream.js +16 -9
  119. package/out/tools/ogr/stream.js.map +1 -1
  120. package/out/tools/{ogr/index.d.ts → ogr.d.ts} +14 -11
  121. package/out/tools/ogr.d.ts.map +1 -0
  122. package/out/tools/{ogr/index.js → ogr.js} +18 -14
  123. package/out/tools/ogr.js.map +1 -0
  124. package/out/tools/tsconfig.tsbuildinfo +1 -0
  125. package/out/tsconfig.test.tsbuildinfo +1 -1
  126. package/out/well-known-text.d.ts +8 -8
  127. package/out/well-known-text.js +7 -7
  128. package/package.json +35 -127
  129. package/{lib/tools → tools}/ogr/stream.ts +25 -14
  130. package/{lib/tools/ogr/index.ts → tools/ogr.ts} +25 -19
  131. package/out/geometries/index.d.ts.map +0 -1
  132. package/out/geometries/index.js.map +0 -1
  133. package/out/google/index.d.ts.map +0 -1
  134. package/out/google/index.js.map +0 -1
  135. package/out/h3/index.d.ts.map +0 -1
  136. package/out/h3/index.js.map +0 -1
  137. package/out/projection/index.d.ts.map +0 -1
  138. package/out/projection/index.js.map +0 -1
  139. package/out/regions/index.d.ts.map +0 -1
  140. package/out/regions/index.js.map +0 -1
  141. package/out/tools/ogr/index.d.ts.map +0 -1
  142. package/out/tools/ogr/index.js.map +0 -1
  143. /package/lib/{geometries/index.ts → geometries.ts} +0 -0
  144. /package/lib/{google/index.ts → google.ts} +0 -0
  145. /package/lib/{regions/index.ts → regions.ts} +0 -0
package/lib/osgb36.ts CHANGED
@@ -2,223 +2,76 @@
2
2
  * @copyright Sister Software.
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * OSGB36 / British National Grid (EPSG:27700) → WGS84 (EPSG:4326).
7
- *
8
- * Ordnance Survey ships its open products in eastings/northings on the National Grid, not in
9
- * degrees — Code-Point Open, OS Open UPRN, OS Open Names all do. Nothing else in the repo speaks
10
- * that coordinate system, so this module is the one place that converts it, and it lives in
11
- * `@mailwoman/spatial` because that is the math home (`projection.ts` names the datums; this file
12
- * moves between two of them).
13
- *
14
- * Two steps, both from OS's own "A Guide to Coordinate Systems in Great Britain":
15
- *
16
- * 1. **Inverse transverse Mercator (Redfearn's series)** — National Grid E/N → OSGB36 geodetic
17
- * lat/lon on the Airy 1830 ellipsoid. This step is EXACT to well below a millimetre; it is
18
- * plain map-projection algebra with no empirical content, and {@link osgb36GridToAiryLatLon}
19
- * reproduces the guide's Annexe C.2 worked example to **1.3e-5 arc-seconds (~0.4 mm)**, which
20
- * is that example's own published rounding rather than our error.
21
- * 2. **Seven-parameter Helmert** — Airy-1830 geodetic → geocentric cartesian → rotate/scale/shift
22
- * → WGS84 (GRS80) geodetic. This step is APPROXIMATE, and that is the whole accuracy story
23
- * below. Against the guide's own Annexe D Helmert worked example — which uses this same
24
- * transform, so it tests the implementation and not the approximation — the round trip lands
25
- * **8.4 mm** out, i.e. the published DMS rounding plus the ignored ellipsoidal height.
26
- *
27
- * ## Accuracy: ±5 m, measured, and why we accept it
28
- *
29
- * OSGB36 is a 1936 theodolite triangulation, not a geocentric datum. Its distortions relative to
30
- * WGS84 are irregular across GB — a couple of metres of local warp that no rigid-body transform can
31
- * absorb. OS's exact answer is **OSTN15**, a published ~1.5 MB grid of per-cell E/N shifts; the
32
- * seven-parameter Helmert implemented here is OS's own documented approximation to it, which the
33
- * guide rates at "up to 3.5 m (95%)".
34
- *
35
- * MEASURED against OS's official 40-point OSTN15/OSGM15 test set (`OSTN15_OSGM15_TestInput_*`, the
36
- * developer-pack vectors, joining each point's published OSGB36 E/N to its published ETRS89 lat/lon):
37
- *
38
- * | p50 | p90 | p95 | max | mean |
39
- * | ------ | ------ | ------ | ------ | ------ |
40
- * | 1.74 m | 3.23 m | 4.18 m | 4.91 m | 1.94 m |
41
- *
42
- * All 40 land under 5 m. The worst are exactly where OS warns the datum thins out — TP31 St Kilda
43
- * (4.91 m), TP01 Scilly (4.72 m), TP32 Outer Hebrides (4.18 m); the mainland points sit near 1–2 m.
44
- * p95 runs a little above the guide's 3.5 m because we discard ellipsoidal height (see
45
- * {@link osgb36AiryToWGS84}), and because two of the three worst points are offshore rocks where
46
- * "OSGB36 does not exist" in OS's own words.
47
- *
48
- * We take that error deliberately, because of what the coordinates are FOR. A Code-Point Open
49
- * centroid is the mean position of a postcode unit's delivery points snapped to the nearest real
50
- * address — the unit itself spans tens to hundreds of metres, and the resolver uses the centroid to
51
- * pick a locality and rank candidates within a few hundred metres. A 2 m datum error is one to two
52
- * orders of magnitude below the quantity being represented. Nothing downstream reads a postcode
53
- * centroid at metre precision.
54
- *
55
- * **Do not use this module for anything that does.** Surveying, cadastral work, or any consumer
56
- * comparing our coordinates against another OSGB36-derived dataset at sub-10 m tolerance needs
57
- * OSTN15, which means shipping the shift grid. If that day comes, the swap point is
58
- * {@link osgb36AiryToWGS84} — swap the Helmert for a grid interpolation and the projection step
59
- * above it is unchanged.
60
- *
61
- * @see https://www.ordnancesurvey.co.uk/documents/resources/guide-coordinate-systems-great-britain.pdf
62
5
  */
63
6
 
64
- import type { Coordinates2D } from "#position"
7
+ import type { Coordinates2D, GeoCoordinate } from "#position"
65
8
 
66
- /**
67
- * Semi-major axis of the Airy 1830 ellipsoid, in metres — the figure of the Earth OSGB36 is referenced to.
68
- */
69
9
  const AIRY_1830_A = 6_377_563.396
70
10
 
71
- /**
72
- * Semi-minor axis of the Airy 1830 ellipsoid, in metres.
73
- */
74
11
  const AIRY_1830_B = 6_356_256.909
75
12
 
76
- /**
77
- * Semi-major axis of the GRS80 ellipsoid, in metres. WGS84's own semi-major axis is identical; the two ellipsoids
78
- * differ only in the flattening's last digits (~0.1 mm at the pole), far below this module's error budget.
79
- */
80
13
  const GRS80_A = 6_378_137
81
14
 
82
- /**
83
- * Semi-minor axis of the GRS80 ellipsoid, in metres.
84
- */
85
15
  const GRS80_B = 6_356_752.3141
86
16
 
87
- /**
88
- * Scale factor on the National Grid's central meridian.
89
- */
90
17
  const NATIONAL_GRID_F0 = 0.9996012717
91
18
 
92
- /**
93
- * Latitude of the National Grid's true origin, 49° N, in radians.
94
- */
95
19
  const NATIONAL_GRID_LAT0 = (49 * Math.PI) / 180
96
20
 
97
- /**
98
- * Longitude of the National Grid's true origin, 2° W, in radians.
99
- */
100
21
  const NATIONAL_GRID_LON0 = (-2 * Math.PI) / 180
101
22
 
102
- /**
103
- * Easting of the National Grid's true origin (the "false easting"), in metres.
104
- */
105
23
  const NATIONAL_GRID_E0 = 400_000
106
24
 
107
- /**
108
- * Northing of the National Grid's true origin (the "false northing"), in metres. Negative because the true origin sits
109
- * south-west of the grid's own zero.
110
- */
111
25
  const NATIONAL_GRID_N0 = -100_000
112
26
 
113
- /**
114
- * Arc-seconds → radians. The Helmert rotations are published in arc-seconds.
115
- */
116
27
  const ARCSEC_TO_RAD = Math.PI / (180 * 3600)
117
28
 
118
- /**
119
- * Parts-per-million → unitless scale. The Helmert scale factor is published in ppm.
120
- */
121
29
  const PPM = 1e-6
122
30
 
123
- /**
124
- * Convergence threshold for the meridional-arc iteration in {@link osgb36GridToAiryLatLon}, in METRES of northing. OS's
125
- * guide specifies 0.01 mm; this is that figure. It bounds the northing residual, not the latitude, which is why it is
126
- * expressed in metres and compared against `northing - N0 - M`.
127
- */
128
31
  const MERIDIONAL_ARC_TOLERANCE_M = 1e-5
129
32
 
130
- /**
131
- * Convergence threshold for the cartesian→geodetic latitude iteration in {@link osgb36AiryToWGS84}, in RADIANS. 1e-13
132
- * rad is ~0.6 µm on the ground — far below anything this module claims, and reached in four or five passes at GB
133
- * latitudes.
134
- */
135
33
  const GEODETIC_LATITUDE_TOLERANCE_RAD = 1e-13
136
34
 
137
- /**
138
- * The seven-parameter Helmert transformation taking OSGB36 (Airy 1830) geocentric cartesian coordinates to WGS84.
139
- *
140
- * OS publishes this transform in the ETRS89/WGS84 → OSGB36 direction (guide table 4), as `tx = -446.448, ty = +125.157,
141
- * tz = -542.060, s = +20.4894 ppm, rx = -0.1502", ry = -0.2470", rz = -0.8421"`. We need the OTHER direction, so every
142
- * parameter is negated — valid to well inside the transform's own metre-scale error because the rotations are
143
- * microradian-scale and the second-order terms of a proper inversion are sub-millimetre.
144
- *
145
- * The rotation convention is **Position Vector** (EPSG method 1033), NOT Coordinate Frame Rotation (EPSG 1032). The two
146
- * differ only in the sign of the three rotations, which is why citing the method code matters more than it looks: paste
147
- * these numbers into a library expecting 1032 and every result moves by roughly 20 m with no error raised.
148
- *
149
- * Getting a sign wrong here does not produce a subtly worse answer; it produces a coordinate tens to hundreds of metres
150
- * out, in a consistent direction — which reads as a plausible coordinate. That is what the Annexe D test is for.
151
- */
152
35
  const OSGB36_TO_WGS84_HELMERT = {
153
- /**
154
- * X translation, metres.
155
- */
156
36
  tx: 446.448,
157
- /**
158
- * Y translation, metres.
159
- */
37
+
160
38
  ty: -125.157,
161
- /**
162
- * Z translation, metres.
163
- */
39
+
164
40
  tz: 542.06,
165
- /**
166
- * Scale correction, parts per million.
167
- */
41
+
168
42
  scalePPM: -20.4894,
169
- /**
170
- * Rotation about the X axis, arc-seconds.
171
- */
43
+
172
44
  rx: 0.1502,
173
- /**
174
- * Rotation about the Y axis, arc-seconds.
175
- */
45
+
176
46
  ry: 0.247,
177
- /**
178
- * Rotation about the Z axis, arc-seconds.
179
- */
47
+
180
48
  rz: 0.8421,
181
49
  } as const
182
50
 
183
51
  /**
184
- * A geodetic position on an unspecified ellipsoid, in degrees. Which ellipsoid is the caller's business — the whole
185
- * point of this module is that the same numbers mean different places on Airy 1830 and on GRS80.
186
- */
187
- export interface GeodeticLatLon {
188
- /**
189
- * Latitude in decimal degrees, positive north.
190
- */
191
- latitude: number
192
- /**
193
- * Longitude in decimal degrees, positive east.
194
- */
195
- longitude: number
196
- }
197
-
198
- /**
199
- * A position on the British National Grid, in metres.
52
+ * A position on the British National Grid, in meters.
200
53
  */
201
54
  export interface NationalGridPoint {
202
55
  /**
203
- * Easting in metres. Valid GB values run roughly 0…700,000.
56
+ * Distance east in meters.
57
+ * Valid GB values run roughly from 0 to 700,000.
204
58
  */
205
59
  easting: number
60
+
206
61
  /**
207
- * Northing in metres. Valid GB values run roughly 0…1,300,000.
62
+ * Distance north in meters.
63
+ * Valid GB values run roughly from 0 to 1,300,000.
208
64
  */
209
65
  northing: number
210
66
  }
211
67
 
212
68
  /**
213
- * Convert National Grid eastings/northings to OSGB36 geodetic lat/lon on the Airy 1830 ellipsoid, by Redfearn's inverse
214
- * transverse Mercator series.
69
+ * Converts a National Grid easting and northing to OSGB36 latitude and longitude
70
+ * on the Airy 1830 ellipsoid, without a datum shift.
215
71
  *
216
- * This is the EXACT half of the pipeline — pure projection algebra, no datum shift. The result is still on Airy 1830,
217
- * so it is NOT a WGS84 coordinate and must not be handed to anything expecting one; feed it to
218
- * {@link osgb36AiryToWGS84}. Exported separately so the projection can be tested against OS's worked example
219
- * independently of the Helmert, which is the only way to tell a projection bug from a datum-shift bug.
72
+ * Pass the result to {@link osgb36AiryToWGS84} to get WGS84 coordinates.
220
73
  */
221
- export function osgb36GridToAiryLatLon({ easting, northing }: NationalGridPoint): GeodeticLatLon {
74
+ export function osgb36GridToAiryLatLon({ easting, northing }: NationalGridPoint): GeoCoordinate {
222
75
  const a = AIRY_1830_A
223
76
  const b = AIRY_1830_B
224
77
  const f0 = NATIONAL_GRID_F0
@@ -227,14 +80,11 @@ export function osgb36GridToAiryLatLon({ easting, northing }: NationalGridPoint)
227
80
  const e0 = NATIONAL_GRID_E0
228
81
  const n0 = NATIONAL_GRID_N0
229
82
 
230
- // Eccentricity squared of the Airy ellipsoid, and the `n` series parameter.
231
83
  const e2 = (a * a - b * b) / (a * a)
232
84
  const n = (a - b) / (a + b)
233
85
  const n2 = n * n
234
86
  const n3 = n2 * n
235
87
 
236
- // Iterate latitude until the meridional arc `M` matches the northing. OS's guide specifies a 0.01 mm
237
- // threshold; it converges in three or four passes anywhere in GB.
238
88
  let lat = lat0
239
89
  let m = 0
240
90
 
@@ -260,7 +110,6 @@ export function osgb36GridToAiryLatLon({ easting, northing }: NationalGridPoint)
260
110
  const tan4 = tan2 * tan2
261
111
  const tan6 = tan4 * tan2
262
112
 
263
- // `nu` = transverse radius of curvature; `rho` = meridional radius; `eta2` = their ratio less one.
264
113
  const nu = a * f0 * (1 - e2 * sinLat * sinLat) ** -0.5
265
114
  const rho = a * f0 * (1 - e2) * (1 - e2 * sinLat * sinLat) ** -1.5
266
115
  const eta2 = nu / rho - 1
@@ -292,17 +141,14 @@ export function osgb36GridToAiryLatLon({ easting, northing }: NationalGridPoint)
292
141
  }
293
142
 
294
143
  /**
295
- * Convert OSGB36 geodetic lat/lon (Airy 1830) to WGS84 lat/lon (GRS80) via the seven-parameter Helmert.
144
+ * Converts OSGB36 latitude and longitude on Airy 1830 to WGS84 with the seven-parameter
145
+ * Helmert transform, accurate to about 5 m.
296
146
  *
297
- * This is the APPROXIMATE half — see the module docstring for the ±5 m budget and when it stops being acceptable.
298
- * Heights are not modelled: the input is treated as sitting on the Airy ellipsoid and the output's ellipsoidal height
299
- * is discarded. For a horizontal postcode centroid that costs well under a metre; for anything vertical it is wrong by
300
- * the ~50 m geoid–ellipsoid separation over GB, so this function does not pretend to return a height.
147
+ * The transform ignores height, so use the result for horizontal positions only.
301
148
  */
302
- export function osgb36AiryToWGS84({ latitude, longitude }: GeodeticLatLon): GeodeticLatLon {
149
+ export function osgb36AiryToWGS84({ latitude, longitude }: GeoCoordinate): GeoCoordinate {
303
150
  const { tx, ty, tz, scalePPM, rx, ry, rz } = OSGB36_TO_WGS84_HELMERT
304
151
 
305
- // Convert Airy 1830 geodetic coordinates to geocentric Cartesian coordinates.
306
152
  const phi = (latitude * Math.PI) / 180
307
153
  const lambda = (longitude * Math.PI) / 180
308
154
  const sinPhi = Math.sin(phi)
@@ -314,7 +160,6 @@ export function osgb36AiryToWGS84({ latitude, longitude }: GeodeticLatLon): Geod
314
160
  const y1 = nuAiry * cosPhi * Math.sin(lambda)
315
161
  const z1 = (1 - e2Airy) * nuAiry * sinPhi
316
162
 
317
- // Apply Helmert rotation, scale, and translation.
318
163
  const s = 1 + scalePPM * PPM
319
164
  const rxRad = rx * ARCSEC_TO_RAD
320
165
  const ryRad = ry * ARCSEC_TO_RAD
@@ -324,14 +169,12 @@ export function osgb36AiryToWGS84({ latitude, longitude }: GeodeticLatLon): Geod
324
169
  const y2 = ty + rzRad * x1 + s * y1 - rxRad * z1
325
170
  const z2 = tz - ryRad * x1 + rxRad * y1 + s * z1
326
171
 
327
- // Convert GRS80 geocentric Cartesian coordinates to geodetic coordinates by latitude iteration.
328
172
  const e2GRS = (GRS80_A * GRS80_A - GRS80_B * GRS80_B) / (GRS80_A * GRS80_A)
329
173
  const p = Math.sqrt(x2 * x2 + y2 * y2)
330
174
 
331
175
  let phi2 = Math.atan2(z2, p * (1 - e2GRS))
332
176
  let phiPrev = 2 * Math.PI
333
177
 
334
- // Converges in a handful of passes at GB latitudes; the guard bounds it regardless.
335
178
  for (let i = 0; i < 100 && Math.abs(phi2 - phiPrev) > GEODETIC_LATITUDE_TOLERANCE_RAD; i++) {
336
179
  phiPrev = phi2
337
180
 
@@ -348,21 +191,19 @@ export function osgb36AiryToWGS84({ latitude, longitude }: GeodeticLatLon): Geod
348
191
  }
349
192
 
350
193
  /**
351
- * Convert a British National Grid (EPSG:27700) easting/northing straight to WGS84 (EPSG:4326) lat/lon — the composition
352
- * of {@link osgb36GridToAiryLatLon} and {@link osgb36AiryToWGS84}, and the function callers actually want.
194
+ * Converts a British National Grid (EPSG:27700) easting and northing to WGS84
195
+ * (EPSG:4326) latitude and longitude, accurate to about 5 m across GB.
353
196
  *
354
- * Accurate to about ±5 m across GB (see the module docstring). Does NOT validate that the input lies within the grid's
355
- * GB extent: `{easting: 0, northing: 0}` is a real point in the Atlantic south-west of the Scillies, and Code-Point
356
- * Open uses exactly that to mean "no coordinate available" (its 865 positional-quality-90 rows). Callers must filter
357
- * those rows themselves — a zero here is a legitimate coordinate, so this module cannot tell the difference.
197
+ * It converts `{ easting: 0, northing: 0 }` like any other point, so callers must
198
+ * filter out Code-Point Open's zero placeholder rows.
358
199
  */
359
- export function osgb36ToWGS84(point: NationalGridPoint): GeodeticLatLon {
200
+ export function osgb36ToWGS84(point: NationalGridPoint): GeoCoordinate {
360
201
  return osgb36AiryToWGS84(osgb36GridToAiryLatLon(point))
361
202
  }
362
203
 
363
204
  /**
364
- * {@link osgb36ToWGS84} in GeoJSON axis order — `[longitude, latitude]`, matching {@link Coordinates2D} and every
365
- * geometry helper in this package.
205
+ * Converts a National Grid point to WGS84 in GeoJSON `[longitude, latitude]` order,
206
+ * matching {@link Coordinates2D}.
366
207
  */
367
208
  export function osgb36ToCoordinates2D(point: NationalGridPoint): Coordinates2D {
368
209
  const { latitude, longitude } = osgb36ToWGS84(point)
package/lib/polyline.ts CHANGED
@@ -17,8 +17,9 @@ import { haversineKm } from "#distance"
17
17
  /**
18
18
  * Clamp a fraction into `[0, 1]`.
19
19
  *
20
- * NaN passes through, which is deliberate: an interpolation whose inputs were not numbers should produce NaN
21
- * coordinates a caller can detect, not silently snap to a segment's start.
20
+ * NaN passes through deliberately.
21
+ * An interpolation whose inputs were not numbers should produce NaN coordinates a
22
+ * caller can detect rather than silently snap to a segment's start.
22
23
  */
23
24
  export function clampFraction(t: number): number {
24
25
  return t < 0 ? 0 : Math.min(1, t)
@@ -27,7 +28,8 @@ export function clampFraction(t: number): number {
27
28
  /**
28
29
  * The point at fraction `t` of a polyline's total arc length (haversine), plus that total in km.
29
30
  *
30
- * `t` is assumed clamped to `[0, 1]`. A zero-length polyline yields its first vertex and a length of 0, so a degenerate
31
+ * `t` is assumed clamped to `[0, 1]`.
32
+ * A zero-length polyline yields its first vertex and a length of 0, so a degenerate
31
33
  * segment still produces a usable coordinate rather than NaN.
32
34
  */
33
35
  export function pointAlong(
package/lib/position.ts CHANGED
@@ -12,6 +12,22 @@ import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
12
12
 
13
13
  export { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
14
14
 
15
+ /**
16
+ * A point in decimal degrees under the `latitude` and `longitude` keys.
17
+ */
18
+ export interface GeoCoordinate {
19
+ latitude: number
20
+ longitude: number
21
+ }
22
+
23
+ /**
24
+ * A point in decimal degrees under the `lat` and `lon` keys.
25
+ */
26
+ export interface LatLon {
27
+ lat: number
28
+ lon: number
29
+ }
30
+
15
31
  /**
16
32
  * Arity of a `[lon, lat]` coordinate tuple.
17
33
  */
@@ -25,8 +41,8 @@ const COORD_TRIPLE_LENGTH = 3
25
41
  /**
26
42
  * An ordered pair of coordinates in the form of [longitude, latitude].
27
43
  *
28
- * Note that unlike the typical order, GeoJSON coordinates are in the order of [longitude, latitude] to match the order
29
- * of [x, y] in Cartesian coordinates.
44
+ * Note that unlike the typical order, GeoJSON coordinates are in the order of
45
+ * [longitude, latitude] to match the order of [x, y] in Cartesian coordinates.
30
46
  *
31
47
  * @category Position
32
48
  * @category GeoJSON
@@ -1,41 +1,42 @@
1
1
  import { runFile } from "@mailwoman/core/process"
2
+ import { TextSpliterator } from "spliterator"
2
3
 
3
4
  /**
4
5
  * @copyright Sister Software
5
6
  * @license AGPL-3.0
6
7
  * @author Teffen Ellis, et al.
7
8
  *
8
- * Ask PROJ which datum transformation it would choose, and refuse a ballpark one.
9
+ * Ask PROJ which datum transformation it would choose. Refuse a ballpark operation.
9
10
  *
10
- * PROJ SUBSTITUTES A BALLPARK DATUM SHIFT WHEN THE ACCURATE GRID IS NOT ON DISK, AND IT DOES SO SILENTLY.
11
- * Measured on the EA flood product: with the OSGB36→WGS84 grid missing, ogr2ogr placed the first
12
- * feature's first vertex at `1.698151293, 52.648130027`; with `uk_os_OSTN15_NTv2_OSGBtoETRS.tif` present
13
- * it placed it at `1.698174628, 52.648157259` — 3.4 m apart. Both look like perfectly ordinary WGS84
14
- * coordinates, both pass a bounding-box check, and the whole layer is offset. It surfaced as eight
15
- * disagreements out of 59 against the authority's own OGC service, every one a point that fell into a
16
- * NEIGHBOURING sliver.
11
+ * PROJ silently substitutes a ballpark datum shift when the accurate grid is absent. On the EA flood product,
12
+ * `ogr2ogr` placed the first feature's first vertex at `1.698151293, 52.648130027` without the OSGB36→WGS84 grid.
13
+ * With `uk_os_OSTN15_NTv2_OSGBtoETRS.tif` present, it placed the vertex at `1.698174628, 52.648157259`, a distance
14
+ * of 3.4 m. Both coordinates look like ordinary WGS84 values and pass a bounding-box check. The layer is offset.
15
+ * The check found eight disagreements among 59 points against the authority's OGC service. Each point fell into a
16
+ * neighboring sliver.
17
17
  *
18
- * `--config PROJ_NETWORK ON` does not reach PROJ through GDAL 3.8, and `PROJ_ONLY_BEST=ON` was observed
19
- * not to refuse, so neither is a usable guard. What is usable is asking PROJ what it would do: `projinfo`
20
- * names the best candidate operation and says when a grid is missing.
18
+ * GDAL 3.8 does not pass `--config PROJ_NETWORK on` through to PROJ. Tests also showed that `PROJ_ONLY_BEST=on`
19
+ * does not refuse a ballpark shift. `projinfo` provides the usable check. Its output identifies the best candidate
20
+ * operation and reports a missing grid.
21
21
  *
22
- * IT IS RUN EVEN WHERE NO TRANSFORMATION IS NEEDED, AND THAT IS THE POINT. A source already in EPSG:4326
23
- * gets `Null geographic offset from WGS 84 to WGS 84, 0 m, World.` — trivially usable, and the check costs
24
- * one process. Skipping it on the reasoning that a source needs no shift makes the guard fire on the day
25
- * a source arrives that does, which is the day nobody is looking.
22
+ * The check runs even when a source needs no transformation. A source already in EPSG:4326 returns
23
+ * `Null geographic offset from WGS 84 to WGS 84, 0 m, World.` This operation is usable. The check costs one process.
24
+ * A skipped case would leave a later source's required grid unchecked.
26
25
  *
27
- * SHARED BY EVERY VECTOR INGEST, because the failure is a property of PROJ rather than of any product. The
28
- * parse is split from the spawn so it can be pinned against captured output: the two states it
29
- * distinguishes were observed from the same command on the same machine, before and after the grid was
30
- * installed, and they are the difference between a metre-accurate layer and a 3 m-offset one.
26
+ * Every vector ingest shares this check because the failure comes from PROJ rather than from a particular product.
27
+ * Output parsing is separate from process execution so tests can use captured output. The two output states came from the
28
+ * same command on the same machine before and after grid installation. One produced a meter-accurate layer. The
29
+ * other produced a 3 m offset.
31
30
  */
32
31
 
33
32
  /**
34
- * What PROJ would do, and whether it can actually do it.
33
+ * Reports the operation PROJ would choose and whether it can run it.
35
34
  */
36
35
  export interface DatumTransformationVerdict {
37
36
  /**
38
- * The candidate operation line PROJ named, verbatim. Absent when it named none.
37
+ * The candidate operation string returned by PROJ, verbatim.
38
+ *
39
+ * Absent when PROJ returned no candidate operation.
39
40
  */
40
41
  best?: string
41
42
  usable: boolean
@@ -43,21 +44,18 @@ export interface DatumTransformationVerdict {
43
44
  }
44
45
 
45
46
  /**
46
- * Read `projinfo --summary` output: which operation PROJ would choose, and whether it can actually run.
47
+ * Reads `projinfo --summary` output.
48
+ *
49
+ * It reports which operation PROJ would choose and whether it can run it.
47
50
  */
48
51
  export function assessDatumTransformation(summary: string): DatumTransformationVerdict {
49
- // The first line naming a candidate operation is the one PROJ will choose. Everything before it is a header, and the
50
- // `Note:` line about `--spatial-test` is not a candidate.
51
- //
52
- // `projinfo --summary` prints a header plus one line per candidate operation: two to nine lines, bounded by PROJ's own
53
- // candidate enumeration rather than by input size. Reaching for a streaming reader would put a `spliterator` dependency
54
- // on this package for a fixed handful of lines.
55
- // oxlint-disable-next-line mailwoman/prefer-spliterator -- bounded output, see above
56
- const lines = summary.split("\n")
57
-
58
- const best = lines
59
- .map((line) => line.trim())
60
- .find((line) => line.includes(", ") && !line.startsWith("Note:") && !line.startsWith("Candidate operations"))
52
+ // The first line naming a candidate operation is the one PROJ will choose.
53
+ // Earlier lines are headers.
54
+ // The `Note:` line about `--spatial-test` also describes no candidate operation.
55
+ // The reader trims each line.
56
+ const best = TextSpliterator.from(summary).find(
57
+ (line) => line.includes(", ") && !line.startsWith("Note:") && !line.startsWith("Candidate operations")
58
+ )
61
59
 
62
60
  if (!best) return { usable: false, reason: "projinfo named no candidate operation" }
63
61
 
@@ -69,18 +67,19 @@ export function assessDatumTransformation(summary: string): DatumTransformationV
69
67
  }
70
68
 
71
69
  /**
72
- * The target every layer ingest reprojects to — H3 takes WGS84 latitude and longitude.
70
+ * The target every layer ingest reprojects to.
71
+ * H3 takes WGS84 latitude and longitude.
73
72
  */
74
73
  export const WGS84_EPSG = 4326
75
74
 
76
75
  export interface AssertDatumTransformationOptions {
77
76
  /**
78
- * Names the caller in the refusal, so a build log says which ingest stopped.
77
+ * Identifies the caller in the refusal, so a build log reports which ingest stopped.
79
78
  */
80
79
  context: string
81
80
  targetEPSG?: number
82
81
  /**
83
- * The area-of-use to name in the `projsync` remedy the message prints.
82
+ * The area-of-use to name in the `projsync` action the message prints.
84
83
  */
85
84
  areaOfUse?: string
86
85
  }
@@ -88,8 +87,8 @@ export interface AssertDatumTransformationOptions {
88
87
  /**
89
88
  * Refuse an ingest whose best available datum transformation is a ballpark one, or is missing its grid.
90
89
  *
91
- * @throws {Error} When PROJ names no candidate, would use a ballpark offset, or would use an operation whose grid is
92
- * not installed.
90
+ * @throws {Error} When proj names no candidate, would use a ballpark offset,
91
+ * or would use an operation whose grid is not installed.
93
92
  */
94
93
  export async function assertDatumTransformationAvailable(
95
94
  sourceEPSG: number,
@@ -109,7 +108,7 @@ export async function assertDatumTransformationAvailable(
109
108
 
110
109
  throw new Error(
111
110
  `${options.context}: the best EPSG:${sourceEPSG} → EPSG:${targetEPSG} transformation is unusable — ${verdict.reason} ` +
112
- `(${verdict.best ?? "projinfo named no candidate"}). PROJ falls back to a ballpark datum shift, which is metres ` +
111
+ `(${verdict.best ?? "projinfo named no candidate"}). PROJ falls back to a ballpark datum shift, which is meters ` +
113
112
  `wrong and looks exactly like a correct answer. ${remedy}`
114
113
  )
115
114
  }
@@ -17,7 +17,7 @@ export const CoordinateProjection = {
17
17
  */
18
18
  WGS84: "4326",
19
19
  /**
20
- * North American Datum 1983, a geodetic reference system used in the TIGER/Line data.
20
+ * North American Datum 1983, a geodetic reference system used in the tiger/Line data.
21
21
  */
22
22
  NAD83: "4269",
23
23
  } as const
@@ -11,7 +11,7 @@ import wkx from "wkx"
11
11
  import type { GeometryCollection, GeometryLiteral } from "#index"
12
12
 
13
13
  /**
14
- * Given an extended Well-Known Binary (EWKB) buffer, parse it into a GeoJSON geometry object.
14
+ * Given an extended Well-Known Binary (ewkb) buffer, parse it into a GeoJSON geometry object.
15
15
  *
16
16
  * @category Geo
17
17
  */
@@ -34,40 +34,40 @@ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(input: Buffer |
34
34
  }
35
35
 
36
36
  /**
37
- * Given a GeoJSON geometry object, convert it to an well-known binary (EWKB) buffer.
37
+ * Given a GeoJSON geometry object, convert it to an well-known binary (ewkb) buffer.
38
38
  *
39
39
  * @category Geo
40
40
  *
41
- * @returns A hex-encoded EWKB string or WKB buffer.
41
+ * @returns A hex-encoded ewkb string or WKB buffer.
42
42
  */
43
43
  export function geometryToWKB(geometry: GeometryLiteral | GeometryCollection) {
44
44
  return wkx.Geometry.parseGeoJSON(geometry).toWkb()
45
45
  }
46
46
 
47
47
  /**
48
- * Given a GeoJSON geometry object, convert it to an extended well-known binary (EWKB) buffer.
48
+ * Given a GeoJSON geometry object, convert it to an extended well-known binary (ewkb) buffer.
49
49
  *
50
50
  * @category Geo
51
51
  *
52
- * @returns A buffer representing the EWKB.
52
+ * @returns A buffer representing the ewkb.
53
53
  */
54
54
  export function geometryToEWKB(geometry: GeometryLiteral | GeometryCollection) {
55
55
  return wkx.Geometry.parseGeoJSON(geometry).toEwkb()
56
56
  }
57
57
 
58
58
  /**
59
- * Given a GeoJSON geometry object, convert it to an extended well-known binary (EWKB) buffer.
59
+ * Given a GeoJSON geometry object, convert it to an extended well-known binary (ewkb) buffer.
60
60
  *
61
61
  * @category Geo
62
62
  *
63
- * @returns A hex-encoded string representing the EWKB.
63
+ * @returns A hex-encoded string representing the ewkb.
64
64
  */
65
65
  export function geometryToEWKH(geometry: GeometryLiteral | GeometryCollection) {
66
66
  return geometryToEWKB(geometry).toString("hex")
67
67
  }
68
68
 
69
69
  /**
70
- * Given a GeoJSON geometry object, convert it to an extended well-known text (EWKT) string.
70
+ * Given a GeoJSON geometry object, convert it to an extended well-known text (ewkt) string.
71
71
  *
72
72
  * @category Geo
73
73
  */