@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
package/README.md CHANGED
@@ -33,7 +33,7 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
33
33
  - [`wkx`](https://github.com/cschwarz/wkx) — Well-Known Binary geometry parser
34
34
  - [`geo-coordinates-parser`](https://github.com/geops/geo-coordinates-parser) — coordinate string parsing
35
35
 
36
- Optional: `@googlemaps/google-maps-services-js` for Google Maps API integration.
36
+ - [`@types/google.maps`](https://www.npmjs.com/package/@types/google.maps) — type declarations for the Google Maps `LatLngLiteral` shape that `GeoPoint` accepts and returns
37
37
 
38
38
  ## Related
39
39
 
package/lib/bbox.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 Bounding Boxes
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
  * Dependency-free WGS-84 coordinate bounds shared by parsers and geometry objects.
7
6
  */
8
7
 
@@ -0,0 +1,42 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file A rectangular cell on the globe, as a plus code, a geohash or an H3 cell's box describes one.
6
+ */
7
+
8
+ import type { LatLonBounds } from "#bbox"
9
+ import { METRES_PER_DEGREE } from "#distance"
10
+
11
+ /**
12
+ * A cell by its center and its span in degrees, from which callers derive its box or an uncertainty radius.
13
+ */
14
+ export interface DegreeCell {
15
+ lat: number
16
+ lon: number
17
+ latSpanDeg: number
18
+ lonSpanDeg: number
19
+ }
20
+
21
+ /**
22
+ * The cell's bounding box.
23
+ */
24
+ export function cellBounds(cell: DegreeCell): LatLonBounds {
25
+ return {
26
+ minLat: cell.lat - cell.latSpanDeg / 2,
27
+ maxLat: cell.lat + cell.latSpanDeg / 2,
28
+ minLon: cell.lon - cell.lonSpanDeg / 2,
29
+ maxLon: cell.lon + cell.lonSpanDeg / 2,
30
+ }
31
+ }
32
+
33
+ /**
34
+ * The cell's half-diagonal in meters, never below one, so a point anywhere in
35
+ * the cell is within it of the center.
36
+ */
37
+ export function cellRadiusMeters(cell: DegreeCell): number {
38
+ const latHalf = (cell.latSpanDeg / 2) * METRES_PER_DEGREE
39
+ const lonHalf = (cell.lonSpanDeg / 2) * METRES_PER_DEGREE * Math.cos((cell.lat * Math.PI) / 180)
40
+
41
+ return Math.max(1, Math.round(Math.hypot(latHalf, lonHalf)))
42
+ }
@@ -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
  import type { AnnotationSet, Annotator } from "@mailwoman/annotations"
8
5
 
9
6
  import { MERCATOR_LATITUDE_MAX, MERCATOR_LATITUDE_MIN } from "#coordinate/bounds"
7
+ import type { DegreeCell } from "#coordinate/cell"
10
8
 
11
9
  const GEOHASH_BITS_PER_CHAR = 5
12
10
 
@@ -127,6 +125,64 @@ export function toGeohash(lat: number, lon: number, precision = 9): string {
127
125
  return hash
128
126
  }
129
127
 
128
+ /**
129
+ * The cell a geohash names: its center and its span in degrees.
130
+ */
131
+ export type DecodedGeohash = DegreeCell
132
+
133
+ /**
134
+ * Decode a geohash to its cell, or `null` for a string outside the geohash alphabet.
135
+ *
136
+ * The alphabet excludes `a`, `i`, `l` and `o`, so the test is a character-class test, and the
137
+ * reading walks the same alternating longitude and latitude bits {@link toGeohash} writes.
138
+ */
139
+ export function fromGeohash(hash: string): DecodedGeohash | null {
140
+ if (!hash) return null
141
+
142
+ let latMin = -90
143
+ let latMax = 90
144
+ let lonMin = -180
145
+ let lonMax = 180
146
+ let evenBit = true
147
+
148
+ for (const char of hash.toLowerCase()) {
149
+ const value = GEOHASH_BASE32.indexOf(char)
150
+
151
+ if (value === -1) return null
152
+
153
+ for (let shift = GEOHASH_BITS_PER_CHAR - 1; shift >= 0; shift--) {
154
+ const bit = (value >> shift) & 1
155
+
156
+ if (evenBit) {
157
+ const mid = (lonMin + lonMax) / 2
158
+
159
+ if (bit) {
160
+ lonMin = mid
161
+ } else {
162
+ lonMax = mid
163
+ }
164
+ } else {
165
+ const mid = (latMin + latMax) / 2
166
+
167
+ if (bit) {
168
+ latMin = mid
169
+ } else {
170
+ latMax = mid
171
+ }
172
+ }
173
+
174
+ evenBit = !evenBit
175
+ }
176
+ }
177
+
178
+ return {
179
+ lat: (latMin + latMax) / 2,
180
+ lon: (lonMin + lonMax) / 2,
181
+ latSpanDeg: latMax - latMin,
182
+ lonSpanDeg: lonMax - lonMin,
183
+ }
184
+ }
185
+
130
186
  const A_CODE = "A".charCodeAt(0)
131
187
 
132
188
  /**
package/lib/distance.ts CHANGED
@@ -1,11 +1,11 @@
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 Great-circle and segment distance math over plain numbers.
6
6
  *
7
7
  * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, which embeds its WebAssembly
8
- * binary, and a caller that only measures a distance must not load that. The object-form distances
8
+ * binary, and a caller that only measures a distance mustn't load that. The object-form distances
9
9
  * that take a `GeoPointInput` live in `#geometries/point/distance`.
10
10
  */
11
11
 
@@ -111,7 +111,7 @@ export const M2_PER_KM2 = 1_000_000
111
111
  * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
112
112
  * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
113
113
  *
114
- * That is the difference between "two channels rendered the same edge slightly
114
+ * That's the difference between "two channels rendered the same edge slightly
115
115
  * differently" and "the conversion is wrong".
116
116
  *
117
117
  * The longitude scaling matters at the same scale: comparing raw degrees treats
package/lib/feature.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
  /* eslint-disable @typescript-eslint/no-explicit-any */
8
5
  import type { GeometryLiteral } from "#geometries"
@@ -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
- * Streams unit-postcode rows from extracted Code-Point Open CSVs and converts their OSGB36
7
- * coordinates to WGS84.
5
+ * Streams unit-postcode rows from extracted Code-Point Open CSVs and converts
6
+ * their OSGB36 coordinates to WGS84.
8
7
  */
9
8
 
10
9
  import { normalizePostcodeDisplay } from "@mailwoman/postcode/display-form"
@@ -28,7 +27,7 @@ const CODEPOINT_COLUMNS = 10
28
27
  /**
29
28
  * The ONS country codes in Code-Point Open, mapped to ISO 3166-2 subdivisions.
30
29
  *
31
- * Northern Ireland has no code because the product does not cover it.
30
+ * Northern Ireland has no code because the product doesn't cover it.
32
31
  */
33
32
  export const CODEPOINT_COUNTRY_CODES = {
34
33
  E92000001: "ENG",
@@ -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
  * The Ordnance Survey Downloads API's record of one downloadable archive.
7
6
  *
8
- * The API serves OS OpenData products without authentication. `GET /products/<id>/downloads`
9
- * lists one record per archive, and every OS product fetcher in this repository reads that list.
7
+ * The API serves OS OpenData products without authentication.
8
+ * `GET /products/<id>/downloads` lists one record per archive, and every OS
9
+ * product fetcher in this repository reads that list.
10
10
  */
11
11
 
12
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
  * GeoJSON Geometry Collection
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
  * GeoJSON Line String
7
6
  */
8
7
 
@@ -1,14 +1,15 @@
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 The shapes a point can arrive in, and the reading of their coordinates.
6
6
  *
7
7
  * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a caller that only needs
8
- * the coordinates of a `GeoPointInput` must not load its embedded WebAssembly binary.
8
+ * the coordinates of a `GeoPointInput` mustn't load its embedded WebAssembly binary.
9
9
  */
10
10
 
11
- import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
11
+ /// <reference types="google.maps" />
12
+
12
13
  import { tryParsingJSON } from "@mailwoman/core/json"
13
14
 
14
15
  import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
@@ -97,14 +98,13 @@ export type GeoPointInput =
97
98
  | Point3DCoordinates
98
99
  | GeolocationCoordinatesLike
99
100
  | InternalPointCoordinates
100
- | LatLngLiteral
101
- | LatLng
101
+ | google.maps.LatLngLiteral
102
102
 
103
103
  /**
104
104
  * Reads the `[longitude, latitude(, altitude)]` coordinates out of a point of any accepted shape.
105
105
  *
106
106
  * It only dispatches on shape: no range check applies, and `null` answers an input
107
- * that is none of them. {@link readPointCoordinates} adds the validation.
107
+ * that's none of them. {@link readPointCoordinates} adds the validation.
108
108
  */
109
109
  export function pointInputCoordinates(input: unknown): Point2DCoordinates | Point3DCoordinates | null {
110
110
  if (isCoordPairLiteral(input)) return input
@@ -1,10 +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
5
  * @file Great-circle distances between `GeoPointInput` values or plain coordinates.
6
6
  *
7
- * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a distance must not pay for that.
7
+ * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a distance mustn't pay for that.
8
8
  * Points are read through `#geometries/point/coordinates` instead.
9
9
  */
10
10
 
@@ -1,11 +1,8 @@
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
+ /// <reference types="google.maps" />
6
5
 
7
- import type google from "@googlemaps/google-maps-services-js"
8
- import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
9
6
  import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
10
7
  import { latLngToCell } from "@sister.software/h3"
11
8
  import { convert as convertCoords } from "geo-coordinates-parser"
@@ -75,7 +72,7 @@ export class GeoPoint implements PointLiteral {
75
72
 
76
73
  /**
77
74
  * Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the interface,
78
- * never inferred from the magnitudes — and rejects a coordinate that is not on the globe.
75
+ * never inferred from the magnitudes — and rejects a coordinate that's not on the globe.
79
76
  *
80
77
  * See {@link GeoPoint.from} for why both halves of that sentence are required.
81
78
  *
@@ -169,18 +166,14 @@ export class GeoPoint implements PointLiteral {
169
166
 
170
167
  /**
171
168
  * Creates a new GeoPoint instance from a Google Maps API
172
- * {@linkcode google.LatLngLiteral | LatLngLiteral} object.
169
+ * {@linkcode google.maps.LatLngLiteral | LatLngLiteral} object.
173
170
  */
174
- constructor(latLngLiteral: google.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
171
+ constructor(latLngLiteral: google.maps.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
175
172
 
176
173
  /**
177
174
  * Creates a new GeoPoint instance from internal coordinates.
178
175
  */
179
176
  constructor(interpolatedCoords: InternalPointCoordinates, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
180
- /**
181
- * Creates a new GeoPoint instance from a Google Maps API {@linkcode google.LatLng | LatLng} object.
182
- */
183
- constructor(latLng: LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
184
177
  /**
185
178
  * Create a new GeoPoint instance from another {@linkcode PointLiteral}.
186
179
  */
@@ -206,7 +199,7 @@ export class GeoPoint implements PointLiteral {
206
199
 
207
200
  /**
208
201
  * Attempts to create a new GeoPoint instance from the given input, returning `null`
209
- * rather than throwing when the input is not a coordinate this class will stand behind.
202
+ * rather than throwing when the input isn't a coordinate this class will stand behind.
210
203
  *
211
204
  * Two rules, both of which this constructor got wrong until 2026-08-05
212
205
  * (the defect was recorded in `e9bfd139` and routed around rather than fixed):
@@ -299,9 +292,9 @@ export class GeoPoint implements PointLiteral {
299
292
  }
300
293
 
301
294
  /**
302
- * Converts the GeoPoint to a Google Maps API {@linkcode google.LatLngLiteral | LatLngLiteral} object.
295
+ * Converts the GeoPoint to a Google Maps API {@linkcode google.maps.LatLngLiteral | LatLngLiteral} object.
303
296
  */
304
- public toGoogleLatLngLiteral(): google.LatLngLiteral {
297
+ public toGoogleLatLngLiteral(): google.maps.LatLngLiteral {
305
298
  return {
306
299
  lat: this.#latitude,
307
300
  lng: this.#longitude,
@@ -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