@mailwoman/zoning 10.0.0 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +169 -163
  2. package/lib/index.ts +103 -135
  3. package/lib/paths.ts +24 -0
  4. package/lib/ring-roles.ts +30 -71
  5. package/lib/rings.ts +2 -13
  6. package/lib/schema.ts +98 -139
  7. package/lib/vocabulary.ts +52 -130
  8. package/out/index.d.ts +81 -101
  9. package/out/index.d.ts.map +1 -1
  10. package/out/index.js +37 -96
  11. package/out/index.js.map +1 -1
  12. package/out/paths.d.ts +19 -0
  13. package/out/paths.d.ts.map +1 -0
  14. package/out/paths.js +21 -0
  15. package/out/paths.js.map +1 -0
  16. package/out/ring-roles.d.ts +16 -50
  17. package/out/ring-roles.d.ts.map +1 -1
  18. package/out/ring-roles.js +18 -51
  19. package/out/ring-roles.js.map +1 -1
  20. package/out/rings.d.ts +2 -13
  21. package/out/rings.d.ts.map +1 -1
  22. package/out/rings.js +2 -13
  23. package/out/rings.js.map +1 -1
  24. package/out/schema.d.ts +90 -132
  25. package/out/schema.d.ts.map +1 -1
  26. package/out/schema.js +17 -45
  27. package/out/schema.js.map +1 -1
  28. package/out/sdk/build-zoning.d.ts +58 -111
  29. package/out/sdk/build-zoning.d.ts.map +1 -1
  30. package/out/sdk/build-zoning.js +50 -139
  31. package/out/sdk/build-zoning.js.map +1 -1
  32. package/out/sdk/cells.d.ts +21 -62
  33. package/out/sdk/cells.d.ts.map +1 -1
  34. package/out/sdk/cells.js +15 -52
  35. package/out/sdk/cells.js.map +1 -1
  36. package/out/sdk/client.d.ts +23 -67
  37. package/out/sdk/client.d.ts.map +1 -1
  38. package/out/sdk/client.js +29 -77
  39. package/out/sdk/client.js.map +1 -1
  40. package/out/sdk/download.d.ts +7 -27
  41. package/out/sdk/download.d.ts.map +1 -1
  42. package/out/sdk/download.js +8 -28
  43. package/out/sdk/download.js.map +1 -1
  44. package/out/sdk/ingest/chunk.d.ts +19 -43
  45. package/out/sdk/ingest/chunk.d.ts.map +1 -1
  46. package/out/sdk/ingest/chunk.js +19 -52
  47. package/out/sdk/ingest/chunk.js.map +1 -1
  48. package/out/sdk/ingest/worker.d.ts +9 -0
  49. package/out/sdk/ingest/worker.d.ts.map +1 -0
  50. package/out/{scripts/ingest-chunk.js → sdk/ingest/worker.js} +8 -10
  51. package/out/sdk/ingest/worker.js.map +1 -0
  52. package/out/sdk/ingest.d.ts +152 -0
  53. package/out/sdk/ingest.d.ts.map +1 -0
  54. package/out/sdk/ingest.js +200 -0
  55. package/out/sdk/ingest.js.map +1 -0
  56. package/out/sdk/measure-resolutions.d.ts +7 -34
  57. package/out/sdk/measure-resolutions.d.ts.map +1 -1
  58. package/out/sdk/measure-resolutions.js +5 -23
  59. package/out/sdk/measure-resolutions.js.map +1 -1
  60. package/out/sdk/test-kit.d.ts +75 -0
  61. package/out/sdk/test-kit.d.ts.map +1 -0
  62. package/out/{test-kit.js → sdk/test-kit.js} +20 -43
  63. package/out/sdk/test-kit.js.map +1 -0
  64. package/out/sdk/verify.d.ts +79 -80
  65. package/out/sdk/verify.d.ts.map +1 -1
  66. package/out/sdk/verify.js +83 -70
  67. package/out/sdk/verify.js.map +1 -1
  68. package/out/vocabulary.d.ts +52 -120
  69. package/out/vocabulary.d.ts.map +1 -1
  70. package/out/vocabulary.js +47 -125
  71. package/out/vocabulary.js.map +1 -1
  72. package/package.json +38 -162
  73. package/{lib/sdk → sdk}/build-zoning.ts +102 -188
  74. package/{lib/sdk → sdk}/cells.ts +27 -66
  75. package/{lib/sdk → sdk}/client.ts +34 -82
  76. package/sdk/download.ts +58 -0
  77. package/{lib/sdk → sdk}/ingest/chunk.ts +36 -65
  78. package/{lib/scripts/ingest-chunk.ts → sdk/ingest/worker.ts} +7 -9
  79. package/sdk/ingest.ts +354 -0
  80. package/sdk/measure-resolutions.ts +72 -0
  81. package/{lib → sdk}/test-kit.ts +21 -44
  82. package/sdk/verify.ts +367 -0
  83. package/lib/sdk/download.ts +0 -78
  84. package/lib/sdk/index.ts +0 -15
  85. package/lib/sdk/ingest/index.ts +0 -393
  86. package/lib/sdk/measure-resolutions.ts +0 -101
  87. package/lib/sdk/verify.ts +0 -357
  88. package/out/scripts/ingest-chunk.d.ts +0 -11
  89. package/out/scripts/ingest-chunk.d.ts.map +0 -1
  90. package/out/scripts/ingest-chunk.js.map +0 -1
  91. package/out/sdk/index.d.ts +0 -15
  92. package/out/sdk/index.d.ts.map +0 -1
  93. package/out/sdk/index.js +0 -15
  94. package/out/sdk/index.js.map +0 -1
  95. package/out/sdk/ingest/index.d.ts +0 -185
  96. package/out/sdk/ingest/index.d.ts.map +0 -1
  97. package/out/sdk/ingest/index.js +0 -232
  98. package/out/sdk/ingest/index.js.map +0 -1
  99. package/out/test-kit.d.ts +0 -96
  100. package/out/test-kit.d.ts.map +0 -1
  101. package/out/test-kit.js.map +0 -1
@@ -3,27 +3,11 @@
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
5
  *
6
- * `@mailwoman/zoning/test-kit` — hand-built geometry for the fixture rung: zones in two plans over the same
7
- * ground, one with a hole encoded the way this service encodes them, one smaller than a cell, and one the
8
- * authority states as UNZONED.
6
+ * `@mailwoman/zoning/test-kit` provides hand-built geometry for fixture tests.
7
+ * It includes zones in two plans over the same ground. It includes a zone with a service-encoded hole.
8
+ * It includes a zone smaller than a cell and a zone the authority states as unzoned.
9
9
  *
10
- * A PACKAGE SUBPATH RATHER THAN A `test/` FILE, for the same reason `packages/mailwoman/lib/test-kit` is one:
11
- * the observation route's suite lives in another workspace, and a relative import across two TypeScript
12
- * projects is refused outright (`TS2878`) because the compiled layout would not match the source layout.
13
- *
14
- * NO NETWORK AND NO GDAL. The builder takes a {@linkcode ZoningFeatureSource}, so a fixture is a list of
15
- * features — which is what lets this rung exercise the ring-role resolution, the vocabulary census, the cell
16
- * classification, the coverage rows, the manifest and the seal on every machine rather than only the ones
17
- * with ogr2ogr.
18
- *
19
- * THE EXTERIOR RING IS THE CLOCKWISE ONE, WHICH IS THE INVERSE OF THE GEOJSON CONVENTION AND OF EVERY
20
- * SIBLING FIXTURE. `@mailwoman/spatial`'s `rectangleRing` winds counter-clockwise and its `reversedRing`
21
- * clockwise, so this file aliases them the other way round from the flood and coastal test kits: under this
22
- * service a clockwise ring is the EXTERIOR and a counter-clockwise one is a HOLE. A fixture that used the
23
- * sibling naming would build geometry the ingest reads inside-out, and every assertion on it would pass.
24
- *
25
- * THE COORDINATES ARE IN THE IRISH SEA east of Dublin — inside the Department's declared extent, so a
26
- * fixture stays in the coordinate space the product occupies, and clear of every real zoning polygon.
10
+ * Under this service a clockwise ring is the exterior and a counter-clockwise one is a hole, the inverse of the GeoJSON convention and of every sibling fixture.
27
11
  */
28
12
 
29
13
  import {
@@ -33,16 +17,16 @@ import {
33
17
  } from "@mailwoman/spatial"
34
18
 
35
19
  import { resolveRingRoles } from "#rings"
36
- import type { ZoningFeatureSource, ZoningSourceFeature } from "#sdk/ingest/index"
20
+ import type { ZoningFeatureSource, ZoningSourceFeature } from "#sdk/ingest"
37
21
  import { GZT_SOURCE_EPSG, GZT_UNZONED_LOCAL_CODE } from "#vocabulary"
38
22
 
39
23
  /**
40
- * The exterior ring builder, under THIS service's convention. Clockwise.
24
+ * The exterior ring builder under this service's convention.
41
25
  */
42
26
  export const exteriorRing = clockwiseRing
43
27
 
44
28
  /**
45
- * The hole ring builder, under THIS service's convention. Counter-clockwise.
29
+ * The hole ring builder under this service's convention.
46
30
  */
47
31
  export const holeRing = counterClockwiseRing
48
32
 
@@ -52,22 +36,18 @@ export const holeRing = counterClockwiseRing
52
36
  export const FIXTURE_ORIGIN = { lon: -5.99, lat: 53.3 } as const
53
37
 
54
38
  /**
55
- * Side of a fixture zone, in degrees. About 1.1 km at this latitude — several res-10 cells across, so a zone has a real
56
- * interior AND a real fringe.
39
+ * Side of a fixture zone in degrees.
57
40
  */
58
41
  export const FIXTURE_SIDE = 0.01
59
42
 
60
43
  /**
61
- * The fixture authority and its two plans.
44
+ * The fixture authority's code and name.
62
45
  */
63
46
  export const FIXTURE_AUTHORITY = { code: "Fx", name: "Fixture County Council" } as const
64
47
 
65
48
  /**
66
- * The two plans the fixture set uses, shaped like the real product's: a Development Plan and a Local Area Plan, each
67
- * with its own stated window, both able to zone the same ground.
68
- *
69
- * The dates are in the source's own RFC 1123 form, because that is what the export publishes and this layer carries
70
- * them as published rather than re-formatting them.
49
+ * The two plans the fixture set uses, shaped like the real product's, with dates
50
+ * preserved as published in the source's own RFC 1123 form.
71
51
  */
72
52
  export const FIXTURE_PLANS = {
73
53
  development: {
@@ -113,18 +93,16 @@ export function fixtureFeature(
113
93
  crosswalkDescription: "Existing residential",
114
94
  crosswalkRollup: "RE",
115
95
  ...overrides,
116
- // RESOLVED THROUGH THE REAL RESOLVER, never hand-assembled: a fixture that nested its own holes would test the
117
- // assertion rather than the resolution, and the resolution is the part of this layer no sibling already has.
96
+ // Resolved through the real resolver rather than hand-assembled.
118
97
  rings: resolveRingRoles(polygons, String(objectID)),
119
98
  }
120
99
  }
121
100
 
122
101
  /**
123
- * The fixture set: two adjacent zones, one of them holed the way this service encodes holes; a second plan over the
124
- * SAME ground as the first; a zone smaller than a cell; and a zone the authority states as unzoned.
102
+ * The fixture set has two adjacent zones and one with a service-encoded hole.
125
103
  *
126
- * THE OVERLAP BETWEEN PLANS IS THE POINT. A point inside the first zone must answer with BOTH rows, each naming its own
127
- * plan — which is what proves a plan is part of the claim rather than a parameter of it.
104
+ * It has a second plan over the same ground.
105
+ * It has a zone smaller than a cell and a zone the authority states as unzoned.
128
106
  */
129
107
  export function fixtureFeatures(): ZoningSourceFeature[] {
130
108
  const { lon, lat } = FIXTURE_ORIGIN
@@ -132,8 +110,8 @@ export function fixtureFeatures(): ZoningSourceFeature[] {
132
110
  const zoneA = exteriorRing(lon, lat, lon + FIXTURE_SIDE, lat + FIXTURE_SIDE)
133
111
  const zoneB = exteriorRing(lon + FIXTURE_SIDE, lat, lon + 2 * FIXTURE_SIDE, lat + FIXTURE_SIDE)
134
112
 
135
- // THE HOLE IS A SEPARATE PART, not a nested ring — which is how the real service encodes it on the features that carry
136
- // one, and the encoding a nesting-aware reader turns into a second zoned area.
113
+ // The real service encodes the hole as a separate part.
114
+ // It does not nest the hole ring.
137
115
  const holed: MultiPolygonRings = [
138
116
  [exteriorRing(lon, lat + 2 * FIXTURE_SIDE, lon + FIXTURE_SIDE, lat + 3 * FIXTURE_SIDE)],
139
117
  [
@@ -146,8 +124,8 @@ export function fixtureFeatures(): ZoningSourceFeature[] {
146
124
  ],
147
125
  ]
148
126
 
149
- // About 5.5 m across — smaller than a res-11 cell, let alone a res-9 one. `polygonToCells` returns nothing for a shape
150
- // this size, so it is the fixture that proves the index takes cell-touches-polygon rather than centre-in-polygon.
127
+ // Smaller than a res-11 cell, so `polygonToCells` returns no cell and the index must
128
+ // cover it by cell-touches-polygon rather than center-in-polygon.
151
129
  const sliver = exteriorRing(lon + 3 * FIXTURE_SIDE, lat, lon + 3 * FIXTURE_SIDE + 0.00005, lat + 0.00005)
152
130
 
153
131
  const unzoned = exteriorRing(lon + 4 * FIXTURE_SIDE, lat, lon + 5 * FIXTURE_SIDE, lat + FIXTURE_SIDE)
@@ -174,7 +152,7 @@ export function fixtureFeatures(): ZoningSourceFeature[] {
174
152
  crosswalkCode: "N1.1",
175
153
  crosswalkDescription: "Road",
176
154
  }),
177
- // The authority states unzoned land POSITIVELY, and uses a code its own domain never declares for it.
155
+ // The authority marks land as unzoned with a code its own domain does not declare.
178
156
  fixtureFeature(5, [[unzoned]], {
179
157
  localCode: GZT_UNZONED_LOCAL_CODE,
180
158
  localDescription: "Unzoned",
@@ -182,8 +160,7 @@ export function fixtureFeatures(): ZoningSourceFeature[] {
182
160
  crosswalkDescription: null,
183
161
  crosswalkRollup: "N/A",
184
162
  }),
185
- // The SAME ground as zone A under a second plan, and the same local code mapped to a DIFFERENT generic type — which
186
- // is the shape that makes the crosswalk non-functional over an (authority, code) pair.
163
+ // The same ground as zone A under a second plan, with the same local code mapped to a different generic type.
187
164
  fixtureFeature(6, [[zoneA]], {
188
165
  planID: localAreaPlan.id,
189
166
  planName: localAreaPlan.name,
package/sdk/verify.ts ADDED
@@ -0,0 +1,367 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The check compares the artifact with a second path. A separate half tests locations outside its coverage.
7
+ *
8
+ * The positive half samples points from the sealed artifact and asks the Department's feature service about them.
9
+ * The service supplies geometry through a separate distribution channel. This package has not touched that geometry.
10
+ * The check runs the point test on the service's rings. It compares two verdicts from the same authority.
11
+ * This measures our conversion against the authority's published geometry.
12
+ *
13
+ * The service returns geometry in the publisher's ring convention. `outSR=4326` returns the same clockwise-exterior
14
+ * rings as the bulk export. This path re-derives ring roles as ingest does. A point inside a hole then falls outside
15
+ * on both paths or inside on both paths.
16
+ *
17
+ * The negative half checks locations outside the publication area. The Department omits Donegal, one of 31 local
18
+ * authorities. The product also excludes Northern Ireland. Points in both areas must return `unknown` with no
19
+ * designation. A positive-only check could pass an artifact that reports the whole island as zoned. The layer treats
20
+ * a missing polygon as no zoning statement.
21
+ *
22
+ * The channels use different coordinate precision. A boundary-point disagreement therefore does not establish a
23
+ * conversion defect. The archive publishes nine decimals through this package's ingest. The service rounds its JSON.
24
+ * A point within roughly a meter of a zone boundary can fall on opposite sides of the two rendered edges. The check
25
+ * reports these cases as `boundary_tolerance` with the distance to the nearest edge. The receipt includes their count.
26
+ *
27
+ * The distance measures to the edge rather than to the nearest vertex. A point a centimeter from a long edge can be
28
+ * meters from every vertex. In one sibling-layer near-miss, vertex distance measured 1.58 m and edge distance
29
+ * measured 0.009 m. The vertex measurement overstated distance by 175 times. It would make the stated boundary
30
+ * tolerance stricter and report a rendering difference as a conversion defect.
31
+ */
32
+
33
+ import type { OGCFeature } from "@mailwoman/core/api"
34
+ import { stringifyJSON } from "@mailwoman/core/json"
35
+ import type { OutsideCoverageRow } from "@mailwoman/core/layers"
36
+ import {
37
+ nearestRingEdgeMetres,
38
+ pointInEncodedRings,
39
+ strideSampleInteriorPoints,
40
+ encodeRings,
41
+ type MultiPolygonRings,
42
+ type PolygonRings,
43
+ } from "@mailwoman/spatial"
44
+ import { DatabaseClient } from "@mailwoman/sqlite/client"
45
+ import type { PathBuilderLike } from "path-ts"
46
+
47
+ import { ZoningLookup, ZoningReadingKind, type ZoningReading } from "#index"
48
+ import { resolveRingRoles } from "#rings"
49
+ import type { ZoningDatabase } from "#schema"
50
+ import type { GZTClient } from "#sdk/client"
51
+
52
+ /**
53
+ * Records one point and both verdicts.
54
+ * It also records whether they agree.
55
+ */
56
+ export interface AgreementRow {
57
+ label: string
58
+ latitude: number
59
+ longitude: number
60
+ /**
61
+ * The artifact's answer.
62
+ */
63
+ local: ZoningReading
64
+ /**
65
+ * Whether the service's own geometry places the point inside a zoning polygon.
66
+ */
67
+ serviceInside: boolean
68
+ /**
69
+ * The local code the service reports at the point, where it reports one.
70
+ *
71
+ * Compared verbatim against the artifact's, because carrying the code verbatim
72
+ * is what this layer is for: two paths that agree on containment and disagree
73
+ * on the code would be a silent vocabulary defect.
74
+ */
75
+ serviceLocalCode: string | null
76
+ result: "agree" | "disagree" | "boundary_tolerance"
77
+ /**
78
+ * Meters from the point to the nearest edge of any polygon the service returned nearby.
79
+ *
80
+ * Every row includes this distance.
81
+ * It separates a real defect from a difference caused by the two channels
82
+ * rendering the same edge differently.
83
+ *
84
+ * A receipt without it forces a re-run.
85
+ * `null` means the service returned no polygon at all near the point.
86
+ */
87
+ nearestEdgeMetres: number | null
88
+ }
89
+
90
+ export interface VerifyZoningResult {
91
+ agreement: AgreementRow[]
92
+ agreed: number
93
+ disagreed: number
94
+ boundaryTolerance: number
95
+ /**
96
+ * Points where both paths placed the location inside a zone and the local codes differed.
97
+ */
98
+ codeMismatches: number
99
+ outside: OutsideCoverageRow[]
100
+ outsidePassed: number
101
+ }
102
+
103
+ /**
104
+ * Point identifiers for locations outside this product's publication area.
105
+ *
106
+ * Each is a place rather than a bare pair of numbers: a coordinate a reader a
107
+ * coordinate without a reference source cannot be checked.
108
+ *
109
+ * The check requires points from two populations.
110
+ * The Donegal points are the case this layer's coverage posture exists for.
111
+ *
112
+ * The Department has not published that authority's zoning.
113
+ * A builder that reads the absence as "unrestricted" would answer these points confidently.
114
+ *
115
+ * The Northern Irish points confirm the artifact is clipped to the Republic rather than to the
116
+ * island: zoning there is a different jurisdiction's instrument under a different planning act.
117
+ */
118
+ export const OUTSIDE_PUBLICATION_POINTS: ReadonlyArray<{ label: string; latitude: number; longitude: number }> = [
119
+ { label: "Letterkenny town centre, Donegal", latitude: 54.9503, longitude: -7.7345 },
120
+ { label: "Buncrana, Donegal", latitude: 55.1367, longitude: -7.4561 },
121
+ { label: "Donegal Town", latitude: 54.6538, longitude: -8.1096 },
122
+ { label: "Belfast city centre, Northern Ireland", latitude: 54.5973, longitude: -5.9301 },
123
+ { label: "Derry city centre, Northern Ireland", latitude: 54.9966, longitude: -7.3086 },
124
+ { label: "Enniskillen, Northern Ireland", latitude: 54.3438, longitude: -7.6316 },
125
+ ]
126
+
127
+ /**
128
+ * Half-width of the bounding box the service is asked for, in degrees.
129
+ *
130
+ * About 11 m at this latitude — wide enough that a polygon containing the point is
131
+ * certainly returned, narrow enough that the response stays small.
132
+ */
133
+ const PROBE_HALF_WIDTH_DEGREES = 0.0001
134
+
135
+ /**
136
+ * How close to a service-polygon edge a disagreement is attributed to the channels'
137
+ * differing coordinate precision rather than to the conversion.
138
+ *
139
+ * Half a meter.
140
+ * The two channels render the same edge from the same source coordinates through different
141
+ * rounding, so a point between the two renderings lands on opposite sides.
142
+ *
143
+ * Half a meter is far below any real zoning boundary and far above the rounding difference.
144
+ */
145
+ const BOUNDARY_TOLERANCE_METRES = 0.5
146
+
147
+ /**
148
+ * One feature as the service publishes it.
149
+ * The only shape the comparison reads.
150
+ */
151
+ export type ServiceFeature = OGCFeature
152
+
153
+ /**
154
+ * The one call the verification makes against the service: the features it publishes near a point.
155
+ *
156
+ * The function exposes only the service read.
157
+ * This keeps the check's own logic testable.
158
+ *
159
+ * The comparison's value is that it decides which of three results a point gets.
160
+ *
161
+ * An HTTP client would let tests observe these decisions only during a live run.
162
+ * A scripted reader pins the decisions. {@link createServiceReader} builds the real reader.
163
+ */
164
+ export type ServiceFeatureReader = (latitude: number, longitude: number) => Promise<ServiceFeature[]>
165
+
166
+ /**
167
+ * The reader the live check uses: a bounding-box query against the Department's own service.
168
+ *
169
+ * The service answers a BOX rather than a point, so this returns what it published nearby
170
+ * and the containment decision is made in {@link readServiceContainment} against
171
+ * those rings — comparing the artifact's verdict against a bare "the service returned
172
+ * something here" would pass on any polygon within eleven meters.
173
+ */
174
+ export function createServiceReader(client: Pick<GZTClient, "readFeaturesNear">): ServiceFeatureReader {
175
+ return async (latitude, longitude) => client.readFeaturesNear(latitude, longitude, PROBE_HALF_WIDTH_DEGREES)
176
+ }
177
+
178
+ export interface VerifyZoningOptions {
179
+ databasePath: PathBuilderLike
180
+ readServiceFeatures: ServiceFeatureReader
181
+ /**
182
+ * Points to re-ask the service about.
183
+ *
184
+ * A caller samples them from the artifact — see {@link sampleAgreementPoints}.
185
+ */
186
+ points: ReadonlyArray<{ label: string; latitude: number; longitude: number; localCode?: string }>
187
+ outsidePoints?: ReadonlyArray<{ label: string; latitude: number; longitude: number }>
188
+ onProgress?: (message: string) => void
189
+ }
190
+
191
+ /**
192
+ * Run both halves.
193
+ */
194
+ export async function verifyZoningDatabase(options: VerifyZoningOptions): Promise<VerifyZoningResult> {
195
+ const lookup = new ZoningLookup({ databasePath: options.databasePath })
196
+
197
+ try {
198
+ const agreement: AgreementRow[] = []
199
+
200
+ for (const point of options.points) {
201
+ const local = lookup.lookup(point.latitude, point.longitude)
202
+ const service = await readServiceContainment(options.readServiceFeatures, point.latitude, point.longitude)
203
+ const localInside = local.kind === ZoningReadingKind.Designated
204
+ const nearEdge = service.nearestEdgeMetres !== null && service.nearestEdgeMetres <= BOUNDARY_TOLERANCE_METRES
205
+
206
+ agreement.push({
207
+ label: point.label,
208
+ latitude: point.latitude,
209
+ longitude: point.longitude,
210
+ local,
211
+ serviceInside: service.inside,
212
+ serviceLocalCode: service.localCode,
213
+ result: localInside === service.inside ? "agree" : nearEdge ? "boundary_tolerance" : "disagree",
214
+ nearestEdgeMetres: service.nearestEdgeMetres,
215
+ })
216
+
217
+ options.onProgress?.(`${agreement.length}/${options.points.length} points compared`)
218
+ }
219
+
220
+ const outside: OutsideCoverageRow[] = []
221
+
222
+ for (const point of options.outsidePoints ?? OUTSIDE_PUBLICATION_POINTS) {
223
+ const reading = lookup.lookup(point.latitude, point.longitude)
224
+
225
+ outside.push({
226
+ label: point.label,
227
+ latitude: point.latitude,
228
+ longitude: point.longitude,
229
+ kind: reading.kind,
230
+ designations: reading.designations.length,
231
+ passed: reading.kind === ZoningReadingKind.Unknown && reading.designations.length === 0,
232
+ })
233
+ }
234
+
235
+ return {
236
+ agreement,
237
+ agreed: agreement.filter((row) => row.result === "agree").length,
238
+ disagreed: agreement.filter((row) => row.result === "disagree").length,
239
+ boundaryTolerance: agreement.filter((row) => row.result === "boundary_tolerance").length,
240
+ codeMismatches: agreement.filter(
241
+ (row) =>
242
+ row.result === "agree" &&
243
+ row.serviceInside &&
244
+ row.serviceLocalCode !== null &&
245
+ !row.local.designations.some((designation) => designation.localCode === row.serviceLocalCode)
246
+ ).length,
247
+ outside,
248
+ outsidePassed: outside.filter((row) => row.passed).length,
249
+ }
250
+ } finally {
251
+ lookup[Symbol.dispose]()
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Whether the service's own geometry contains the point, decided here with the same
257
+ * ring-role resolution and the same even-odd rule the artifact's reader uses.
258
+ * So what is compared is a verdict against a verdict.
259
+ */
260
+ async function readServiceContainment(
261
+ readServiceFeatures: ServiceFeatureReader,
262
+ latitude: number,
263
+ longitude: number
264
+ ): Promise<{ inside: boolean; localCode: string | null; nearestEdgeMetres: number | null }> {
265
+ const features = await readServiceFeatures(latitude, longitude)
266
+
267
+ let nearest = Infinity
268
+ let inside = false
269
+ let localCode: string | null = null
270
+
271
+ for (const feature of features) {
272
+ const geometry = feature.geometry
273
+
274
+ if (!geometry) continue
275
+
276
+ const raw: MultiPolygonRings =
277
+ geometry.type === "MultiPolygon"
278
+ ? (geometry.coordinates as MultiPolygonRings)
279
+ : [geometry.coordinates as PolygonRings]
280
+
281
+ const distance = nearestRingEdgeMetres(geometry, longitude, latitude)
282
+
283
+ if (distance < nearest) {
284
+ nearest = distance
285
+ }
286
+
287
+ // the service'S rings GET the same role resolution the ingest gave the archive'S.
288
+ // The publisher uses one convention on both channels, so reading this side as nested
289
+ // GeoJSON would answer "inside" for a point in a hole and report the artifact as
290
+ // wrong at exactly the locations the hole handling exists for.
291
+ const resolved = resolveRingRoles(raw, `service feature near ${latitude},${longitude}`)
292
+
293
+ // encoded and RE-read rather than RAY-cast directly, deliberately: the artifact answers
294
+ // through `pointInEncodedRings`, and running the service's geometry through a different
295
+ // predicate would compare two answers that were never asked the same question.
296
+ if (pointInEncodedRings(encodeRings(resolved.polygons), longitude, latitude)) {
297
+ inside = true
298
+
299
+ const code = feature.properties?.ZONE_ORIG
300
+
301
+ if (typeof code === "string" && !localCode) {
302
+ localCode = code
303
+ }
304
+ }
305
+ }
306
+
307
+ return {
308
+ inside,
309
+ localCode,
310
+ nearestEdgeMetres: Number.isFinite(nearest) ? nearest : null,
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Draw a reproducible sample of points from the artifact — interior points of
316
+ * stored polygons, spread across authorities.
317
+ *
318
+ * Spread across authorities rather than drawn from one, because 30 local authorities
319
+ * publish 581 distinct local codes between them and a sample from one would verify
320
+ * one authority's conversion while reporting on all of them.
321
+ * The stride discipline — keys chosen before any geometry is read, deterministic
322
+ * rather than random — is `strideSampleInteriorPoints`'s.
323
+ */
324
+ export function sampleAgreementPoints(
325
+ databasePath: PathBuilderLike,
326
+ options: { count?: number } = {}
327
+ ): Array<{ label: string; latitude: number; longitude: number; localCode: string }> {
328
+ const count = options.count ?? 48
329
+ using database = new DatabaseClient<ZoningDatabase>(databasePath, { readOnly: true })
330
+
331
+ // Sort by authority so the stride samples across the 30 authorities
332
+ // instead of sampling one authority repeatedly.
333
+ // A stride over `area_id` by itself would follow the publisher's feature numbering.
334
+ // That numbering is grouped by authority, so the stride would select only the
335
+ // authorities that fall on its positions.
336
+ const areaIDs = (
337
+ database.prepare("SELECT area_id FROM zoning_area ORDER BY jurisdiction_id, area_id").all() as Array<{
338
+ area_id: string
339
+ }>
340
+ ).map((row) => row.area_id)
341
+
342
+ const selectArea = database.prepare(
343
+ "SELECT area_id, jurisdiction_id, local_code, min_lat, min_lon, max_lat, max_lon, rings FROM zoning_area WHERE area_id = ?"
344
+ )
345
+
346
+ return strideSampleInteriorPoints(areaIDs, count, {
347
+ fetch: (key) =>
348
+ selectArea.get(key) as
349
+ | {
350
+ area_id: string
351
+ jurisdiction_id: string
352
+ local_code: string
353
+ min_lat: number
354
+ min_lon: number
355
+ max_lat: number
356
+ max_lon: number
357
+ rings: Uint8Array
358
+ }
359
+ | undefined,
360
+ gridSteps: 17,
361
+ toPoint: (area, interior) => ({
362
+ label: `${area.jurisdiction_id} ${stringifyJSON(area.local_code)} polygon ${area.area_id}`,
363
+ localCode: area.local_code,
364
+ ...interior,
365
+ }),
366
+ })
367
+ }
@@ -1,78 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * Acquire the Department's bulk GeoJSON export — a 247,452,342-byte file streamed to disk.
7
- *
8
- * THIS IS A FILE TRANSFER, NOT AN API REQUEST, AND IT KEEPS RAW `fetch` ON PURPOSE. The repo's rule sends
9
- * HTTP clients through `@mailwoman/core/api`'s `APIClient`, and the rule draws its line at what that class
10
- * is for: pacing, bounded retry, response caching and error mapping over small bodies and repeated calls.
11
- * None of it applies here. Caching a 247 MB body through a JSON-validating disk cache would write a second,
12
- * unreadable copy of a file already on disk; there is nothing to pace, because this runs once per product
13
- * vintage; and axios buffers any non-stream response type in memory. `packages/osm/lib/sdk/fetch.ts`,
14
- * `packages/tiger/lib/sdk/download.ts`, `packages/flood/lib/sdk/download.ts` and `packages/coastal/lib/sdk/download.ts`
15
- * are the existing transfers that say the same thing in the same place. The JOB that produces this URL, and
16
- * every other metadata read around it, DO go through `APIClient` — see `client.ts`.
17
- *
18
- * THE RESULT URL REDIRECTS AND THE FETCH MUST FOLLOW IT. The Hub download job answers
19
- * `{"status":"Completed","resultUrl":…}`; the result URL itself 302s to the generated file. Node's `fetch`
20
- * follows redirects by default, and the option is passed explicitly anyway — a transfer that stopped at the
21
- * redirect would write a short body to disk and report a successful download.
22
- *
23
- * FRESHNESS IS THE ITEM'S OWN MODIFIED DATE, NEVER A LENGTH PROBE. The cache is keyed on the vintage the
24
- * item declares, so a re-run against the same vintage never re-transfers and a new vintage never overwrites
25
- * the old one in place.
26
- */
27
-
28
- import { tryStat } from "@mailwoman/core/fs/readers"
29
- import { makeDirectories } from "@mailwoman/core/fs/writers"
30
- import { streamToDisk } from "@mailwoman/core/utils"
31
- import { join } from "path-ts"
32
-
33
- /**
34
- * The file name one vintage's export is kept under.
35
- */
36
- export const GZT_EXPORT_FILE = "gzt-current-plan.geojson"
37
-
38
- export interface DownloadZoningExportOptions {
39
- /**
40
- * The Hub job's `resultUrl`, read rather than assembled — it carries a generated file id with no relationship to the
41
- * item id, so a hard-coded URL survives a republish by pointing at a file that is no longer the product.
42
- */
43
- url: string
44
- /**
45
- * The product vintage the cache is keyed on.
46
- */
47
- vintage: string
48
- cacheRoot: string
49
- onProgress?: (message: string) => void
50
- }
51
-
52
- /**
53
- * Download the bulk export for one product vintage, returning the path of the GeoJSON file.
54
- *
55
- * Downloads to a `.part` file and renames only on a clean finish, so an interrupted transfer never presents as a
56
- * complete export — the same discipline the database build uses, for the same reason.
57
- */
58
- export async function downloadZoningExport(options: DownloadZoningExportOptions): Promise<string> {
59
- const vintageDir = join(options.cacheRoot, options.vintage)
60
- const exportPath = join(vintageDir, GZT_EXPORT_FILE)
61
-
62
- if (await tryStat(exportPath)) {
63
- options.onProgress?.(`export for ${options.vintage} already downloaded`)
64
-
65
- return exportPath
66
- }
67
-
68
- await makeDirectories(vintageDir)
69
-
70
- await streamToDisk({
71
- url: options.url,
72
- destination: exportPath,
73
- context: "zoning download",
74
- ...(options.onProgress ? { onProgress: options.onProgress } : {}),
75
- })
76
-
77
- return exportPath
78
- }
package/lib/sdk/index.ts DELETED
@@ -1,15 +0,0 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- * @file The acquisition + build surface for the Irish zoning layer. The READER is the package root.
6
- */
7
-
8
- export * from "#sdk/build-zoning"
9
- export * from "#sdk/cells"
10
- export * from "#sdk/client"
11
- export * from "#sdk/download"
12
- export * from "#sdk/ingest/index"
13
- export * from "#sdk/ingest/chunk"
14
- export * from "#sdk/measure-resolutions"
15
- export * from "#sdk/verify"