@mailwoman/zoning 9.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +315 -0
- package/lib/index.ts +616 -0
- package/lib/ring-roles.ts +233 -0
- package/lib/rings.ts +34 -0
- package/lib/schema.ts +449 -0
- package/lib/scripts/ingest-chunk.ts +41 -0
- package/lib/sdk/build-zoning.ts +790 -0
- package/lib/sdk/cells.ts +276 -0
- package/lib/sdk/client.ts +335 -0
- package/lib/sdk/download.ts +78 -0
- package/lib/sdk/index.ts +15 -0
- package/lib/sdk/ingest-chunk.ts +360 -0
- package/lib/sdk/ingest.ts +402 -0
- package/lib/sdk/measure-resolutions.ts +100 -0
- package/lib/sdk/verify.ts +350 -0
- package/lib/test-kit.ts +207 -0
- package/lib/vocabulary.ts +342 -0
- package/out/index.d.ts +257 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +325 -0
- package/out/index.js.map +1 -0
- package/out/ring-roles.d.ts +88 -0
- package/out/ring-roles.d.ts.map +1 -0
- package/out/ring-roles.js +151 -0
- package/out/ring-roles.js.map +1 -0
- package/out/rings.d.ts +22 -0
- package/out/rings.d.ts.map +1 -0
- package/out/rings.js +22 -0
- package/out/rings.js.map +1 -0
- package/out/schema.d.ts +325 -0
- package/out/schema.d.ts.map +1 -0
- package/out/schema.js +186 -0
- package/out/schema.js.map +1 -0
- package/out/scripts/ingest-chunk.d.ts +11 -0
- package/out/scripts/ingest-chunk.d.ts.map +1 -0
- package/out/scripts/ingest-chunk.js +36 -0
- package/out/scripts/ingest-chunk.js.map +1 -0
- package/out/sdk/build-zoning.d.ts +283 -0
- package/out/sdk/build-zoning.d.ts.map +1 -0
- package/out/sdk/build-zoning.js +462 -0
- package/out/sdk/build-zoning.js.map +1 -0
- package/out/sdk/cells.d.ts +137 -0
- package/out/sdk/cells.d.ts.map +1 -0
- package/out/sdk/cells.js +176 -0
- package/out/sdk/cells.js.map +1 -0
- package/out/sdk/client.d.ts +151 -0
- package/out/sdk/client.d.ts.map +1 -0
- package/out/sdk/client.js +239 -0
- package/out/sdk/client.js.map +1 -0
- package/out/sdk/download.d.ts +51 -0
- package/out/sdk/download.d.ts.map +1 -0
- package/out/sdk/download.js +57 -0
- package/out/sdk/download.js.map +1 -0
- package/out/sdk/index.d.ts +15 -0
- package/out/sdk/index.d.ts.map +1 -0
- package/out/sdk/index.js +15 -0
- package/out/sdk/index.js.map +1 -0
- package/out/sdk/ingest-chunk.d.ts +130 -0
- package/out/sdk/ingest-chunk.d.ts.map +1 -0
- package/out/sdk/ingest-chunk.js +206 -0
- package/out/sdk/ingest-chunk.js.map +1 -0
- package/out/sdk/ingest.d.ts +191 -0
- package/out/sdk/ingest.d.ts.map +1 -0
- package/out/sdk/ingest.js +238 -0
- package/out/sdk/ingest.js.map +1 -0
- package/out/sdk/measure-resolutions.d.ts +51 -0
- package/out/sdk/measure-resolutions.d.ts.map +1 -0
- package/out/sdk/measure-resolutions.js +64 -0
- package/out/sdk/measure-resolutions.js.map +1 -0
- package/out/sdk/verify.d.ts +176 -0
- package/out/sdk/verify.d.ts.map +1 -0
- package/out/sdk/verify.js +202 -0
- package/out/sdk/verify.js.map +1 -0
- package/out/test-kit.d.ts +97 -0
- package/out/test-kit.d.ts.map +1 -0
- package/out/test-kit.js +180 -0
- package/out/test-kit.js.map +1 -0
- package/out/vocabulary.d.ts +221 -0
- package/out/vocabulary.d.ts.map +1 -0
- package/out/vocabulary.js +299 -0
- package/out/vocabulary.js.map +1 -0
- package/package.json +245 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Resolve which of a feature's rings are EXTERIORS and which are HOLES, from ring orientation — the one
|
|
7
|
+
* piece of this layer that no sibling builder already has, and the one whose absence is silent.
|
|
8
|
+
*
|
|
9
|
+
* THE SERVICE ENCODES HOLE ROLES BY ORIENTATION RATHER THAN BY NESTING, AND IT DOES IT ON MOST FEATURES.
|
|
10
|
+
* Measured over the whole national export (85,330 features, 93,483 rings): 89,967 rings are clockwise and
|
|
11
|
+
* 3,516 counter-clockwise; 84,021 features are wholly clockwise; and 1,309 features carry both windings.
|
|
12
|
+
* Of those, 1,210 nest their holes inside one polygon part the way RFC 7946 expects, and the rest put every
|
|
13
|
+
* ring in its own `MultiPolygon` part — the largest feature in the country, Meath's `RA - Rural Area`,
|
|
14
|
+
* arrives as 107 single-ring parts of which 5 are clockwise and 102 counter-clockwise. So the source uses
|
|
15
|
+
* BOTH encodings, the orientation is the only signal common to them, and a reader that took the nesting at
|
|
16
|
+
* face value would read 102 holes as 102 separate zoned areas.
|
|
17
|
+
*
|
|
18
|
+
* CLOCKWISE IS EXTERIOR, WHICH IS THE INVERSE OF RFC 7946, AND THE PUBLISHER'S OWN ARITHMETIC PROVES IT.
|
|
19
|
+
* Summing every ring's signed area over the export gives 5,444.492956 km²; the Department's own
|
|
20
|
+
* `Shape__Area` column sums to 5,444.492956 km². Summing the same rings' ABSOLUTE areas gives 5,666.569
|
|
21
|
+
* km². The signed reading matches the publisher to eight significant figures and the absolute one is 4.1%
|
|
22
|
+
* too large, so the counter-clockwise rings subtract under the publisher's own accounting: they are holes.
|
|
23
|
+
*
|
|
24
|
+
* THE HARMLESS HALF OF GETTING IT WRONG IS THE AREA. The harmful half is that a ray cast reading all 107 of
|
|
25
|
+
* Meath's rings as exteriors answers "inside `P5` rural zoning" for every location the plan carved out.
|
|
26
|
+
*
|
|
27
|
+
* AND GDAL CANNOT BE ASKED TO PRESERVE THIS. Its GeoJSON writer enforces the RFC 7946 winding
|
|
28
|
+
* unconditionally — `-lco RFC7946=NO` is not a GeoJSONSeq option and `--config OGR_ORGANIZE_POLYGONS SKIP`
|
|
29
|
+
* changes nothing — so a reprojected GeoJSON stream returns Meath as 107 counter-clockwise exteriors
|
|
30
|
+
* totalling 2,371.9 km² against the Department's 2,232.1 km². The same conversion written as WKT keeps the
|
|
31
|
+
* source's 5/102 split intact, which is why `sdk/ingest.ts` streams WKT.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { pointInRing, ringSignedAreaM2, type MultiPolygonRings } from "@mailwoman/spatial"
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How many of a hole's vertices are tested against a candidate exterior.
|
|
38
|
+
*
|
|
39
|
+
* ONE VERTEX IS NOT ENOUGH, AND THAT WAS MEASURED. Holes in this product routinely share vertices with the exterior
|
|
40
|
+
* they sit in, and a ray cast at a point exactly on an edge is implementation-defined — so a first-vertex test leaves
|
|
41
|
+
* 26 of the 3,516 holes unplaced, while a majority vote over nine of their own vertices leaves 9. Every one of those 9
|
|
42
|
+
* is a degenerate sliver of between 0.0008 m² and 1.7 m², and the publisher's own area accounting subtracts all of
|
|
43
|
+
* them.
|
|
44
|
+
*/
|
|
45
|
+
const HOLE_VERTEX_SAMPLES = 9
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* One feature's rings, with the publisher's own hole roles resolved.
|
|
49
|
+
*/
|
|
50
|
+
export interface ResolvedRingRoles {
|
|
51
|
+
/**
|
|
52
|
+
* `[exterior, ...holes]` per polygon — the shape the ring blob stores and the point test reads.
|
|
53
|
+
*/
|
|
54
|
+
polygons: MultiPolygonRings
|
|
55
|
+
exteriorCount: number
|
|
56
|
+
holeCount: number
|
|
57
|
+
/**
|
|
58
|
+
* Holes placed under an exterior that contains a majority of their sampled vertices.
|
|
59
|
+
*/
|
|
60
|
+
nestedHoles: number
|
|
61
|
+
/**
|
|
62
|
+
* Holes NO exterior contains a majority of, placed under the smallest exterior of the same feature and counted here.
|
|
63
|
+
*
|
|
64
|
+
* Measured at 9 of 3,516 nationally, every one a sliver under 1.7 m² sitting on its parent's boundary. They are
|
|
65
|
+
* carried rather than dropped: the publisher's own area accounting subtracts them, so dropping one would add ground
|
|
66
|
+
* the plan carved out, and a feature with exactly one exterior — 8 of the 9 — has no ambiguity at all.
|
|
67
|
+
*/
|
|
68
|
+
adjacentHoles: number
|
|
69
|
+
/**
|
|
70
|
+
* `1` where the feature's exterior was chosen by MAGNITUDE because no ring read as one by orientation.
|
|
71
|
+
*
|
|
72
|
+
* MEASURED AT ONE FEATURE OF 85,330, and its size is the reason the fallback exists rather than a refusal. `OBJECTID`
|
|
73
|
+
* 74040 — Galway County Council, `Agriculture` — is a single three-vertex ring enclosing 3.0 × 10⁻⁷ m², a third of a
|
|
74
|
+
* square micrometre. At that magnitude a ring's winding is floating-point noise rather than something the publisher
|
|
75
|
+
* stated: the same ring reads clockwise in the source's own Irish Transverse Mercator metres and counter-clockwise
|
|
76
|
+
* after reprojection. Refusing it would fail the build on the publisher's own data; dropping it would invent an
|
|
77
|
+
* absence. So the largest ring by magnitude becomes the exterior, which is also the correct reading for a feature
|
|
78
|
+
* published wholly inverted, and the count rides on the receipt rather than being implied to be zero.
|
|
79
|
+
*/
|
|
80
|
+
exteriorByMagnitude: number
|
|
81
|
+
/**
|
|
82
|
+
* The signed ring sum over the source's rings AS PUBLISHED, in square metres — POSITIVE under this service's
|
|
83
|
+
* clockwise-exterior convention.
|
|
84
|
+
*
|
|
85
|
+
* The ingest's own receipt, stored on the row. It is what the build compares against the Department's `Shape__Area`
|
|
86
|
+
* sum, and its SIGN is the record that the orientation was read: a source that started publishing counter-clockwise
|
|
87
|
+
* exteriors would flip it, which is a fact about the source rather than a rounding difference.
|
|
88
|
+
*/
|
|
89
|
+
signedAreaM2: number
|
|
90
|
+
ringCount: number
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Flatten a feature's rings, whichever way the source nested them.
|
|
95
|
+
*
|
|
96
|
+
* DELIBERATELY DISCARDS THE ARRIVING NESTING. Both encodings reach here — a properly nested polygon and a pile of
|
|
97
|
+
* single-ring parts — and the orientation is the only signal that means the same thing in both. Keeping the nesting for
|
|
98
|
+
* the features that have it and inferring it for the ones that do not would put two rules over one artifact.
|
|
99
|
+
*/
|
|
100
|
+
function flattenRings(polygons: MultiPolygonRings): ReadonlyArray<ReadonlyArray<readonly number[]>> {
|
|
101
|
+
const rings: Array<ReadonlyArray<readonly number[]>> = []
|
|
102
|
+
|
|
103
|
+
for (const part of polygons) {
|
|
104
|
+
for (const ring of part) {
|
|
105
|
+
rings.push(ring)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return rings
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Does `outer` contain a majority of `ring`'s sampled vertices?
|
|
114
|
+
*/
|
|
115
|
+
function containsMajority(ring: ReadonlyArray<readonly number[]>, outer: ReadonlyArray<readonly number[]>): boolean {
|
|
116
|
+
const stride = Math.max(1, Math.floor(ring.length / HOLE_VERTEX_SAMPLES))
|
|
117
|
+
|
|
118
|
+
let inside = 0
|
|
119
|
+
let tested = 0
|
|
120
|
+
|
|
121
|
+
for (let index = 0; index < ring.length; index += stride) {
|
|
122
|
+
tested++
|
|
123
|
+
|
|
124
|
+
if (pointInRing(ring[index]![0]!, ring[index]![1]!, outer)) {
|
|
125
|
+
inside++
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return inside * 2 > tested
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Resolve one feature's hole roles from ring orientation.
|
|
134
|
+
*
|
|
135
|
+
* @param featureID Named in every refusal, so a build log says which feature rather than only that one failed.
|
|
136
|
+
* @throws {Error} When the feature carries no ring at all. That is the one case with no reading: a feature reduced to
|
|
137
|
+
* nothing reads downstream as an absence of zoning, which is the one answer this layer must never invent. A feature
|
|
138
|
+
* whose rings all read as holes DOES have a reading — see {@link ResolvedRingRoles.exteriorByMagnitude}.
|
|
139
|
+
*/
|
|
140
|
+
export function resolveRingRoles(polygons: MultiPolygonRings, featureID: string): ResolvedRingRoles {
|
|
141
|
+
const rings = flattenRings(polygons)
|
|
142
|
+
|
|
143
|
+
if (!rings.length) {
|
|
144
|
+
throw new Error(`zoning rings: feature ${featureID} carries no ring`)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const exteriors: Array<{
|
|
148
|
+
ring: ReadonlyArray<readonly number[]>
|
|
149
|
+
area: number
|
|
150
|
+
holes: Array<ReadonlyArray<readonly number[]>>
|
|
151
|
+
}> = []
|
|
152
|
+
|
|
153
|
+
const holes: Array<ReadonlyArray<readonly number[]>> = []
|
|
154
|
+
|
|
155
|
+
let signedAreaM2 = 0
|
|
156
|
+
|
|
157
|
+
for (const ring of rings) {
|
|
158
|
+
const signed = ringSignedAreaM2(ring)
|
|
159
|
+
|
|
160
|
+
signedAreaM2 += signed
|
|
161
|
+
|
|
162
|
+
// CLOCKWISE IS EXTERIOR UNDER THIS SERVICE, and `ringSignedAreaM2` signs clockwise POSITIVE — see its docstring.
|
|
163
|
+
// A zero-area ring is degenerate rather than either role, and is carried as a hole so it can never enclose anything.
|
|
164
|
+
if (signed > 0) {
|
|
165
|
+
exteriors.push({ ring, area: signed, holes: [] })
|
|
166
|
+
} else {
|
|
167
|
+
holes.push(ring)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let exteriorByMagnitude = 0
|
|
172
|
+
|
|
173
|
+
// NO RING READ AS AN EXTERIOR, SO MAGNITUDE DECIDES — measured at exactly one feature of 85,330. The largest ring is
|
|
174
|
+
// the one that encloses the area, which is the correct reading both for a degenerate sliver whose winding is
|
|
175
|
+
// floating-point noise and for a feature published wholly inverted. Refusing here would fail the build on the
|
|
176
|
+
// publisher's own data, and skipping the feature would invent an absence.
|
|
177
|
+
if (!exteriors.length) {
|
|
178
|
+
let largestIndex = 0
|
|
179
|
+
let largestArea = Math.abs(ringSignedAreaM2(holes[0]!))
|
|
180
|
+
|
|
181
|
+
for (let index = 1; index < holes.length; index++) {
|
|
182
|
+
const area = Math.abs(ringSignedAreaM2(holes[index]!))
|
|
183
|
+
|
|
184
|
+
if (area > largestArea) {
|
|
185
|
+
largestIndex = index
|
|
186
|
+
largestArea = area
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const [largest] = holes.splice(largestIndex, 1)
|
|
191
|
+
|
|
192
|
+
exteriors.push({ ring: largest!, area: largestArea, holes: [] })
|
|
193
|
+
|
|
194
|
+
exteriorByMagnitude = 1
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// SMALLEST CONTAINING EXTERIOR, so a hole inside an island inside a hole lands on the island rather than on the outer
|
|
198
|
+
// ring. Sorting once is cheaper than choosing per hole, and it makes the choice deterministic on a tie.
|
|
199
|
+
const bySize = [...exteriors].toSorted((left, right) => left.area - right.area)
|
|
200
|
+
|
|
201
|
+
let nestedHoles = 0
|
|
202
|
+
let adjacentHoles = 0
|
|
203
|
+
|
|
204
|
+
for (const hole of holes) {
|
|
205
|
+
const parent = bySize.find((exterior) => containsMajority(hole, exterior.ring))
|
|
206
|
+
|
|
207
|
+
if (parent) {
|
|
208
|
+
parent.holes.push(hole)
|
|
209
|
+
|
|
210
|
+
nestedHoles++
|
|
211
|
+
|
|
212
|
+
continue
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// A hole no exterior contains a majority of sits ON its parent's boundary — measured at 9 of 3,516 nationally, all
|
|
216
|
+
// under 1.7 m². It goes to the smallest exterior of the same feature, which is the only exterior at all on 8 of the
|
|
217
|
+
// 9, and is counted so a receipt can carry the number rather than imply it is zero.
|
|
218
|
+
bySize[0]!.holes.push(hole)
|
|
219
|
+
|
|
220
|
+
adjacentHoles++
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
polygons: exteriors.map((exterior) => [exterior.ring, ...exterior.holes]),
|
|
225
|
+
exteriorCount: exteriors.length,
|
|
226
|
+
holeCount: holes.length,
|
|
227
|
+
nestedHoles,
|
|
228
|
+
adjacentHoles,
|
|
229
|
+
exteriorByMagnitude,
|
|
230
|
+
signedAreaM2,
|
|
231
|
+
ringCount: rings.length,
|
|
232
|
+
}
|
|
233
|
+
}
|
package/lib/rings.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* `zoning_area.rings` — the blob layout and the point test that reads it.
|
|
7
|
+
*
|
|
8
|
+
* THE CODE ITSELF LIVES IN `@mailwoman/spatial`, re-exported here so this package's call sites and its
|
|
9
|
+
* `@mailwoman/zoning/rings` subpath keep reading the same. Nothing in it is zoning-specific: it is byte
|
|
10
|
+
* layout plus spherical geometry, and a second copy of the alignment arithmetic or of the signed-area
|
|
11
|
+
* reading is a second place for either to stop being right. Both failure modes are silent — a mis-read blob
|
|
12
|
+
* answers a containment question wrongly, and a hole-blind area reading answers "inside" for every point in
|
|
13
|
+
* a hole.
|
|
14
|
+
*
|
|
15
|
+
* WHAT IS NOT SHARED IS {@linkcode resolveRingRoles}, and the reason is that it is a fact about THIS
|
|
16
|
+
* PUBLISHER rather than about geometry: the service encodes hole roles by ring ORIENTATION, clockwise
|
|
17
|
+
* exterior, which is the inverse of RFC 7946's convention. `ring-roles.ts` carries it with the measurement
|
|
18
|
+
* that establishes it.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
decodeRings,
|
|
23
|
+
encodeRings,
|
|
24
|
+
pointInEncodedRings,
|
|
25
|
+
RING_BLOB_VERSION,
|
|
26
|
+
ringAreaReadings,
|
|
27
|
+
ringSignedAreaM2,
|
|
28
|
+
ringsBoundingBox,
|
|
29
|
+
type DecodedRings,
|
|
30
|
+
type MultiPolygonRings,
|
|
31
|
+
type PolygonRings,
|
|
32
|
+
} from "@mailwoman/spatial"
|
|
33
|
+
|
|
34
|
+
export { resolveRingRoles, type ResolvedRingRoles } from "#ring-roles"
|