@mailwoman/spatial 9.2.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.
- package/{bbox.ts → lib/bbox.ts} +4 -4
- package/{coordinate-bounds.ts → lib/coordinate-bounds.ts} +11 -0
- package/{coordinate-formats.ts → lib/coordinate-formats.ts} +3 -1
- package/lib/distance.ts +165 -0
- package/{feature.ts → lib/feature.ts} +1 -1
- package/{geometries → lib/geometries}/collection.ts +4 -4
- package/lib/geometries/index.ts +13 -0
- package/{geometries → lib/geometries}/line-string.ts +2 -2
- package/{geometries → lib/geometries}/point.ts +9 -9
- package/{geometries → lib/geometries}/polygon.ts +82 -158
- package/lib/geometries/ring-blob.ts +521 -0
- package/{google → lib/google}/index.ts +1 -1
- package/{h3 → lib/h3}/cell.ts +83 -1
- package/lib/h3/coverage.ts +45 -0
- package/{h3 → lib/h3}/index.ts +5 -3
- package/lib/h3/interior-cells.ts +138 -0
- package/lib/h3/polygon-cells.ts +462 -0
- package/lib/index.ts +21 -0
- package/{objects.ts → lib/objects.ts} +1 -1
- package/{open-location-code.ts → lib/open-location-code.ts} +1 -1
- package/{osgb36.ts → lib/osgb36.ts} +2 -2
- package/{polyline.ts → lib/polyline.ts} +1 -1
- package/{position.ts → lib/position.ts} +2 -2
- package/lib/projection-transform.ts +115 -0
- package/{regions → lib/regions}/codes.ts +1 -1
- package/{regions → lib/regions}/index.ts +2 -2
- package/lib/tools/ogr-stream.ts +99 -0
- package/lib/tools/ogr.ts +201 -0
- package/out/bbox.d.ts +2 -2
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +3 -3
- package/out/bbox.js.map +1 -1
- package/out/coordinate-bounds.d.ts +9 -0
- package/out/coordinate-bounds.d.ts.map +1 -1
- package/out/coordinate-bounds.js +9 -0
- package/out/coordinate-bounds.js.map +1 -1
- package/out/coordinate-formats.d.ts.map +1 -1
- package/out/coordinate-formats.js +2 -1
- package/out/coordinate-formats.js.map +1 -1
- package/out/distance.d.ts +39 -1
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +63 -1
- package/out/distance.js.map +1 -1
- package/out/feature.d.ts +1 -1
- package/out/feature.d.ts.map +1 -1
- package/out/feature.js.map +1 -1
- package/out/geometries/collection.d.ts +4 -4
- package/out/geometries/collection.d.ts.map +1 -1
- package/out/geometries/collection.js.map +1 -1
- package/out/geometries/index.d.ts +5 -4
- package/out/geometries/index.d.ts.map +1 -1
- package/out/geometries/index.js +5 -4
- package/out/geometries/index.js.map +1 -1
- package/out/geometries/line-string.d.ts +2 -2
- package/out/geometries/line-string.d.ts.map +1 -1
- package/out/geometries/line-string.js.map +1 -1
- package/out/geometries/point.d.ts +9 -9
- package/out/geometries/point.d.ts.map +1 -1
- package/out/geometries/point.js +9 -9
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +51 -100
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +60 -90
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +183 -0
- package/out/geometries/ring-blob.d.ts.map +1 -0
- package/out/geometries/ring-blob.js +376 -0
- package/out/geometries/ring-blob.js.map +1 -0
- package/out/google/index.d.ts +1 -1
- package/out/google/index.d.ts.map +1 -1
- package/out/google/index.js +1 -1
- package/out/google/index.js.map +1 -1
- package/out/google/place-id.d.ts.map +1 -1
- package/out/google/place-id.js.map +1 -1
- package/out/h3/cell.d.ts +32 -0
- package/out/h3/cell.d.ts.map +1 -1
- package/out/h3/cell.js +68 -1
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +32 -0
- package/out/h3/coverage.d.ts.map +1 -0
- package/out/h3/coverage.js +27 -0
- package/out/h3/coverage.js.map +1 -0
- package/out/h3/index.d.ts +5 -3
- package/out/h3/index.d.ts.map +1 -1
- package/out/h3/index.js +5 -3
- package/out/h3/index.js.map +1 -1
- package/out/h3/interior-cells.d.ts +54 -0
- package/out/h3/interior-cells.d.ts.map +1 -0
- package/out/h3/interior-cells.js +111 -0
- package/out/h3/interior-cells.js.map +1 -0
- package/out/h3/polygon-cells.d.ts +182 -0
- package/out/h3/polygon-cells.d.ts.map +1 -0
- package/out/h3/polygon-cells.js +339 -0
- package/out/h3/polygon-cells.js.map +1 -0
- package/out/index.d.ts +15 -15
- package/out/index.d.ts.map +1 -1
- package/out/index.js +15 -15
- package/out/index.js.map +1 -1
- package/out/objects.d.ts +1 -1
- package/out/objects.d.ts.map +1 -1
- package/out/objects.js +1 -1
- package/out/objects.js.map +1 -1
- package/out/open-location-code.d.ts.map +1 -1
- package/out/open-location-code.js +1 -1
- package/out/open-location-code.js.map +1 -1
- package/out/osgb36.d.ts +2 -2
- package/out/osgb36.d.ts.map +1 -1
- package/out/osgb36.js +1 -1
- package/out/osgb36.js.map +1 -1
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +1 -1
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +1 -1
- package/out/position.d.ts.map +1 -1
- package/out/position.js +2 -2
- package/out/position.js.map +1 -1
- package/out/projection-transform.d.ts +67 -0
- package/out/projection-transform.d.ts.map +1 -0
- package/out/projection-transform.js +48 -0
- package/out/projection-transform.js.map +1 -0
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js.map +1 -1
- package/out/regions/codes.d.ts.map +1 -1
- package/out/regions/codes.js.map +1 -1
- package/out/regions/index.d.ts +2 -2
- package/out/regions/index.d.ts.map +1 -1
- package/out/regions/index.js +2 -2
- package/out/regions/index.js.map +1 -1
- package/out/regions/names.d.ts.map +1 -1
- package/out/regions/names.js.map +1 -1
- package/out/tools/ogr-stream.d.ts +34 -0
- package/out/tools/ogr-stream.d.ts.map +1 -0
- package/out/tools/ogr-stream.js +74 -0
- package/out/tools/ogr-stream.js.map +1 -0
- package/out/tools/ogr.d.ts +87 -0
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/ogr.js +84 -0
- package/out/tools/ogr.js.map +1 -0
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/well-known-text.d.ts.map +1 -0
- package/out/well-known-text.js.map +1 -0
- package/package.json +94 -25
- package/distance.ts +0 -84
- package/geometries/index.ts +0 -12
- package/index.ts +0 -21
- package/out/sdk/index.d.ts +0 -7
- package/out/sdk/index.d.ts.map +0 -1
- package/out/sdk/index.js +0 -7
- package/out/sdk/index.js.map +0 -1
- package/out/sdk/well-known-text.d.ts.map +0 -1
- package/out/sdk/well-known-text.js.map +0 -1
- package/out/tsconfig.tsbuildinfo +0 -1
- package/sdk/index.ts +0 -7
- /package/{google → lib/google}/place-id.ts +0 -0
- /package/{projection.ts → lib/projection.ts} +0 -0
- /package/{regions → lib/regions}/names.ts +0 -0
- /package/{sdk → lib}/well-known-text.ts +0 -0
- /package/out/{sdk/well-known-text.d.ts → well-known-text.d.ts} +0 -0
- /package/out/{sdk/well-known-text.js → well-known-text.js} +0 -0
package/{bbox.ts → lib/bbox.ts}
RENAMED
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* GeoJSON Bounding Boxes
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { toRad } from "
|
|
10
|
-
import type { PolygonLiteral, SolidPolygonPath } from "
|
|
11
|
-
import { clampLatitude, wrapLongitude } from "
|
|
12
|
-
import { CoordinateProjection } from "
|
|
9
|
+
import { toRad } from "#coordinate-formats"
|
|
10
|
+
import type { PolygonLiteral, SolidPolygonPath } from "#geometries/polygon"
|
|
11
|
+
import { clampLatitude, wrapLongitude } from "#position"
|
|
12
|
+
import { CoordinateProjection } from "#projection"
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Kilometres per degree of latitude, which is very nearly constant on a sphere.
|
|
@@ -26,6 +26,17 @@ export const LONGITUDE_MIN = -180
|
|
|
26
26
|
*/
|
|
27
27
|
export const LONGITUDE_MAX = 180
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Southern limit of the Web Mercator projection in WGS-84 degrees — the latitude at which the square projection's
|
|
31
|
+
* vertical extent equals its horizontal one.
|
|
32
|
+
*/
|
|
33
|
+
export const MERCATOR_LATITUDE_MIN = -85.05112878
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Northern limit of the Web Mercator projection in WGS-84 degrees.
|
|
37
|
+
*/
|
|
38
|
+
export const MERCATOR_LATITUDE_MAX = 85.05112878
|
|
39
|
+
|
|
29
40
|
/**
|
|
30
41
|
* Whether a finite number is a latitude on the globe, including both poles.
|
|
31
42
|
*/
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
import type { AnnotationSet, Annotator } from "@mailwoman/annotations"
|
|
13
13
|
|
|
14
|
+
import { MERCATOR_LATITUDE_MAX, MERCATOR_LATITUDE_MIN } from "#coordinate-bounds"
|
|
15
|
+
|
|
14
16
|
/**
|
|
15
17
|
* Bits packed into each base-32 geohash character.
|
|
16
18
|
*/
|
|
@@ -67,7 +69,7 @@ const WEB_MERCATOR_R = 6_378_137
|
|
|
67
69
|
* Web Mercator (EPSG:3857) projection of a coordinate.
|
|
68
70
|
*/
|
|
69
71
|
export function toMercator(lat: number, lon: number): { x: number; y: number } {
|
|
70
|
-
const clampedLat = Math.max(
|
|
72
|
+
const clampedLat = Math.max(MERCATOR_LATITUDE_MIN, Math.min(MERCATOR_LATITUDE_MAX, lat))
|
|
71
73
|
|
|
72
74
|
return {
|
|
73
75
|
x: WEB_MERCATOR_R * toRad(lon),
|
package/lib/distance.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software.
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file Great-circle distance utilities. This module imports geometry directly so spatial's
|
|
6
|
+
* lowest-level math never reaches back through its public barrel.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { GeoPoint, type GeoPointInput } from "#geometries/point"
|
|
10
|
+
import { arealPolygons } from "#geometries/polygon"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Conversion factors for converting between degrees and radians.
|
|
14
|
+
*
|
|
15
|
+
* @category Position
|
|
16
|
+
* @see {@link https://en.wikipedia.org/wiki/Radian Wikipedia: Radian}
|
|
17
|
+
* @see {@link https://en.wikipedia.org/wiki/Degree_(angle) Wikipedia: Degree (angle)}
|
|
18
|
+
*/
|
|
19
|
+
export const ConversionFactor = {
|
|
20
|
+
DegreesToRadians: Math.PI / 180,
|
|
21
|
+
RadiansToDegrees: 180 / Math.PI,
|
|
22
|
+
} as const
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Available conversion units for the radius of the Earth.
|
|
26
|
+
*/
|
|
27
|
+
export type EarthRadiusUnit = "km" | "miles" | "meters"
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Radius of the Earth in various units.
|
|
31
|
+
*/
|
|
32
|
+
const RADII = {
|
|
33
|
+
km: 6371,
|
|
34
|
+
miles: 3958.8,
|
|
35
|
+
meters: 6_371_000,
|
|
36
|
+
} as const satisfies Record<EarthRadiusUnit, number>
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Shared great-circle math with no sentinel handling. `unit` selects the Earth radius.
|
|
40
|
+
*/
|
|
41
|
+
function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
|
|
42
|
+
const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
|
|
43
|
+
const dLon = (lon2 - lon1) * ConversionFactor.DegreesToRadians
|
|
44
|
+
|
|
45
|
+
const a =
|
|
46
|
+
Math.pow(Math.sin(dLat / 2), 2) +
|
|
47
|
+
Math.cos(lat1 * ConversionFactor.DegreesToRadians) *
|
|
48
|
+
Math.cos(lat2 * ConversionFactor.DegreesToRadians) *
|
|
49
|
+
Math.pow(Math.sin(dLon / 2), 2)
|
|
50
|
+
|
|
51
|
+
return RADII[unit] * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Calculate the distance between two points on the Earth's surface.
|
|
56
|
+
*
|
|
57
|
+
* @category Position
|
|
58
|
+
* @param point1 The first point to calculate the distance from.
|
|
59
|
+
* @param point2 The second point to calculate the distance to.
|
|
60
|
+
* @param unit The unit of measurement to return the distance in.
|
|
61
|
+
*
|
|
62
|
+
* @returns The distance between the two points in the specified unit.
|
|
63
|
+
*/
|
|
64
|
+
export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: EarthRadiusUnit = "km"): number {
|
|
65
|
+
const p1 = GeoPoint.from(point1)
|
|
66
|
+
const p2 = GeoPoint.from(point2)
|
|
67
|
+
|
|
68
|
+
if (!p1 || !p2) return Number.NaN
|
|
69
|
+
|
|
70
|
+
return greatCircle(p1.latitude, p1.longitude, p2.latitude, p2.longitude, unit)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Great-circle distance in kilometres between two lat/lon pairs given as raw scalars. The formula's one true home —
|
|
75
|
+
* every resolver + eval consumer of the `(aLat, aLon, bLat, bLon)` shape imports this instead of re-declaring it.
|
|
76
|
+
*
|
|
77
|
+
* Unlike {@link haversine}, this is pure math with NO Null-Island sentinel: `(0, 0)` is the Gulf of Guinea — a real
|
|
78
|
+
* point, not "missing coordinate". That sentinel convention belongs to the `GeoPointInput` object form (where a 0/0
|
|
79
|
+
* input means "no coordinate"), not to a raw scalar distance.
|
|
80
|
+
*
|
|
81
|
+
* @category Position
|
|
82
|
+
*/
|
|
83
|
+
export function haversineKm(aLat: number, aLon: number, bLat: number, bLon: number): number {
|
|
84
|
+
return greatCircle(aLat, aLon, bLat, bLon, "km")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Metres per degree of latitude — the scale {@link segmentDistanceMetres} reports in, and the constant the bounding-box
|
|
89
|
+
* estimates in `#h3/polygon-cells` are built on.
|
|
90
|
+
*/
|
|
91
|
+
export const METRES_PER_DEGREE = 111_320
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Square metres in a square kilometre — the conversion every polygon layer's area receipt reports through.
|
|
95
|
+
*/
|
|
96
|
+
export const M2_PER_KM2 = 1_000_000
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Metres from a point to a line SEGMENT, with longitude scaled for the latitude so the two axes are comparable.
|
|
100
|
+
*
|
|
101
|
+
* MEASURE TO THE EDGE, NOT TO THE NEAREST VERTEX. A point a centimetre from a long edge can be metres from every vertex
|
|
102
|
+
* of it, so a vertex distance overstates the gap without bound — measured on the flood layer's verification, one
|
|
103
|
+
* near-miss read 1.58 m to vertices and 0.009 m to edges, a 9 mm difference overstated 175-fold. That is the difference
|
|
104
|
+
* between "two channels rendered the same edge slightly differently" and "the conversion is wrong".
|
|
105
|
+
*
|
|
106
|
+
* The longitude scaling matters at the same scale: comparing raw degrees treats a degree of longitude as a degree of
|
|
107
|
+
* latitude, which at 54°N overstates east-west distance by 70%.
|
|
108
|
+
*
|
|
109
|
+
* @category Position
|
|
110
|
+
*/
|
|
111
|
+
export function segmentDistanceMetres(
|
|
112
|
+
lon: number,
|
|
113
|
+
lat: number,
|
|
114
|
+
from: readonly number[],
|
|
115
|
+
to: readonly number[]
|
|
116
|
+
): number {
|
|
117
|
+
const scale = Math.cos((lat * Math.PI) / 180)
|
|
118
|
+
const px = (lon - from[0]!) * scale
|
|
119
|
+
const py = lat - from[1]!
|
|
120
|
+
const vx = (to[0]! - from[0]!) * scale
|
|
121
|
+
const vy = to[1]! - from[1]!
|
|
122
|
+
const lengthSquared = vx * vx + vy * vy
|
|
123
|
+
|
|
124
|
+
// A zero-length segment is a repeated vertex; the distance to it is the distance to the point.
|
|
125
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, (px * vx + py * vy) / lengthSquared))
|
|
126
|
+
|
|
127
|
+
return Math.hypot(px - t * vx, py - t * vy) * METRES_PER_DEGREE
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Metres from a point to the nearest ring EDGE of an areal geometry — `Infinity` when the geometry bounds no area or
|
|
132
|
+
* carries no segment.
|
|
133
|
+
*
|
|
134
|
+
* To the edge, not to the nearest vertex: a point a centimetre from a long edge can be metres from every vertex of it,
|
|
135
|
+
* so a vertex distance overstates the gap without bound — measured on the flood layer's verification, one near-miss
|
|
136
|
+
* read 1.58 m to vertices and 0.009 m to edges, a 9 mm difference overstated 175-fold. Every polygon layer's two-path
|
|
137
|
+
* verification measures its boundary tolerance with this.
|
|
138
|
+
*
|
|
139
|
+
* @category Position
|
|
140
|
+
*/
|
|
141
|
+
export function nearestRingEdgeMetres(
|
|
142
|
+
geometry: { type: string; coordinates: unknown },
|
|
143
|
+
longitude: number,
|
|
144
|
+
latitude: number
|
|
145
|
+
): number {
|
|
146
|
+
const polygons = arealPolygons(geometry)
|
|
147
|
+
|
|
148
|
+
let nearest = Infinity
|
|
149
|
+
|
|
150
|
+
if (!polygons) return nearest
|
|
151
|
+
|
|
152
|
+
for (const rings of polygons) {
|
|
153
|
+
for (const ring of rings) {
|
|
154
|
+
for (let index = 1; index < ring.length; index++) {
|
|
155
|
+
const distance = segmentDistanceMetres(longitude, latitude, ring[index - 1]!, ring[index]!)
|
|
156
|
+
|
|
157
|
+
if (distance < nearest) {
|
|
158
|
+
nearest = distance
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return nearest
|
|
165
|
+
}
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* GeoJSON Geometry Collection
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
import type {
|
|
11
|
-
import type {
|
|
12
|
-
import type {
|
|
9
|
+
import type { LineStringLiteral, MultiLineStringLiteral } from "#geometries/line-string"
|
|
10
|
+
import type { MultiPointLiteral, PointLiteral } from "#geometries/point"
|
|
11
|
+
import type { MultiPolygonLiteral, PolygonLiteral } from "#geometries/polygon"
|
|
12
|
+
import type { GeoObjectLiteral } from "#objects"
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Union of the GeoJSON geometry types.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software.
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* GeoJSON Geometry Entry Point
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export * from "#geometries/collection"
|
|
10
|
+
export * from "#geometries/line-string"
|
|
11
|
+
export * from "#geometries/point"
|
|
12
|
+
export * from "#geometries/polygon"
|
|
13
|
+
export * from "#geometries/ring-blob"
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* GeoJSON Line String
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { GeoObjectLiteral } from "
|
|
10
|
-
import type { Coordinates2D, Coordinates3D } from "
|
|
9
|
+
import type { GeoObjectLiteral } from "#objects"
|
|
10
|
+
import type { Coordinates2D, Coordinates3D } from "#position"
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* An array of positions forming a line, such as a road or a path.
|
|
@@ -6,13 +6,13 @@
|
|
|
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/
|
|
9
|
+
import { tryParsingJSON } from "@mailwoman/core/json"
|
|
10
10
|
import { convert as convertCoords } from "geo-coordinates-parser"
|
|
11
11
|
import { latLngToCell } from "h3-js"
|
|
12
12
|
|
|
13
|
-
import { type BBox2DLiteral, type BBox3DLiteral, GeoBoundingBox, isBBox } from "
|
|
14
|
-
import { type H3Cell, shortenH3Cell } from "
|
|
15
|
-
import { type GeoObjectLiteral, GeometryType } from "
|
|
13
|
+
import { type BBox2DLiteral, type BBox3DLiteral, GeoBoundingBox, isBBox } from "#bbox"
|
|
14
|
+
import { type H3Cell, shortenH3Cell } from "#h3/cell"
|
|
15
|
+
import { type GeoObjectLiteral, GeometryType } from "#objects"
|
|
16
16
|
import {
|
|
17
17
|
type InternalPointCoordinates,
|
|
18
18
|
type Coordinates2D as Point2DCoordinates,
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
isValidLatitude,
|
|
25
25
|
isValidLongitude,
|
|
26
26
|
wrapLongitude,
|
|
27
|
-
} from "
|
|
27
|
+
} from "#position"
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* A JSON-serializeable single point geometry, such as a specific location, address, or longitude, latitude pair.
|
|
@@ -149,7 +149,7 @@ export class GeoPoint implements PointLiteral {
|
|
|
149
149
|
/**
|
|
150
150
|
* Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the contract, never inferred from
|
|
151
151
|
* the magnitudes — and REJECTS a coordinate that is not on the globe. See {@link GeoPoint.from} for why both halves of
|
|
152
|
-
* that sentence are
|
|
152
|
+
* that sentence are required.
|
|
153
153
|
*
|
|
154
154
|
* @throws {RangeError} When longitude is outside [-180, 180] or latitude is outside [-90, 90].
|
|
155
155
|
*/
|
|
@@ -298,9 +298,9 @@ export class GeoPoint implements PointLiteral {
|
|
|
298
298
|
* Dallas and left corrupted in Berlin — behaviour selected by the data, from one code path. This change is a no-op
|
|
299
299
|
* for every WELL-FORMED input: a valid `[longitude, latitude]` pair can never have an out-of-range second element,
|
|
300
300
|
* so the heuristic never fired on one.
|
|
301
|
-
* 2. **An out-of-range magnitude is rejected, not repaired.** `[999, 999]` used to produce a GeoPoint reporting latitude
|
|
302
|
-
*
|
|
303
|
-
*
|
|
301
|
+
* 2. **An out-of-range magnitude is rejected, not repaired.** `[999, 999]` used to produce a GeoPoint reporting latitude 999.
|
|
302
|
+
* It now returns `null` here and throws a `RangeError` from the constructor. Note the deliberate asymmetry with
|
|
303
|
+
* the scalar `longitude` / `latitude` setters, which still wrap and clamp: mutating a point is a pan gesture,
|
|
304
304
|
* where 190° meaning -170° is right; PARSING one is a claim about the world, where an impossible magnitude means
|
|
305
305
|
* the input was malformed and any repair invents a location.
|
|
306
306
|
*
|
|
@@ -4,11 +4,9 @@
|
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import type { GeoObjectLiteral } from "../objects.ts"
|
|
11
|
-
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"
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
12
|
* An array of positions forming a closed shape, such as a country or a lake.
|
|
@@ -91,7 +89,7 @@ export type PolygonPath = SolidPolygonPath | NestedPolygonPath
|
|
|
91
89
|
/**
|
|
92
90
|
* An array of positions forming a closed shape, such as a country or a lake.
|
|
93
91
|
*/
|
|
94
|
-
export interface PolygonLiteral<P extends PolygonPath =
|
|
92
|
+
export interface PolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
|
|
95
93
|
/**
|
|
96
94
|
* Declares the type of GeoJSON object as a `Polygon` geometry.
|
|
97
95
|
*/
|
|
@@ -118,7 +116,9 @@ export function isPolygonLiteral<P extends PolygonPath = PolygonPath>(input: unk
|
|
|
118
116
|
/**
|
|
119
117
|
* Predicate for checking if a polygon geometry is a solid, i.e. it has no holes.
|
|
120
118
|
*/
|
|
121
|
-
|
|
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 {
|
|
122
122
|
return input.coordinates.length === 1
|
|
123
123
|
}
|
|
124
124
|
|
|
@@ -132,6 +132,18 @@ export function isSolidPolygonPath(input: PolygonLiteral): boolean {
|
|
|
132
132
|
*/
|
|
133
133
|
export type ContainmentRing = readonly (readonly number[])[]
|
|
134
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
|
+
|
|
135
147
|
/**
|
|
136
148
|
* Ray-cast a point against ONE linear ring — the even-odd crossing count. Shoot a ray along +lon and toggle on every
|
|
137
149
|
* edge crossing.
|
|
@@ -164,7 +176,7 @@ export function pointInRing(lon: number, lat: number, ring: ContainmentRing): bo
|
|
|
164
176
|
* without depending on ring winding order. GeoJSON nominally specifies orientation, but the gazetteer sources do not
|
|
165
177
|
* reliably honour it, so the orientation-free rule is the one that survives real data.
|
|
166
178
|
*/
|
|
167
|
-
export function pointInPolygon(lon: number, lat: number, rings:
|
|
179
|
+
export function pointInPolygon(lon: number, lat: number, rings: PolygonRings): boolean {
|
|
168
180
|
let inside = false
|
|
169
181
|
|
|
170
182
|
for (const ring of rings) {
|
|
@@ -190,7 +202,7 @@ export function pointInMultiPolygon(
|
|
|
190
202
|
/**
|
|
191
203
|
* A collection of polygons, such as a country with islands or a lake with islands.
|
|
192
204
|
*/
|
|
193
|
-
export interface MultiPolygonLiteral<P extends PolygonPath =
|
|
205
|
+
export interface MultiPolygonLiteral<P extends PolygonPath = PolygonPath> extends GeoObjectLiteral {
|
|
194
206
|
type: "MultiPolygon"
|
|
195
207
|
|
|
196
208
|
/**
|
|
@@ -202,163 +214,46 @@ export interface MultiPolygonLiteral<P extends PolygonPath = SolidPolygonPath> e
|
|
|
202
214
|
coordinates: P[]
|
|
203
215
|
}
|
|
204
216
|
|
|
205
|
-
|
|
206
|
-
* Predicate for checking if a GeoJSON object is a `MultiPolygon` geometry.
|
|
207
|
-
*/
|
|
217
|
+
//#region Ring-list geometry — the parsed-GeoJSON shape
|
|
208
218
|
|
|
209
219
|
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
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.
|
|
213
223
|
*/
|
|
214
|
-
export
|
|
215
|
-
if (!isPolygonLiteral(input)) return ""
|
|
216
|
-
|
|
217
|
-
const [exteriorRing] = input.coordinates
|
|
218
|
-
|
|
219
|
-
const filter = exteriorRing.map(([lon, lat]) => `${lat} ${lon}`).join(" ")
|
|
220
|
-
|
|
221
|
-
return `poly:'${filter}'`
|
|
222
|
-
}
|
|
224
|
+
export type ParsedGeometry = GeometryLiteral | { type: string; coordinates?: unknown }
|
|
223
225
|
|
|
224
226
|
/**
|
|
225
|
-
*
|
|
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).
|
|
226
229
|
*
|
|
227
|
-
*
|
|
228
|
-
|
|
229
|
-
export const OSMNodeTag = {
|
|
230
|
-
HouseNumber: "addr:housenumber",
|
|
231
|
-
PostCode: "addr:postcode",
|
|
232
|
-
Street: "addr:street",
|
|
233
|
-
State: "addr:state",
|
|
234
|
-
City: "addr:city",
|
|
235
|
-
Website: "website",
|
|
236
|
-
Email: "email",
|
|
237
|
-
Phone: "phone",
|
|
238
|
-
Shop: "shop",
|
|
239
|
-
Brand: "brand",
|
|
240
|
-
Cuisine: "cuisine",
|
|
241
|
-
Name: "name",
|
|
242
|
-
Healthcare: "healthcare",
|
|
243
|
-
Office: "office",
|
|
244
|
-
Amenity: "amenity",
|
|
245
|
-
} as const
|
|
246
|
-
|
|
247
|
-
export type OSMNodeTag = (typeof OSMNodeTag)[keyof typeof OSMNodeTag]
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* OSM node tags that disqualify a node from being treated as residential — a node carrying one of these is
|
|
251
|
-
* infrastructure or commercial, whatever else it claims.
|
|
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.
|
|
252
232
|
*/
|
|
253
|
-
export
|
|
254
|
-
|
|
255
|
-
OSMNodeTag.Brand,
|
|
256
|
-
OSMNodeTag.Cuisine,
|
|
257
|
-
OSMNodeTag.Office,
|
|
258
|
-
OSMNodeTag.Healthcare,
|
|
259
|
-
])
|
|
260
|
-
|
|
261
|
-
export type OSMNodeTagRecord = Record<OSMNodeTag, string | undefined>
|
|
262
|
-
|
|
263
|
-
export interface OSMOverpassElement {
|
|
264
|
-
type: "node"
|
|
265
|
-
id: number
|
|
266
|
-
lat: number
|
|
267
|
-
lon: number
|
|
268
|
-
tags: OSMNodeTagRecord
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
export interface OSMOverpassResponseBody {
|
|
272
|
-
version: string
|
|
273
|
-
generator: string
|
|
274
|
-
osm3s: {
|
|
275
|
-
timestamp_osm_base: string
|
|
276
|
-
copyright: string
|
|
277
|
-
}
|
|
278
|
-
elements: OSMOverpassElement[]
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Given an OSM element, attempts to infer if the result is a residential address.
|
|
283
|
-
*/
|
|
284
|
-
export function isResidentialElement(element: OSMOverpassElement): boolean {
|
|
285
|
-
for (const key in element.tags) {
|
|
286
|
-
if (ForbiddenResidentialOSMNodeTags.has(key as OSMNodeTag)) return false
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (element.tags[OSMNodeTag.Amenity] === "restaurant") return false
|
|
290
|
-
|
|
291
|
-
return true
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const overpassClient = new APIClient({ displayName: "overpass", retry: true })
|
|
233
|
+
export function arealPolygons(geometry: ParsedGeometry | null | undefined): MultiPolygonRings | null {
|
|
234
|
+
if (!geometry) return null
|
|
295
235
|
|
|
296
|
-
|
|
297
|
-
const filter = polygonToOSMFilter(input)
|
|
236
|
+
if (geometry.type === "Polygon") return [geometry.coordinates as PolygonRings]
|
|
298
237
|
|
|
299
|
-
|
|
300
|
-
url.searchParams.set("data", `[out:json];(node['addr:housenumber'](${filter}););out body;>;out skel qt;`)
|
|
238
|
+
if (geometry.type === "MultiPolygon") return geometry.coordinates as MultiPolygonRings
|
|
301
239
|
|
|
302
|
-
|
|
303
|
-
// reads that header, which is the server stating its own limit rather than this file guessing one. `ResourceError`
|
|
304
|
-
// now arrives from the client instead of being assembled here.
|
|
305
|
-
return overpassClient
|
|
306
|
-
.fetch<OSMOverpassResponseBody>({ url: url.toString() })
|
|
307
|
-
.then(pluckResponseData)
|
|
308
|
-
.then((body) => body.elements)
|
|
309
|
-
.catch((error) => {
|
|
310
|
-
throw ResourceError.wrap(error, "osm", "overpass-api", "fetch")
|
|
311
|
-
})
|
|
240
|
+
return null
|
|
312
241
|
}
|
|
313
242
|
|
|
314
|
-
//#region Ring-list geometry — the parsed-GeoJSON shape
|
|
315
|
-
|
|
316
243
|
/**
|
|
317
|
-
*
|
|
244
|
+
* The polygons of a geometry that MUST be areal — {@linkcode arealPolygons} with the refusal every polygon ingest was
|
|
245
|
+
* writing for itself.
|
|
318
246
|
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
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`.
|
|
321
250
|
*/
|
|
322
|
-
export
|
|
251
|
+
export function requireArealPolygons(geometry: ParsedGeometry, subject: string, context: string): MultiPolygonRings {
|
|
252
|
+
const polygons = arealPolygons(geometry)
|
|
323
253
|
|
|
324
|
-
|
|
325
|
-
* A GeoJSON `Polygon` as a RING LIST — `[outerRing, hole1, hole2, …]`.
|
|
326
|
-
*
|
|
327
|
-
* This coexists with {@linkcode PolygonLiteral} on purpose, and the difference is load-bearing. `PolygonLiteral`
|
|
328
|
-
* defaults to {@linkcode SolidPolygonPath}, a one-element tuple that cannot express a hole; a real administrative
|
|
329
|
-
* boundary routinely has them (a country with a lake, a locality with an enclave). Use this type for geometry read off
|
|
330
|
-
* a gazetteer, and `PolygonLiteral` where the solid/nested distinction is one you are asserting rather than
|
|
331
|
-
* discovering.
|
|
332
|
-
*/
|
|
333
|
-
export interface GeojsonPolygon {
|
|
334
|
-
type: "Polygon"
|
|
335
|
-
coordinates: GeojsonPosition[][]
|
|
336
|
-
}
|
|
254
|
+
if (polygons) return polygons
|
|
337
255
|
|
|
338
|
-
|
|
339
|
-
* A GeoJSON `MultiPolygon` — one ring list per polygon.
|
|
340
|
-
*/
|
|
341
|
-
export interface GeojsonMultiPolygon {
|
|
342
|
-
type: "MultiPolygon"
|
|
343
|
-
coordinates: GeojsonPosition[][][]
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Either areal geometry, or an open fallback for the types containment cannot answer (Point, LineString, …).
|
|
348
|
-
*/
|
|
349
|
-
export type GeojsonGeometry = GeojsonPolygon | GeojsonMultiPolygon | { type: string; coordinates?: unknown }
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Even-odd containment over a polygon's ring list (`[outer, hole1, …]`).
|
|
353
|
-
*
|
|
354
|
-
* A named alias for {@linkcode pointInPolygon}: the "rings" spelling is what makes the ring-list argument obvious at a
|
|
355
|
-
* call site that has just pulled `coordinates` off a parsed geometry.
|
|
356
|
-
*
|
|
357
|
-
* `scripts/eval/pip-containment.py` grades the same containment truth against its own ray cast and has to be matched BY
|
|
358
|
-
* HAND if this one changes — it is the one copy no import can reach.
|
|
359
|
-
*/
|
|
360
|
-
export function pointInPolygonRings(lon: number, lat: number, rings: readonly GeojsonPosition[][]): boolean {
|
|
361
|
-
return pointInPolygon(lon, lat, rings)
|
|
256
|
+
throw new Error(`${context}: ${subject} is a ${geometry.type}, expected Polygon or MultiPolygon`)
|
|
362
257
|
}
|
|
363
258
|
|
|
364
259
|
/**
|
|
@@ -368,23 +263,52 @@ export function pointInPolygonRings(lon: number, lat: number, rings: readonly Ge
|
|
|
368
263
|
* LineString cannot contain anything — and a caller must read that as "no polygon on record", the same as a missing
|
|
369
264
|
* geometry, never as a rejection. Collapsing it to `false` is how a place with a point-only record gets excluded from a
|
|
370
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.
|
|
371
269
|
*/
|
|
372
270
|
export function geometryContains(
|
|
373
|
-
geometry:
|
|
271
|
+
geometry: ParsedGeometry | null | undefined,
|
|
374
272
|
lon: number,
|
|
375
273
|
lat: number
|
|
376
274
|
): boolean | null {
|
|
377
|
-
|
|
275
|
+
const polygons = arealPolygons(geometry)
|
|
378
276
|
|
|
379
|
-
if (
|
|
380
|
-
return pointInPolygonRings(lon, lat, (geometry as GeojsonPolygon).coordinates)
|
|
381
|
-
}
|
|
277
|
+
if (!polygons) return null
|
|
382
278
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
279
|
+
return polygons.some((rings) => pointInPolygon(lon, lat, rings))
|
|
280
|
+
}
|
|
386
281
|
|
|
387
|
-
|
|
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
|
+
}
|
|
300
|
+
|
|
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
|
+
]
|
|
388
312
|
}
|
|
389
313
|
|
|
390
314
|
//#endregion
|