@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.
- package/{bbox.ts → lib/bbox.ts} +54 -3
- package/lib/coordinate-bounds.ts +52 -0
- package/{coordinate-formats.ts → lib/coordinate-formats.ts} +16 -3
- 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 +93 -89
- 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/lib/open-location-code.ts +168 -0
- package/{osgb36.ts → lib/osgb36.ts} +2 -2
- package/{polyline.ts → lib/polyline.ts} +1 -1
- package/lib/position.ts +216 -0
- 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/{sdk → lib}/well-known-text.ts +2 -1
- package/out/bbox.d.ts +23 -2
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +33 -2
- package/out/bbox.js.map +1 -1
- package/out/coordinate-bounds.d.ts +41 -0
- package/out/coordinate-bounds.d.ts.map +1 -0
- package/out/coordinate-bounds.js +45 -0
- package/out/coordinate-bounds.js.map +1 -0
- package/out/coordinate-formats.d.ts +8 -0
- package/out/coordinate-formats.d.ts.map +1 -1
- package/out/coordinate-formats.js +14 -3
- package/out/coordinate-formats.js.map +1 -1
- package/out/distance.d.ts +84 -0
- package/out/distance.d.ts.map +1 -0
- package/out/distance.js +132 -0
- package/out/distance.js.map +1 -0
- 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 +60 -56
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +66 -63
- 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 -12
- package/out/index.d.ts.map +1 -1
- package/out/index.js +15 -12
- 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 +48 -0
- package/out/open-location-code.d.ts.map +1 -0
- package/out/open-location-code.js +137 -0
- package/out/open-location-code.js.map +1 -0
- 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 +4 -61
- package/out/position.d.ts.map +1 -1
- package/out/position.js +14 -126
- 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/{sdk/well-known-text.d.ts → well-known-text.d.ts} +1 -1
- package/out/well-known-text.d.ts.map +1 -0
- package/out/well-known-text.js.map +1 -0
- package/package.json +143 -14
- package/geometries/index.ts +0 -12
- package/index.ts +0 -18
- 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/position.ts +0 -345
- 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/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 {
|
|
8
|
-
|
|
9
|
-
import type { GeoObjectLiteral } from "
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
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 =
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
214
|
-
if (!
|
|
233
|
+
export function arealPolygons(geometry: ParsedGeometry | null | undefined): MultiPolygonRings | null {
|
|
234
|
+
if (!geometry) return null
|
|
215
235
|
|
|
216
|
-
|
|
236
|
+
if (geometry.type === "Polygon") return [geometry.coordinates as PolygonRings]
|
|
217
237
|
|
|
218
|
-
|
|
238
|
+
if (geometry.type === "MultiPolygon") return geometry.coordinates as MultiPolygonRings
|
|
219
239
|
|
|
220
|
-
return
|
|
240
|
+
return null
|
|
221
241
|
}
|
|
222
242
|
|
|
223
243
|
/**
|
|
224
|
-
*
|
|
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
|
-
* @
|
|
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
|
|
229
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
|
284
|
-
|
|
285
|
-
|
|
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 (
|
|
277
|
+
if (!polygons) return null
|
|
289
278
|
|
|
290
|
-
return
|
|
279
|
+
return polygons.some((rings) => pointInPolygon(lon, lat, rings))
|
|
291
280
|
}
|
|
292
281
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|