@mailwoman/spatial 7.8.1 → 8.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/spatial",
3
- "version": "7.8.1",
3
+ "version": "8.0.0",
4
4
  "description": "Spatial analysis, geocoding, and other geo-related utilities.",
5
5
  "keywords": [
6
6
  "geo",
@@ -18,10 +18,21 @@
18
18
  "url": "https://github.com/sister-software/mailwoman.git",
19
19
  "directory": "spatial"
20
20
  },
21
+ "files": [
22
+ "out/",
23
+ "*.ts",
24
+ "*.tsx",
25
+ "**/*.ts",
26
+ "**/*.tsx",
27
+ "!*.test.ts",
28
+ "!*.test.tsx",
29
+ "!**/*.test.ts",
30
+ "!**/*.test.tsx",
31
+ "!__snapshots__/"
32
+ ],
21
33
  "type": "module",
22
34
  "exports": {
23
35
  "./package.json": "./package.json",
24
- "./schema/*.json": "./dist/schema/*.json",
25
36
  "./sdk": {
26
37
  "types": "./out/sdk/index.d.ts",
27
38
  "default": "./out/sdk/index.js"
@@ -35,7 +46,6 @@
35
46
  "access": "public",
36
47
  "exports": {
37
48
  "./package.json": "./package.json",
38
- "./schema/*.json": "./dist/schema/*.json",
39
49
  "./sdk": {
40
50
  "types": "./out/sdk/index.d.ts",
41
51
  "default": "./out/sdk/index.js"
@@ -47,20 +57,20 @@
47
57
  }
48
58
  },
49
59
  "dependencies": {
50
- "@mailwoman/core": "7.8.1",
60
+ "@mailwoman/core": "8.0.0",
51
61
  "geo-coordinates-parser": "^1.7.4",
52
62
  "h3-js": "^4.5.0",
53
63
  "wkx": "^0.5.0"
54
64
  },
55
65
  "devDependencies": {
56
66
  "@googlemaps/google-maps-services-js": "^3.4.2",
57
- "@mailwoman/annotations": "7.8.1",
58
- "@types/google.maps": "^3.65.2",
67
+ "@mailwoman/annotations": "8.0.0",
68
+ "@types/google.maps": "^3.65.3",
59
69
  "type-fest": "^5.8.0"
60
70
  },
61
71
  "optionalDependencies": {
62
72
  "@googlemaps/google-maps-services-js": "^3.4.2",
63
- "@types/google.maps": "^3.65.2"
73
+ "@types/google.maps": "^3.65.3"
64
74
  },
65
75
  "engines": {
66
76
  "node": ">=24.18.0"
package/bbox.test.ts DELETED
@@ -1,39 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { is2DBBox, is3DBBox, isBBox } from "@mailwoman/spatial"
8
- import { expect, test } from "vitest"
9
-
10
- // 2D GeoJSON bbox: [west, south, east, north]. 3D adds min/max altitude: [w, s, minA, e, n, maxA].
11
- const BBOX_2D = [-74.1, 40.6, -73.9, 40.9]
12
- const BBOX_3D = [-74.1, 40.6, 0, -73.9, 40.9, 100]
13
-
14
- test("is2DBBox: a length-4 tuple is 2D; a length-6 tuple is not", () => {
15
- // Regression: this guard used to check length === 6 (a copy of is3DBBox), so it never
16
- // recognized a real 2D bbox.
17
- expect(is2DBBox(BBOX_2D)).toBe(true)
18
- expect(is2DBBox(BBOX_3D)).toBe(false)
19
- })
20
-
21
- test("is3DBBox: a length-6 tuple is 3D; a length-4 tuple is not", () => {
22
- expect(is3DBBox(BBOX_3D)).toBe(true)
23
- expect(is3DBBox(BBOX_2D)).toBe(false)
24
- })
25
-
26
- test("isBBox: accepts length 4 or 6, rejects other shapes", () => {
27
- expect(isBBox(BBOX_2D)).toBe(true)
28
- expect(isBBox(BBOX_3D)).toBe(true)
29
- expect(isBBox([1, 2, 3, 4, 5])).toBe(false) // length 5
30
- expect(isBBox([1, 2, 3])).toBe(false) // length 3
31
- })
32
-
33
- test("the bbox guards reject non-array input", () => {
34
- for (const input of [null, undefined, {}, "bbox", 4, { length: 4 }]) {
35
- expect(is2DBBox(input)).toBe(false)
36
- expect(is3DBBox(input)).toBe(false)
37
- expect(isBBox(input)).toBe(false)
38
- }
39
- })
@@ -1,85 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { expect, test } from "vitest"
8
-
9
- import {
10
- coordinateFormatAnnotator,
11
- qiblaBearing,
12
- sunTimes,
13
- toDMS,
14
- toGeohash,
15
- toMaidenhead,
16
- toMercator,
17
- toMGRS,
18
- } from "./coordinate-formats.ts"
19
-
20
- // The White House (38.8977, -77.0365) — reference values cross-checked against published converters.
21
- const LAT = 38.8977
22
- const LON = -77.0365
23
-
24
- test("toDMS: signed decimals → D° M′ S″ with hemisphere", () => {
25
- const dms = toDMS(LAT, LON)
26
- expect(dms.lat).toBe("38° 53′ 51.72″ N")
27
- expect(dms.lon).toBe("77° 2′ 11.40″ W")
28
- })
29
-
30
- test("toMaidenhead: 6-char locator with lowercase subsquare", () => {
31
- expect(toMaidenhead(LAT, LON)).toBe("FM18lv")
32
- })
33
-
34
- test("toGeohash: precision-9, deterministic, DC prefix", () => {
35
- const gh = toGeohash(LAT, LON)
36
- expect(gh).toHaveLength(9)
37
- expect(gh.startsWith("dqc")).toBe(true)
38
- expect(toGeohash(LAT, LON)).toBe(gh)
39
- })
40
-
41
- test("toMercator: EPSG:3857 projection in range", () => {
42
- const { x, y } = toMercator(LAT, LON)
43
- expect(x).toBeGreaterThan(-8576000)
44
- expect(x).toBeLessThan(-8575000)
45
- expect(y).toBeGreaterThan(4690000)
46
- expect(y).toBeLessThan(4730000)
47
- })
48
-
49
- test("qiblaBearing: from DC the Kaaba is ~58° (ENE)", () => {
50
- const b = qiblaBearing(LAT, LON)
51
- expect(b).toBeGreaterThan(54)
52
- expect(b).toBeLessThan(62)
53
- })
54
-
55
- test("sunTimes: NYC summer solstice — sunrise ~09:26 UTC, ordered, ~15h day", () => {
56
- const s = sunTimes(40.7128, -74.006, new Date("2026-06-21T12:00:00Z"))
57
- const expectedRise = new Date("2026-06-21T09:26:00Z").getTime() / 1000
58
- expect(Math.abs(s.rise! - expectedRise)).toBeLessThan(600) // within 10 min
59
- expect(s.rise!).toBeLessThan(s.noon)
60
- expect(s.noon).toBeLessThan(s.set!)
61
- expect((s.set! - s.rise!) / 3600).toBeGreaterThan(14.5)
62
- })
63
-
64
- test("sunTimes: polar day has no sunrise/sunset, only solar noon", () => {
65
- const s = sunTimes(80, 0, new Date("2026-06-21T12:00:00Z"))
66
- expect(s.rise).toBeUndefined()
67
- expect(s.set).toBeUndefined()
68
- expect(typeof s.noon).toBe("number")
69
- })
70
-
71
- test("toMGRS: Washington Monument matches Wikipedia's vector (~4m); zone+band elsewhere; empty in polar bands", () => {
72
- // Wikipedia MGRS article cites 18S UJ 23487 06483 for the monument; we match to ~4m.
73
- expect(toMGRS(38.88949, -77.03524)).toBe("18SUJ2348306482")
74
- expect(toMGRS(-33.8688, 151.2093).startsWith("56H")).toBe(true) // Sydney, zone 56 band H
75
- expect(toMGRS(85, 0)).toBe("") // above 84°N — MGRS bands stop
76
- })
77
-
78
- test("coordinateFormatAnnotator: fills the coordinate-format slice of an AnnotationSet", () => {
79
- const set = coordinateFormatAnnotator({ lat: LAT, lon: LON })
80
- expect(set.maidenhead).toBe("FM18lv")
81
- expect(set.geohash).toHaveLength(9)
82
- expect(set.dms?.lat).toContain("N")
83
- expect(typeof set.qiblaBearing).toBe("number")
84
- expect(set.mercator?.x).toBeLessThan(0)
85
- })
@@ -1,144 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { GeoPoint } from "@mailwoman/spatial"
8
- import { expect, test } from "vitest"
9
-
10
- // GeoPoint stores GeoJSON [longitude, latitude(, altitude)] order. `from` recognizes several input
11
- // shapes and treats a 0/0 coordinate (Null Island) as a "missing coordinate" sentinel → null.
12
-
13
- test("GeoPoint.from: a 2-tuple is read as GeoJSON [longitude, latitude]", () => {
14
- // coordA = -74 is in [-90, 90] so inferGeoJSONCoordOrder can't disambiguate; it falls through to
15
- // the default, leaving the pair as-is: longitude = -74, latitude = 40.7.
16
- const point = GeoPoint.from([-74.006, 40.7128])!
17
-
18
- expect(point).not.toBeNull()
19
- expect(point.longitude).toBe(-74.006)
20
- expect(point.latitude).toBe(40.7128)
21
- expect(point.altitude).toBe(0)
22
- expect(point.is2D()).toBe(true)
23
- })
24
-
25
- test("GeoPoint.from: an out-of-lat-range longitude is recognized as longitude", () => {
26
- // coordA = -118.24 is outside [-90, 90] → unambiguously the longitude; coordB = 34.05 is latitude.
27
- const point = GeoPoint.from([-118.2437, 34.0522])!
28
-
29
- expect(point.longitude).toBe(-118.2437)
30
- expect(point.latitude).toBe(34.0522)
31
- })
32
-
33
- test("GeoPoint.from: a 3-tuple is used directly as [longitude, latitude, altitude]", () => {
34
- const point = GeoPoint.from([-74.006, 40.7128, 125])!
35
-
36
- expect(point.longitude).toBe(-74.006)
37
- expect(point.latitude).toBe(40.7128)
38
- expect(point.altitude).toBe(125)
39
- expect(point.is3D()).toBe(true)
40
- expect(point.coordinates).toEqual([-74.006, 40.7128, 125])
41
- })
42
-
43
- test("GeoPoint.from: a PointLiteral copies coordinates verbatim (no axis inference)", () => {
44
- const point = GeoPoint.from({ type: "Point", coordinates: [-74.006, 40.7128] })!
45
-
46
- expect(point.type).toBe("Point")
47
- expect(point.longitude).toBe(-74.006)
48
- expect(point.latitude).toBe(40.7128)
49
- })
50
-
51
- test("GeoPoint.from: a Google Maps LatLngLiteral maps lng→longitude, lat→latitude", () => {
52
- const point = GeoPoint.from({ lat: 40.7128, lng: -74.006 })!
53
-
54
- expect(point.longitude).toBe(-74.006)
55
- expect(point.latitude).toBe(40.7128)
56
- })
57
-
58
- test("GeoPoint.from: a GeolocationCoordinates-like object carries altitude through", () => {
59
- const point = GeoPoint.from({ latitude: 48.8566, longitude: 2.3522, altitude: 35 })!
60
-
61
- expect(point.longitude).toBe(2.3522)
62
- expect(point.latitude).toBe(48.8566)
63
- expect(point.altitude).toBe(35)
64
- expect(point.is3D()).toBe(true)
65
- })
66
-
67
- test("GeoPoint.from: internal {x, y} coordinates map x→longitude, y→latitude", () => {
68
- const point = GeoPoint.from({ x: -74.006, y: 40.7128 })!
69
-
70
- expect(point.longitude).toBe(-74.006)
71
- expect(point.latitude).toBe(40.7128)
72
- expect(point.altitude).toBe(0)
73
- })
74
-
75
- test("GeoPoint.from: a bracketless coordinate string is parsed into a pair", () => {
76
- // "-74.006,40.7128" isn't valid JSON, so `from` retries as "[-74.006,40.7128]".
77
- const point = GeoPoint.from("-74.006,40.7128")!
78
-
79
- expect(point).not.toBeNull()
80
- expect(point.longitude).toBe(-74.006)
81
- expect(point.latitude).toBe(40.7128)
82
- })
83
-
84
- test("GeoPoint.from: a JSON array string is parsed", () => {
85
- const point = GeoPoint.from("[2.3522, 48.8566]")!
86
-
87
- expect(point.longitude).toBe(2.3522)
88
- expect(point.latitude).toBe(48.8566)
89
- })
90
-
91
- test("GeoPoint.from: an existing GeoPoint is returned unchanged", () => {
92
- const original = GeoPoint.from([12.4924, 41.8902])!
93
- const passed = GeoPoint.from(original)
94
-
95
- expect(passed).toBe(original)
96
- })
97
-
98
- test("GeoPoint.from: the 0/0 (Null Island) sentinel resolves to null", () => {
99
- expect(GeoPoint.from([0, 0])).toBeNull()
100
- expect(GeoPoint.from({ type: "Point", coordinates: [0, 0] })).toBeNull()
101
- expect(GeoPoint.from({ lat: 0, lng: 0 })).toBeNull()
102
- expect(GeoPoint.from({ x: 0, y: 0 })).toBeNull()
103
- expect(GeoPoint.from("0,0")).toBeNull()
104
- })
105
-
106
- test("GeoPoint.from: falsy and unparseable input resolves to null", () => {
107
- expect(GeoPoint.from(null)).toBeNull()
108
- expect(GeoPoint.from(undefined)).toBeNull()
109
- expect(GeoPoint.from("")).toBeNull()
110
- expect(GeoPoint.from(0)).toBeNull()
111
- // A garbage string that is neither valid JSON nor a wrappable pair falls back to the default 0/0
112
- // coordinate, which the Null-Island sentinel then rejects.
113
- expect(GeoPoint.from("not-a-coordinate")).toBeNull()
114
- })
115
-
116
- test("GeoPoint.from: a non-zero point that is NOT Null Island survives", () => {
117
- // Guards against an over-eager sentinel: a real coordinate near, but not at, the origin.
118
- const point = GeoPoint.from([0.0001, 0.0001])!
119
-
120
- expect(point).not.toBeNull()
121
- expect(point.isNullIsland()).toBe(false)
122
- })
123
-
124
- test("GeoPoint: longitude wraps and latitude clamps on assignment", () => {
125
- const point = new GeoPoint([0, 0])
126
-
127
- point.longitude = 190 // 190 wraps to -170
128
- point.latitude = 100 // clamped to the north pole
129
-
130
- expect(point.longitude).toBe(-170)
131
- expect(point.latitude).toBe(90)
132
- })
133
-
134
- test("GeoPoint is iterable, yielding its coordinate tuple", () => {
135
- const point = GeoPoint.from([-74.006, 40.7128, 5])!
136
-
137
- expect([...point]).toEqual([-74.006, 40.7128, 5])
138
- })
139
-
140
- test("GeoPoint.toJSON emits a GeoJSON Point literal", () => {
141
- const point = GeoPoint.from([-74.006, 40.7128])!
142
-
143
- expect(point.toJSON()).toEqual({ type: "Point", coordinates: [-74.006, 40.7128] })
144
- })
@@ -1,63 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { expect, test } from "vitest"
8
-
9
- import {
10
- isPolygonLiteral,
11
- isResidentialElement,
12
- isSolidPolygonPath,
13
- polygonToOSMFilter,
14
- type OSMOverpassElement,
15
- type PolygonLiteral,
16
- } from "./polygon.ts"
17
-
18
- const SOLID: PolygonLiteral = {
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
- test("isPolygonLiteral: only a {type:'Polygon', coordinates: []} object qualifies", () => {
32
- expect(isPolygonLiteral(SOLID)).toBe(true)
33
- expect(isPolygonLiteral({ type: "Point", coordinates: [0, 0] })).toBe(false)
34
- expect(isPolygonLiteral({ type: "Polygon" })).toBe(false) // no coordinates
35
- expect(isPolygonLiteral(null)).toBe(false)
36
- expect(isPolygonLiteral("Polygon")).toBe(false)
37
- })
38
-
39
- test("isSolidPolygonPath: one ring = solid, more rings = has holes", () => {
40
- expect(isSolidPolygonPath(SOLID)).toBe(true)
41
- const withHole = {
42
- type: "Polygon",
43
- coordinates: [SOLID.coordinates[0], SOLID.coordinates[0]],
44
- } as unknown as PolygonLiteral
45
- expect(isSolidPolygonPath(withHole)).toBe(false)
46
- })
47
-
48
- test("polygonToOSMFilter: emits the exterior ring as Overpass 'lat lon' pairs (NOT GeoJSON lon,lat)", () => {
49
- // GeoJSON positions are [lon, lat]; Overpass wants "lat lon" — this swap is the foot-gun.
50
- expect(polygonToOSMFilter(SOLID)).toBe("poly:'0 100 0 101 1 101 1 100 0 100'")
51
- expect(polygonToOSMFilter({ type: "Point" } as unknown as PolygonLiteral)).toBe("") // non-polygon → empty
52
- })
53
-
54
- test("isResidentialElement: rejects commercial tags + restaurants, accepts a plain address node", () => {
55
- const el = (tags: Record<string, string>): OSMOverpassElement =>
56
- ({ type: "node", id: 1, lat: 0, lon: 0, tags }) as unknown as OSMOverpassElement
57
-
58
- expect(isResidentialElement(el({ "addr:housenumber": "5", "addr:street": "Main" }))).toBe(true)
59
- expect(isResidentialElement(el({ shop: "bakery" }))).toBe(false) // forbidden commercial tag
60
- expect(isResidentialElement(el({ office: "company" }))).toBe(false)
61
- expect(isResidentialElement(el({ amenity: "restaurant" }))).toBe(false) // restaurant special-case
62
- expect(isResidentialElement(el({ amenity: "bench" }))).toBe(true) // a non-forbidden amenity is fine
63
- })
@@ -1,54 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { GOOGLE_PLACE_ID_PATTERN, isGooglePlaceID } from "@mailwoman/spatial"
8
- import { expect, test } from "vitest"
9
-
10
- // A Google Place ID is a non-empty string of base64url-style characters: letters, digits, "_", "-".
11
- // The pattern is anchored end-to-end, so any other character (or an empty string) is rejected.
12
-
13
- test("isGooglePlaceID: a real 27-char Place ID is accepted", () => {
14
- // Google's documented example ID for the Sydney Opera House.
15
- expect(isGooglePlaceID("ChIJN1t_tDeuEmsRUsoyG83frY4")).toBe(true)
16
- })
17
-
18
- test("isGooglePlaceID: underscores and dashes are valid characters", () => {
19
- expect(isGooglePlaceID("a_b-c_D-9")).toBe(true)
20
- expect(isGooglePlaceID("____")).toBe(true)
21
- expect(isGooglePlaceID("----")).toBe(true)
22
- })
23
-
24
- test("isGooglePlaceID: alphanumerics of any length are accepted", () => {
25
- expect(isGooglePlaceID("a")).toBe(true)
26
- expect(isGooglePlaceID("ABC123xyz")).toBe(true)
27
- })
28
-
29
- test("isGooglePlaceID: an empty string is rejected (minLength 1)", () => {
30
- expect(isGooglePlaceID("")).toBe(false)
31
- })
32
-
33
- test("isGooglePlaceID: characters outside [A-Za-z0-9_-] are rejected", () => {
34
- expect(isGooglePlaceID("has space")).toBe(false)
35
- expect(isGooglePlaceID("has.dot")).toBe(false)
36
- expect(isGooglePlaceID("plus+sign")).toBe(false)
37
- expect(isGooglePlaceID("slash/here")).toBe(false)
38
- expect(isGooglePlaceID("equals=pad")).toBe(false)
39
- expect(isGooglePlaceID("emoji😀")).toBe(false)
40
- })
41
-
42
- test("isGooglePlaceID: the pattern is fully anchored (a bad char anywhere fails)", () => {
43
- // A leading or trailing invalid character must fail even when the rest is valid — proving the
44
- // regex is anchored at both ends rather than merely "contains a valid run".
45
- expect(isGooglePlaceID(" ChIJN1t_tDeuEmsRUsoyG83frY4")).toBe(false)
46
- expect(isGooglePlaceID("ChIJN1t_tDeuEmsRUsoyG83frY4 ")).toBe(false)
47
- expect(isGooglePlaceID("valid\ninvalid")).toBe(false)
48
- })
49
-
50
- test("GOOGLE_PLACE_ID_PATTERN is the anchored character-class pattern", () => {
51
- expect(GOOGLE_PLACE_ID_PATTERN.source).toBe("^[A-Za-z0-9_-]+$")
52
- expect(GOOGLE_PLACE_ID_PATTERN.test("ChIJN1t_tDeuEmsRUsoyG83frY4")).toBe(true)
53
- expect(GOOGLE_PLACE_ID_PATTERN.test("bad char")).toBe(false)
54
- })
package/position.test.ts DELETED
@@ -1,48 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import { haversineKm } from "@mailwoman/spatial"
8
- import { expect, test } from "vitest"
9
-
10
- // Earth mean radius the formula uses (RADII.km). Reference distances below are derived from it, not
11
- // looked up — so they pin the exact constant + formula, not an approximation.
12
- const R = 6371
13
-
14
- test("haversineKm: a point to itself is zero", () => {
15
- expect(haversineKm(40.7128, -74.006, 40.7128, -74.006)).toBe(0)
16
- })
17
-
18
- test("haversineKm: one degree along the equator / a meridian is R·(π/180)", () => {
19
- const oneDegree = (R * Math.PI) / 180 // ≈ 111.195 km
20
- expect(haversineKm(0, 0, 0, 1)).toBeCloseTo(oneDegree, 3) // 1° longitude at the equator
21
- expect(haversineKm(0, 0, 1, 0)).toBeCloseTo(oneDegree, 3) // 1° latitude along a meridian
22
- })
23
-
24
- test("haversineKm: antipodal points are half the great circle (R·π)", () => {
25
- expect(haversineKm(0, 0, 0, 180)).toBeCloseTo(R * Math.PI, 2) // ≈ 20015.09 km
26
- })
27
-
28
- test("haversineKm: a real-world pair lands in the right ballpark (NYC ↔ LA)", () => {
29
- const d = haversineKm(40.7128, -74.006, 34.0522, -118.2437)
30
- expect(d).toBeGreaterThan(3900)
31
- expect(d).toBeLessThan(3970) // ~3936 km
32
- })
33
-
34
- test("haversineKm: symmetric in its arguments", () => {
35
- const ab = haversineKm(51.5074, -0.1278, 48.8566, 2.3522) // London → Paris
36
- const ba = haversineKm(48.8566, 2.3522, 51.5074, -0.1278)
37
- expect(ab).toBeCloseTo(ba, 10)
38
- expect(ab).toBeGreaterThan(330)
39
- expect(ab).toBeLessThan(355) // ~343 km
40
- })
41
-
42
- test("haversineKm: (0,0) is a real point (Gulf of Guinea), not a missing-coordinate sentinel", () => {
43
- // Unlike the object-form `haversine`, the raw-scalar form has no Null-Island sentinel — 0/0 is a
44
- // real coordinate, so this returns a finite distance rather than NaN.
45
- const d = haversineKm(0, 0, 0.5, 0.5)
46
- expect(Number.isNaN(d)).toBe(false)
47
- expect(d).toBeGreaterThan(0)
48
- })
@@ -1,134 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
6
-
7
- import type { GeometryLiteral } from "@mailwoman/spatial"
8
- import { expect, test } from "vitest"
9
- import wkx from "wkx"
10
-
11
- import {
12
- geometryToEWKB,
13
- geometryToEWKH,
14
- geometryToSQL,
15
- geometryToWKB,
16
- geometryToWKT,
17
- wellKnownGeometryToGeoJSON,
18
- } from "./well-known-text.ts"
19
-
20
- const POINT: GeometryLiteral = { type: "Point", coordinates: [30, 10] }
21
-
22
- // A closed exterior ring (last vertex repeats the first), as GeoJSON requires.
23
- const POLYGON: GeometryLiteral = {
24
- type: "Polygon",
25
- coordinates: [
26
- [
27
- [30, 10],
28
- [40, 40],
29
- [20, 40],
30
- [10, 20],
31
- [30, 10],
32
- ],
33
- ],
34
- }
35
-
36
- // The 4-level MultiPolygon literal doesn't narrow against the GeometryLiteral union; cast it.
37
- const MULTIPOLYGON = {
38
- type: "MultiPolygon",
39
- coordinates: [
40
- [
41
- [
42
- [30, 20],
43
- [45, 40],
44
- [10, 40],
45
- [30, 20],
46
- ],
47
- ],
48
- [
49
- [
50
- [15, 5],
51
- [40, 10],
52
- [10, 20],
53
- [5, 10],
54
- [15, 5],
55
- ],
56
- ],
57
- ],
58
- } as unknown as GeometryLiteral
59
-
60
- test("wellKnownGeometryToGeoJSON: parses a WKT POINT into exact GeoJSON coordinates", () => {
61
- const geo = wellKnownGeometryToGeoJSON<GeometryLiteral>("POINT(30 10)")
62
- expect(geo).toEqual(POINT)
63
- })
64
-
65
- test("wellKnownGeometryToGeoJSON: parses a WKT POLYGON, preserving ring order and closure", () => {
66
- const geo = wellKnownGeometryToGeoJSON<GeometryLiteral>("POLYGON((30 10,40 40,20 40,10 20,30 10))")
67
- expect(geo).toEqual(POLYGON)
68
- })
69
-
70
- test("wellKnownGeometryToGeoJSON: parses a WKT MULTIPOLYGON into two distinct polygons", () => {
71
- const geo = wellKnownGeometryToGeoJSON<GeometryLiteral>(
72
- "MULTIPOLYGON(((30 20,45 40,10 40,30 20)),((15 5,40 10,10 20,5 10,15 5)))"
73
- )
74
- expect(geo).toEqual(MULTIPOLYGON)
75
- })
76
-
77
- test("wellKnownGeometryToGeoJSON: parses an EWKB buffer back into the source geometry", () => {
78
- // Produce a known EWKB buffer, then round-trip it through the parser.
79
- const ewkb = wkx.Geometry.parseGeoJSON(POINT).toEwkb()
80
- const geo = wellKnownGeometryToGeoJSON<GeometryLiteral>(ewkb)
81
- expect(geo).toEqual(POINT)
82
- })
83
-
84
- test("wellKnownGeometryToGeoJSON: throws on malformed WKT", () => {
85
- expect(() => wellKnownGeometryToGeoJSON("NOT A GEOMETRY")).toThrow()
86
- })
87
-
88
- test("geometryToWKT: serializes GeoJSON back to its canonical WKT string", () => {
89
- expect(geometryToWKT(POINT)).toBe("POINT(30 10)")
90
- expect(geometryToWKT(POLYGON)).toBe("POLYGON((30 10,40 40,20 40,10 20,30 10))")
91
- expect(geometryToWKT(MULTIPOLYGON)).toBe("MULTIPOLYGON(((30 20,45 40,10 40,30 20)),((15 5,40 10,10 20,5 10,15 5)))")
92
- })
93
-
94
- test("geometryToWKT ↔ wellKnownGeometryToGeoJSON: lossless round-trip for a polygon", () => {
95
- const wkt = geometryToWKT(POLYGON)
96
- const back = wellKnownGeometryToGeoJSON<GeometryLiteral>(wkt)
97
- expect(back).toEqual(POLYGON)
98
- })
99
-
100
- test("geometryToWKB: produces the exact little-endian WKB byte string for a POINT", () => {
101
- // 01 (LE) | 01000000 (type=Point) | 30 as float64 LE | 10 as float64 LE
102
- const wkb = geometryToWKB(POINT)
103
- expect(wkb.toString("hex")).toBe("01010000000000000000003e400000000000002440")
104
- })
105
-
106
- test("geometryToWKB: round-trips through the parser back to source GeoJSON", () => {
107
- const wkb = geometryToWKB(MULTIPOLYGON)
108
- const back = wkx.Geometry.parse(wkb).toGeoJSON()
109
- expect(back).toEqual(MULTIPOLYGON)
110
- })
111
-
112
- test("geometryToEWKB: tags the geometry with the SRID-flag the plain WKB lacks", () => {
113
- // EWKB sets the 0x20000000 SRID flag on the type word, so the hex differs from plain WKB.
114
- const ewkb = geometryToEWKB(POINT)
115
- expect(ewkb.toString("hex")).toBe("0101000020e61000000000000000003e400000000000002440")
116
- // Distinct from plain WKB.
117
- expect(ewkb.toString("hex")).not.toBe(geometryToWKB(POINT).toString("hex"))
118
- })
119
-
120
- test("geometryToEWKH: is the hex-encoded form of the EWKB buffer", () => {
121
- expect(geometryToEWKH(POINT)).toBe(geometryToEWKB(POINT).toString("hex"))
122
- expect(geometryToEWKH(POINT)).toBe("0101000020e61000000000000000003e400000000000002440")
123
- })
124
-
125
- test("geometryToSQL: returns a thunk emitting a GeomFromEWKB literal for a real geometry", () => {
126
- const thunk = geometryToSQL(POINT)
127
- expect(typeof thunk).toBe("function")
128
- expect(thunk()).toBe(`GeomFromEWKB('${geometryToEWKH(POINT)}')`)
129
- })
130
-
131
- test("geometryToSQL: null/undefined geometry yields a thunk that emits the SQL literal NULL", () => {
132
- expect(geometryToSQL(null)()).toBe("NULL")
133
- expect(geometryToSQL(undefined)()).toBe("NULL")
134
- })
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "extends": "@sister.software/tsconfig",
3
- "compilerOptions": {
4
- "allowSyntheticDefaultImports": true,
5
- "emitDecoratorMetadata": true,
6
- "experimentalDecorators": true,
7
- "emitDeclarationOnly": false,
8
- "rewriteRelativeImportExtensions": true,
9
- "erasableSyntaxOnly": true
10
- },
11
- "exclude": ["./out/**/*", "./**/*.test.ts", "./**/*.test.tsx"],
12
- "references": [
13
- // ---
14
- { "path": "../annotations" },
15
- { "path": "../core" }
16
- ]
17
- }
package/typedoc.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": ["../typedoc.base.jsonc"],
3
- "entryPoints": ["index.ts"]
4
- }