@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
package/{bbox.ts → lib/bbox.ts}
RENAMED
|
@@ -6,9 +6,20 @@
|
|
|
6
6
|
* GeoJSON Bounding Boxes
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
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
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Kilometres per degree of latitude, which is very nearly constant on a sphere.
|
|
16
|
+
*/
|
|
17
|
+
const KM_PER_DEGREE_LATITUDE = 111
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Floor for cos(latitude) so a polar bbox stays finite. See {@linkcode bboxAround}.
|
|
21
|
+
*/
|
|
22
|
+
const MIN_COS_LATITUDE = 1e-6
|
|
12
23
|
|
|
13
24
|
/**
|
|
14
25
|
* Arity of a 2D bounding box: `[west, south, east, north]`.
|
|
@@ -452,3 +463,43 @@ export class GeoBoundingBox {
|
|
|
452
463
|
|
|
453
464
|
//#endregion
|
|
454
465
|
}
|
|
466
|
+
|
|
467
|
+
//#region Equirectangular bbox around a point
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* A flat latitude/longitude range, as a query filter rather than a model.
|
|
471
|
+
*
|
|
472
|
+
* Distinct from {@linkcode GeoBoundingBox} on purpose: that is a class carrying a projection and private state, which an
|
|
473
|
+
* SQL row cannot be. This is the plain shape a spatial-index query is built from — four numbers, no behaviour.
|
|
474
|
+
*/
|
|
475
|
+
export interface LatLonBounds {
|
|
476
|
+
minLat: number
|
|
477
|
+
maxLat: number
|
|
478
|
+
minLon: number
|
|
479
|
+
maxLon: number
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Approximate bounds `radiusKM` in each direction around a point.
|
|
484
|
+
*
|
|
485
|
+
* The spherical-Earth equirectangular approximation: 1° latitude ≈ 111 km globally, 1° longitude ≈ 111 km × cos(lat).
|
|
486
|
+
* It is a FILTER, not an answer — it over-selects near the poles and along a long east-west span, and a caller is
|
|
487
|
+
* expected to re-check survivors with an exact haversine distance. That is what makes the approximation safe: it may
|
|
488
|
+
* admit a point it should not, and never excludes one it should keep.
|
|
489
|
+
*/
|
|
490
|
+
export function bboxAround(lat: number, lon: number, radiusKM: number): LatLonBounds {
|
|
491
|
+
const latDelta = radiusKM / KM_PER_DEGREE_LATITUDE
|
|
492
|
+
// cos(±90°) is 0, which would divide the longitude delta to Infinity and select the whole globe. Clamp so a polar
|
|
493
|
+
// query stays a wide band rather than becoming unbounded.
|
|
494
|
+
const cosLat = Math.max(Math.cos(toRad(lat)), MIN_COS_LATITUDE)
|
|
495
|
+
const lonDelta = radiusKM / (KM_PER_DEGREE_LATITUDE * cosLat)
|
|
496
|
+
|
|
497
|
+
return {
|
|
498
|
+
minLat: lat - latDelta,
|
|
499
|
+
maxLat: lat + latDelta,
|
|
500
|
+
minLon: lon - lonDelta,
|
|
501
|
+
maxLon: lon + lonDelta,
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
//#endregion
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software.
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Dependency-free WGS-84 coordinate bounds shared by parsers and geometry objects.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Southern limit of latitude in WGS-84 degrees.
|
|
11
|
+
*/
|
|
12
|
+
export const LATITUDE_MIN = -90
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Northern limit of latitude in WGS-84 degrees.
|
|
16
|
+
*/
|
|
17
|
+
export const LATITUDE_MAX = 90
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Western limit of longitude in WGS-84 degrees.
|
|
21
|
+
*/
|
|
22
|
+
export const LONGITUDE_MIN = -180
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Eastern limit of longitude in WGS-84 degrees.
|
|
26
|
+
*/
|
|
27
|
+
export const LONGITUDE_MAX = 180
|
|
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
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether a finite number is a latitude on the globe, including both poles.
|
|
42
|
+
*/
|
|
43
|
+
export function isValidLatitude(value: number): boolean {
|
|
44
|
+
return Number.isFinite(value) && value >= LATITUDE_MIN && value <= LATITUDE_MAX
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether a finite number is a longitude on the globe, including the antimeridian.
|
|
49
|
+
*/
|
|
50
|
+
export function isValidLongitude(value: number): boolean {
|
|
51
|
+
return Number.isFinite(value) && value >= LONGITUDE_MIN && value <= LONGITUDE_MAX
|
|
52
|
+
}
|
|
@@ -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
|
*/
|
|
@@ -26,8 +28,19 @@ const MGRS_LAT_MIN = -80
|
|
|
26
28
|
*/
|
|
27
29
|
const MGRS_LAT_MAX = 84
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
/**
|
|
32
|
+
* WGS-84 degrees → radians.
|
|
33
|
+
*/
|
|
34
|
+
export function toRad(deg: number): number {
|
|
35
|
+
return (deg * Math.PI) / 180
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* WGS-84 radians → degrees.
|
|
40
|
+
*/
|
|
41
|
+
export function toDeg(rad: number): number {
|
|
42
|
+
return (rad * 180) / Math.PI
|
|
43
|
+
}
|
|
31
44
|
|
|
32
45
|
/**
|
|
33
46
|
* Render a single signed degree as `D° M′ S″ H` with the given hemisphere letters `[positive, negative]`.
|
|
@@ -56,7 +69,7 @@ const WEB_MERCATOR_R = 6_378_137
|
|
|
56
69
|
* Web Mercator (EPSG:3857) projection of a coordinate.
|
|
57
70
|
*/
|
|
58
71
|
export function toMercator(lat: number, lon: number): { x: number; y: number } {
|
|
59
|
-
const clampedLat = Math.max(
|
|
72
|
+
const clampedLat = Math.max(MERCATOR_LATITUDE_MIN, Math.min(MERCATOR_LATITUDE_MAX, lat))
|
|
60
73
|
|
|
61
74
|
return {
|
|
62
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
|
*
|