@mailwoman/spatial 9.4.0 → 10.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.
- package/README.md +2 -2
- package/lib/bbox.ts +41 -34
- package/lib/{coordinate-bounds.ts → coordinate/bounds.ts} +2 -2
- package/lib/{coordinate-formats.ts → coordinate/formats.ts} +11 -28
- package/lib/distance.ts +53 -27
- package/lib/feature.ts +2 -2
- package/lib/geometries/point.ts +41 -33
- package/lib/geometries/polygon.ts +24 -128
- package/lib/geometries/ring-blob.ts +33 -130
- package/lib/google/place-id.ts +4 -2
- package/lib/h3/cell.ts +21 -76
- package/lib/h3/coverage.ts +11 -7
- package/lib/h3/interior-cells.ts +13 -34
- package/lib/h3/polygon-cells.ts +59 -165
- package/lib/{h3/index.ts → h3.ts} +1 -1
- package/lib/index.ts +6 -6
- package/lib/objects.ts +6 -6
- package/lib/open-location-code.ts +11 -42
- package/lib/osgb36.ts +28 -187
- package/lib/polyline.ts +5 -3
- package/lib/position.ts +20 -4
- package/lib/projection/transform.ts +114 -0
- package/lib/projection.ts +1 -1
- package/lib/well-known-text.ts +8 -8
- package/out/bbox.d.ts +16 -13
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +32 -23
- package/out/bbox.js.map +1 -1
- package/out/{coordinate-bounds.d.ts → coordinate/bounds.d.ts} +3 -3
- package/out/coordinate/bounds.d.ts.map +1 -0
- package/out/{coordinate-bounds.js → coordinate/bounds.js} +3 -3
- package/out/coordinate/bounds.js.map +1 -0
- package/out/{coordinate-formats.d.ts → coordinate/formats.d.ts} +9 -12
- package/out/coordinate/formats.d.ts.map +1 -0
- package/out/{coordinate-formats.js → coordinate/formats.js} +12 -29
- package/out/coordinate/formats.js.map +1 -0
- package/out/distance.d.ts +48 -25
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +53 -27
- package/out/distance.js.map +1 -1
- package/out/feature.d.ts +2 -2
- package/out/feature.d.ts.map +1 -1
- package/out/geometries/point.d.ts +30 -22
- package/out/geometries/point.d.ts.map +1 -1
- package/out/geometries/point.js +35 -29
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +23 -122
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +13 -41
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +23 -109
- package/out/geometries/ring-blob.d.ts.map +1 -1
- package/out/geometries/ring-blob.js +29 -115
- package/out/geometries/ring-blob.js.map +1 -1
- package/out/{geometries/index.js → geometries.d.ts} +1 -1
- package/out/geometries.d.ts.map +1 -0
- package/out/{geometries/index.d.ts → geometries.js} +1 -1
- package/out/geometries.js.map +1 -0
- package/out/google/place-id.d.ts +4 -2
- package/out/google/place-id.d.ts.map +1 -1
- package/out/google/place-id.js.map +1 -1
- package/out/{google/index.d.ts → google.d.ts} +1 -1
- package/out/google.d.ts.map +1 -0
- package/out/{google/index.js → google.js} +1 -1
- package/out/google.js.map +1 -0
- package/out/h3/cell.d.ts +19 -73
- package/out/h3/cell.d.ts.map +1 -1
- package/out/h3/cell.js +20 -63
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +11 -7
- package/out/h3/coverage.d.ts.map +1 -1
- package/out/h3/coverage.js +11 -7
- package/out/h3/coverage.js.map +1 -1
- package/out/h3/interior-cells.d.ts +9 -28
- package/out/h3/interior-cells.d.ts.map +1 -1
- package/out/h3/interior-cells.js +13 -32
- package/out/h3/interior-cells.js.map +1 -1
- package/out/h3/polygon-cells.d.ts +28 -120
- package/out/h3/polygon-cells.d.ts.map +1 -1
- package/out/h3/polygon-cells.js +48 -146
- package/out/h3/polygon-cells.js.map +1 -1
- package/out/{h3/index.d.ts → h3.d.ts} +2 -2
- package/out/h3.d.ts.map +1 -0
- package/out/{h3/index.js → h3.js} +2 -2
- package/out/h3.js.map +1 -0
- package/out/index.d.ts +6 -6
- package/out/index.d.ts.map +1 -1
- package/out/index.js +6 -6
- package/out/index.js.map +1 -1
- package/out/objects.d.ts +6 -6
- package/out/objects.d.ts.map +1 -1
- package/out/open-location-code.d.ts +11 -22
- package/out/open-location-code.d.ts.map +1 -1
- package/out/open-location-code.js +9 -40
- package/out/open-location-code.js.map +1 -1
- package/out/osgb36.d.ts +21 -97
- package/out/osgb36.d.ts.map +1 -1
- package/out/osgb36.js +12 -165
- package/out/osgb36.js.map +1 -1
- package/out/polyline.d.ts +5 -3
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +5 -3
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +17 -3
- package/out/position.d.ts.map +1 -1
- package/out/position.js +2 -2
- package/out/position.js.map +1 -1
- package/out/projection/transform.d.ts +70 -0
- package/out/projection/transform.d.ts.map +1 -0
- package/out/{projection-transform.js → projection/transform.js} +15 -17
- package/out/projection/transform.js.map +1 -0
- package/out/projection.d.ts +1 -1
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js +1 -1
- package/out/regions/codes.d.ts.map +1 -1
- package/out/{regions/index.js → regions.d.ts} +1 -1
- package/out/regions.d.ts.map +1 -0
- package/out/{regions/index.d.ts → regions.js} +1 -1
- package/out/regions.js.map +1 -0
- package/out/tools/ogr/stream.d.ts +44 -0
- package/out/tools/ogr/stream.d.ts.map +1 -0
- package/out/tools/{ogr-stream.js → ogr/stream.js} +17 -10
- package/out/tools/ogr/stream.js.map +1 -0
- package/out/tools/ogr.d.ts +13 -10
- package/out/tools/ogr.d.ts.map +1 -1
- package/out/tools/ogr.js +18 -14
- package/out/tools/ogr.js.map +1 -1
- package/out/tools/tsconfig.tsbuildinfo +1 -0
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/well-known-text.d.ts +8 -8
- package/out/well-known-text.js +7 -7
- package/package.json +35 -127
- package/{lib/tools/ogr-stream.ts → tools/ogr/stream.ts} +25 -14
- package/{lib/tools → tools}/ogr.ts +26 -20
- package/lib/projection-transform.ts +0 -115
- package/out/coordinate-bounds.d.ts.map +0 -1
- package/out/coordinate-bounds.js.map +0 -1
- package/out/coordinate-formats.d.ts.map +0 -1
- package/out/coordinate-formats.js.map +0 -1
- package/out/geometries/index.d.ts.map +0 -1
- package/out/geometries/index.js.map +0 -1
- package/out/google/index.d.ts.map +0 -1
- package/out/google/index.js.map +0 -1
- package/out/h3/index.d.ts.map +0 -1
- package/out/h3/index.js.map +0 -1
- package/out/projection-transform.d.ts +0 -67
- package/out/projection-transform.d.ts.map +0 -1
- package/out/projection-transform.js.map +0 -1
- package/out/regions/index.d.ts.map +0 -1
- package/out/regions/index.js.map +0 -1
- package/out/tools/ogr-stream.d.ts +0 -34
- package/out/tools/ogr-stream.d.ts.map +0 -1
- package/out/tools/ogr-stream.js.map +0 -1
- /package/lib/{geometries/index.ts → geometries.ts} +0 -0
- /package/lib/{google/index.ts → google.ts} +0 -0
- /package/lib/{regions/index.ts → regions.ts} +0 -0
package/lib/h3/cell.ts
CHANGED
|
@@ -3,40 +3,26 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
* @file H3 cell primitives — the index types and the widen/shorten conversions.
|
|
6
|
-
|
|
7
|
-
* Split out from `./index.ts` because these carry no dependency on `GeoPoint`, while the
|
|
8
|
-
* point-valued helpers there do. `geometries/point.ts` needs the primitives and is itself imported
|
|
9
|
-
* by `./index.ts`, so keeping them together closed an import cycle.
|
|
6
|
+
|
|
10
7
|
*/
|
|
11
8
|
|
|
12
9
|
import { cellToParent, isValidCell } from "h3-js"
|
|
13
10
|
import type { Tagged } from "type-fest"
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
|
-
* The finest resolution H3 defines.
|
|
13
|
+
* The finest resolution H3 defines.
|
|
14
|
+
* Resolutions run `[0, H3_MAX_RESOLUTION]`.
|
|
17
15
|
*/
|
|
18
16
|
export const H3_MAX_RESOLUTION = 15
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
|
-
* A H3 cell index, full 64 bits
|
|
22
|
-
*
|
|
23
|
-
* Written as 15 hex characters because the top nibble of a cell index is always zero: bit 63 is reserved, and the 4-bit
|
|
24
|
-
* mode field that follows is `1` for a cell, which lands entirely inside the second nibble. That is why every cell
|
|
25
|
-
* index printed here begins with `8`.
|
|
26
|
-
*
|
|
27
|
-
* @type {string}
|
|
28
|
-
* @title H3 Cell Index
|
|
29
|
-
* @pattern ^[0-9a-f]{15}$
|
|
19
|
+
* A H3 cell index, full 64 bits, written as 15 lowercase hex characters matching `^[0-9a-f]{15}$`.
|
|
30
20
|
*/
|
|
31
21
|
export type H3Cell = Tagged<string, "H3Cell">
|
|
32
22
|
|
|
33
23
|
/**
|
|
34
|
-
* Is `value` a real H3 cell index
|
|
35
|
-
*
|
|
36
|
-
* Delegates to h3-js rather than testing the surface shape. The shape is necessary but nowhere near sufficient —
|
|
37
|
-
* `000000000000000`, `fffffffffffffff` and `123456789abcdef` are all fifteen lowercase hex characters and none of them
|
|
38
|
-
* is a cell. A guard that returns `value is H3Cell` on those hands the caller a branded type it has not earned, and the
|
|
39
|
-
* error surfaces later inside h3-js with no reference to where the bad value entered.
|
|
24
|
+
* Is `value` a real H3 cell index, delegating to h3-js because the hex surface
|
|
25
|
+
* shape is necessary but not sufficient.
|
|
40
26
|
*/
|
|
41
27
|
export function isH3Cell(value: string): value is H3Cell {
|
|
42
28
|
return isValidCell(value)
|
|
@@ -44,15 +30,11 @@ export function isH3Cell(value: string): value is H3Cell {
|
|
|
44
30
|
|
|
45
31
|
/**
|
|
46
32
|
* A H3 cell index with its mode and resolution nibbles removed — the low 52 bits, fixed width.
|
|
47
|
-
*
|
|
48
|
-
* @type {string}
|
|
49
|
-
* @title H3 Cell Index (Short)
|
|
50
|
-
* @pattern ^[0-9a-f]{13}$
|
|
51
33
|
*/
|
|
52
34
|
export type H3CellShort = Tagged<string, "H3CellShort">
|
|
53
35
|
|
|
54
36
|
/**
|
|
55
|
-
* Number of hex characters in the short form
|
|
37
|
+
* Number of hex characters in the short form: 52 bits, a 7-bit base cell followed by fifteen 3-bit digits.
|
|
56
38
|
*/
|
|
57
39
|
const SHORT_CELL_HEX_LENGTH = 13
|
|
58
40
|
|
|
@@ -62,15 +44,8 @@ const SHORT_CELL_HEX_LENGTH = 13
|
|
|
62
44
|
const SHORT_CELL_MASK = 0xf_ff_ff_ff_ff_ff_ffn
|
|
63
45
|
|
|
64
46
|
/**
|
|
65
|
-
* Strip the mode and resolution nibbles off a full H3 cell index, keeping the base cell
|
|
66
|
-
*
|
|
67
|
-
* The digits past the cell's own resolution are all `7` in a valid index, so nothing is discarded and nothing is
|
|
68
|
-
* inferred: the short form carries the cell losslessly for every resolution, and {@link expandH3Cell} reverses it
|
|
69
|
-
* exactly once you tell it which resolution the cell was captured at.
|
|
70
|
-
*
|
|
71
|
-
* Zero-padded to a fixed 13 characters, so the hex form orders and compares the same way the integer
|
|
72
|
-
* {@link shortCellToInt} packs does. Base cells 0-7 leave the leading nibble zero, and an unpadded string would both
|
|
73
|
-
* mis-sort and mis-expand.
|
|
47
|
+
* Strip the mode and resolution nibbles off a full H3 cell index, keeping the base cell
|
|
48
|
+
* and the whole digit path, zero-padded to a fixed 13 characters so hex order matches integer order.
|
|
74
49
|
*/
|
|
75
50
|
export function shortenH3Cell(cell: H3Cell): H3CellShort {
|
|
76
51
|
const cellBigInt = BigInt(`0x${cell}`)
|
|
@@ -80,21 +55,8 @@ export function shortenH3Cell(cell: H3Cell): H3CellShort {
|
|
|
80
55
|
}
|
|
81
56
|
|
|
82
57
|
/**
|
|
83
|
-
* Rebuild a full H3 cell index from a short cell captured at `resolution
|
|
84
|
-
*
|
|
85
|
-
* The short form drops only the mode and resolution nibbles, so reconstruction is a straight concatenation: `"8"` (cell
|
|
86
|
-
* mode) + the resolution nibble + the 52 bits verbatim. The result is the identical index `latLngToCell` would have
|
|
87
|
-
* produced at that resolution.
|
|
88
|
-
*
|
|
89
|
-
* `resolution` is a required piece of external knowledge — a short cell does not name its own resolution, so the caller
|
|
90
|
-
* has to supply the one the cell was shortened at. Supplying the wrong one is caught rather than tolerated: a mismatch
|
|
91
|
-
* leaves digits past the stated resolution set to something other than `7`, which H3 rejects, and this throws instead
|
|
92
|
-
* of returning an index that downstream `cellToParent` calls would refuse with `Cell arguments had incompatible
|
|
93
|
-
* resolutions`.
|
|
94
|
-
*
|
|
95
|
-
* @throws {RangeError} If `resolution` is not an integer in `[0, 15]`, or `h3CellShort` is wider than 13 hex
|
|
96
|
-
* characters.
|
|
97
|
-
* @throws {Error} If the short cell and resolution do not together name a valid H3 cell.
|
|
58
|
+
* Rebuild a full H3 cell index from a short cell captured at `resolution`, throwing
|
|
59
|
+
* when the resolution is out of range or the pair does not name a valid cell.
|
|
98
60
|
*/
|
|
99
61
|
export function expandH3Cell(h3CellShort: H3CellShort, resolution = H3_MAX_RESOLUTION): H3Cell {
|
|
100
62
|
if (!Number.isInteger(resolution) || resolution < 0 || resolution > H3_MAX_RESOLUTION) {
|
|
@@ -121,38 +83,24 @@ export function expandH3Cell(h3CellShort: H3CellShort, resolution = H3_MAX_RESOL
|
|
|
121
83
|
}
|
|
122
84
|
|
|
123
85
|
/**
|
|
124
|
-
* The full H3 index for a short cell held as an integer, stored at `resolution
|
|
125
|
-
*
|
|
126
|
-
* Through {@link expandH3Cell} rather than a string concatenation, because it VALIDATES: a short cell that does not name
|
|
127
|
-
* a valid cell at the stated resolution throws here instead of reaching `compactCells` as a plausible-looking index.
|
|
86
|
+
* The full H3 index for a short cell held as an integer, stored at `resolution`,
|
|
87
|
+
* through {@link expandH3Cell} so an invalid pair throws rather than reaching `compactCells`.
|
|
128
88
|
*/
|
|
129
89
|
export function expandShortCellInt(shortCell: number, resolution: number): H3Cell {
|
|
130
90
|
return expandH3Cell(shortCell.toString(16).padStart(SHORT_CELL_HEX_LENGTH, "0") as H3CellShort, resolution)
|
|
131
91
|
}
|
|
132
92
|
|
|
133
93
|
/**
|
|
134
|
-
* Pack an H3 cell into the short-cell integer used as a clustered B-tree key across layer databases
|
|
135
|
-
*
|
|
94
|
+
* Pack an H3 cell into the short-cell integer used as a clustered B-tree key across layer databases —
|
|
95
|
+
* 52 bits, inside `Number.MAX_SAFE_INTEGER` and SQLite's signed 64-bit integer column.
|
|
136
96
|
*/
|
|
137
97
|
export function shortCellToInt(cell: H3Cell): number {
|
|
138
98
|
return Number(BigInt(`0x${shortenH3Cell(cell)}`))
|
|
139
99
|
}
|
|
140
100
|
|
|
141
101
|
/**
|
|
142
|
-
* The one resolution a set of stored short cells was captured at
|
|
143
|
-
*
|
|
144
|
-
* A short cell drops the resolution nibble, so a table of them does not say what resolution it is keyed at — and every
|
|
145
|
-
* `layer_coverage` reader needs that number to derive a probe's coverage cell. It is recoverable because
|
|
146
|
-
* {@link expandH3Cell} validates: a short cell expands to a valid index at EXACTLY ONE resolution, so trying all
|
|
147
|
-
* sixteen and keeping the one that survives is an exact answer rather than an inference.
|
|
148
|
-
*
|
|
149
|
-
* SHARED RATHER THAN COPIED, because both failure modes it names are silent in a second copy. A table that mixes
|
|
150
|
-
* resolutions read at one of them reports every cell at the other as unsurveyed, and an empty table read as "resolution
|
|
151
|
-
* 0" puts every probe in the same cell. `packages/mailwoman/lib/observations/absence-route.ts` re-exports this under
|
|
152
|
-
* its own name; `@mailwoman/coastal` calls it directly.
|
|
153
|
-
*
|
|
154
|
-
* @param context Names the caller in both messages, so a failure says which reader refused.
|
|
155
|
-
* @throws {Error} When `cells` is empty, when a value is not a short cell, or when the set mixes resolutions.
|
|
102
|
+
* The one resolution a set of stored short cells was captured at, recovered by keeping
|
|
103
|
+
* the resolution at which a short cell expands to a valid index.
|
|
156
104
|
*/
|
|
157
105
|
export function recoverShortCellResolution(cells: readonly number[], context = "layer coverage"): number {
|
|
158
106
|
if (!cells.length) {
|
|
@@ -170,8 +118,7 @@ export function recoverShortCellResolution(cells: readonly number[], context = "
|
|
|
170
118
|
expandH3Cell(short, resolution)
|
|
171
119
|
valid.push(resolution)
|
|
172
120
|
} catch {
|
|
173
|
-
// A resolution the short cell does not expand at
|
|
174
|
-
// ordinary case for fifteen of the sixteen probes.
|
|
121
|
+
// A resolution the short cell does not expand at — every cell expands at exactly one.
|
|
175
122
|
}
|
|
176
123
|
}
|
|
177
124
|
|
|
@@ -200,11 +147,9 @@ export function recoverShortCellResolution(cells: readonly number[], context = "
|
|
|
200
147
|
}
|
|
201
148
|
|
|
202
149
|
/**
|
|
203
|
-
* Reconstruct a short-cell int's ancestor at a coarser resolution
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* the original cell belonged to (measured at ~6% of CONUS blocks between res 9 and res 6), so a coverage read and the
|
|
207
|
-
* build that wrote it disagree on a fraction of cells. Reconstructing from the stored cell itself is exact.
|
|
150
|
+
* Reconstruct a short-cell int's ancestor at a coarser resolution from the
|
|
151
|
+
* stored cell itself rather than a centroid.
|
|
152
|
+
* A centroid can land in a different parent.
|
|
208
153
|
*/
|
|
209
154
|
export function shortCellToParentInt(shortCell: number, from: number, to: number): number {
|
|
210
155
|
return shortCellToInt(cellToParent(expandShortCellInt(shortCell, from), to) as H3Cell)
|
package/lib/h3/coverage.ts
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
6
|
* The coverage probe every polygon layer's reader repeats: an index cell's parent at the coverage
|
|
7
|
-
* resolution, read through the layer
|
|
7
|
+
* resolution, read through the layer interface's shared row mapping.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* barrel serves browser bundles that must not
|
|
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
11
|
* `@mailwoman/spatial/h3/coverage` subpath.
|
|
12
12
|
*/
|
|
13
13
|
|
|
@@ -24,11 +24,15 @@ export interface CoverageRowProbe {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* The coverage row for `indexCell`'s parent at the coverage resolution,
|
|
28
|
-
*
|
|
27
|
+
* The coverage row for `indexCell`'s parent at the coverage resolution,
|
|
28
|
+
* or `undefined` when the cell was never surveyed.
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
30
|
+
* A caller must treat it as an unknown value, separate from `{completeness: 0}`.
|
|
31
|
+
*
|
|
32
|
+
* The shared mapping owns the NULL-basis rule.
|
|
33
|
+
* A NULL column means the artifact predates `basis`.
|
|
34
|
+
*
|
|
35
|
+
* It recorded source presence and no stronger basis than the builder had.
|
|
32
36
|
*/
|
|
33
37
|
export function readCoverageAt(
|
|
34
38
|
select: CoverageRowProbe,
|
package/lib/h3/interior-cells.ts
CHANGED
|
@@ -3,53 +3,33 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
* Turn an outline into the H3 cell set a coverage claim may be keyed to
|
|
7
|
-
*
|
|
8
|
-
* Polyfilling a rectangle is right for a rectangular extract and wrong for a region: a region is
|
|
9
|
-
* clipped to a polygon, so a rectangle polyfilled over it claims survey across whatever the rectangle
|
|
10
|
-
* overhangs. These helpers answer the narrower question — which cells lie wholly INSIDE the outline —
|
|
11
|
-
* and they answer conservatively, because the cells they return are the ones a completeness claim will
|
|
12
|
-
* be written to.
|
|
13
|
-
*
|
|
14
|
-
* Interior is two tests, both required. A cell must have its whole `gridDisk(cell, 1)` inside the
|
|
15
|
-
* outline's own polyfill, and all six of its boundary vertices inside the polygon. The first removes the
|
|
16
|
-
* polyfill's edge ring (H3's polyfill keeps a cell whose CENTRE is inside, so an edge cell can be half
|
|
17
|
-
* outside); the second catches a boundary that re-enters between two neighbours. Neither test alone is
|
|
18
|
-
* sufficient, and the cost of being wrong is asymmetric — a cell wrongly called interior claims a survey
|
|
19
|
-
* over ground the source never covered.
|
|
20
|
-
*
|
|
21
|
-
* Measured on the Île-de-France région (OSM relation 8649, admin_level 4) at res 6: 371 polyfilled
|
|
22
|
-
* cells, 290 interior, holding 3,248 of the extract's 3,308 `amenity=pharmacy` features.
|
|
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.
|
|
23
8
|
*/
|
|
24
9
|
|
|
10
|
+
import { stringifyJSON } from "@mailwoman/core/json"
|
|
25
11
|
import { cellToBoundary, gridDisk, polygonToCells } from "h3-js"
|
|
26
12
|
|
|
27
13
|
import { arealPolygons, geometryContains, type ParsedGeometry, type PolygonRings } from "#geometries/polygon"
|
|
28
14
|
import { shortCellToInt, type H3Cell } from "#h3/cell"
|
|
29
15
|
|
|
30
16
|
/**
|
|
31
|
-
* The rings a polyfill walks: every polygon's
|
|
32
|
-
*
|
|
33
|
-
* geometry (holes included), so a hole cannot survive into the result.
|
|
17
|
+
* The rings a polyfill walks: every polygon's outer ring, with holes deliberately not
|
|
18
|
+
* subtracted because the interior test re-checks vertices against the full geometry.
|
|
34
19
|
*/
|
|
35
20
|
function outerRings(geometry: ParsedGeometry): PolygonRings {
|
|
36
21
|
const polygons = arealPolygons(geometry)
|
|
37
22
|
|
|
38
23
|
if (!polygons) {
|
|
39
|
-
throw new Error(
|
|
40
|
-
`coverage region: expected a Polygon or MultiPolygon geometry, got ${JSON.stringify(geometry.type)}`
|
|
41
|
-
)
|
|
24
|
+
throw new Error(`coverage region: expected a Polygon or MultiPolygon geometry, got ${stringifyJSON(geometry.type)}`)
|
|
42
25
|
}
|
|
43
26
|
|
|
44
27
|
return polygons.filter((rings) => rings.length).map((rings) => rings[0]!)
|
|
45
28
|
}
|
|
46
29
|
|
|
47
30
|
/**
|
|
48
|
-
* The outline's bounding rectangle, for pre-clipping a reference inventory that can only
|
|
49
|
-
*
|
|
50
|
-
* A coarse filter and nothing more — it CONTAINS the outline and is never the outline. The exact clip is
|
|
51
|
-
* {@link interiorCoverageCellSet}; using this rectangle as the region would claim survey over every corner the outline
|
|
52
|
-
* does not reach.
|
|
31
|
+
* The outline's bounding rectangle, for pre-clipping a reference inventory that can only
|
|
32
|
+
* be probed by range — a coarse filter that contains the outline and is never the outline.
|
|
53
33
|
*/
|
|
54
34
|
export function geometryBBox(geometry: ParsedGeometry): {
|
|
55
35
|
minLon: number
|
|
@@ -90,8 +70,8 @@ export function geometryBBox(geometry: ParsedGeometry): {
|
|
|
90
70
|
}
|
|
91
71
|
|
|
92
72
|
/**
|
|
93
|
-
* Every cell whose
|
|
94
|
-
* narrows
|
|
73
|
+
* Every cell whose center falls inside `geometry`, at `resolution` — the raw
|
|
74
|
+
* polyfill that {@link interiorCoverageCells} narrows.
|
|
95
75
|
*/
|
|
96
76
|
export function regionCoverageCells(geometry: ParsedGeometry, resolution: number): H3Cell[] {
|
|
97
77
|
const cells = new Set<string>()
|
|
@@ -109,8 +89,7 @@ export function regionCoverageCells(geometry: ParsedGeometry, resolution: number
|
|
|
109
89
|
}
|
|
110
90
|
|
|
111
91
|
/**
|
|
112
|
-
* The cells of {@link regionCoverageCells} that lie wholly inside `geometry
|
|
113
|
-
* tests are applied.
|
|
92
|
+
* The cells of {@link regionCoverageCells} that lie wholly inside `geometry`.
|
|
114
93
|
*/
|
|
115
94
|
export function interiorCoverageCells(geometry: ParsedGeometry, resolution: number): H3Cell[] {
|
|
116
95
|
const polyfilled = new Set<string>(regionCoverageCells(geometry, resolution))
|
|
@@ -130,8 +109,8 @@ export function interiorCoverageCells(geometry: ParsedGeometry, resolution: numb
|
|
|
130
109
|
}
|
|
131
110
|
|
|
132
111
|
/**
|
|
133
|
-
* The 48-bit short-cell form of {@link interiorCoverageCells}, as a membership set —
|
|
134
|
-
* coverage writer probe.
|
|
112
|
+
* The 48-bit short-cell form of {@link interiorCoverageCells}, as a membership set —
|
|
113
|
+
* the shape both a row clipper and a coverage writer probe.
|
|
135
114
|
*/
|
|
136
115
|
export function interiorCoverageCellSet(geometry: ParsedGeometry, resolution: number): Set<number> {
|
|
137
116
|
return new Set(interiorCoverageCells(geometry, resolution).map((cell) => shortCellToInt(cell)))
|