@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.
- package/README.md +2 -2
- package/lib/bbox.ts +41 -34
- package/lib/coordinate/bounds.ts +2 -2
- package/lib/coordinate/formats.ts +10 -27
- package/lib/distance.ts +53 -27
- package/lib/feature.ts +2 -2
- package/lib/geometries/point.ts +41 -33
- package/lib/geometries/polygon.ts +24 -128
- package/lib/geometries/ring-blob.ts +33 -130
- package/lib/google/place-id.ts +4 -2
- package/lib/h3/cell.ts +21 -76
- package/lib/h3/coverage.ts +11 -7
- package/lib/h3/interior-cells.ts +13 -34
- package/lib/h3/polygon-cells.ts +59 -165
- package/lib/{h3/index.ts → h3.ts} +1 -1
- package/lib/index.ts +5 -5
- package/lib/objects.ts +6 -6
- package/lib/open-location-code.ts +11 -42
- package/lib/osgb36.ts +28 -187
- package/lib/polyline.ts +5 -3
- package/lib/position.ts +18 -2
- package/lib/projection/transform.ts +39 -40
- package/lib/{projection/index.ts → projection.ts} +1 -1
- package/lib/well-known-text.ts +8 -8
- package/out/bbox.d.ts +17 -14
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +32 -23
- package/out/bbox.js.map +1 -1
- package/out/coordinate/bounds.d.ts +2 -2
- package/out/coordinate/bounds.js +2 -2
- package/out/coordinate/formats.d.ts +8 -11
- package/out/coordinate/formats.d.ts.map +1 -1
- package/out/coordinate/formats.js +10 -27
- package/out/coordinate/formats.js.map +1 -1
- package/out/distance.d.ts +48 -25
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +53 -27
- package/out/distance.js.map +1 -1
- package/out/feature.d.ts +2 -2
- package/out/feature.d.ts.map +1 -1
- package/out/geometries/point.d.ts +30 -22
- package/out/geometries/point.d.ts.map +1 -1
- package/out/geometries/point.js +35 -29
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +23 -122
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +13 -41
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +23 -109
- package/out/geometries/ring-blob.d.ts.map +1 -1
- package/out/geometries/ring-blob.js +29 -115
- package/out/geometries/ring-blob.js.map +1 -1
- package/out/{geometries/index.js → geometries.d.ts} +1 -1
- package/out/geometries.d.ts.map +1 -0
- package/out/{geometries/index.d.ts → geometries.js} +1 -1
- package/out/geometries.js.map +1 -0
- package/out/google/place-id.d.ts +4 -2
- package/out/google/place-id.d.ts.map +1 -1
- package/out/google/place-id.js.map +1 -1
- package/out/{google/index.d.ts → google.d.ts} +1 -1
- package/out/google.d.ts.map +1 -0
- package/out/{google/index.js → google.js} +1 -1
- package/out/google.js.map +1 -0
- package/out/h3/cell.d.ts +19 -73
- package/out/h3/cell.d.ts.map +1 -1
- package/out/h3/cell.js +20 -63
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +11 -7
- package/out/h3/coverage.d.ts.map +1 -1
- package/out/h3/coverage.js +11 -7
- package/out/h3/coverage.js.map +1 -1
- package/out/h3/interior-cells.d.ts +9 -28
- package/out/h3/interior-cells.d.ts.map +1 -1
- package/out/h3/interior-cells.js +13 -32
- package/out/h3/interior-cells.js.map +1 -1
- package/out/h3/polygon-cells.d.ts +28 -120
- package/out/h3/polygon-cells.d.ts.map +1 -1
- package/out/h3/polygon-cells.js +48 -146
- package/out/h3/polygon-cells.js.map +1 -1
- package/out/{h3/index.d.ts → h3.d.ts} +2 -2
- package/out/h3.d.ts.map +1 -0
- package/out/{h3/index.js → h3.js} +2 -2
- package/out/h3.js.map +1 -0
- package/out/index.d.ts +5 -5
- package/out/index.d.ts.map +1 -1
- package/out/index.js +5 -5
- package/out/index.js.map +1 -1
- package/out/objects.d.ts +6 -6
- package/out/open-location-code.d.ts +11 -22
- package/out/open-location-code.d.ts.map +1 -1
- package/out/open-location-code.js +9 -40
- package/out/open-location-code.js.map +1 -1
- package/out/osgb36.d.ts +21 -97
- package/out/osgb36.d.ts.map +1 -1
- package/out/osgb36.js +12 -165
- package/out/osgb36.js.map +1 -1
- package/out/polyline.d.ts +5 -3
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +5 -3
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +16 -2
- package/out/position.d.ts.map +1 -1
- package/out/position.js.map +1 -1
- package/out/projection/transform.d.ts +30 -27
- package/out/projection/transform.d.ts.map +1 -1
- package/out/projection/transform.js +14 -16
- package/out/projection/transform.js.map +1 -1
- package/out/{projection/index.d.ts → projection.d.ts} +2 -2
- package/out/projection.d.ts.map +1 -0
- package/out/{projection/index.js → projection.js} +2 -2
- package/out/projection.js.map +1 -0
- package/out/{regions/index.js → regions.d.ts} +1 -1
- package/out/regions.d.ts.map +1 -0
- package/out/{regions/index.d.ts → regions.js} +1 -1
- package/out/regions.js.map +1 -0
- package/out/tools/ogr/stream.d.ts +21 -11
- package/out/tools/ogr/stream.d.ts.map +1 -1
- package/out/tools/ogr/stream.js +16 -9
- package/out/tools/ogr/stream.js.map +1 -1
- package/out/tools/{ogr/index.d.ts → ogr.d.ts} +14 -11
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/{ogr/index.js → ogr.js} +18 -14
- package/out/tools/ogr.js.map +1 -0
- package/out/tools/tsconfig.tsbuildinfo +1 -0
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/well-known-text.d.ts +8 -8
- package/out/well-known-text.js +7 -7
- package/package.json +35 -127
- package/{lib/tools → tools}/ogr/stream.ts +25 -14
- package/{lib/tools/ogr/index.ts → tools/ogr.ts} +25 -19
- package/out/geometries/index.d.ts.map +0 -1
- package/out/geometries/index.js.map +0 -1
- package/out/google/index.d.ts.map +0 -1
- package/out/google/index.js.map +0 -1
- package/out/h3/index.d.ts.map +0 -1
- package/out/h3/index.js.map +0 -1
- package/out/projection/index.d.ts.map +0 -1
- package/out/projection/index.js.map +0 -1
- package/out/regions/index.d.ts.map +0 -1
- package/out/regions/index.js.map +0 -1
- package/out/tools/ogr/index.d.ts.map +0 -1
- package/out/tools/ogr/index.js.map +0 -1
- /package/lib/{geometries/index.ts → geometries.ts} +0 -0
- /package/lib/{google/index.ts → google.ts} +0 -0
- /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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
214
|
-
*
|
|
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
|
-
*
|
|
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):
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 }:
|
|
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
|
-
*
|
|
352
|
-
*
|
|
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
|
-
*
|
|
355
|
-
*
|
|
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):
|
|
200
|
+
export function osgb36ToWGS84(point: NationalGridPoint): GeoCoordinate {
|
|
360
201
|
return osgb36AiryToWGS84(osgb36GridToAiryLatLon(point))
|
|
361
202
|
}
|
|
362
203
|
|
|
363
204
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
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
|
|
21
|
-
*
|
|
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]`.
|
|
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
|
|
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
|
|
9
|
+
* Ask PROJ which datum transformation it would choose. Refuse a ballpark operation.
|
|
9
10
|
*
|
|
10
|
-
* PROJ
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
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
|
|
19
|
-
* not
|
|
20
|
-
*
|
|
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
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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.
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
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
|
-
*
|
|
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`
|
|
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
|
|
92
|
-
*
|
|
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
|
|
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
|
|
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
|
package/lib/well-known-text.ts
CHANGED
|
@@ -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 (
|
|
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 (
|
|
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
|
|
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 (
|
|
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
|
|
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 (
|
|
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
|
|
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 (
|
|
70
|
+
* Given a GeoJSON geometry object, convert it to an extended well-known text (ewkt) string.
|
|
71
71
|
*
|
|
72
72
|
* @category Geo
|
|
73
73
|
*/
|