@mailwoman/spatial 11.0.0 → 11.1.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 (204) hide show
  1. package/README.md +1 -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/distance.ts +5 -5
  7. package/lib/feature.ts +2 -5
  8. package/lib/gb/codepoint.ts +6 -7
  9. package/lib/gb/os-downloads.ts +6 -6
  10. package/lib/geometries/collection.ts +3 -4
  11. package/lib/geometries/line-string.ts +3 -4
  12. package/lib/geometries/point/coordinates.ts +8 -8
  13. package/lib/geometries/point/distance.ts +4 -4
  14. package/lib/geometries/point.ts +10 -17
  15. package/lib/geometries/polygon.ts +3 -6
  16. package/lib/geometries/ring-blob.ts +9 -10
  17. package/lib/geometries/round.ts +73 -0
  18. package/lib/geometries/simplify.ts +126 -0
  19. package/lib/geometries.ts +3 -4
  20. package/lib/google/place-id.ts +3 -4
  21. package/lib/google.ts +2 -5
  22. package/lib/h3/cell.ts +5 -5
  23. package/lib/h3/coverage.ts +8 -9
  24. package/lib/h3/interior-cells.ts +5 -6
  25. package/lib/h3/polygon-cells.ts +9 -10
  26. package/lib/h3.ts +2 -5
  27. package/lib/index.ts +4 -5
  28. package/lib/objects.ts +2 -5
  29. package/lib/open-location-code.ts +47 -16
  30. package/lib/osgb36.ts +2 -5
  31. package/lib/point-query.ts +294 -0
  32. package/lib/polyline.ts +9 -10
  33. package/lib/position.ts +8 -7
  34. package/lib/projection/transform.ts +36 -21
  35. package/lib/projection.ts +2 -5
  36. package/lib/regions/codes.ts +2 -5
  37. package/lib/regions/names.ts +2 -5
  38. package/lib/regions.ts +2 -5
  39. package/lib/well-known-text.ts +3 -4
  40. package/out/bbox.d.ts +0 -7
  41. package/out/bbox.d.ts.map +1 -1
  42. package/out/bbox.js +2 -4
  43. package/out/bbox.js.map +1 -1
  44. package/out/coordinate/bounds.d.ts +0 -4
  45. package/out/coordinate/bounds.d.ts.map +1 -1
  46. package/out/coordinate/bounds.js +2 -4
  47. package/out/coordinate/bounds.js.map +1 -1
  48. package/out/coordinate/cell.d.ts +23 -0
  49. package/out/coordinate/cell.d.ts.map +1 -0
  50. package/out/coordinate/cell.js +24 -0
  51. package/out/coordinate/cell.js.map +1 -0
  52. package/out/coordinate/formats.d.ts +12 -5
  53. package/out/coordinate/formats.d.ts.map +1 -1
  54. package/out/coordinate/formats.js +50 -5
  55. package/out/coordinate/formats.js.map +1 -1
  56. package/out/distance.d.ts +1 -11
  57. package/out/distance.d.ts.map +1 -1
  58. package/out/distance.js +4 -5
  59. package/out/distance.js.map +1 -1
  60. package/out/feature.d.ts +0 -5
  61. package/out/feature.d.ts.map +1 -1
  62. package/out/feature.js +2 -5
  63. package/out/feature.js.map +1 -1
  64. package/out/gb/codepoint.d.ts +1 -9
  65. package/out/gb/codepoint.d.ts.map +1 -1
  66. package/out/gb/codepoint.js +5 -7
  67. package/out/gb/codepoint.js.map +1 -1
  68. package/out/gb/os-downloads.d.ts +3 -6
  69. package/out/gb/os-downloads.d.ts.map +1 -1
  70. package/out/gb/os-downloads.js +5 -6
  71. package/out/gb/os-downloads.js.map +1 -1
  72. package/out/geometries/collection.d.ts +0 -4
  73. package/out/geometries/collection.d.ts.map +1 -1
  74. package/out/geometries/collection.js +2 -7
  75. package/out/geometries/collection.js.map +1 -1
  76. package/out/geometries/line-string.d.ts +0 -4
  77. package/out/geometries/line-string.d.ts.map +1 -1
  78. package/out/geometries/line-string.js +2 -7
  79. package/out/geometries/line-string.js.map +1 -1
  80. package/out/geometries/point/coordinates.d.ts +2 -12
  81. package/out/geometries/point/coordinates.d.ts.map +1 -1
  82. package/out/geometries/point/coordinates.js +5 -5
  83. package/out/geometries/point/coordinates.js.map +1 -1
  84. package/out/geometries/point/distance.d.ts +1 -4
  85. package/out/geometries/point/distance.d.ts.map +1 -1
  86. package/out/geometries/point/distance.js +3 -4
  87. package/out/geometries/point/distance.js.map +1 -1
  88. package/out/geometries/point.d.ts +6 -17
  89. package/out/geometries/point.d.ts.map +1 -1
  90. package/out/geometries/point.js +6 -8
  91. package/out/geometries/point.js.map +1 -1
  92. package/out/geometries/polygon.d.ts +1 -6
  93. package/out/geometries/polygon.d.ts.map +1 -1
  94. package/out/geometries/polygon.js +3 -6
  95. package/out/geometries/polygon.js.map +1 -1
  96. package/out/geometries/ring-blob.d.ts +3 -7
  97. package/out/geometries/ring-blob.d.ts.map +1 -1
  98. package/out/geometries/ring-blob.js +6 -13
  99. package/out/geometries/ring-blob.js.map +1 -1
  100. package/out/geometries/round.d.ts +25 -0
  101. package/out/geometries/round.d.ts.map +1 -0
  102. package/out/geometries/round.js +47 -0
  103. package/out/geometries/round.js.map +1 -0
  104. package/out/geometries/simplify.d.ts +22 -0
  105. package/out/geometries/simplify.d.ts.map +1 -0
  106. package/out/geometries/simplify.js +93 -0
  107. package/out/geometries/simplify.js.map +1 -0
  108. package/out/geometries.d.ts +0 -4
  109. package/out/geometries.d.ts.map +1 -1
  110. package/out/geometries.js +2 -4
  111. package/out/geometries.js.map +1 -1
  112. package/out/google/place-id.d.ts +0 -4
  113. package/out/google/place-id.d.ts.map +1 -1
  114. package/out/google/place-id.js +2 -7
  115. package/out/google/place-id.js.map +1 -1
  116. package/out/google.d.ts +0 -5
  117. package/out/google.d.ts.map +1 -1
  118. package/out/google.js +2 -5
  119. package/out/google.js.map +1 -1
  120. package/out/h3/cell.d.ts +1 -4
  121. package/out/h3/cell.d.ts.map +1 -1
  122. package/out/h3/cell.js +4 -5
  123. package/out/h3/cell.js.map +1 -1
  124. package/out/h3/coverage.d.ts +5 -9
  125. package/out/h3/coverage.d.ts.map +1 -1
  126. package/out/h3/coverage.js +7 -9
  127. package/out/h3/coverage.js.map +1 -1
  128. package/out/h3/interior-cells.d.ts +0 -8
  129. package/out/h3/interior-cells.d.ts.map +1 -1
  130. package/out/h3/interior-cells.js +4 -6
  131. package/out/h3/interior-cells.js.map +1 -1
  132. package/out/h3/polygon-cells.d.ts +6 -10
  133. package/out/h3/polygon-cells.d.ts.map +1 -1
  134. package/out/h3/polygon-cells.js +6 -8
  135. package/out/h3/polygon-cells.js.map +1 -1
  136. package/out/h3.d.ts +0 -5
  137. package/out/h3.d.ts.map +1 -1
  138. package/out/h3.js +2 -5
  139. package/out/h3.js.map +1 -1
  140. package/out/index.d.ts +2 -5
  141. package/out/index.d.ts.map +1 -1
  142. package/out/index.js +4 -5
  143. package/out/index.js.map +1 -1
  144. package/out/objects.d.ts +0 -5
  145. package/out/objects.d.ts.map +1 -1
  146. package/out/objects.js +2 -5
  147. package/out/objects.js.map +1 -1
  148. package/out/open-location-code.d.ts +14 -12
  149. package/out/open-location-code.d.ts.map +1 -1
  150. package/out/open-location-code.js +24 -9
  151. package/out/open-location-code.js.map +1 -1
  152. package/out/osgb36.d.ts +0 -5
  153. package/out/osgb36.d.ts.map +1 -1
  154. package/out/osgb36.js +2 -5
  155. package/out/osgb36.js.map +1 -1
  156. package/out/point-query.d.ts +71 -0
  157. package/out/point-query.d.ts.map +1 -0
  158. package/out/point-query.js +194 -0
  159. package/out/point-query.js.map +1 -0
  160. package/out/polyline.d.ts +1 -14
  161. package/out/polyline.d.ts.map +1 -1
  162. package/out/polyline.js +8 -10
  163. package/out/polyline.js.map +1 -1
  164. package/out/position.d.ts +3 -7
  165. package/out/position.d.ts.map +1 -1
  166. package/out/position.js +4 -8
  167. package/out/position.js.map +1 -1
  168. package/out/projection/transform.d.ts +33 -21
  169. package/out/projection/transform.d.ts.map +1 -1
  170. package/out/projection/transform.js +4 -2
  171. package/out/projection/transform.js.map +1 -1
  172. package/out/projection.d.ts +0 -5
  173. package/out/projection.d.ts.map +1 -1
  174. package/out/projection.js +2 -5
  175. package/out/projection.js.map +1 -1
  176. package/out/regions/codes.d.ts +0 -5
  177. package/out/regions/codes.d.ts.map +1 -1
  178. package/out/regions/codes.js +2 -5
  179. package/out/regions/codes.js.map +1 -1
  180. package/out/regions/names.d.ts +0 -5
  181. package/out/regions/names.d.ts.map +1 -1
  182. package/out/regions/names.js +2 -5
  183. package/out/regions/names.js.map +1 -1
  184. package/out/regions.d.ts +0 -5
  185. package/out/regions.d.ts.map +1 -1
  186. package/out/regions.js +2 -5
  187. package/out/regions.js.map +1 -1
  188. package/out/tools/ogr/stream.d.ts +0 -6
  189. package/out/tools/ogr/stream.d.ts.map +1 -1
  190. package/out/tools/ogr/stream.js +2 -3
  191. package/out/tools/ogr/stream.js.map +1 -1
  192. package/out/tools/ogr.d.ts +0 -14
  193. package/out/tools/ogr.d.ts.map +1 -1
  194. package/out/tools/ogr.js +10 -12
  195. package/out/tools/ogr.js.map +1 -1
  196. package/out/tools/tsconfig.tsbuildinfo +1 -1
  197. package/out/tsconfig.test.tsbuildinfo +1 -1
  198. package/out/well-known-text.d.ts +0 -7
  199. package/out/well-known-text.d.ts.map +1 -1
  200. package/out/well-known-text.js +2 -4
  201. package/out/well-known-text.js.map +1 -1
  202. package/package.json +5 -9
  203. package/tools/ogr/stream.ts +3 -3
  204. package/tools/ogr.ts +11 -12
@@ -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,7 @@ export * from "#google"
15
13
  export * from "#h3"
16
14
  export * from "#objects"
17
15
  export * from "#osgb36"
16
+ export * from "#point-query"
18
17
  export * from "#polyline"
19
18
  export * from "#position"
20
19
  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,8 +1,7 @@
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
3
+
4
+ import type { DegreeCell } from "#coordinate/cell"
6
5
 
7
6
  const OLC_ALPHABET = "23456789CFGHJMPQRVWX"
8
7
 
@@ -14,15 +13,10 @@ const GRID_COLUMNS = 4
14
13
  const GRID_ROWS = 5
15
14
 
16
15
  /**
17
- * Describes a decoded plus-code cell by its center and its span in degrees, from
16
+ * A decoded plus-code cell by its center and its span in degrees, from
18
17
  * which callers derive an uncertainty radius.
19
18
  */
20
- export interface DecodedPlusCode {
21
- lat: number
22
- lon: number
23
- latSpanDeg: number
24
- lonSpanDeg: number
25
- }
19
+ export type DecodedPlusCode = DegreeCell
26
20
 
27
21
  /**
28
22
  * Returns true for a full plus code of exactly 8 digits, `+`, and 2 or 3 digits.
@@ -81,25 +75,62 @@ export function decodePlusCode(code: string): DecodedPlusCode | null {
81
75
  }
82
76
  }
83
77
 
84
- function encodePairDigits(lat: number, lon: number, length: number): string {
78
+ /**
79
+ * The pair digits of a code and the position's remainder inside the last pair's cell, in degrees.
80
+ */
81
+ interface PairEncoding {
82
+ digits: string
83
+ latRemainder: number
84
+ lonRemainder: number
85
+ }
86
+
87
+ function encodePairs(lat: number, lon: number, length: number): PairEncoding {
85
88
  let latVal = Math.min(Math.max(lat + 90, 0), 180 - 1e-12)
86
89
  let lonVal = lon + 180
87
90
 
88
91
  lonVal -= Math.floor(lonVal / 360) * 360
89
92
 
90
- let out = ""
93
+ let digits = ""
91
94
 
92
95
  for (let i = 0; i < length / 2; i++) {
93
96
  const resolution = PAIR_RESOLUTIONS[i]!
94
97
  const latDigit = Math.min(Math.floor(latVal / resolution), 19)
95
98
  const lonDigit = Math.min(Math.floor(lonVal / resolution), 19)
96
99
 
97
- out += OLC_ALPHABET[latDigit]! + OLC_ALPHABET[lonDigit]!
100
+ digits += OLC_ALPHABET[latDigit]! + OLC_ALPHABET[lonDigit]!
98
101
  latVal -= latDigit * resolution
99
102
  lonVal -= lonDigit * resolution
100
103
  }
101
104
 
102
- return out
105
+ return { digits, latRemainder: latVal, lonRemainder: lonVal }
106
+ }
107
+
108
+ function encodePairDigits(lat: number, lon: number, length: number): string {
109
+ return encodePairs(lat, lon, length).digits
110
+ }
111
+
112
+ /**
113
+ * The code lengths this encoder writes: the ten-digit code Google prints,
114
+ * or the eleven-digit code with one grid refinement digit.
115
+ */
116
+ export type PlusCodeLength = 10 | 11
117
+
118
+ /**
119
+ * Encodes a coordinate as a full plus code of 10 or 11 digits, the forms {@link isFullPlusCode} accepts.
120
+ *
121
+ * The eleventh digit refines the last pair's cell on the 4 × 5 grid the spec defines.
122
+ */
123
+ export function encodePlusCode(lat: number, lon: number, length: PlusCodeLength = 10): string {
124
+ const { digits, latRemainder, lonRemainder } = encodePairs(lat, lon, 10)
125
+ const code = `${digits.slice(0, 8)}+${digits.slice(8)}`
126
+
127
+ if (length === 10) return code
128
+
129
+ const cell = PAIR_RESOLUTIONS[4]!
130
+ const row = Math.min(Math.floor(latRemainder / (cell / GRID_ROWS)), GRID_ROWS - 1)
131
+ const column = Math.min(Math.floor(lonRemainder / (cell / GRID_COLUMNS)), GRID_COLUMNS - 1)
132
+
133
+ return code + OLC_ALPHABET[row * GRID_COLUMNS + column]!
103
134
  }
104
135
 
105
136
  /**
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
 
@@ -0,0 +1,294 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file The shapes a typed query takes when it is a point rather than an address, and the one parser for them.
6
+ *
7
+ * A search field receives coordinates in a dozen spellings, a plus code, an H3 cell, a geohash or a
8
+ * Who's On First id beside ordinary addresses. {@link parsePointQuery} reads the whole text as one of those shapes or
9
+ * answers `null`, so a caller sends only an address to the classifier. It reads text and decodes no cell: decoding a cell
10
+ * to a coordinate belongs to the module that owns the format, and the H3 reader loads a WebAssembly binary
11
+ * that a text test mustn't pay for.
12
+ */
13
+
14
+ import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
15
+ import { isFullPlusCode, isShortPlusCode } from "#open-location-code"
16
+
17
+ /**
18
+ * The axis order a coordinate pair was read in.
19
+ *
20
+ * `lat_lon` is the order people write and the order inside a Google Maps link.
21
+ * `lon_lat` is reported when the first number is out of latitude range, so it can only be a longitude.
22
+ */
23
+ export type CoordinateOrder = "lat_lon" | "lon_lat"
24
+
25
+ export type PointQuery =
26
+ | {
27
+ kind: "coordinates"
28
+ lat: number
29
+ lon: number
30
+ order: CoordinateOrder
31
+ }
32
+ | {
33
+ /**
34
+ * An Open Location Code, upper-cased.
35
+ *
36
+ * A short code (`VFQ6+92P`) decodes only beside a reference position.
37
+ * The caller supplies that position.
38
+ */
39
+ kind: "plus_code"
40
+ code: string
41
+ full: boolean
42
+ }
43
+ | {
44
+ /**
45
+ * An H3 cell index in its hexadecimal form, lower-cased.
46
+ *
47
+ * Only the shape is checked here: the H3 library decides validity when it decodes the cell.
48
+ */
49
+ kind: "h3"
50
+ cell: string
51
+ }
52
+ | {
53
+ /**
54
+ * A geohash, lower-cased.
55
+ *
56
+ * It is accepted only behind a `geohash:` or `gh:` prefix, because a bare hash is also a word.
57
+ */
58
+ kind: "geohash"
59
+ hash: string
60
+ }
61
+ | {
62
+ /**
63
+ * A Who's On First id, from `wof:85633793`, `wof 85633793` or a Spelunker link.
64
+ */
65
+ kind: "wof"
66
+ id: number
67
+ }
68
+
69
+ /**
70
+ * `wof:85633793`, `wof 85633793`, `whosonfirst:85633793`.
71
+ */
72
+ const WOF_REFERENCE = /^(?:wof|whosonfirst)[\s:]+(\d{1,16})$/iu
73
+
74
+ /**
75
+ * A Spelunker place page, with or without the trailing slash.
76
+ */
77
+ const SPELUNKER_URL = /^https?:\/\/spelunker\.whosonfirst\.org\/id\/(\d{1,16})\/?$/iu
78
+
79
+ /**
80
+ * The hexadecimal form of an H3 cell: mode 1 in the high nibble, then fourteen more digits.
81
+ */
82
+ const H3_CELL = /^(?:h3:)?(8[0-9a-f]{14})$/iu
83
+
84
+ /**
85
+ * A geohash behind its prefix, in the base-32 alphabet that omits `a`, `i`, `l` and `o`.
86
+ */
87
+ const GEOHASH_REFERENCE = /^(?:geohash|gh):\s*([0-9b-hjkmnp-z]{1,12})$/iu
88
+
89
+ /**
90
+ * A `geo:` URI, `geo:48.8584,2.2945` with an optional altitude and parameters.
91
+ */
92
+ const GEO_URI = /^geo:(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)(?:,-?\d+(?:\.\d+)?)?(?:;.*)?$/iu
93
+
94
+ /**
95
+ * Google Maps' path segment, `@48.8584,2.2945,17z`, with the zoom optional.
96
+ */
97
+ const MAPS_SEGMENT = /^@(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)(?:,[\d.]+z)?$/iu
98
+
99
+ /**
100
+ * A labeled pair in either order: `lat 48.85 lon 2.29`, `lng=2.29, lat=48.85`.
101
+ */
102
+ const LABELED_PAIR =
103
+ /^(lat(?:itude)?|lon(?:gitude)?|lng)\s*[:=]?\s*(-?\d+(?:\.\d+)?)\s*[,;\s]\s*(lat(?:itude)?|lon(?:gitude)?|lng)\s*[:=]?\s*(-?\d+(?:\.\d+)?)$/iu
104
+
105
+ /**
106
+ * One angle: a signed decimal, or degrees with optional minutes and seconds,
107
+ * each with its mark, and an optional hemisphere letter after it.
108
+ */
109
+ const ANGLE =
110
+ /^([+-])?\s*(\d+(?:\.\d+)?)\s*(?:°|º|d\b)?\s*(?:(\d+(?:\.\d+)?)\s*(?:'|′|’|m\b)\s*)?(?:(\d+(?:\.\d+)?)\s*(?:"|″|”|''|s\b)\s*)?([NSEW])?$/iu
111
+
112
+ /**
113
+ * A pair of angles is split at the comma or semicolon, or after the first hemisphere letter,
114
+ * or between two decimal numbers at the whitespace between them.
115
+ */
116
+ const PAIR_AT_HEMISPHERE = /^(.+?[NSEW])\s+(.+)$/iu
117
+ const PAIR_AT_SPACE = /^(\S+)\s+(\S+)$/u
118
+
119
+ interface Angle {
120
+ degrees: number
121
+ hemisphere: "N" | "S" | "E" | "W" | null
122
+ }
123
+
124
+ function parseAngle(text: string): Angle | null {
125
+ const match = ANGLE.exec(text.trim())
126
+
127
+ if (!match) return null
128
+
129
+ const [, sign, degrees, minutes = "0", seconds = "0", letter] = match
130
+ const magnitude = Number(degrees) + Number(minutes) / 60 + Number(seconds) / 3600
131
+
132
+ if (!Number.isFinite(magnitude)) return null
133
+
134
+ const hemisphere = letter ? (letter.toUpperCase() as Angle["hemisphere"]) : null
135
+ const negative = sign === "-" || hemisphere === "S" || hemisphere === "W"
136
+
137
+ return { degrees: negative ? -magnitude : magnitude, hemisphere }
138
+ }
139
+
140
+ /**
141
+ * A pair of angles as the point they name, or `null` when the pair is out of range in both orders.
142
+ *
143
+ * Hemisphere letters decide the axis.
144
+ * Without them the first number is the latitude, as in a Google Maps link,
145
+ * unless only the other order is in range.
146
+ */
147
+ function pairToPoint(first: Angle, second: Angle): PointQuery | null {
148
+ const latLetters = new Set(["N", "S"])
149
+ const lonLetters = new Set(["E", "W"])
150
+
151
+ if (first.hemisphere && second.hemisphere) {
152
+ if (latLetters.has(first.hemisphere) && lonLetters.has(second.hemisphere)) {
153
+ return coordinates(first.degrees, second.degrees, "lat_lon")
154
+ }
155
+
156
+ if (lonLetters.has(first.hemisphere) && latLetters.has(second.hemisphere)) {
157
+ return coordinates(second.degrees, first.degrees, "lon_lat")
158
+ }
159
+
160
+ return null
161
+ }
162
+
163
+ if (first.hemisphere) {
164
+ return latLetters.has(first.hemisphere)
165
+ ? coordinates(first.degrees, second.degrees, "lat_lon")
166
+ : coordinates(second.degrees, first.degrees, "lon_lat")
167
+ }
168
+
169
+ if (second.hemisphere) {
170
+ return lonLetters.has(second.hemisphere)
171
+ ? coordinates(first.degrees, second.degrees, "lat_lon")
172
+ : coordinates(second.degrees, first.degrees, "lon_lat")
173
+ }
174
+
175
+ return coordinates(first.degrees, second.degrees, "lat_lon") ?? coordinates(second.degrees, first.degrees, "lon_lat")
176
+ }
177
+
178
+ function coordinates(lat: number, lon: number, order: CoordinateOrder): PointQuery | null {
179
+ if (!isValidLatitude(lat) || !isValidLongitude(lon)) return null
180
+
181
+ return { kind: "coordinates", lat, lon, order }
182
+ }
183
+
184
+ /**
185
+ * Whether a bare pair of numbers reads as a coordinate rather than two counts.
186
+ *
187
+ * `12 34` reads as a house number beside a postcode and as a position, so a bare pair
188
+ * needs a comma, a decimal point, a degree mark or a hemisphere letter.
189
+ */
190
+ const COORDINATE_MARK = /[,;.°º'′"″NSEW]/iu
191
+
192
+ function parseCoordinatePair(text: string): PointQuery | null {
193
+ if (!COORDINATE_MARK.test(text)) return null
194
+
195
+ const halves =
196
+ (/[,;]/u.test(text) ? text.split(/[,;]/u, 2) : null) ??
197
+ PAIR_AT_HEMISPHERE.exec(text)?.slice(1, 3) ??
198
+ PAIR_AT_SPACE.exec(text)?.slice(1, 3)
199
+
200
+ if (!halves || halves.length !== 2) return null
201
+
202
+ const first = parseAngle(halves[0]!)
203
+ const second = parseAngle(halves[1]!)
204
+
205
+ if (!first || !second) return null
206
+
207
+ return pairToPoint(first, second)
208
+ }
209
+
210
+ /**
211
+ * Reads a whole query as the point it names: a coordinate pair in decimal,
212
+ * degrees-minutes-seconds, `geo:` or Google Maps form, a full or short plus code,
213
+ * an H3 cell, a prefixed geohash, or a Who's On First id.
214
+ *
215
+ * Returns `null` for text that's none of these.
216
+ * Every address is such a text.
217
+ *
218
+ * A coordinate outside the valid range in both axis orders is also `null`, never clamped.
219
+ */
220
+ export function parsePointQuery(text: string): PointQuery | null {
221
+ const trimmed = text.trim()
222
+
223
+ if (!trimmed) return null
224
+
225
+ const wof = WOF_REFERENCE.exec(trimmed) ?? SPELUNKER_URL.exec(trimmed)
226
+
227
+ if (wof) {
228
+ const id = Number(wof[1])
229
+
230
+ return Number.isSafeInteger(id) && id > 0 ? { kind: "wof", id } : null
231
+ }
232
+
233
+ const h3 = H3_CELL.exec(trimmed)
234
+
235
+ if (h3) return { kind: "h3", cell: h3[1]!.toLowerCase() }
236
+
237
+ const geohash = GEOHASH_REFERENCE.exec(trimmed)
238
+
239
+ if (geohash) return { kind: "geohash", hash: geohash[1]!.toLowerCase() }
240
+
241
+ if (isFullPlusCode(trimmed)) return { kind: "plus_code", code: trimmed.toUpperCase(), full: true }
242
+
243
+ if (isShortPlusCode(trimmed)) return { kind: "plus_code", code: trimmed.toUpperCase(), full: false }
244
+
245
+ const uri = GEO_URI.exec(trimmed) ?? MAPS_SEGMENT.exec(trimmed)
246
+
247
+ if (uri) return coordinates(Number(uri[1]), Number(uri[2]), "lat_lon")
248
+
249
+ const labeled = LABELED_PAIR.exec(trimmed)
250
+
251
+ if (labeled) {
252
+ const [, firstLabel, firstValue, secondLabel, secondValue] = labeled
253
+ const firstIsLat = /^lat/iu.test(firstLabel!)
254
+
255
+ if (firstIsLat === /^lat/iu.test(secondLabel!)) return null
256
+
257
+ return firstIsLat
258
+ ? coordinates(Number(firstValue), Number(secondValue), "lat_lon")
259
+ : coordinates(Number(secondValue), Number(firstValue), "lon_lat")
260
+ }
261
+
262
+ return parseCoordinatePair(trimmed)
263
+ }
264
+
265
+ /**
266
+ * A coordinate pair as text, latitude first, in the form {@link parsePointQuery} reads back.
267
+ *
268
+ * Six decimals place a point within about 11 cm.
269
+ */
270
+ export function coordinatePairText(lat: number, lon: number, decimals = 6): string {
271
+ return `${lat.toFixed(decimals)}, ${lon.toFixed(decimals)}`
272
+ }
273
+
274
+ /**
275
+ * The label a point query is shown under, such as the tag column of a parse.
276
+ */
277
+ export const POINT_QUERY_LABEL: Record<PointQuery["kind"], string> = {
278
+ coordinates: "coordinates",
279
+ plus_code: "plus code",
280
+ h3: "H3 cell",
281
+ geohash: "geohash",
282
+ wof: "Who's On First id",
283
+ }
284
+
285
+ /**
286
+ * The same shapes as a noun phrase with its article, for a sentence: "this is a plus code".
287
+ */
288
+ export const POINT_QUERY_PHRASE: Record<PointQuery["kind"], string> = {
289
+ coordinates: "a coordinate pair",
290
+ plus_code: "a plus code",
291
+ h3: "an H3 cell",
292
+ geohash: "a geohash",
293
+ wof: "a Who's On First id",
294
+ }
package/lib/polyline.ts CHANGED
@@ -1,15 +1,14 @@
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
  * Position along a polyline, by arc length.
7
6
  *
8
- * This is what house-number interpolation stands on: given a street segment's geometry and how far
9
- * along its house-number range an address sits, it answers where that is on the ground. The node
10
- * reader (`resolver-wof-sqlite/interpolation.ts`) and the browser twin
11
- * (`resolver-wof-wasm/httpvfs/street.ts`) both call it: the geometry never differs between them,
12
- * only the I/O around it does.
7
+ * This is what house-number interpolation stands on: given a street segment's geometry
8
+ * and how far along its house-number range an address sits, it answers where that's on the ground.
9
+ * The node reader (`resolver-wof-sqlite/interpolation.ts`) and the browser twin
10
+ * (`resolver-wof-wasm/httpvfs/street.ts`) both call it: the geometry never
11
+ * differs between them, only the I/O around it does.
13
12
  */
14
13
 
15
14
  import { centralAngle, EARTH_RADII } from "#distance"
@@ -18,7 +17,7 @@ import { centralAngle, EARTH_RADII } from "#distance"
18
17
  * Clamp a fraction into `[0, 1]`.
19
18
  *
20
19
  * NaN passes through deliberately.
21
- * An interpolation whose inputs were not numbers should produce NaN coordinates a
20
+ * An interpolation whose inputs weren't numbers should produce NaN coordinates a
22
21
  * caller can detect rather than silently snap to a segment's start.
23
22
  */
24
23
  export function clampFraction(t: number): number {
package/lib/position.ts CHANGED
@@ -1,12 +1,12 @@
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
  * This file contains types and utilities for working with geographic positions.
7
6
  */
8
7
 
9
- import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
8
+ /// <reference types="google.maps" />
9
+
10
10
  import type { PlanarPoint } from "@mailwoman/annotations"
11
11
 
12
12
  import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
@@ -180,13 +180,14 @@ export function isCoordPairLiteral(input: unknown): input is [number, number] |
180
180
  }
181
181
 
182
182
  /**
183
- * Type-predicate to determine if the given input is a {@linkcode LatLngLiteral} object.
183
+ * Type-predicate to determine if the given input is a
184
+ * {@linkcode google.maps.LatLngLiteral | LatLngLiteral} object.
184
185
  *
185
186
  * @category Position
186
187
  * @category Type Predicates
187
188
  * @see {@link https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral Google Maps API Documentation}
188
189
  */
189
- export function isGoogleMapsLatLngLiteral(input: unknown): input is LatLngLiteral {
190
+ export function isGoogleMapsLatLngLiteral(input: unknown): input is google.maps.LatLngLiteral {
190
191
  if (!input || typeof input !== "object") return false
191
192
 
192
193
  if (!Object.hasOwn(input, "lat") || !Object.hasOwn(input, "lng")) return false