@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
|
@@ -10,79 +10,28 @@ import type { GeoObjectLiteral } from "#objects"
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* An array of positions forming a closed shape, such as a country or a lake.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* A polygon without holes:
|
|
16
|
-
*
|
|
17
|
-
* ```json
|
|
18
|
-
* {
|
|
19
|
-
* "type": "Polygon",
|
|
20
|
-
* "coordinates": [
|
|
21
|
-
* [
|
|
22
|
-
* [100, 0],
|
|
23
|
-
* [101, 0],
|
|
24
|
-
* [101, 1],
|
|
25
|
-
* [100, 1],
|
|
26
|
-
* [100, 0]
|
|
27
|
-
* ]
|
|
28
|
-
* ]
|
|
29
|
-
* }
|
|
30
|
-
* ```
|
|
31
13
|
*/
|
|
32
14
|
export type SolidPolygonPath = [
|
|
33
15
|
/**
|
|
34
|
-
*
|
|
35
|
-
* - The first and last positions are equivalent (they represent equivalent points).
|
|
16
|
+
* A linear ring is a closed LineString with four or more positions.
|
|
36
17
|
*/
|
|
37
18
|
exteriorRing: LineStringPath,
|
|
38
19
|
]
|
|
39
20
|
|
|
40
21
|
/**
|
|
41
|
-
* An array of positions forming a closed shape with holes, such as a country
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* A polygon with holes:
|
|
45
|
-
*
|
|
46
|
-
* ```json
|
|
47
|
-
* {
|
|
48
|
-
* "type": "Polygon",
|
|
49
|
-
* "coordinates": [
|
|
50
|
-
* [
|
|
51
|
-
* [100.0, 0.0],
|
|
52
|
-
* [101.0, 0.0],
|
|
53
|
-
* [101.0, 1.0],
|
|
54
|
-
* [100.0, 1.0],
|
|
55
|
-
* [100.0, 0.0]
|
|
56
|
-
* ],
|
|
57
|
-
* [
|
|
58
|
-
* [100.8, 0.8],
|
|
59
|
-
* [100.8, 0.2],
|
|
60
|
-
* [100.2, 0.2],
|
|
61
|
-
* [100.2, 0.8],
|
|
62
|
-
* [100.8, 0.8]
|
|
63
|
-
* ]
|
|
64
|
-
* ]
|
|
65
|
-
* }
|
|
66
|
-
* ```
|
|
22
|
+
* An array of positions forming a closed shape with holes, such as a country
|
|
23
|
+
* with islands or a lake with islands.
|
|
67
24
|
*/
|
|
68
25
|
export type NestedPolygonPath = [
|
|
69
26
|
/**
|
|
70
|
-
*
|
|
71
|
-
* - The first and last positions are equivalent (they represent equivalent points).
|
|
27
|
+
* A linear ring is a closed LineString with four or more positions.
|
|
72
28
|
*/
|
|
73
29
|
exteriorRing: LineStringPath,
|
|
74
|
-
/**
|
|
75
|
-
* - The interior rings are arrays of positions forming holes in the polygon.
|
|
76
|
-
*/
|
|
77
30
|
...interiorRings: LineStringPath[],
|
|
78
31
|
]
|
|
79
32
|
|
|
80
33
|
/**
|
|
81
34
|
* A polygon geometry.
|
|
82
|
-
*
|
|
83
|
-
* @see {@linkcode PolygonLiteral} for applicable JSON schema.
|
|
84
|
-
* @see {@linkcode SolidPolygonPath} for more information.
|
|
85
|
-
* @see {@linkcode NestedPolygonPath} for more information.
|
|
86
35
|
*/
|
|
87
36
|
export type PolygonPath = SolidPolygonPath | NestedPolygonPath
|
|
88
37
|
|
|
@@ -90,17 +39,7 @@ export type PolygonPath = SolidPolygonPath | NestedPolygonPath
|
|
|
90
39
|
* An array of positions forming a closed shape, such as a country or a lake.
|
|
91
40
|
*/
|
|
92
41
|
export interface PolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
|
|
93
|
-
/**
|
|
94
|
-
* Declares the type of GeoJSON object as a `Polygon` geometry.
|
|
95
|
-
*/
|
|
96
42
|
type: "Polygon"
|
|
97
|
-
/**
|
|
98
|
-
* An array of positions for each point in the geometry.
|
|
99
|
-
*
|
|
100
|
-
* @see {@link https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6 | RFC 7946 Section 3.1.6}
|
|
101
|
-
* @see {@linkcode SolidPolygonPath}
|
|
102
|
-
* @see {@linkcode NestedPolygonPath}
|
|
103
|
-
*/
|
|
104
43
|
coordinates: P
|
|
105
44
|
}
|
|
106
45
|
|
|
@@ -116,40 +55,31 @@ export function isPolygonLiteral<P extends PolygonPath = PolygonPath>(input: unk
|
|
|
116
55
|
/**
|
|
117
56
|
* Predicate for checking if a polygon geometry is a solid, i.e. it has no holes.
|
|
118
57
|
*/
|
|
119
|
-
// The parameter admits
|
|
120
|
-
// `SolidPolygonPath`, it cannot be asked about a polygon with holes without the caller asserting past the signature.
|
|
58
|
+
// The parameter admits both paths because distinguishing them is the function's job.
|
|
121
59
|
export function isSolidPolygonPath(input: PolygonLiteral<PolygonPath>): boolean {
|
|
122
60
|
return input.coordinates.length === 1
|
|
123
61
|
}
|
|
124
62
|
|
|
125
63
|
/**
|
|
126
|
-
* A linear ring as the containment predicates read it
|
|
127
|
-
*
|
|
128
|
-
* Deliberately looser than {@link LineStringPath} — the ray cast only ever indexes `[0]` and `[1]`, and the callers
|
|
129
|
-
* arrive with different position types (`[number, number, ...number[]]` from the resolver's GeoJSON reader, plain
|
|
130
|
-
* `number[][]` from a `JSON.parse` of a stored geometry column). A tight tuple type here would force a cast at every
|
|
131
|
-
* call site and buy nothing the predicate uses.
|
|
64
|
+
* A linear ring as the containment predicates read it, deliberately looser than
|
|
65
|
+
* {@link LineStringPath} because callers arrive with different position types.
|
|
132
66
|
*/
|
|
133
67
|
export type ContainmentRing = readonly (readonly number[])[]
|
|
134
68
|
|
|
135
69
|
/**
|
|
136
|
-
* One polygon's rings: `[exterior, ...holes]
|
|
137
|
-
* {@linkcode ContainmentRing}).
|
|
70
|
+
* One polygon's rings: `[exterior, ...holes]`.
|
|
138
71
|
*/
|
|
139
72
|
export type PolygonRings = readonly ContainmentRing[]
|
|
140
73
|
|
|
141
74
|
/**
|
|
142
|
-
* A feature's polygons — `MultiPolygon` coordinates, with a bare `Polygon` lifted
|
|
143
|
-
* {@linkcode arealPolygons}.
|
|
75
|
+
* A feature's polygons — `MultiPolygon` coordinates, with a bare `Polygon` lifted
|
|
76
|
+
* into the same shape by {@linkcode arealPolygons}.
|
|
144
77
|
*/
|
|
145
78
|
export type MultiPolygonRings = readonly PolygonRings[]
|
|
146
79
|
|
|
147
80
|
/**
|
|
148
|
-
* Ray-cast a point against
|
|
149
|
-
* edge
|
|
150
|
-
*
|
|
151
|
-
* Points exactly on an edge are implementation-defined; either side is acceptable for geocoding, where admin boundaries
|
|
152
|
-
* are Douglas-Peucker–simplified before they ever reach us.
|
|
81
|
+
* Ray-cast a point against one linear ring — the even-odd crossing count,
|
|
82
|
+
* with points exactly on an edge left implementation-defined.
|
|
153
83
|
*/
|
|
154
84
|
export function pointInRing(lon: number, lat: number, ring: ContainmentRing): boolean {
|
|
155
85
|
let inside = false
|
|
@@ -170,11 +100,8 @@ export function pointInRing(lon: number, lat: number, ring: ContainmentRing): bo
|
|
|
170
100
|
}
|
|
171
101
|
|
|
172
102
|
/**
|
|
173
|
-
* Even-odd containment over a polygon's ring list (`[outer, hole₁, …]`)
|
|
174
|
-
*
|
|
175
|
-
* Being inside an odd number of rings means inside the polygon, which handles holes — and islands inside holes —
|
|
176
|
-
* without depending on ring winding order. GeoJSON nominally specifies orientation, but the gazetteer sources do not
|
|
177
|
-
* reliably honour it, so the orientation-free rule is the one that survives real data.
|
|
103
|
+
* Even-odd containment over a polygon's ring list (`[outer, hole₁, …]`), independent of
|
|
104
|
+
* ring winding order because the gazetteer sources do not honor it reliably.
|
|
178
105
|
*/
|
|
179
106
|
export function pointInPolygon(lon: number, lat: number, rings: PolygonRings): boolean {
|
|
180
107
|
let inside = false
|
|
@@ -189,7 +116,7 @@ export function pointInPolygon(lon: number, lat: number, rings: PolygonRings): b
|
|
|
189
116
|
}
|
|
190
117
|
|
|
191
118
|
/**
|
|
192
|
-
* Inside
|
|
119
|
+
* Inside any polygon of a multi-polygon.
|
|
193
120
|
*/
|
|
194
121
|
export function pointInMultiPolygon(
|
|
195
122
|
lon: number,
|
|
@@ -205,30 +132,18 @@ export function pointInMultiPolygon(
|
|
|
205
132
|
export interface MultiPolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
|
|
206
133
|
type: "MultiPolygon"
|
|
207
134
|
|
|
208
|
-
/**
|
|
209
|
-
* One ring array per polygon — `coordinates[polygon][ring][position]`, matching a `Polygon`'s `coordinates` lifted by
|
|
210
|
-
* exactly one level.
|
|
211
|
-
*
|
|
212
|
-
* @see {@link https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.7 | RFC 7946 Section 3.1.7}
|
|
213
|
-
*/
|
|
214
135
|
coordinates: P[]
|
|
215
136
|
}
|
|
216
137
|
|
|
217
|
-
//#region Ring-list geometry — the parsed-GeoJSON shape
|
|
218
|
-
|
|
219
138
|
/**
|
|
220
|
-
* A geometry as it arrives from `JSON.parse`, or a typed literal
|
|
221
|
-
*
|
|
222
|
-
* is the one place that happens for the areal types.
|
|
139
|
+
* A geometry as it arrives from `JSON.parse`, or a typed literal, with `type` unchecked
|
|
140
|
+
* and no arity validation on `coordinates`.
|
|
223
141
|
*/
|
|
224
142
|
export type ParsedGeometry = GeometryLiteral | { type: string; coordinates?: unknown }
|
|
225
143
|
|
|
226
144
|
/**
|
|
227
|
-
* A geometry's polygons in the `MultiPolygon` coordinate shape,
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
* The one place a `Polygon` is lifted to `[rings]`; a caller that must refuse a non-areal geometry does so on the
|
|
231
|
-
* `null`, with its own message.
|
|
145
|
+
* A geometry's polygons in the `MultiPolygon` coordinate shape, with a bare `Polygon`
|
|
146
|
+
* lifted to `[rings]` and `null` for any non-areal geometry.
|
|
232
147
|
*/
|
|
233
148
|
export function arealPolygons(geometry: ParsedGeometry | null | undefined): MultiPolygonRings | null {
|
|
234
149
|
if (!geometry) return null
|
|
@@ -241,12 +156,8 @@ export function arealPolygons(geometry: ParsedGeometry | null | undefined): Mult
|
|
|
241
156
|
}
|
|
242
157
|
|
|
243
158
|
/**
|
|
244
|
-
* The polygons of a geometry that
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* @param subject Names the feature in the refusal, e.g. `feature 41209`.
|
|
248
|
-
* @param context Names the calling ingest, so a build log says which layer stopped.
|
|
249
|
-
* @throws {Error} When the geometry is neither a `Polygon` nor a `MultiPolygon`.
|
|
159
|
+
* The polygons of a geometry that must be areal, throwing when the geometry
|
|
160
|
+
* is not a `Polygon` or `MultiPolygon`.
|
|
250
161
|
*/
|
|
251
162
|
export function requireArealPolygons(geometry: ParsedGeometry, subject: string, context: string): MultiPolygonRings {
|
|
252
163
|
const polygons = arealPolygons(geometry)
|
|
@@ -257,15 +168,8 @@ export function requireArealPolygons(geometry: ParsedGeometry, subject: string,
|
|
|
257
168
|
}
|
|
258
169
|
|
|
259
170
|
/**
|
|
260
|
-
* Does an areal GeoJSON geometry contain the point
|
|
261
|
-
*
|
|
262
|
-
* The three-valued return is the point of the function. `null` means the geometry is NOT AREAL — a Point or a
|
|
263
|
-
* LineString cannot contain anything — and a caller must read that as "no polygon on record", the same as a missing
|
|
264
|
-
* geometry, never as a rejection. Collapsing it to `false` is how a place with a point-only record gets excluded from a
|
|
265
|
-
* containment pass instead of falling through to the approximate path.
|
|
266
|
-
*
|
|
267
|
-
* `scripts/eval/pip-containment.py` grades the same containment truth against its own ray cast and has to be matched BY
|
|
268
|
-
* HAND if this one changes — it is the one copy no import can reach.
|
|
171
|
+
* Does an areal GeoJSON geometry contain the point, returning `null` for a
|
|
172
|
+
* non-areal geometry rather than `false`.
|
|
269
173
|
*/
|
|
270
174
|
export function geometryContains(
|
|
271
175
|
geometry: ParsedGeometry | null | undefined,
|
|
@@ -280,13 +184,7 @@ export function geometryContains(
|
|
|
280
184
|
}
|
|
281
185
|
|
|
282
186
|
/**
|
|
283
|
-
* An axis-aligned rectangle as a closed ring, in GeoJSON `[lon, lat]` order and counter-clockwise
|
|
284
|
-
* winding.
|
|
285
|
-
*
|
|
286
|
-
* SHARED BECAUSE THE WINDING IS A CONVENTION AND A SECOND COPY IS A SECOND PLACE FOR IT TO DRIFT. Three layer builders
|
|
287
|
-
* hand-build rectangles for their fixture rungs, and each pairs this with {@link reversedRing} to make a hole. A copy
|
|
288
|
-
* whose hole is wound the same way as its exterior produces a fixture that passes every structural check and tests
|
|
289
|
-
* nothing about hole handling — which is the exact failure the area cross-check exists to catch in production data.
|
|
187
|
+
* An axis-aligned rectangle as a closed ring, in GeoJSON `[lon, lat]` order and counter-clockwise.
|
|
290
188
|
*/
|
|
291
189
|
export function rectangleRing(minLon: number, minLat: number, maxLon: number, maxLat: number): number[][] {
|
|
292
190
|
return [
|
|
@@ -310,5 +208,3 @@ export function reversedRing(minLon: number, minLat: number, maxLon: number, max
|
|
|
310
208
|
[minLon, minLat],
|
|
311
209
|
]
|
|
312
210
|
}
|
|
313
|
-
|
|
314
|
-
//#endregion
|
|
@@ -3,73 +3,40 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* THE GEOMETRY IS THE TRUTH TABLE. An H3 index above it answers a cell that lies wholly inside one
|
|
10
|
-
* feature; a cell a boundary crosses falls through to a ray cast against the few features the index
|
|
11
|
-
* already named. A rooftop answer at a boundary — which is where the answer usually matters most — has
|
|
12
|
-
* no other source, so the rings are stored at the resolution the authority published them and never
|
|
13
|
-
* simplified.
|
|
14
|
-
*
|
|
15
|
-
* THE LAYOUT PUTS THE COORDINATES ON AN 8-BYTE BOUNDARY, so the reader takes a `Float64Array` view over
|
|
16
|
-
* them instead of a `DataView.getFloat64` per ordinate. Header is `u32 version`, `u32 ringCount`, then
|
|
17
|
-
* one `(u32 pointCount, u32 polygonIndex)` pair per ring — `8 + 8 × ringCount` bytes, a multiple of
|
|
18
|
-
* eight by construction. Coordinates follow as `[lon, lat]` pairs, ring by ring.
|
|
19
|
-
*
|
|
20
|
-
* `polygonIndex` IS required AND CANNOT BE INFERRED. A source feature is a MultiPolygon, so its
|
|
21
|
-
* rings belong to several polygons and "the first ring is the exterior" is true only per polygon. The
|
|
22
|
-
* point test is orientation-free and nesting-free (even-odd across a polygon's own rings, the rule
|
|
23
|
-
* {@link pointInPolygon} uses), but it still has to know which rings share a polygon: an island sitting
|
|
24
|
-
* inside another polygon's hole is inside the layer and would cancel to "outside" if every ring were
|
|
25
|
-
* pooled into one list.
|
|
26
|
-
*
|
|
27
|
-
* SHARED BY EVERY POLYGON LAYER RATHER THAN COPIED INTO EACH. `@mailwoman/flood` and `@mailwoman/soil`
|
|
28
|
-
* both store an authority's rings this way, and a second copy of the alignment arithmetic or of the
|
|
29
|
-
* signed-area reading would be a second thing to get right — both failure modes are silent, since a
|
|
30
|
-
* mis-read blob answers a containment question wrongly and a hole-blind area reading answers "inside"
|
|
31
|
-
* for every point in a hole.
|
|
6
|
+
* A polygon layer's stored ring blob: an 8-byte header, then a `(u32 pointCount, u32 polygonIndex)`
|
|
7
|
+
* pair per ring, then `[lon, lat]` pairs on an 8-byte boundary so the reader takes a
|
|
8
|
+
* `Float64Array` view; `polygonIndex` is required because one feature's rings belong to several polygons.
|
|
32
9
|
*/
|
|
33
10
|
|
|
11
|
+
import type { LatLonBounds } from "#bbox"
|
|
34
12
|
import type { MultiPolygonRings } from "#geometries/polygon"
|
|
35
13
|
|
|
36
14
|
/**
|
|
37
|
-
* Format version stamped into every blob
|
|
38
|
-
* bytes it does not know the shape of.
|
|
15
|
+
* Format version stamped into every blob, rejected by a reader that meets a different number.
|
|
39
16
|
*/
|
|
40
17
|
export const RING_BLOB_VERSION = 1
|
|
41
18
|
|
|
42
|
-
/**
|
|
43
|
-
* Bytes of fixed header before the per-ring table.
|
|
44
|
-
*/
|
|
45
19
|
const HEADER_BYTES = 8
|
|
46
20
|
|
|
47
|
-
/**
|
|
48
|
-
* Bytes per ring-table entry: `u32 pointCount`, `u32 polygonIndex`.
|
|
49
|
-
*/
|
|
50
21
|
const RING_ENTRY_BYTES = 8
|
|
51
22
|
|
|
52
23
|
/**
|
|
53
|
-
* Positions a linear ring needs to bound an area: three distinct vertices plus
|
|
54
|
-
* §3.1.6).
|
|
24
|
+
* Positions a linear ring needs to bound an area: three distinct vertices plus
|
|
25
|
+
* the repeat that closes it (RFC 7946 §3.1.6).
|
|
55
26
|
*/
|
|
56
27
|
const MINIMUM_RING_POSITIONS = 4
|
|
57
28
|
|
|
58
29
|
/**
|
|
59
|
-
* One decoded feature: `[exteriorRing, ...holes]` per polygon, each ring a
|
|
30
|
+
* One decoded feature: `[exteriorRing, ...holes]` per polygon, each ring a
|
|
31
|
+
* flat `[lon, lat, lon, lat, …]` run.
|
|
60
32
|
*/
|
|
61
33
|
export interface DecodedRings {
|
|
62
|
-
/**
|
|
63
|
-
* `polygons[p][r]` is a flat coordinate run for ring `r` of polygon `p`.
|
|
64
|
-
*/
|
|
65
34
|
polygons: number[][][]
|
|
66
35
|
}
|
|
67
36
|
|
|
68
37
|
/**
|
|
69
|
-
* Pack a GeoJSON `MultiPolygon`/`Polygon` coordinate tree into the stored blob
|
|
70
|
-
*
|
|
71
|
-
* @throws {RangeError} When the geometry carries no ring, or a ring carries fewer than four positions (a linear ring is
|
|
72
|
-
* closed, so three is the minimum distinct-vertex count plus the repeat).
|
|
38
|
+
* Pack a GeoJSON `MultiPolygon`/`Polygon` coordinate tree into the stored blob, throwing
|
|
39
|
+
* when the geometry has no ring or a ring has fewer than four positions.
|
|
73
40
|
*/
|
|
74
41
|
export function encodeRings(polygons: MultiPolygonRings): Uint8Array {
|
|
75
42
|
const entries: Array<{ pointCount: number; polygonIndex: number }> = []
|
|
@@ -120,11 +87,8 @@ export function encodeRings(polygons: MultiPolygonRings): Uint8Array {
|
|
|
120
87
|
}
|
|
121
88
|
|
|
122
89
|
/**
|
|
123
|
-
* The ring table plus a `Float64Array` over the coordinates
|
|
124
|
-
*
|
|
125
|
-
* @throws {Error} When the blob's version is not {@linkcode RING_BLOB_VERSION}, or its declared ring table does not
|
|
126
|
-
* account for the bytes present. A blob that is silently mis-read answers a containment question wrongly, and a wrong
|
|
127
|
-
* containment answer is indistinguishable from a real one.
|
|
90
|
+
* The ring table plus a `Float64Array` over the coordinates, throwing when the blob's version is not
|
|
91
|
+
* {@linkcode RING_BLOB_VERSION} or its declared ring table does not account for the bytes present.
|
|
128
92
|
*/
|
|
129
93
|
function openRings(blob: Uint8Array): {
|
|
130
94
|
ringCount: number
|
|
@@ -160,8 +124,8 @@ function openRings(blob: Uint8Array): {
|
|
|
160
124
|
throw new Error(`ring blob: blob declares ${expected} bytes of geometry, holds ${blob.byteLength}`)
|
|
161
125
|
}
|
|
162
126
|
|
|
163
|
-
// The header is a multiple of eight by construction, so the coordinate run is 8-byte aligned
|
|
164
|
-
//
|
|
127
|
+
// The header is a multiple of eight by construction, so the coordinate run is 8-byte aligned.
|
|
128
|
+
// A misaligned source buffer is copied rather than rejected.
|
|
165
129
|
const absoluteOffset = blob.byteOffset + headerBytes
|
|
166
130
|
|
|
167
131
|
const coordinates =
|
|
@@ -173,12 +137,8 @@ function openRings(blob: Uint8Array): {
|
|
|
173
137
|
}
|
|
174
138
|
|
|
175
139
|
/**
|
|
176
|
-
* Is the point inside the stored geometry
|
|
177
|
-
*
|
|
178
|
-
* Even-odd within each polygon's own ring list, inside-any-polygon across them — the orientation-free rule
|
|
179
|
-
* {@linkcode pointInPolygon} states, applied without allocating the ring arrays. Reading a hole as an exterior ring is
|
|
180
|
-
* what this rule survives: a point inside a hole crosses two rings and comes out even, whichever way either ring
|
|
181
|
-
* winds.
|
|
140
|
+
* Is the point inside the stored geometry, even-odd within each polygon's own ring list
|
|
141
|
+
* and inside-any-polygon across them, without allocating the ring arrays.
|
|
182
142
|
*/
|
|
183
143
|
export function pointInEncodedRings(blob: Uint8Array, lon: number, lat: number): boolean {
|
|
184
144
|
const { ringCount, pointCounts, polygonIndices, coordinates } = openRings(blob)
|
|
@@ -210,8 +170,8 @@ export function pointInEncodedRings(blob: Uint8Array, lon: number, lat: number):
|
|
|
210
170
|
}
|
|
211
171
|
|
|
212
172
|
/**
|
|
213
|
-
* The even-odd crossing count over one ring held in a flat coordinate run
|
|
214
|
-
*
|
|
173
|
+
* The even-odd crossing count over one ring held in a flat coordinate run, mirroring
|
|
174
|
+
* {@linkcode pointInRing} exactly so a decoded ring and an encoded one never disagree.
|
|
215
175
|
*/
|
|
216
176
|
function crossesOdd(coordinates: Float64Array, offset: number, pointCount: number, lon: number, lat: number): boolean {
|
|
217
177
|
let inside = false
|
|
@@ -231,8 +191,7 @@ function crossesOdd(coordinates: Float64Array, offset: number, pointCount: numbe
|
|
|
231
191
|
}
|
|
232
192
|
|
|
233
193
|
/**
|
|
234
|
-
* Unpack the blob back into per-polygon flat coordinate runs
|
|
235
|
-
* check a build's fixtures assert.
|
|
194
|
+
* Unpack the blob back into per-polygon flat coordinate runs.
|
|
236
195
|
*/
|
|
237
196
|
export function decodeRings(blob: Uint8Array): DecodedRings {
|
|
238
197
|
const { ringCount, pointCounts, polygonIndices, coordinates } = openRings(blob)
|
|
@@ -252,23 +211,13 @@ export function decodeRings(blob: Uint8Array): DecodedRings {
|
|
|
252
211
|
}
|
|
253
212
|
|
|
254
213
|
/**
|
|
255
|
-
* Mean Earth radius in
|
|
214
|
+
* Mean Earth radius in meters, the sphere the ring areas are measured on.
|
|
256
215
|
*/
|
|
257
216
|
const EARTH_RADIUS_M = 6_371_008.8
|
|
258
217
|
|
|
259
218
|
/**
|
|
260
|
-
* Signed spherical area of one linear ring, in square
|
|
261
|
-
*
|
|
262
|
-
* The sign is the whole point: an orientation-respecting sum over a polygon's rings subtracts its holes, while a sum of
|
|
263
|
-
* absolute values adds them. Comparing the two against the source's own area figure is what tells a builder whether it
|
|
264
|
-
* has read the holes at all — the failure mode is silent, because a hole read as an exterior ring produces a perfectly
|
|
265
|
-
* well-formed polygon that simply covers more ground than the authority mapped.
|
|
266
|
-
*
|
|
267
|
-
* WHICH WINDING IS POSITIVE IS A CONTRACT, NOT A DETAIL, because a builder whose source encodes hole roles by
|
|
268
|
-
* ORIENTATION reads roles off this sign. It is the opposite of the standard planar shoelace: this sum runs `(lonᵢ −
|
|
269
|
-
* lonⱼ)` against the shoelace's `(xⱼ − xᵢ)`, so a ring `@mailwoman/spatial`'s own {@link rectangleRing} builds
|
|
270
|
-
* counter-clockwise answers NEGATIVE here. `@mailwoman/zoning` is the caller that depends on it, and
|
|
271
|
-
* `packages/zoning/test/unit/ring-roles.test.ts` pins the sign directly.
|
|
219
|
+
* Signed spherical area of one linear ring, in square meters, with clockwise positive
|
|
220
|
+
* and counter-clockwise negative.
|
|
272
221
|
*/
|
|
273
222
|
export function ringSignedAreaM2(ring: ReadonlyArray<readonly number[]>): number {
|
|
274
223
|
let total = 0
|
|
@@ -285,11 +234,8 @@ export function ringSignedAreaM2(ring: ReadonlyArray<readonly number[]>): number
|
|
|
285
234
|
}
|
|
286
235
|
|
|
287
236
|
/**
|
|
288
|
-
* Both readings of one feature's area, in square
|
|
289
|
-
* `allExterior` does not (holes add).
|
|
290
|
-
*
|
|
291
|
-
* A source whose holes are correctly nested makes `nested` match the authority's own figure and `allExterior` exceed
|
|
292
|
-
* it. The gap between them is the area a hole-blind reader would answer "inside" for.
|
|
237
|
+
* Both readings of one feature's area, in square meters: `nested` respects ring
|
|
238
|
+
* orientation (holes subtract) and `allExterior` does not (holes add).
|
|
293
239
|
*/
|
|
294
240
|
export function ringAreaReadings(polygons: MultiPolygonRings): {
|
|
295
241
|
nested: number
|
|
@@ -299,8 +245,7 @@ export function ringAreaReadings(polygons: MultiPolygonRings): {
|
|
|
299
245
|
let allExterior = 0
|
|
300
246
|
|
|
301
247
|
for (const rings of polygons) {
|
|
302
|
-
// Per
|
|
303
|
-
// hole, and a pooled sum would silently cancel them against each other.
|
|
248
|
+
// Per polygon rather than pooled, so two disjoint polygons wound opposite ways do not cancel out.
|
|
304
249
|
let signedTotal = 0
|
|
305
250
|
|
|
306
251
|
for (const ring of rings) {
|
|
@@ -317,34 +262,13 @@ export function ringAreaReadings(polygons: MultiPolygonRings): {
|
|
|
317
262
|
}
|
|
318
263
|
|
|
319
264
|
/**
|
|
320
|
-
*
|
|
321
|
-
|
|
322
|
-
export interface DegreeExtent {
|
|
323
|
-
minLon: number
|
|
324
|
-
minLat: number
|
|
325
|
-
maxLon: number
|
|
326
|
-
maxLat: number
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Refuse a feature whose reprojected vertices fall outside the publisher's own declared extent.
|
|
331
|
-
*
|
|
332
|
-
* THE CHECK A PROJECTION CHECK CANNOT MAKE. A swapped coordinate order survives an authority-code comparison — both
|
|
333
|
-
* axes are still numbers in a plausible range — and a source read in its own metres as if they were degrees produces
|
|
334
|
-
* perfectly well-formed coordinates in the wrong ocean. Both show up here on the first feature, before a whole layer is
|
|
335
|
-
* written to the wrong side of the planet.
|
|
336
|
-
*
|
|
337
|
-
* SHARED BY EVERY POLYGON INGEST, because it is rectangle arithmetic over the ring types and knows nothing about any
|
|
338
|
-
* product. `marginDegrees` is the caller's, because a declared extent is itself a rounded published value and how
|
|
339
|
-
* tightly a source hugs its own is a fact about that source.
|
|
340
|
-
*
|
|
341
|
-
* @param context Names the calling ingest in the refusal, so a build log says which layer stopped.
|
|
342
|
-
* @throws {RangeError} On the first vertex outside the extent.
|
|
265
|
+
* Refuse a feature whose reprojected vertices fall outside the publisher's own
|
|
266
|
+
* declared extent, throwing on the first vertex outside it.
|
|
343
267
|
*/
|
|
344
268
|
export function assertRingsInsideExtent(
|
|
345
269
|
polygons: MultiPolygonRings,
|
|
346
270
|
label: string,
|
|
347
|
-
extent:
|
|
271
|
+
extent: LatLonBounds,
|
|
348
272
|
marginDegrees: number,
|
|
349
273
|
context = "polygon ingest"
|
|
350
274
|
): void {
|
|
@@ -382,20 +306,8 @@ export interface EncodedArea {
|
|
|
382
306
|
}
|
|
383
307
|
|
|
384
308
|
/**
|
|
385
|
-
* A point
|
|
386
|
-
*
|
|
387
|
-
* The bounding-box centre is tried first; where it is not inside — a crescent, a band hugging a river, a polygon with a
|
|
388
|
-
* hole through its middle — a small deterministic grid over the box is scanned. A polygon no grid point lands inside is
|
|
389
|
-
* refused (`undefined`) rather than approximated, because a sample point that is not actually inside the polygon turns
|
|
390
|
-
* an agreement check into a check on the sampler.
|
|
391
|
-
*
|
|
392
|
-
* SHARED BY EVERY POLYGON LAYER'S VERIFY, because it is bounding-box arithmetic over the ring blob and knows nothing
|
|
393
|
-
* about any product. `gridSteps` is the one thing that differs between them: a layer whose polygons are narrow strips
|
|
394
|
-
* needs a finer grid than one whose polygons are compact, and the value is part of a layer's sampling receipt — two
|
|
395
|
-
* runs of the same layer must draw the same points, so it is a caller's choice rather than a shared default nobody
|
|
396
|
-
* owns.
|
|
397
|
-
*
|
|
398
|
-
* @param gridSteps Grid divisions per axis. Only `steps − 1` interior lines are tested, so 7 gives a 6 × 6 grid.
|
|
309
|
+
* A point inside one stored polygon for a verification sampler, returning `undefined` when neither
|
|
310
|
+
* the bounding-box center nor any of the `gridSteps − 1` interior grid lines lands inside.
|
|
399
311
|
*/
|
|
400
312
|
export function interiorPointOfEncodedRings(
|
|
401
313
|
area: EncodedArea,
|
|
@@ -421,8 +333,7 @@ export function interiorPointOfEncodedRings(
|
|
|
421
333
|
}
|
|
422
334
|
|
|
423
335
|
/**
|
|
424
|
-
* Whether a stored polygon's precomputed bounding box contains the point
|
|
425
|
-
* only pulled off disk for a polygon that could contain the point.
|
|
336
|
+
* Whether a stored polygon's precomputed bounding box contains the point.
|
|
426
337
|
*/
|
|
427
338
|
export function bboxContains(
|
|
428
339
|
bounds: Pick<EncodedArea, "min_lat" | "min_lon" | "max_lat" | "max_lon">,
|
|
@@ -438,15 +349,7 @@ export function bboxContains(
|
|
|
438
349
|
}
|
|
439
350
|
|
|
440
351
|
/**
|
|
441
|
-
* A reproducible sample of interior points drawn by a deterministic
|
|
442
|
-
* layer's verification sampler shares.
|
|
443
|
-
*
|
|
444
|
-
* The keys are chosen before any geometry is read: selecting them alone is an index-only walk over the primary key, and
|
|
445
|
-
* the rows they name are then fetched by key. The draw is deterministic rather than random, so a re-run compares the
|
|
446
|
-
* same points and a disagreement can be looked at rather than re-rolled.
|
|
447
|
-
*
|
|
448
|
-
* @param options.gridSteps The interior-point search depth, per {@link interiorPointOfEncodedRings} — part of a layer's
|
|
449
|
-
* sampling receipt, so it is a caller's choice rather than a shared default nobody owns.
|
|
352
|
+
* A reproducible sample of interior points drawn by a deterministic stride over a key list.
|
|
450
353
|
*/
|
|
451
354
|
export function strideSampleInteriorPoints<Area extends EncodedArea, Point>(
|
|
452
355
|
keys: readonly string[],
|
package/lib/google/place-id.ts
CHANGED
|
@@ -11,12 +11,14 @@ import type { Tagged } from "type-fest"
|
|
|
11
11
|
/**
|
|
12
12
|
* A Place ID uniquely identifies a place in the Google Places database and on Google Maps.
|
|
13
13
|
*
|
|
14
|
-
* The length of the identifier may vary.
|
|
14
|
+
* The length of the identifier may vary.
|
|
15
|
+
* Generally, the identifier is a 27-character string, however, more specific
|
|
15
16
|
* places may have longer identifiers.
|
|
16
17
|
*
|
|
17
18
|
* Place IDs appear to be base64-encoded strings, delimited by underscores and dashes.
|
|
18
19
|
*
|
|
19
|
-
* Note that Place IDs do change.
|
|
20
|
+
* Note that Place IDs do change.
|
|
21
|
+
* Consider a them stale after a few days.
|
|
20
22
|
*
|
|
21
23
|
* @category Google
|
|
22
24
|
* @category Geocoding
|