@mailwoman/spatial 10.0.0 → 10.2.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 +21 -33
- package/lib/distance.ts +56 -28
- package/lib/feature.ts +2 -2
- package/lib/geometries/point.ts +42 -35
- package/lib/geometries/polygon.ts +27 -135
- package/lib/geometries/ring-blob.ts +35 -132
- package/lib/google/place-id.ts +4 -2
- package/lib/h3/cell.ts +23 -78
- package/lib/h3/coverage.ts +13 -9
- package/lib/h3/interior-cells.ts +13 -34
- package/lib/h3/polygon-cells.ts +61 -167
- package/lib/{h3/index.ts → h3.ts} +1 -1
- package/lib/index.ts +6 -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 +39 -20
- package/lib/projection/transform.ts +43 -44
- package/lib/{projection/index.ts → projection.ts} +1 -1
- package/lib/well-known-text.ts +9 -9
- 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 +17 -16
- package/out/coordinate/formats.d.ts.map +1 -1
- package/out/coordinate/formats.js +20 -32
- package/out/coordinate/formats.js.map +1 -1
- package/out/distance.d.ts +51 -26
- 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 +37 -30
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +27 -126
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +14 -42
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +24 -110
- package/out/geometries/ring-blob.d.ts.map +1 -1
- package/out/geometries/ring-blob.js +30 -116
- 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 +22 -65
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +12 -8
- package/out/h3/coverage.d.ts.map +1 -1
- package/out/h3/coverage.js +12 -8
- 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 +50 -148
- 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 +6 -5
- package/out/index.d.ts.map +1 -1
- package/out/index.js +6 -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 +36 -20
- package/out/position.d.ts.map +1 -1
- package/out/position.js +0 -1
- package/out/position.js.map +1 -1
- package/out/projection/transform.d.ts +33 -30
- package/out/projection/transform.d.ts.map +1 -1
- package/out/projection/transform.js +15 -17
- 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 +17 -10
- package/out/tools/ogr/stream.js.map +1 -1
- package/out/tools/{ogr/index.d.ts → ogr.d.ts} +25 -15
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/{ogr/index.js → ogr.js} +25 -21
- 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 +9 -9
- package/out/well-known-text.d.ts.map +1 -1
- package/out/well-known-text.js +7 -7
- package/out/well-known-text.js.map +1 -1
- package/package.json +37 -129
- package/{lib/tools → tools}/ogr/stream.ts +26 -15
- package/{lib/tools/ogr/index.ts → tools/ogr.ts} +44 -30
- package/out/geometries/index.d.ts.map +0 -1
- package/out/geometries/index.js.map +0 -1
- package/out/google/index.d.ts.map +0 -1
- package/out/google/index.js.map +0 -1
- package/out/h3/index.d.ts.map +0 -1
- package/out/h3/index.js.map +0 -1
- package/out/projection/index.d.ts.map +0 -1
- package/out/projection/index.js.map +0 -1
- package/out/regions/index.d.ts.map +0 -1
- package/out/regions/index.js.map +0 -1
- package/out/tools/ogr/index.d.ts.map +0 -1
- package/out/tools/ogr/index.js.map +0 -1
- /package/lib/{geometries/index.ts → geometries.ts} +0 -0
- /package/lib/{google/index.ts → google.ts} +0 -0
- /package/lib/{regions/index.ts → regions.ts} +0 -0
package/lib/geometries/point.ts
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
import type google from "@googlemaps/google-maps-services-js"
|
|
8
8
|
import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
|
|
9
|
-
import { tryParsingJSON } from "@mailwoman/core/json"
|
|
9
|
+
import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
|
|
10
10
|
import { convert as convertCoords } from "geo-coordinates-parser"
|
|
11
11
|
import { latLngToCell } from "h3-js"
|
|
12
12
|
|
|
13
13
|
import { type BBox2DLiteral, type BBox3DLiteral, GeoBoundingBox, isBBox } from "#bbox"
|
|
14
|
+
import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
|
|
14
15
|
import { type H3Cell, shortenH3Cell } from "#h3/cell"
|
|
15
16
|
import { type GeoObjectLiteral, GeometryType } from "#objects"
|
|
16
17
|
import {
|
|
@@ -21,13 +22,12 @@ import {
|
|
|
21
22
|
isCoordPairLiteral,
|
|
22
23
|
isGoogleMapsLatLngLiteral,
|
|
23
24
|
isInterpolatedCoordinates,
|
|
24
|
-
isValidLatitude,
|
|
25
|
-
isValidLongitude,
|
|
26
25
|
wrapLongitude,
|
|
27
26
|
} from "#position"
|
|
28
27
|
|
|
29
28
|
/**
|
|
30
|
-
* A JSON-
|
|
29
|
+
* A JSON-serializable single point geometry, such as a specific location,
|
|
30
|
+
* address, or longitude, latitude pair.
|
|
31
31
|
*
|
|
32
32
|
* ```js
|
|
33
33
|
* {
|
|
@@ -79,8 +79,8 @@ export interface GeolocationCoordinatesLike {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
* Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike}
|
|
83
|
-
* shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
|
|
82
|
+
* Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike}
|
|
83
|
+
* object — the shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
|
|
84
84
|
*/
|
|
85
85
|
export function isGeolocationCoordinatesLike(input: unknown): input is GeolocationCoordinatesLike {
|
|
86
86
|
if (!input || typeof input !== "object") return false
|
|
@@ -103,13 +103,13 @@ export type GeoPointInput =
|
|
|
103
103
|
| LatLngLiteral
|
|
104
104
|
| LatLng
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
// #region GeoPoint
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* A single point geometry, such as a specific location, address, or longitude, latitude pair.
|
|
110
110
|
*/
|
|
111
111
|
export class GeoPoint implements PointLiteral {
|
|
112
|
-
|
|
112
|
+
// #region Properties
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* Declares the type of GeoJSON object as a `Point` geometry.
|
|
@@ -148,9 +148,10 @@ export class GeoPoint implements PointLiteral {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
|
-
* Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the
|
|
152
|
-
* the magnitudes — and
|
|
153
|
-
*
|
|
151
|
+
* Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the interface,
|
|
152
|
+
* never inferred from the magnitudes — and rejects a coordinate that is not on the globe.
|
|
153
|
+
*
|
|
154
|
+
* See {@link GeoPoint.from} for why both halves of that sentence are required.
|
|
154
155
|
*
|
|
155
156
|
* @throws {RangeError} When longitude is outside [-180, 180] or latitude is outside [-90, 90].
|
|
156
157
|
*/
|
|
@@ -217,9 +218,9 @@ export class GeoPoint implements PointLiteral {
|
|
|
217
218
|
this.#altitude = value
|
|
218
219
|
}
|
|
219
220
|
|
|
220
|
-
|
|
221
|
+
// #endregion
|
|
221
222
|
|
|
222
|
-
|
|
223
|
+
// #region Constructors
|
|
223
224
|
|
|
224
225
|
/**
|
|
225
226
|
* Create a new GeoPoint object with default coordinates.
|
|
@@ -241,7 +242,8 @@ export class GeoPoint implements PointLiteral {
|
|
|
241
242
|
constructor(geoLocationCoordinates: GeolocationCoordinatesLike, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
|
|
242
243
|
|
|
243
244
|
/**
|
|
244
|
-
* Creates a new GeoPoint instance from a Google Maps API
|
|
245
|
+
* Creates a new GeoPoint instance from a Google Maps API
|
|
246
|
+
* {@linkcode google.LatLngLiteral | LatLngLiteral} object.
|
|
245
247
|
*/
|
|
246
248
|
constructor(latLngLiteral: google.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
|
|
247
249
|
|
|
@@ -287,23 +289,28 @@ export class GeoPoint implements PointLiteral {
|
|
|
287
289
|
}
|
|
288
290
|
|
|
289
291
|
/**
|
|
290
|
-
* Attempts to create a new GeoPoint instance from the given input, returning `null`
|
|
291
|
-
* input is not a coordinate this class will stand behind.
|
|
292
|
+
* Attempts to create a new GeoPoint instance from the given input, returning `null`
|
|
293
|
+
* rather than throwing when the input is not a coordinate this class will stand behind.
|
|
292
294
|
*
|
|
293
|
-
* Two rules, both of which this constructor got wrong until 2026-08-05
|
|
294
|
-
* routed around rather than fixed):
|
|
295
|
+
* Two rules, both of which this constructor got wrong until 2026-08-05
|
|
296
|
+
* (the defect was recorded in `e9bfd139` and routed around rather than fixed):
|
|
295
297
|
*
|
|
296
|
-
* 1. **A 2-tuple is GeoJSON [longitude, latitude].
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
298
|
+
* 1. **A 2-tuple is GeoJSON [longitude, latitude].
|
|
299
|
+
* The axis order is never inferred.** The old path ran the pair through
|
|
300
|
+
* `inferGeoJSONCoordOrder`, whose only signal is the [-90, 90] latitude range,
|
|
301
|
+
* so it transposed a pair exactly when |the second magnitude| > 90.
|
|
302
|
+
* A caller handing it `[latitude, longitude]` therefore got the pair repaired in Dallas
|
|
303
|
+
* and left corrupted in Berlin — behavior selected by the data, from one code path.
|
|
304
|
+
* This change is a no-op for every well-formed input: a valid `[longitude, latitude]` pair
|
|
305
|
+
* can never have an out-of-range second element, so the heuristic never fired on one.
|
|
306
|
+
* 2. **An out-of-range magnitude is rejected rather than repaired.** `[999, 999]`
|
|
307
|
+
* used to produce a GeoPoint reporting latitude 999.
|
|
308
|
+
* It now returns `null` here and throws a `RangeError` from the constructor.
|
|
309
|
+
* Note the deliberate asymmetry with the scalar `longitude` / `latitude` setters.
|
|
310
|
+
* which still wrap and clamp: mutating a point is a pan gesture, where 190°.
|
|
311
|
+
* This means -170° is right.
|
|
312
|
+
* A parse result is a claim about the world.
|
|
313
|
+
* An impossible magnitude means the input was malformed and any repair invents a location.
|
|
307
314
|
*
|
|
308
315
|
* A 0/0 result is treated as the "missing coordinate" sentinel (Null Island) and also returns `null`.
|
|
309
316
|
*/
|
|
@@ -331,9 +338,9 @@ export class GeoPoint implements PointLiteral {
|
|
|
331
338
|
}
|
|
332
339
|
}
|
|
333
340
|
|
|
334
|
-
|
|
341
|
+
// #endregion
|
|
335
342
|
|
|
336
|
-
|
|
343
|
+
// #region Predicates
|
|
337
344
|
|
|
338
345
|
/**
|
|
339
346
|
* Whether the GeoPoint is 2-dimensional.
|
|
@@ -356,9 +363,9 @@ export class GeoPoint implements PointLiteral {
|
|
|
356
363
|
return this.#latitude === 0 && this.#longitude === 0
|
|
357
364
|
}
|
|
358
365
|
|
|
359
|
-
|
|
366
|
+
// #endregion
|
|
360
367
|
|
|
361
|
-
|
|
368
|
+
// #region Conversion
|
|
362
369
|
|
|
363
370
|
public toJSON(): PointLiteral {
|
|
364
371
|
return {
|
|
@@ -413,9 +420,9 @@ export class GeoPoint implements PointLiteral {
|
|
|
413
420
|
}
|
|
414
421
|
|
|
415
422
|
public toString(): string {
|
|
416
|
-
return
|
|
423
|
+
return stringifyJSON(this.toJSON())
|
|
417
424
|
}
|
|
418
|
-
|
|
425
|
+
// #endregion
|
|
419
426
|
|
|
420
427
|
public [Symbol.iterator](): Iterator<number> {
|
|
421
428
|
return this.coordinates[Symbol.iterator]()
|
|
@@ -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
|
|
|
@@ -108,7 +47,7 @@ export interface PolygonLiteral<P extends PolygonPath = PolygonPath> extends Geo
|
|
|
108
47
|
* Predicate for checking if a GeoJSON object is a `Polygon` geometry.
|
|
109
48
|
*/
|
|
110
49
|
export function isPolygonLiteral<P extends PolygonPath = PolygonPath>(input: unknown): input is PolygonLiteral<P> {
|
|
111
|
-
if (typeof input !== "object" || input
|
|
50
|
+
if (typeof input !== "object" || !input) return false
|
|
112
51
|
|
|
113
52
|
return "type" in input && input.type === "Polygon" && "coordinates" in input && Array.isArray(input.coordinates)
|
|
114
53
|
}
|
|
@@ -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,32 +132,20 @@ 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
|
-
export function arealPolygons(geometry: ParsedGeometry | null
|
|
148
|
+
export function arealPolygons(geometry: ParsedGeometry | null): MultiPolygonRings | null {
|
|
234
149
|
if (!geometry) return null
|
|
235
150
|
|
|
236
151
|
if (geometry.type === "Polygon") return [geometry.coordinates as PolygonRings]
|
|
@@ -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,21 +168,10 @@ 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
|
-
export function geometryContains(
|
|
271
|
-
geometry: ParsedGeometry | null | undefined,
|
|
272
|
-
lon: number,
|
|
273
|
-
lat: number
|
|
274
|
-
): boolean | null {
|
|
174
|
+
export function geometryContains(geometry: ParsedGeometry | null, lon: number, lat: number): boolean | null {
|
|
275
175
|
const polygons = arealPolygons(geometry)
|
|
276
176
|
|
|
277
177
|
if (!polygons) return null
|
|
@@ -280,13 +180,7 @@ export function geometryContains(
|
|
|
280
180
|
}
|
|
281
181
|
|
|
282
182
|
/**
|
|
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.
|
|
183
|
+
* An axis-aligned rectangle as a closed ring, in GeoJSON `[lon, lat]` order and counter-clockwise.
|
|
290
184
|
*/
|
|
291
185
|
export function rectangleRing(minLon: number, minLat: number, maxLon: number, maxLat: number): number[][] {
|
|
292
186
|
return [
|
|
@@ -310,5 +204,3 @@ export function reversedRing(minLon: number, minLat: number, maxLon: number, max
|
|
|
310
204
|
[minLon, minLat],
|
|
311
205
|
]
|
|
312
206
|
}
|
|
313
|
-
|
|
314
|
-
//#endregion
|