@mailwoman/spatial 9.4.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 (156) hide show
  1. package/README.md +2 -2
  2. package/lib/bbox.ts +41 -34
  3. package/lib/{coordinate-bounds.ts → coordinate/bounds.ts} +2 -2
  4. package/lib/{coordinate-formats.ts → coordinate/formats.ts} +11 -28
  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 +6 -6
  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 +20 -4
  22. package/lib/projection/transform.ts +114 -0
  23. package/lib/projection.ts +1 -1
  24. package/lib/well-known-text.ts +8 -8
  25. package/out/bbox.d.ts +16 -13
  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 → coordinate/bounds.d.ts} +3 -3
  30. package/out/coordinate/bounds.d.ts.map +1 -0
  31. package/out/{coordinate-bounds.js → coordinate/bounds.js} +3 -3
  32. package/out/coordinate/bounds.js.map +1 -0
  33. package/out/{coordinate-formats.d.ts → coordinate/formats.d.ts} +9 -12
  34. package/out/coordinate/formats.d.ts.map +1 -0
  35. package/out/{coordinate-formats.js → coordinate/formats.js} +12 -29
  36. package/out/coordinate/formats.js.map +1 -0
  37. package/out/distance.d.ts +48 -25
  38. package/out/distance.d.ts.map +1 -1
  39. package/out/distance.js +53 -27
  40. package/out/distance.js.map +1 -1
  41. package/out/feature.d.ts +2 -2
  42. package/out/feature.d.ts.map +1 -1
  43. package/out/geometries/point.d.ts +30 -22
  44. package/out/geometries/point.d.ts.map +1 -1
  45. package/out/geometries/point.js +35 -29
  46. package/out/geometries/point.js.map +1 -1
  47. package/out/geometries/polygon.d.ts +23 -122
  48. package/out/geometries/polygon.d.ts.map +1 -1
  49. package/out/geometries/polygon.js +13 -41
  50. package/out/geometries/polygon.js.map +1 -1
  51. package/out/geometries/ring-blob.d.ts +23 -109
  52. package/out/geometries/ring-blob.d.ts.map +1 -1
  53. package/out/geometries/ring-blob.js +29 -115
  54. package/out/geometries/ring-blob.js.map +1 -1
  55. package/out/{geometries/index.js → geometries.d.ts} +1 -1
  56. package/out/geometries.d.ts.map +1 -0
  57. package/out/{geometries/index.d.ts → geometries.js} +1 -1
  58. package/out/geometries.js.map +1 -0
  59. package/out/google/place-id.d.ts +4 -2
  60. package/out/google/place-id.d.ts.map +1 -1
  61. package/out/google/place-id.js.map +1 -1
  62. package/out/{google/index.d.ts → google.d.ts} +1 -1
  63. package/out/google.d.ts.map +1 -0
  64. package/out/{google/index.js → google.js} +1 -1
  65. package/out/google.js.map +1 -0
  66. package/out/h3/cell.d.ts +19 -73
  67. package/out/h3/cell.d.ts.map +1 -1
  68. package/out/h3/cell.js +20 -63
  69. package/out/h3/cell.js.map +1 -1
  70. package/out/h3/coverage.d.ts +11 -7
  71. package/out/h3/coverage.d.ts.map +1 -1
  72. package/out/h3/coverage.js +11 -7
  73. package/out/h3/coverage.js.map +1 -1
  74. package/out/h3/interior-cells.d.ts +9 -28
  75. package/out/h3/interior-cells.d.ts.map +1 -1
  76. package/out/h3/interior-cells.js +13 -32
  77. package/out/h3/interior-cells.js.map +1 -1
  78. package/out/h3/polygon-cells.d.ts +28 -120
  79. package/out/h3/polygon-cells.d.ts.map +1 -1
  80. package/out/h3/polygon-cells.js +48 -146
  81. package/out/h3/polygon-cells.js.map +1 -1
  82. package/out/{h3/index.d.ts → h3.d.ts} +2 -2
  83. package/out/h3.d.ts.map +1 -0
  84. package/out/{h3/index.js → h3.js} +2 -2
  85. package/out/h3.js.map +1 -0
  86. package/out/index.d.ts +6 -6
  87. package/out/index.d.ts.map +1 -1
  88. package/out/index.js +6 -6
  89. package/out/index.js.map +1 -1
  90. package/out/objects.d.ts +6 -6
  91. package/out/objects.d.ts.map +1 -1
  92. package/out/open-location-code.d.ts +11 -22
  93. package/out/open-location-code.d.ts.map +1 -1
  94. package/out/open-location-code.js +9 -40
  95. package/out/open-location-code.js.map +1 -1
  96. package/out/osgb36.d.ts +21 -97
  97. package/out/osgb36.d.ts.map +1 -1
  98. package/out/osgb36.js +12 -165
  99. package/out/osgb36.js.map +1 -1
  100. package/out/polyline.d.ts +5 -3
  101. package/out/polyline.d.ts.map +1 -1
  102. package/out/polyline.js +5 -3
  103. package/out/polyline.js.map +1 -1
  104. package/out/position.d.ts +17 -3
  105. package/out/position.d.ts.map +1 -1
  106. package/out/position.js +2 -2
  107. package/out/position.js.map +1 -1
  108. package/out/projection/transform.d.ts +70 -0
  109. package/out/projection/transform.d.ts.map +1 -0
  110. package/out/{projection-transform.js → projection/transform.js} +15 -17
  111. package/out/projection/transform.js.map +1 -0
  112. package/out/projection.d.ts +1 -1
  113. package/out/projection.d.ts.map +1 -1
  114. package/out/projection.js +1 -1
  115. package/out/regions/codes.d.ts.map +1 -1
  116. package/out/{regions/index.js → regions.d.ts} +1 -1
  117. package/out/regions.d.ts.map +1 -0
  118. package/out/{regions/index.d.ts → regions.js} +1 -1
  119. package/out/regions.js.map +1 -0
  120. package/out/tools/ogr/stream.d.ts +44 -0
  121. package/out/tools/ogr/stream.d.ts.map +1 -0
  122. package/out/tools/{ogr-stream.js → ogr/stream.js} +17 -10
  123. package/out/tools/ogr/stream.js.map +1 -0
  124. package/out/tools/ogr.d.ts +13 -10
  125. package/out/tools/ogr.d.ts.map +1 -1
  126. package/out/tools/ogr.js +18 -14
  127. package/out/tools/ogr.js.map +1 -1
  128. package/out/tools/tsconfig.tsbuildinfo +1 -0
  129. package/out/tsconfig.test.tsbuildinfo +1 -1
  130. package/out/well-known-text.d.ts +8 -8
  131. package/out/well-known-text.js +7 -7
  132. package/package.json +35 -127
  133. package/{lib/tools/ogr-stream.ts → tools/ogr/stream.ts} +25 -14
  134. package/{lib/tools → tools}/ogr.ts +26 -20
  135. package/lib/projection-transform.ts +0 -115
  136. package/out/coordinate-bounds.d.ts.map +0 -1
  137. package/out/coordinate-bounds.js.map +0 -1
  138. package/out/coordinate-formats.d.ts.map +0 -1
  139. package/out/coordinate-formats.js.map +0 -1
  140. package/out/geometries/index.d.ts.map +0 -1
  141. package/out/geometries/index.js.map +0 -1
  142. package/out/google/index.d.ts.map +0 -1
  143. package/out/google/index.js.map +0 -1
  144. package/out/h3/index.d.ts.map +0 -1
  145. package/out/h3/index.js.map +0 -1
  146. package/out/projection-transform.d.ts +0 -67
  147. package/out/projection-transform.d.ts.map +0 -1
  148. package/out/projection-transform.js.map +0 -1
  149. package/out/regions/index.d.ts.map +0 -1
  150. package/out/regions/index.js.map +0 -1
  151. package/out/tools/ogr-stream.d.ts +0 -34
  152. package/out/tools/ogr-stream.d.ts.map +0 -1
  153. package/out/tools/ogr-stream.js.map +0 -1
  154. /package/lib/{geometries/index.ts → geometries.ts} +0 -0
  155. /package/lib/{google/index.ts → google.ts} +0 -0
  156. /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
- * - A linear ring is a closed LineString with four or more positions.
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 with islands or a lake with islands.
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
- * - A linear ring is a closed LineString with four or more positions.
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 BOTH paths because distinguishing them is the function's job: defaulted to
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: positions of `[lon, lat, …]`.
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]` — a `Polygon`'s `coordinates`, read loosely (see
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 into the same shape by
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 ONE linear ring — the even-odd crossing count. Shoot a ray along +lon and toggle on every
149
- * edge crossing.
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 ANY polygon of a multi-polygon.
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. Nothing has checked the arity of a position and
221
- * `type` is whatever the source wrote, so a reader narrows on `type` and casts `coordinates` — {@linkcode arealPolygons}
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, whichever areal type it arrived as — `null` when the
228
- * geometry is not areal (a Point or a LineString bounds no area).
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 MUST be areal — {@linkcode arealPolygons} with the refusal every polygon ingest was
245
- * writing for itself.
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 — the exterior
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
- * The ring blob — how a polygon layer stores one authority feature's UNSIMPLIFIED coordinates, and the
7
- * point test that reads it without materializing them.
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. A reader that meets a different number throws rather than reinterpreting
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 the repeat that closes it (RFC 7946
54
- * §3.1.6). Fewer is a degenerate ring, and encoding one would store a polygon no point can be inside.
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 flat `[lon, lat, lon, lat, …]` run.
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 — what both the point test and the decoder walk.
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 and this view is legal
164
- // on a shared buffer whose own offset is aligned. A misaligned source buffer is copied rather than rejected.
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. Mirrors {@linkcode pointInRing} exactly —
214
- * same predicate, same tie behavior — so a decoded ring and an encoded one never disagree.
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 — for tests, for a debug render, and for the round-trip
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 metres — the sphere the ring areas below are measured on.
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 metres. **CLOCKWISE is positive**, counter-clockwise negative.
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 metres: `nested` respects ring orientation (holes subtract) and
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 POLYGON, not pooled: two disjoint polygons of one feature can wind opposite ways without either being a
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
- * A rectangle in CRS84 degrees.
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: DegreeExtent,
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 INSIDE one stored polygon, for a verification sampler.
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 — the ray cast's prefilter, so the blob is
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 STRIDE over a key list — the shape every polygon
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[],
@@ -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. Generally, the identifier is a 27-character string, however, more specific
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. Consider a them stale after a few days.
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