@mailwoman/spatial 11.0.0 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +2 -1
  2. package/lib/bbox.ts +3 -4
  3. package/lib/coordinate/bounds.ts +3 -4
  4. package/lib/coordinate/cell.ts +42 -0
  5. package/lib/coordinate/formats.ts +61 -5
  6. package/lib/digipin.ts +282 -0
  7. package/lib/distance.ts +5 -5
  8. package/lib/feature.ts +2 -5
  9. package/lib/gb/codepoint.ts +6 -7
  10. package/lib/gb/os-downloads.ts +6 -6
  11. package/lib/geometries/collection.ts +3 -4
  12. package/lib/geometries/line-string.ts +3 -4
  13. package/lib/geometries/point/coordinates.ts +8 -8
  14. package/lib/geometries/point/distance.ts +4 -4
  15. package/lib/geometries/point.ts +10 -17
  16. package/lib/geometries/polygon.ts +3 -6
  17. package/lib/geometries/ring-blob.ts +9 -10
  18. package/lib/geometries/round.ts +73 -0
  19. package/lib/geometries/simplify.ts +126 -0
  20. package/lib/geometries.ts +3 -4
  21. package/lib/google/place-id.ts +3 -4
  22. package/lib/google.ts +2 -5
  23. package/lib/h3/cell.ts +5 -5
  24. package/lib/h3/coverage.ts +8 -9
  25. package/lib/h3/interior-cells.ts +5 -6
  26. package/lib/h3/polygon-cells.ts +9 -10
  27. package/lib/h3.ts +2 -5
  28. package/lib/index.ts +5 -5
  29. package/lib/objects.ts +2 -5
  30. package/lib/open-location-code.ts +85 -39
  31. package/lib/osgb36.ts +2 -5
  32. package/lib/point/reading.ts +323 -0
  33. package/lib/point/text.ts +70 -0
  34. package/lib/polyline.ts +9 -10
  35. package/lib/position.ts +8 -7
  36. package/lib/projection/transform.ts +36 -21
  37. package/lib/projection.ts +2 -5
  38. package/lib/regions/codes.ts +2 -5
  39. package/lib/regions/names.ts +2 -5
  40. package/lib/regions.ts +2 -5
  41. package/lib/well-known-text.ts +3 -4
  42. package/out/bbox.d.ts +0 -7
  43. package/out/bbox.d.ts.map +1 -1
  44. package/out/bbox.js +2 -4
  45. package/out/bbox.js.map +1 -1
  46. package/out/coordinate/bounds.d.ts +0 -4
  47. package/out/coordinate/bounds.d.ts.map +1 -1
  48. package/out/coordinate/bounds.js +2 -4
  49. package/out/coordinate/bounds.js.map +1 -1
  50. package/out/coordinate/cell.d.ts +23 -0
  51. package/out/coordinate/cell.d.ts.map +1 -0
  52. package/out/coordinate/cell.js +24 -0
  53. package/out/coordinate/cell.js.map +1 -0
  54. package/out/coordinate/formats.d.ts +12 -5
  55. package/out/coordinate/formats.d.ts.map +1 -1
  56. package/out/coordinate/formats.js +50 -5
  57. package/out/coordinate/formats.js.map +1 -1
  58. package/out/digipin.d.ts +94 -0
  59. package/out/digipin.d.ts.map +1 -0
  60. package/out/digipin.js +242 -0
  61. package/out/digipin.js.map +1 -0
  62. package/out/distance.d.ts +1 -11
  63. package/out/distance.d.ts.map +1 -1
  64. package/out/distance.js +4 -5
  65. package/out/distance.js.map +1 -1
  66. package/out/feature.d.ts +0 -5
  67. package/out/feature.d.ts.map +1 -1
  68. package/out/feature.js +2 -5
  69. package/out/feature.js.map +1 -1
  70. package/out/gb/codepoint.d.ts +1 -9
  71. package/out/gb/codepoint.d.ts.map +1 -1
  72. package/out/gb/codepoint.js +5 -7
  73. package/out/gb/codepoint.js.map +1 -1
  74. package/out/gb/os-downloads.d.ts +3 -6
  75. package/out/gb/os-downloads.d.ts.map +1 -1
  76. package/out/gb/os-downloads.js +5 -6
  77. package/out/gb/os-downloads.js.map +1 -1
  78. package/out/geometries/collection.d.ts +0 -4
  79. package/out/geometries/collection.d.ts.map +1 -1
  80. package/out/geometries/collection.js +2 -7
  81. package/out/geometries/collection.js.map +1 -1
  82. package/out/geometries/line-string.d.ts +0 -4
  83. package/out/geometries/line-string.d.ts.map +1 -1
  84. package/out/geometries/line-string.js +2 -7
  85. package/out/geometries/line-string.js.map +1 -1
  86. package/out/geometries/point/coordinates.d.ts +2 -12
  87. package/out/geometries/point/coordinates.d.ts.map +1 -1
  88. package/out/geometries/point/coordinates.js +5 -5
  89. package/out/geometries/point/coordinates.js.map +1 -1
  90. package/out/geometries/point/distance.d.ts +1 -4
  91. package/out/geometries/point/distance.d.ts.map +1 -1
  92. package/out/geometries/point/distance.js +3 -4
  93. package/out/geometries/point/distance.js.map +1 -1
  94. package/out/geometries/point.d.ts +6 -17
  95. package/out/geometries/point.d.ts.map +1 -1
  96. package/out/geometries/point.js +6 -8
  97. package/out/geometries/point.js.map +1 -1
  98. package/out/geometries/polygon.d.ts +1 -6
  99. package/out/geometries/polygon.d.ts.map +1 -1
  100. package/out/geometries/polygon.js +3 -6
  101. package/out/geometries/polygon.js.map +1 -1
  102. package/out/geometries/ring-blob.d.ts +3 -7
  103. package/out/geometries/ring-blob.d.ts.map +1 -1
  104. package/out/geometries/ring-blob.js +6 -13
  105. package/out/geometries/ring-blob.js.map +1 -1
  106. package/out/geometries/round.d.ts +25 -0
  107. package/out/geometries/round.d.ts.map +1 -0
  108. package/out/geometries/round.js +47 -0
  109. package/out/geometries/round.js.map +1 -0
  110. package/out/geometries/simplify.d.ts +22 -0
  111. package/out/geometries/simplify.d.ts.map +1 -0
  112. package/out/geometries/simplify.js +93 -0
  113. package/out/geometries/simplify.js.map +1 -0
  114. package/out/geometries.d.ts +0 -4
  115. package/out/geometries.d.ts.map +1 -1
  116. package/out/geometries.js +2 -4
  117. package/out/geometries.js.map +1 -1
  118. package/out/google/place-id.d.ts +0 -4
  119. package/out/google/place-id.d.ts.map +1 -1
  120. package/out/google/place-id.js +2 -7
  121. package/out/google/place-id.js.map +1 -1
  122. package/out/google.d.ts +0 -5
  123. package/out/google.d.ts.map +1 -1
  124. package/out/google.js +2 -5
  125. package/out/google.js.map +1 -1
  126. package/out/h3/cell.d.ts +1 -4
  127. package/out/h3/cell.d.ts.map +1 -1
  128. package/out/h3/cell.js +4 -5
  129. package/out/h3/cell.js.map +1 -1
  130. package/out/h3/coverage.d.ts +5 -9
  131. package/out/h3/coverage.d.ts.map +1 -1
  132. package/out/h3/coverage.js +7 -9
  133. package/out/h3/coverage.js.map +1 -1
  134. package/out/h3/interior-cells.d.ts +0 -8
  135. package/out/h3/interior-cells.d.ts.map +1 -1
  136. package/out/h3/interior-cells.js +4 -6
  137. package/out/h3/interior-cells.js.map +1 -1
  138. package/out/h3/polygon-cells.d.ts +6 -10
  139. package/out/h3/polygon-cells.d.ts.map +1 -1
  140. package/out/h3/polygon-cells.js +6 -8
  141. package/out/h3/polygon-cells.js.map +1 -1
  142. package/out/h3.d.ts +0 -5
  143. package/out/h3.d.ts.map +1 -1
  144. package/out/h3.js +2 -5
  145. package/out/h3.js.map +1 -1
  146. package/out/index.d.ts +3 -5
  147. package/out/index.d.ts.map +1 -1
  148. package/out/index.js +5 -5
  149. package/out/index.js.map +1 -1
  150. package/out/objects.d.ts +0 -5
  151. package/out/objects.d.ts.map +1 -1
  152. package/out/objects.js +2 -5
  153. package/out/objects.js.map +1 -1
  154. package/out/open-location-code.d.ts +30 -20
  155. package/out/open-location-code.d.ts.map +1 -1
  156. package/out/open-location-code.js +47 -32
  157. package/out/open-location-code.js.map +1 -1
  158. package/out/osgb36.d.ts +0 -5
  159. package/out/osgb36.d.ts.map +1 -1
  160. package/out/osgb36.js +2 -5
  161. package/out/osgb36.js.map +1 -1
  162. package/out/point/reading.d.ts +144 -0
  163. package/out/point/reading.d.ts.map +1 -0
  164. package/out/point/reading.js +167 -0
  165. package/out/point/reading.js.map +1 -0
  166. package/out/point/text.d.ts +117 -0
  167. package/out/point/text.d.ts.map +1 -0
  168. package/out/point/text.js +44 -0
  169. package/out/point/text.js.map +1 -0
  170. package/out/polyline.d.ts +1 -14
  171. package/out/polyline.d.ts.map +1 -1
  172. package/out/polyline.js +8 -10
  173. package/out/polyline.js.map +1 -1
  174. package/out/position.d.ts +3 -7
  175. package/out/position.d.ts.map +1 -1
  176. package/out/position.js +4 -8
  177. package/out/position.js.map +1 -1
  178. package/out/projection/transform.d.ts +33 -21
  179. package/out/projection/transform.d.ts.map +1 -1
  180. package/out/projection/transform.js +4 -2
  181. package/out/projection/transform.js.map +1 -1
  182. package/out/projection.d.ts +0 -5
  183. package/out/projection.d.ts.map +1 -1
  184. package/out/projection.js +2 -5
  185. package/out/projection.js.map +1 -1
  186. package/out/regions/codes.d.ts +0 -5
  187. package/out/regions/codes.d.ts.map +1 -1
  188. package/out/regions/codes.js +2 -5
  189. package/out/regions/codes.js.map +1 -1
  190. package/out/regions/names.d.ts +0 -5
  191. package/out/regions/names.d.ts.map +1 -1
  192. package/out/regions/names.js +2 -5
  193. package/out/regions/names.js.map +1 -1
  194. package/out/regions.d.ts +0 -5
  195. package/out/regions.d.ts.map +1 -1
  196. package/out/regions.js +2 -5
  197. package/out/regions.js.map +1 -1
  198. package/out/tools/ogr/stream.d.ts +0 -6
  199. package/out/tools/ogr/stream.d.ts.map +1 -1
  200. package/out/tools/ogr/stream.js +2 -3
  201. package/out/tools/ogr/stream.js.map +1 -1
  202. package/out/tools/ogr.d.ts +0 -14
  203. package/out/tools/ogr.d.ts.map +1 -1
  204. package/out/tools/ogr.js +10 -12
  205. package/out/tools/ogr.js.map +1 -1
  206. package/out/tools/tsconfig.tsbuildinfo +1 -1
  207. package/out/tsconfig.test.tsbuildinfo +1 -1
  208. package/out/well-known-text.d.ts +0 -7
  209. package/out/well-known-text.d.ts.map +1 -1
  210. package/out/well-known-text.js +2 -4
  211. package/out/well-known-text.js.map +1 -1
  212. package/package.json +7 -10
  213. package/tools/ogr/stream.ts +3 -3
  214. package/tools/ogr.ts +11 -12
@@ -1,8 +1,5 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  import type { GeometryLiteral } from "#geometries/collection"
8
5
  import type { LineStringPath } from "#geometries/line-string"
@@ -157,7 +154,7 @@ export function arealPolygons(geometry: ParsedGeometry | null): MultiPolygonRing
157
154
 
158
155
  /**
159
156
  * The polygons of a geometry that must be areal, throwing when the geometry
160
- * is not a `Polygon` or `MultiPolygon`.
157
+ * isn't a `Polygon` or `MultiPolygon`.
161
158
  */
162
159
  export function requireArealPolygons(geometry: ParsedGeometry, subject: string, context: string): MultiPolygonRings {
163
160
  const polygons = arealPolygons(geometry)
@@ -1,11 +1,10 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * A polygon layer's stored ring blob: an 8-byte header, then a `(u32 pointCount, u32 polygonIndex)`
7
- * pair per ring, then `[lon, lat]` pairs on an 8-byte boundary so the reader takes a
8
- * `Float64Array` view; `polygonIndex` is required because one feature's rings belong to several polygons.
6
+ * pair per ring, then `[lon, lat]` pairs on an 8-byte boundary so the reader takes a `Float64Array`
7
+ * view; `polygonIndex` is required because one feature's rings belong to several polygons.
9
8
  */
10
9
 
11
10
  import type { LatLonBounds } from "#bbox"
@@ -87,8 +86,8 @@ export function encodeRings(polygons: MultiPolygonRings): Uint8Array {
87
86
  }
88
87
 
89
88
  /**
90
- * The ring table plus a `Float64Array` over the coordinates, throwing when the blob's version is not
91
- * {@linkcode RING_BLOB_VERSION} or its declared ring table does not account for the bytes present.
89
+ * The ring table plus a `Float64Array` over the coordinates, throwing when the blob's version isn't
90
+ * {@linkcode RING_BLOB_VERSION} or its declared ring table doesn't account for the bytes present.
92
91
  */
93
92
  function openRings(blob: Uint8Array): {
94
93
  ringCount: number
@@ -96,7 +95,7 @@ function openRings(blob: Uint8Array): {
96
95
  polygonIndices: Uint32Array
97
96
  coordinates: Float64Array
98
97
  } {
99
- // `blob.byteOffset` is not always zero: node:sqlite hands back a view into a shared buffer.
98
+ // `blob.byteOffset` isn't always zero: node:sqlite hands back a view into a shared buffer.
100
99
  const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
101
100
  const version = view.getUint32(0, true)
102
101
 
@@ -235,7 +234,7 @@ export function ringSignedAreaM2(ring: ReadonlyArray<readonly number[]>): number
235
234
 
236
235
  /**
237
236
  * Both readings of one feature's area, in square meters: `nested` respects ring
238
- * orientation (holes subtract) and `allExterior` does not (holes add).
237
+ * orientation (holes subtract) and `allExterior` doesn't (holes add).
239
238
  */
240
239
  export function ringAreaReadings(polygons: MultiPolygonRings): {
241
240
  nested: number
@@ -0,0 +1,73 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * Rounds GeoJSON coordinates to a fixed number of decimal places.
6
+ *
7
+ * A coordinate rounded to `d` places moves by at most half of 10^-d degrees.
8
+ * At six places that bound is 0.0000005 degrees, or 0.056 m north–south.
9
+ */
10
+
11
+ import { isCoordPairLiteral } from "#position"
12
+
13
+ /**
14
+ * A coordinate rounded to `decimals` places.
15
+ * Negative zero becomes zero.
16
+ */
17
+ export function roundCoordinate(value: number, decimals: number): number {
18
+ const factor = 10 ** decimals
19
+ const rounded = Math.round(value * factor) / factor
20
+
21
+ return Object.is(rounded, -0) ? 0 : rounded
22
+ }
23
+
24
+ /**
25
+ * What {@link roundGeometryCoordinates} produced.
26
+ */
27
+ export interface RoundedCoordinates {
28
+ /**
29
+ * The coordinates array, with every position rounded and the nesting unchanged.
30
+ */
31
+ coordinates: unknown
32
+ /**
33
+ * Whether two neighboring positions of a ring or line became equal that differed before.
34
+ *
35
+ * The rounded geometry then holds a repeated vertex the source didn't.
36
+ */
37
+ collapsed: boolean
38
+ }
39
+
40
+ /**
41
+ * Round every position of a GeoJSON `coordinates` array, of any geometry type, to `decimals` places.
42
+ */
43
+ export function roundGeometryCoordinates(coordinates: unknown, decimals: number): RoundedCoordinates {
44
+ let collapsed = false
45
+
46
+ const walk = (value: unknown): unknown => {
47
+ if (isCoordPairLiteral(value)) return value.map((coordinate) => roundCoordinate(coordinate, decimals))
48
+
49
+ if (!Array.isArray(value)) return value
50
+
51
+ const items = value.map(walk)
52
+
53
+ // A sequence of positions is a ring or a line.
54
+ // Equal neighbors after rounding that differed before collapsed.
55
+ if (value.length > 1 && value.every(isCoordPairLiteral)) {
56
+ for (let i = 1; i < items.length; i++) {
57
+ const after = [items[i - 1] as number[], items[i] as number[]]
58
+ const before = [value[i - 1] as number[], value[i] as number[]]
59
+
60
+ const equalAfter = after[0]!.every((n, k) => n === after[1]![k])
61
+ const equalBefore = before[0]!.every((n, k) => n === before[1]![k])
62
+
63
+ if (equalAfter && !equalBefore) {
64
+ collapsed = true
65
+ }
66
+ }
67
+ }
68
+
69
+ return items
70
+ }
71
+
72
+ return { coordinates: walk(coordinates), collapsed }
73
+ }
@@ -0,0 +1,126 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * Simplifies a polygon or multipolygon by Douglas-Peucker over each ring,
6
+ * for a place boundary drawn on a map.
7
+ */
8
+
9
+ import { roundCoordinate } from "#geometries/round"
10
+
11
+ type Position = [number, number, ...number[]]
12
+
13
+ type Ring = Position[]
14
+
15
+ /**
16
+ * A polygon or multipolygon geometry, as GeoJSON holds it.
17
+ */
18
+ export type AreaGeometry = { type: "Polygon"; coordinates: Ring[] } | { type: "MultiPolygon"; coordinates: Ring[][] }
19
+
20
+ /**
21
+ * The fewest positions a closed ring can hold.
22
+ */
23
+ const MIN_RING_POSITIONS = 4
24
+
25
+ /**
26
+ * The positions a ring keeps when the tolerance collapses it.
27
+ */
28
+ const COLLAPSED_RING_POSITIONS = 8
29
+
30
+ const squaredDistanceToSegment = ([x, y]: Position, [ax, ay]: Position, [bx, by]: Position): number => {
31
+ const dx = bx - ax
32
+ const dy = by - ay
33
+ const length = dx * dx + dy * dy
34
+ const t = length === 0 ? 0 : Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / length))
35
+
36
+ return (x - ax - t * dx) ** 2 + (y - ay - t * dy) ** 2
37
+ }
38
+
39
+ /**
40
+ * Lists a ring's kept positions, with an explicit stack so a ring of a million
41
+ * positions cannot overflow the call stack.
42
+ */
43
+ function simplifyRing(ring: Ring, tolerance: number): Ring {
44
+ const keep = new Uint8Array(ring.length)
45
+ const last = ring.length - 1
46
+
47
+ // A closed ring's end points coincide, so the farthest position from the start also anchors the split.
48
+ let anchor = 0
49
+ let farthest = -1
50
+
51
+ for (let i = 1; i < last; i++) {
52
+ const distance = squaredDistanceToSegment(ring[i]!, ring[0]!, ring[0]!)
53
+
54
+ if (distance > farthest) {
55
+ farthest = distance
56
+ anchor = i
57
+ }
58
+ }
59
+
60
+ keep[0] = keep[last] = keep[anchor] = 1
61
+
62
+ const stack: Array<[number, number]> = [
63
+ [0, anchor],
64
+ [anchor, last],
65
+ ]
66
+
67
+ const squaredTolerance = tolerance * tolerance
68
+
69
+ while (stack.length) {
70
+ const [start, end] = stack.pop()!
71
+ let worst = -1
72
+ let worstDistance = squaredTolerance
73
+
74
+ for (let i = start + 1; i < end; i++) {
75
+ const distance = squaredDistanceToSegment(ring[i]!, ring[start]!, ring[end]!)
76
+
77
+ if (distance > worstDistance) {
78
+ worst = i
79
+ worstDistance = distance
80
+ }
81
+ }
82
+
83
+ if (worst !== -1) {
84
+ keep[worst] = 1
85
+ stack.push([start, worst], [worst, end])
86
+ }
87
+ }
88
+
89
+ return ring.filter((_, i) => keep[i] === 1)
90
+ }
91
+
92
+ /**
93
+ * Returns a closed ring of evenly spaced positions from a ring too small to simplify.
94
+ */
95
+ function sampleRing(ring: Ring): Ring {
96
+ if (ring.length <= COLLAPSED_RING_POSITIONS) return ring
97
+
98
+ const last = ring.length - 1
99
+ const steps = COLLAPSED_RING_POSITIONS - 1
100
+
101
+ return Array.from({ length: COLLAPSED_RING_POSITIONS }, (_, step) => ring[Math.round((step * last) / steps)]!)
102
+ }
103
+
104
+ /**
105
+ * Simplifies each ring and rounds its positions to two dimensions at the given precision.
106
+ *
107
+ * A polygon whose outer ring collapses below a closed ring keeps a few evenly spaced
108
+ * positions from that ring, so a small island still draws at a bounded size.
109
+ * An inner ring that collapses drops.
110
+ */
111
+ export function simplifyAreaGeometry(geometry: AreaGeometry, tolerance: number, decimals = 5): AreaGeometry {
112
+ const polygon = (rings: Ring[]): Ring[] =>
113
+ rings.flatMap((ring, index) => {
114
+ const simplified = ring.length > MIN_RING_POSITIONS ? simplifyRing(ring, tolerance) : ring
115
+
116
+ const kept = simplified.length >= MIN_RING_POSITIONS ? simplified : index === 0 ? sampleRing(ring) : null
117
+
118
+ return kept
119
+ ? [kept.map(([lon, lat]): Position => [roundCoordinate(lon, decimals), roundCoordinate(lat, decimals)])]
120
+ : []
121
+ })
122
+
123
+ return geometry.type === "Polygon"
124
+ ? { type: "Polygon", coordinates: polygon(geometry.coordinates) }
125
+ : { type: "MultiPolygon", coordinates: geometry.coordinates.map(polygon) }
126
+ }
package/lib/geometries.ts CHANGED
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * GeoJSON Geometry Entry Point
7
6
  */
8
7
 
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * Google Place ID utilities.
7
6
  */
8
7
 
package/lib/google.ts CHANGED
@@ -1,7 +1,4 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  export * from "#google/place-id"
package/lib/h3/cell.ts CHANGED
@@ -1,7 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
5
  * @file H3 cell primitives — the index types and the widen/shorten conversions.
6
6
 
7
7
  */
@@ -52,7 +52,7 @@ export function shortenH3Cell(cell: H3Cell): H3CellShort {
52
52
 
53
53
  /**
54
54
  * Rebuild a full H3 cell index from a short cell captured at `resolution`, throwing
55
- * when the resolution is out of range or the pair does not name a valid cell.
55
+ * when the resolution is out of range or the pair doesn't name a valid cell.
56
56
  */
57
57
  export function expandH3Cell(h3CellShort: H3CellShort, resolution = H3_MAX_RESOLUTION): H3Cell {
58
58
  if (!Number.isInteger(resolution) || resolution < 0 || resolution > H3_MAX_RESOLUTION) {
@@ -114,7 +114,7 @@ export function recoverShortCellResolution(cells: readonly number[], context = "
114
114
  expandH3Cell(short, resolution)
115
115
  valid.push(resolution)
116
116
  } catch {
117
- // A resolution the short cell does not expand at — every cell expands at exactly one.
117
+ // A resolution the short cell doesn't expand at — every cell expands at exactly one.
118
118
  }
119
119
  }
120
120
 
@@ -1,14 +1,13 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * The coverage probe every polygon layer's reader repeats: an index cell's parent at the coverage
7
- * resolution, read through the layer interface's shared row mapping.
5
+ * The coverage probe every polygon layer's reader repeats: an index cell's parent at
6
+ * the coverage resolution, read through the layer interface's shared row mapping.
8
7
  *
9
- * Not exported from this package's main barrel on purpose — it reaches `@mailwoman/core/layers`, and the
10
- * barrel serves browser bundles that must not include the layer interface's Kysely graph. Import it from the
11
- * `@mailwoman/spatial/h3/coverage` subpath.
8
+ * Not exported from this package's main barrel on purpose — it reaches `@mailwoman/core/layers`,
9
+ * and the barrel serves browser bundles that mustn't include the layer interface's Kysely graph.
10
+ * Import it from the `@mailwoman/spatial/h3/coverage` subpath.
12
11
  */
13
12
 
14
13
  import { toCoverageCell, type CoverageCell, type CoverageRow } from "@mailwoman/core/layers"
@@ -1,10 +1,9 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * Turn an outline into the H3 cell set a coverage claim may be keyed to, conservatively: a cell
7
- * must have its whole `gridDisk(cell, 1)` and all six boundary vertices inside the outline.
5
+ * Turn an outline into the H3 cell set a coverage claim may be keyed to, conservatively:
6
+ * a cell must have its whole `gridDisk(cell, 1)` and all six boundary vertices inside the outline.
8
7
  */
9
8
 
10
9
  import { stringifyJSON } from "@mailwoman/core/json"
@@ -1,10 +1,9 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * Turn one authority polygon into the H3 cells that summarize it, done once at build time so the
7
- * runtime probes structure instead of geometry.
5
+ * Turn one authority polygon into the H3 cells that summarize it, done once at build time
6
+ * so the runtime probes structure instead of geometry.
8
7
  */
9
8
 
10
9
  import {
@@ -90,7 +89,7 @@ function canContainCell(box: LatLonBounds, resolution: number): boolean {
90
89
  export interface FeatureCells {
91
90
  /**
92
91
  * The resolution this feature was indexed at — the target, or coarser
93
- * where the target's estimate did not fit.
92
+ * where the target's estimate didn't fit.
94
93
  */
95
94
  resolution: number
96
95
  /**
@@ -98,7 +97,7 @@ export interface FeatureCells {
98
97
  */
99
98
  whole: H3Cell[]
100
99
  /**
101
- * Cells the feature reaches but does not fill, so a point in one needs the ray cast.
100
+ * Cells the feature reaches but doesn't fill, so a point in one needs the ray cast.
102
101
  */
103
102
  partial: H3Cell[]
104
103
  }
@@ -321,8 +320,8 @@ export function featureCellRows(cells: FeatureCells): Array<{
321
320
  }
322
321
 
323
322
  /**
324
- * The coverage cell a row at `cell` belongs to, derived with `cellToParent` rather than a
325
- * fresh `latLngToCell` so a fringe row does not land in a neighboring coverage cell.
323
+ * The coverage cell a row at `cell` belongs to, derived with `cellToParent` rather than
324
+ * a fresh `latLngToCell` so a fringe row doesn't land in a neighboring coverage cell.
326
325
  */
327
326
  export function coverageCellFor(cell: H3Cell, coverageResolution: number): H3Cell {
328
327
  return cellToParent(cell, coverageResolution)
package/lib/h3.ts CHANGED
@@ -1,8 +1,5 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  import { cellToLatLng, type H3Cell } from "@sister.software/h3"
8
5
 
package/lib/index.ts CHANGED
@@ -1,12 +1,10 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  export * from "#bbox"
8
5
  export * from "#coordinate/formats"
9
6
  export * from "#coordinate/bounds"
7
+ export * from "#coordinate/cell"
10
8
  export * from "#distance"
11
9
  export * from "#feature"
12
10
  export * from "#open-location-code"
@@ -15,6 +13,8 @@ export * from "#google"
15
13
  export * from "#h3"
16
14
  export * from "#objects"
17
15
  export * from "#osgb36"
16
+ export * from "#point/reading"
17
+ export * from "#point/text"
18
18
  export * from "#polyline"
19
19
  export * from "#position"
20
20
  export { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
package/lib/objects.ts CHANGED
@@ -1,8 +1,5 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  import { type BBox2DLiteral, type BBox3DLiteral, is2DBBox } from "#bbox"
8
5
 
@@ -1,11 +1,27 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
+ * @file Open Location Code arithmetic.
6
+ *
7
+ * A code is read by the `spatial` family of `@mailwoman/recognizer`, which owns the alphabet, the separator
8
+ * and the digit counts. The decoders here take the validated, branded `Validated.FullPlusCode` and `Validated.ShortPlusCode` the family returns,
9
+ * so a caller cannot pass an unchecked string. A test or a tool that holds a string reads it through the recognizer.
5
10
  */
6
11
 
12
+ import type { Validated } from "@mailwoman/recognizer/types"
13
+
14
+ import type { DegreeCell } from "#coordinate/cell"
15
+
7
16
  const OLC_ALPHABET = "23456789CFGHJMPQRVWX"
8
17
 
18
+ /**
19
+ * The digits before the separator of a full code.
20
+ *
21
+ * The validator reads eight as a full code and two, four or six as a short one.
22
+ */
23
+ const FULL_PREFIX_LENGTH = 8
24
+
9
25
  const OLC_DIGIT_VALUE = new Map<string, number>([...OLC_ALPHABET].map((c, i) => [c, i]))
10
26
 
11
27
  const PAIR_RESOLUTIONS = [20, 1, 1 / 20, 1 / 400, 1 / 8000] as const
@@ -14,40 +30,26 @@ const GRID_COLUMNS = 4
14
30
  const GRID_ROWS = 5
15
31
 
16
32
  /**
17
- * Describes a decoded plus-code cell by its center and its span in degrees, from
33
+ * A decoded plus-code cell by its center and its span in degrees, from
18
34
  * which callers derive an uncertainty radius.
19
35
  */
20
- export interface DecodedPlusCode {
21
- lat: number
22
- lon: number
23
- latSpanDeg: number
24
- lonSpanDeg: number
25
- }
36
+ export type DecodedPlusCode = DegreeCell
26
37
 
27
38
  /**
28
- * Returns true for a full plus code of exactly 8 digits, `+`, and 2 or 3 digits.
39
+ * Decodes a full code to its cell.
29
40
  *
30
- * It rejects the padded and longer forms the spec allows, since addresses use only the 10–11 digit form.
41
+ * The validator guarantees the alphabet and the digit counts, and the brand guarantees
42
+ * the eight digits before the separator, so no input to this function fails.
43
+ * A short code has its own brand and decodes through {@link recoverNearestPlusCode}.
31
44
  */
32
- export function isFullPlusCode(token: string): boolean {
33
- return /^[23456789CFGHJMPQRVWX]{8}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token)
45
+ export function decodePlusCode(code: Validated.FullPlusCode): DecodedPlusCode {
46
+ return decodeDigits(code.replace("+", ""))
34
47
  }
35
48
 
36
49
  /**
37
- * Returns true for a short plus code with 2, 4 or 6 digits before the `+`,
38
- * such as the `VFQ6+92P` form Google prints on place cards.
50
+ * Decodes the digits of a full code, 10 or 11 of them, without the separator.
39
51
  */
40
- export function isShortPlusCode(token: string): boolean {
41
- return /^[23456789CFGHJMPQRVWX]{2,6}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token) && token.indexOf("+") % 2 === 0
42
- }
43
-
44
- /**
45
- * Decodes a full plus code to its cell, returning null for anything {@link isFullPlusCode} rejects.
46
- */
47
- export function decodePlusCode(code: string): DecodedPlusCode | null {
48
- if (!isFullPlusCode(code)) return null
49
- const digits = code.toUpperCase().replace("+", "")
50
-
52
+ function decodeDigits(digits: string): DecodedPlusCode {
51
53
  let latLo = -90
52
54
  let lonLo = -180
53
55
  let latSpan = 400
@@ -81,40 +83,84 @@ export function decodePlusCode(code: string): DecodedPlusCode | null {
81
83
  }
82
84
  }
83
85
 
84
- function encodePairDigits(lat: number, lon: number, length: number): string {
86
+ /**
87
+ * The pair digits of a code and the position's remainder inside the last pair's cell, in degrees.
88
+ */
89
+ interface PairEncoding {
90
+ digits: string
91
+ latRemainder: number
92
+ lonRemainder: number
93
+ }
94
+
95
+ function encodePairs(lat: number, lon: number, length: number): PairEncoding {
85
96
  let latVal = Math.min(Math.max(lat + 90, 0), 180 - 1e-12)
86
97
  let lonVal = lon + 180
87
98
 
88
99
  lonVal -= Math.floor(lonVal / 360) * 360
89
100
 
90
- let out = ""
101
+ let digits = ""
91
102
 
92
103
  for (let i = 0; i < length / 2; i++) {
93
104
  const resolution = PAIR_RESOLUTIONS[i]!
94
105
  const latDigit = Math.min(Math.floor(latVal / resolution), 19)
95
106
  const lonDigit = Math.min(Math.floor(lonVal / resolution), 19)
96
107
 
97
- out += OLC_ALPHABET[latDigit]! + OLC_ALPHABET[lonDigit]!
108
+ digits += OLC_ALPHABET[latDigit]! + OLC_ALPHABET[lonDigit]!
98
109
  latVal -= latDigit * resolution
99
110
  lonVal -= lonDigit * resolution
100
111
  }
101
112
 
102
- return out
113
+ return { digits, latRemainder: latVal, lonRemainder: lonVal }
114
+ }
115
+
116
+ function encodePairDigits(lat: number, lon: number, length: number): string {
117
+ return encodePairs(lat, lon, length).digits
118
+ }
119
+
120
+ /**
121
+ * The code lengths this encoder writes: the ten-digit code Google prints,
122
+ * or the eleven-digit code with one grid refinement digit.
123
+ */
124
+ export type PlusCodeLength = 10 | 11
125
+
126
+ /**
127
+ * Encodes a coordinate as a full plus code of 10 or 11 digits, the forms an address holds.
128
+ *
129
+ * The eleventh digit refines the last pair's cell on the 4 × 5 grid the spec defines.
130
+ * The digits come from the specification's alphabet and the separator follows the eighth,
131
+ * so the code has the form the validator accepts and the decoders take.
132
+ */
133
+ export function encodePlusCode(lat: number, lon: number, length: PlusCodeLength = 10): Validated.FullPlusCode {
134
+ const { digits, latRemainder, lonRemainder } = encodePairs(lat, lon, 10)
135
+ const code = `${digits.slice(0, FULL_PREFIX_LENGTH)}+${digits.slice(FULL_PREFIX_LENGTH)}`
136
+
137
+ if (length === 10) return code as Validated.FullPlusCode
138
+
139
+ const cell = PAIR_RESOLUTIONS[4]!
140
+ const row = Math.min(Math.floor(latRemainder / (cell / GRID_ROWS)), GRID_ROWS - 1)
141
+ const column = Math.min(Math.floor(lonRemainder / (cell / GRID_COLUMNS)), GRID_COLUMNS - 1)
142
+
143
+ return (code + OLC_ALPHABET[row * GRID_COLUMNS + column]!) as Validated.FullPlusCode
103
144
  }
104
145
 
105
146
  /**
106
147
  * Recovers a short plus code to the matching cell nearest a reference coordinate,
107
148
  * following the spec's `recoverNearest`.
108
- * It returns null for an invalid short code.
149
+ *
150
+ * The brand guarantees two, four or six digits before the separator, so every short code recovers to a cell.
151
+ * The reference must be a finite coordinate.
152
+ *
153
+ * Every caller already holds one.
154
+ * A full code has its own brand and decodes through {@link decodePlusCode}.
109
155
  */
110
- export function recoverNearestPlusCode(shortCode: string, refLat: number, refLon: number): DecodedPlusCode | null {
111
- if (!isShortPlusCode(shortCode)) return null
112
- const upper = shortCode.toUpperCase()
113
- const missing = 8 - upper.indexOf("+")
156
+ export function recoverNearestPlusCode(
157
+ shortCode: Validated.ShortPlusCode,
158
+ refLat: number,
159
+ refLon: number
160
+ ): DecodedPlusCode {
161
+ const missing = FULL_PREFIX_LENGTH - shortCode.indexOf("+")
114
162
  const prefix = encodePairDigits(refLat, refLon, missing)
115
- const candidate = decodePlusCode(prefix + upper)
116
-
117
- if (!candidate) return null
163
+ const candidate = decodeDigits(prefix + shortCode.replace("+", ""))
118
164
 
119
165
  const LAT_MIN = -90
120
166
  const LAT_MAX = 90
package/lib/osgb36.ts CHANGED
@@ -1,8 +1,5 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  import type { Coordinates2D, GeoCoordinate } from "#position"
8
5