@mailwoman/spatial 9.1.0 → 9.3.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 (161) hide show
  1. package/{bbox.ts → lib/bbox.ts} +54 -3
  2. package/lib/coordinate-bounds.ts +52 -0
  3. package/{coordinate-formats.ts → lib/coordinate-formats.ts} +16 -3
  4. package/lib/distance.ts +165 -0
  5. package/{feature.ts → lib/feature.ts} +1 -1
  6. package/{geometries → lib/geometries}/collection.ts +4 -4
  7. package/lib/geometries/index.ts +13 -0
  8. package/{geometries → lib/geometries}/line-string.ts +2 -2
  9. package/{geometries → lib/geometries}/point.ts +9 -9
  10. package/{geometries → lib/geometries}/polygon.ts +93 -89
  11. package/lib/geometries/ring-blob.ts +521 -0
  12. package/{google → lib/google}/index.ts +1 -1
  13. package/{h3 → lib/h3}/cell.ts +83 -1
  14. package/lib/h3/coverage.ts +45 -0
  15. package/{h3 → lib/h3}/index.ts +5 -3
  16. package/lib/h3/interior-cells.ts +138 -0
  17. package/lib/h3/polygon-cells.ts +462 -0
  18. package/lib/index.ts +21 -0
  19. package/{objects.ts → lib/objects.ts} +1 -1
  20. package/lib/open-location-code.ts +168 -0
  21. package/{osgb36.ts → lib/osgb36.ts} +2 -2
  22. package/{polyline.ts → lib/polyline.ts} +1 -1
  23. package/lib/position.ts +216 -0
  24. package/lib/projection-transform.ts +115 -0
  25. package/{regions → lib/regions}/codes.ts +1 -1
  26. package/{regions → lib/regions}/index.ts +2 -2
  27. package/lib/tools/ogr-stream.ts +99 -0
  28. package/lib/tools/ogr.ts +201 -0
  29. package/{sdk → lib}/well-known-text.ts +2 -1
  30. package/out/bbox.d.ts +23 -2
  31. package/out/bbox.d.ts.map +1 -1
  32. package/out/bbox.js +33 -2
  33. package/out/bbox.js.map +1 -1
  34. package/out/coordinate-bounds.d.ts +41 -0
  35. package/out/coordinate-bounds.d.ts.map +1 -0
  36. package/out/coordinate-bounds.js +45 -0
  37. package/out/coordinate-bounds.js.map +1 -0
  38. package/out/coordinate-formats.d.ts +8 -0
  39. package/out/coordinate-formats.d.ts.map +1 -1
  40. package/out/coordinate-formats.js +14 -3
  41. package/out/coordinate-formats.js.map +1 -1
  42. package/out/distance.d.ts +84 -0
  43. package/out/distance.d.ts.map +1 -0
  44. package/out/distance.js +132 -0
  45. package/out/distance.js.map +1 -0
  46. package/out/feature.d.ts +1 -1
  47. package/out/feature.d.ts.map +1 -1
  48. package/out/feature.js.map +1 -1
  49. package/out/geometries/collection.d.ts +4 -4
  50. package/out/geometries/collection.d.ts.map +1 -1
  51. package/out/geometries/collection.js.map +1 -1
  52. package/out/geometries/index.d.ts +5 -4
  53. package/out/geometries/index.d.ts.map +1 -1
  54. package/out/geometries/index.js +5 -4
  55. package/out/geometries/index.js.map +1 -1
  56. package/out/geometries/line-string.d.ts +2 -2
  57. package/out/geometries/line-string.d.ts.map +1 -1
  58. package/out/geometries/line-string.js.map +1 -1
  59. package/out/geometries/point.d.ts +9 -9
  60. package/out/geometries/point.d.ts.map +1 -1
  61. package/out/geometries/point.js +9 -9
  62. package/out/geometries/point.js.map +1 -1
  63. package/out/geometries/polygon.d.ts +60 -56
  64. package/out/geometries/polygon.d.ts.map +1 -1
  65. package/out/geometries/polygon.js +66 -63
  66. package/out/geometries/polygon.js.map +1 -1
  67. package/out/geometries/ring-blob.d.ts +183 -0
  68. package/out/geometries/ring-blob.d.ts.map +1 -0
  69. package/out/geometries/ring-blob.js +376 -0
  70. package/out/geometries/ring-blob.js.map +1 -0
  71. package/out/google/index.d.ts +1 -1
  72. package/out/google/index.d.ts.map +1 -1
  73. package/out/google/index.js +1 -1
  74. package/out/google/index.js.map +1 -1
  75. package/out/google/place-id.d.ts.map +1 -1
  76. package/out/google/place-id.js.map +1 -1
  77. package/out/h3/cell.d.ts +32 -0
  78. package/out/h3/cell.d.ts.map +1 -1
  79. package/out/h3/cell.js +68 -1
  80. package/out/h3/cell.js.map +1 -1
  81. package/out/h3/coverage.d.ts +32 -0
  82. package/out/h3/coverage.d.ts.map +1 -0
  83. package/out/h3/coverage.js +27 -0
  84. package/out/h3/coverage.js.map +1 -0
  85. package/out/h3/index.d.ts +5 -3
  86. package/out/h3/index.d.ts.map +1 -1
  87. package/out/h3/index.js +5 -3
  88. package/out/h3/index.js.map +1 -1
  89. package/out/h3/interior-cells.d.ts +54 -0
  90. package/out/h3/interior-cells.d.ts.map +1 -0
  91. package/out/h3/interior-cells.js +111 -0
  92. package/out/h3/interior-cells.js.map +1 -0
  93. package/out/h3/polygon-cells.d.ts +182 -0
  94. package/out/h3/polygon-cells.d.ts.map +1 -0
  95. package/out/h3/polygon-cells.js +339 -0
  96. package/out/h3/polygon-cells.js.map +1 -0
  97. package/out/index.d.ts +15 -12
  98. package/out/index.d.ts.map +1 -1
  99. package/out/index.js +15 -12
  100. package/out/index.js.map +1 -1
  101. package/out/objects.d.ts +1 -1
  102. package/out/objects.d.ts.map +1 -1
  103. package/out/objects.js +1 -1
  104. package/out/objects.js.map +1 -1
  105. package/out/open-location-code.d.ts +48 -0
  106. package/out/open-location-code.d.ts.map +1 -0
  107. package/out/open-location-code.js +137 -0
  108. package/out/open-location-code.js.map +1 -0
  109. package/out/osgb36.d.ts +2 -2
  110. package/out/osgb36.d.ts.map +1 -1
  111. package/out/osgb36.js +1 -1
  112. package/out/osgb36.js.map +1 -1
  113. package/out/polyline.d.ts.map +1 -1
  114. package/out/polyline.js +1 -1
  115. package/out/polyline.js.map +1 -1
  116. package/out/position.d.ts +4 -61
  117. package/out/position.d.ts.map +1 -1
  118. package/out/position.js +14 -126
  119. package/out/position.js.map +1 -1
  120. package/out/projection-transform.d.ts +67 -0
  121. package/out/projection-transform.d.ts.map +1 -0
  122. package/out/projection-transform.js +48 -0
  123. package/out/projection-transform.js.map +1 -0
  124. package/out/projection.d.ts.map +1 -1
  125. package/out/projection.js.map +1 -1
  126. package/out/regions/codes.d.ts.map +1 -1
  127. package/out/regions/codes.js.map +1 -1
  128. package/out/regions/index.d.ts +2 -2
  129. package/out/regions/index.d.ts.map +1 -1
  130. package/out/regions/index.js +2 -2
  131. package/out/regions/index.js.map +1 -1
  132. package/out/regions/names.d.ts.map +1 -1
  133. package/out/regions/names.js.map +1 -1
  134. package/out/tools/ogr-stream.d.ts +34 -0
  135. package/out/tools/ogr-stream.d.ts.map +1 -0
  136. package/out/tools/ogr-stream.js +74 -0
  137. package/out/tools/ogr-stream.js.map +1 -0
  138. package/out/tools/ogr.d.ts +87 -0
  139. package/out/tools/ogr.d.ts.map +1 -0
  140. package/out/tools/ogr.js +84 -0
  141. package/out/tools/ogr.js.map +1 -0
  142. package/out/tsconfig.test.tsbuildinfo +1 -1
  143. package/out/{sdk/well-known-text.d.ts → well-known-text.d.ts} +1 -1
  144. package/out/well-known-text.d.ts.map +1 -0
  145. package/out/well-known-text.js.map +1 -0
  146. package/package.json +143 -14
  147. package/geometries/index.ts +0 -12
  148. package/index.ts +0 -18
  149. package/out/sdk/index.d.ts +0 -7
  150. package/out/sdk/index.d.ts.map +0 -1
  151. package/out/sdk/index.js +0 -7
  152. package/out/sdk/index.js.map +0 -1
  153. package/out/sdk/well-known-text.d.ts.map +0 -1
  154. package/out/sdk/well-known-text.js.map +0 -1
  155. package/out/tsconfig.tsbuildinfo +0 -1
  156. package/position.ts +0 -345
  157. package/sdk/index.ts +0 -7
  158. /package/{google → lib/google}/place-id.ts +0 -0
  159. /package/{projection.ts → lib/projection.ts} +0 -0
  160. /package/{regions → lib/regions}/names.ts +0 -0
  161. /package/out/{sdk/well-known-text.js → well-known-text.js} +0 -0
@@ -4,10 +4,9 @@
4
4
  * @author Teffen Ellis, et al.
5
5
  */
6
6
 
7
- import { ResourceError } from "@mailwoman/core/errors"
8
-
9
- import type { GeoObjectLiteral } from "../objects.ts"
10
- import type { LineStringPath } from "./line-string.ts"
7
+ import type { GeometryLiteral } from "#geometries/collection"
8
+ import type { LineStringPath } from "#geometries/line-string"
9
+ import type { GeoObjectLiteral } from "#objects"
11
10
 
12
11
  /**
13
12
  * An array of positions forming a closed shape, such as a country or a lake.
@@ -90,7 +89,7 @@ export type PolygonPath = SolidPolygonPath | NestedPolygonPath
90
89
  /**
91
90
  * An array of positions forming a closed shape, such as a country or a lake.
92
91
  */
93
- export interface PolygonLiteral<P extends PolygonPath = SolidPolygonPath> extends GeoObjectLiteral {
92
+ export interface PolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
94
93
  /**
95
94
  * Declares the type of GeoJSON object as a `Polygon` geometry.
96
95
  */
@@ -117,7 +116,9 @@ export function isPolygonLiteral<P extends PolygonPath = PolygonPath>(input: unk
117
116
  /**
118
117
  * Predicate for checking if a polygon geometry is a solid, i.e. it has no holes.
119
118
  */
120
- export function isSolidPolygonPath(input: PolygonLiteral): boolean {
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.
121
+ export function isSolidPolygonPath(input: PolygonLiteral<PolygonPath>): boolean {
121
122
  return input.coordinates.length === 1
122
123
  }
123
124
 
@@ -131,6 +132,18 @@ export function isSolidPolygonPath(input: PolygonLiteral): boolean {
131
132
  */
132
133
  export type ContainmentRing = readonly (readonly number[])[]
133
134
 
135
+ /**
136
+ * One polygon's rings: `[exterior, ...holes]` — a `Polygon`'s `coordinates`, read loosely (see
137
+ * {@linkcode ContainmentRing}).
138
+ */
139
+ export type PolygonRings = readonly ContainmentRing[]
140
+
141
+ /**
142
+ * A feature's polygons — `MultiPolygon` coordinates, with a bare `Polygon` lifted into the same shape by
143
+ * {@linkcode arealPolygons}.
144
+ */
145
+ export type MultiPolygonRings = readonly PolygonRings[]
146
+
134
147
  /**
135
148
  * Ray-cast a point against ONE linear ring — the even-odd crossing count. Shoot a ray along +lon and toggle on every
136
149
  * edge crossing.
@@ -163,7 +176,7 @@ export function pointInRing(lon: number, lat: number, ring: ContainmentRing): bo
163
176
  * without depending on ring winding order. GeoJSON nominally specifies orientation, but the gazetteer sources do not
164
177
  * reliably honour it, so the orientation-free rule is the one that survives real data.
165
178
  */
166
- export function pointInPolygon(lon: number, lat: number, rings: readonly ContainmentRing[]): boolean {
179
+ export function pointInPolygon(lon: number, lat: number, rings: PolygonRings): boolean {
167
180
  let inside = false
168
181
 
169
182
  for (const ring of rings) {
@@ -189,7 +202,7 @@ export function pointInMultiPolygon(
189
202
  /**
190
203
  * A collection of polygons, such as a country with islands or a lake with islands.
191
204
  */
192
- export interface MultiPolygonLiteral<P extends PolygonPath = SolidPolygonPath> extends GeoObjectLiteral {
205
+ export interface MultiPolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
193
206
  type: "MultiPolygon"
194
207
 
195
208
  /**
@@ -201,110 +214,101 @@ export interface MultiPolygonLiteral<P extends PolygonPath = SolidPolygonPath> e
201
214
  coordinates: P[]
202
215
  }
203
216
 
217
+ //#region Ring-list geometry — the parsed-GeoJSON shape
218
+
204
219
  /**
205
- * Predicate for checking if a GeoJSON object is a `MultiPolygon` geometry.
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.
206
223
  */
224
+ export type ParsedGeometry = GeometryLiteral | { type: string; coordinates?: unknown }
207
225
 
208
226
  /**
209
- * Given a polygon geometry, return an OSM filter string.
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).
210
229
  *
211
- * This is useful when working with the Overpass API.
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.
212
232
  */
213
- export function polygonToOSMFilter(input: PolygonLiteral): string {
214
- if (!isPolygonLiteral(input)) return ""
233
+ export function arealPolygons(geometry: ParsedGeometry | null | undefined): MultiPolygonRings | null {
234
+ if (!geometry) return null
215
235
 
216
- const [exteriorRing] = input.coordinates
236
+ if (geometry.type === "Polygon") return [geometry.coordinates as PolygonRings]
217
237
 
218
- const filter = exteriorRing.map(([lon, lat]) => `${lat} ${lon}`).join(" ")
238
+ if (geometry.type === "MultiPolygon") return geometry.coordinates as MultiPolygonRings
219
239
 
220
- return `poly:'${filter}'`
240
+ return null
221
241
  }
222
242
 
223
243
  /**
224
- * Tags returned by the Overpass API for a node.
244
+ * The polygons of a geometry that MUST be areal — {@linkcode arealPolygons} with the refusal every polygon ingest was
245
+ * writing for itself.
225
246
  *
226
- * @category OSM
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`.
227
250
  */
228
- export const OSMNodeTag = {
229
- HouseNumber: "addr:housenumber",
230
- PostCode: "addr:postcode",
231
- Street: "addr:street",
232
- State: "addr:state",
233
- City: "addr:city",
234
- Website: "website",
235
- Email: "email",
236
- Phone: "phone",
237
- Shop: "shop",
238
- Brand: "brand",
239
- Cuisine: "cuisine",
240
- Name: "name",
241
- Healthcare: "healthcare",
242
- Office: "office",
243
- Amenity: "amenity",
244
- } as const
245
-
246
- export type OSMNodeTag = (typeof OSMNodeTag)[keyof typeof OSMNodeTag]
251
+ export function requireArealPolygons(geometry: ParsedGeometry, subject: string, context: string): MultiPolygonRings {
252
+ const polygons = arealPolygons(geometry)
247
253
 
248
- /**
249
- * OSM node tags that disqualify a node from being treated as residential — a node carrying one of these is
250
- * infrastructure or commercial, whatever else it claims.
251
- */
252
- export const ForbiddenResidentialOSMNodeTags: ReadonlySet<OSMNodeTag> = new Set<OSMNodeTag>([
253
- OSMNodeTag.Shop,
254
- OSMNodeTag.Brand,
255
- OSMNodeTag.Cuisine,
256
- OSMNodeTag.Office,
257
- OSMNodeTag.Healthcare,
258
- ])
259
-
260
- export type OSMNodeTagRecord = Record<OSMNodeTag, string | undefined>
261
-
262
- export interface OSMOverpassElement {
263
- type: "node"
264
- id: number
265
- lat: number
266
- lon: number
267
- tags: OSMNodeTagRecord
268
- }
254
+ if (polygons) return polygons
269
255
 
270
- export interface OSMOverpassResponseBody {
271
- version: string
272
- generator: string
273
- osm3s: {
274
- timestamp_osm_base: string
275
- copyright: string
276
- }
277
- elements: OSMOverpassElement[]
256
+ throw new Error(`${context}: ${subject} is a ${geometry.type}, expected Polygon or MultiPolygon`)
278
257
  }
279
258
 
280
259
  /**
281
- * Given an OSM element, attempts to infer if the result is a residential address.
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.
282
269
  */
283
- export function isResidentialElement(element: OSMOverpassElement): boolean {
284
- for (const key in element.tags) {
285
- if (ForbiddenResidentialOSMNodeTags.has(key as OSMNodeTag)) return false
286
- }
270
+ export function geometryContains(
271
+ geometry: ParsedGeometry | null | undefined,
272
+ lon: number,
273
+ lat: number
274
+ ): boolean | null {
275
+ const polygons = arealPolygons(geometry)
287
276
 
288
- if (element.tags[OSMNodeTag.Amenity] === "restaurant") return false
277
+ if (!polygons) return null
289
278
 
290
- return true
279
+ return polygons.some((rings) => pointInPolygon(lon, lat, rings))
291
280
  }
292
281
 
293
- export function fetchOSMElementViaOverpassAPI(input: PolygonLiteral): Promise<OSMOverpassElement[]> {
294
- const filter = polygonToOSMFilter(input)
295
-
296
- const url = new URL("http://overpass-api.de/api/interpreter")
297
- url.searchParams.set("data", `[out:json];(node['addr:housenumber'](${filter}););out body;>;out skel qt;`)
298
-
299
- return fetch(url)
300
- .then((response) => {
301
- if (!response.ok) throw ResourceError.from(response.status, response.statusText, "osm", "overpass-api", "fetch")
282
+ /**
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.
290
+ */
291
+ export function rectangleRing(minLon: number, minLat: number, maxLon: number, maxLat: number): number[][] {
292
+ return [
293
+ [minLon, minLat],
294
+ [maxLon, minLat],
295
+ [maxLon, maxLat],
296
+ [minLon, maxLat],
297
+ [minLon, minLat],
298
+ ]
299
+ }
302
300
 
303
- return response
304
- })
305
- .then((response) => response.json() as Promise<OSMOverpassResponseBody>)
306
- .then((body) => body.elements)
307
- .catch((error) => {
308
- throw ResourceError.wrap(error, "osm", "overpass-api", "fetch")
309
- })
301
+ /**
302
+ * The same rectangle wound the other way — a hole, under the GeoJSON convention.
303
+ */
304
+ export function reversedRing(minLon: number, minLat: number, maxLon: number, maxLat: number): number[][] {
305
+ return [
306
+ [minLon, minLat],
307
+ [minLon, maxLat],
308
+ [maxLon, maxLat],
309
+ [maxLon, minLat],
310
+ [minLon, minLat],
311
+ ]
310
312
  }
313
+
314
+ //#endregion