@mailwoman/soil 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.
- package/README.md +118 -116
- package/lib/index.ts +88 -113
- package/lib/paths.ts +24 -0
- package/lib/schema.ts +189 -134
- package/lib/vocabulary.ts +40 -107
- package/out/index.d.ts +59 -77
- package/out/index.d.ts.map +1 -1
- package/out/index.js +38 -79
- package/out/index.js.map +1 -1
- package/out/paths.d.ts +19 -0
- package/out/paths.d.ts.map +1 -0
- package/out/paths.js +21 -0
- package/out/paths.js.map +1 -0
- package/out/schema.d.ts +183 -132
- package/out/schema.d.ts.map +1 -1
- package/out/schema.js +32 -32
- package/out/schema.js.map +1 -1
- package/out/sdk/acquire.d.ts +15 -24
- package/out/sdk/acquire.d.ts.map +1 -1
- package/out/sdk/acquire.js +6 -21
- package/out/sdk/acquire.js.map +1 -1
- package/out/sdk/build-soil.d.ts +67 -70
- package/out/sdk/build-soil.d.ts.map +1 -1
- package/out/sdk/build-soil.js +67 -97
- package/out/sdk/build-soil.js.map +1 -1
- package/out/sdk/cell-tiers.d.ts +7 -19
- package/out/sdk/cell-tiers.d.ts.map +1 -1
- package/out/sdk/cell-tiers.js +21 -40
- package/out/sdk/cell-tiers.js.map +1 -1
- package/out/sdk/cells.d.ts +15 -41
- package/out/sdk/cells.d.ts.map +1 -1
- package/out/sdk/cells.js +11 -38
- package/out/sdk/cells.js.map +1 -1
- package/out/sdk/client.d.ts +24 -49
- package/out/sdk/client.d.ts.map +1 -1
- package/out/sdk/client.js +20 -64
- package/out/sdk/client.js.map +1 -1
- package/out/sdk/download.d.ts +24 -47
- package/out/sdk/download.d.ts.map +1 -1
- package/out/sdk/download.js +15 -55
- package/out/sdk/download.js.map +1 -1
- package/out/sdk/ingest/chunk.d.ts +29 -25
- package/out/sdk/ingest/chunk.d.ts.map +1 -1
- package/out/sdk/ingest/chunk.js +18 -16
- package/out/sdk/ingest/chunk.js.map +1 -1
- package/out/sdk/ingest/worker.d.ts +9 -0
- package/out/sdk/ingest/worker.d.ts.map +1 -0
- package/out/{scripts/ingest-chunk.js → sdk/ingest/worker.js} +15 -14
- package/out/sdk/ingest/worker.js.map +1 -0
- package/out/sdk/ingest.d.ts +122 -0
- package/out/sdk/ingest.d.ts.map +1 -0
- package/out/sdk/ingest.js +127 -0
- package/out/sdk/ingest.js.map +1 -0
- package/out/sdk/measure-resolutions.d.ts +6 -17
- package/out/sdk/measure-resolutions.d.ts.map +1 -1
- package/out/sdk/measure-resolutions.js +4 -16
- package/out/sdk/measure-resolutions.js.map +1 -1
- package/out/sdk/reduce.d.ts +33 -59
- package/out/sdk/reduce.d.ts.map +1 -1
- package/out/sdk/reduce.js +48 -79
- package/out/sdk/reduce.js.map +1 -1
- package/out/sdk/survey-area.d.ts +16 -48
- package/out/sdk/survey-area.d.ts.map +1 -1
- package/out/sdk/survey-area.js +35 -79
- package/out/sdk/survey-area.js.map +1 -1
- package/out/sdk/tabular.d.ts +32 -31
- package/out/sdk/tabular.d.ts.map +1 -1
- package/out/sdk/tabular.js +57 -49
- package/out/sdk/tabular.js.map +1 -1
- package/out/sdk/test-kit.d.ts +57 -0
- package/out/sdk/test-kit.d.ts.map +1 -0
- package/out/{test-kit.js → sdk/test-kit.js} +18 -39
- package/out/sdk/test-kit.js.map +1 -0
- package/out/sdk/verify.d.ts +17 -53
- package/out/sdk/verify.d.ts.map +1 -1
- package/out/sdk/verify.js +27 -85
- package/out/sdk/verify.js.map +1 -1
- package/out/vocabulary.d.ts +37 -103
- package/out/vocabulary.d.ts.map +1 -1
- package/out/vocabulary.js +34 -107
- package/out/vocabulary.js.map +1 -1
- package/package.json +37 -191
- package/{lib/sdk → sdk}/acquire.ts +18 -28
- package/{lib/sdk → sdk}/build-soil.ts +116 -130
- package/{lib/sdk → sdk}/cell-tiers.ts +22 -41
- package/{lib/sdk → sdk}/cells.ts +17 -43
- package/sdk/client.ts +147 -0
- package/sdk/download.ts +131 -0
- package/{lib/sdk → sdk}/ingest/chunk.ts +35 -27
- package/{lib/scripts/ingest-chunk.ts → sdk/ingest/worker.ts} +14 -13
- package/sdk/ingest.ts +255 -0
- package/{lib/sdk → sdk}/measure-resolutions.ts +6 -17
- package/sdk/reduce.ts +344 -0
- package/{lib/sdk → sdk}/survey-area.ts +49 -88
- package/{lib/sdk → sdk}/tabular.ts +61 -52
- package/{lib → sdk}/test-kit.ts +19 -41
- package/sdk/verify.ts +313 -0
- package/lib/sdk/client.ts +0 -184
- package/lib/sdk/download.ts +0 -161
- package/lib/sdk/index.ts +0 -20
- package/lib/sdk/ingest/index.ts +0 -278
- package/lib/sdk/reduce.ts +0 -375
- package/lib/sdk/verify.ts +0 -369
- package/out/scripts/ingest-chunk.d.ts +0 -11
- package/out/scripts/ingest-chunk.d.ts.map +0 -1
- package/out/scripts/ingest-chunk.js.map +0 -1
- package/out/sdk/index.d.ts +0 -20
- package/out/sdk/index.d.ts.map +0 -1
- package/out/sdk/index.js +0 -20
- package/out/sdk/index.js.map +0 -1
- package/out/sdk/ingest/index.d.ts +0 -132
- package/out/sdk/ingest/index.d.ts.map +0 -1
- package/out/sdk/ingest/index.js +0 -170
- package/out/sdk/ingest/index.js.map +0 -1
- package/out/test-kit.d.ts +0 -79
- package/out/test-kit.d.ts.map +0 -1
- package/out/test-kit.js.map +0 -1
package/sdk/verify.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
decodeRings,
|
|
9
|
+
interiorPointOfEncodedRings,
|
|
10
|
+
pointInEncodedRings,
|
|
11
|
+
segmentDistanceMetres,
|
|
12
|
+
shortCellToInt,
|
|
13
|
+
type H3Cell,
|
|
14
|
+
} from "@mailwoman/spatial"
|
|
15
|
+
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
16
|
+
import { cellToParent, latLngToCell } from "h3-js"
|
|
17
|
+
import type { PathBuilderLike } from "path-ts"
|
|
18
|
+
|
|
19
|
+
import { SoilCapabilityLookup, SoilReadingKind } from "#index"
|
|
20
|
+
import type { SoilDatabase } from "#schema"
|
|
21
|
+
import type { SoilDataAccessClient } from "#sdk/client"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* One comparison row for a point.
|
|
25
|
+
* It records both verdicts and whether they agree.
|
|
26
|
+
*/
|
|
27
|
+
export interface SoilAgreementRow {
|
|
28
|
+
label: string
|
|
29
|
+
latitude: number
|
|
30
|
+
longitude: number
|
|
31
|
+
/**
|
|
32
|
+
* The map unit the artifact's own geometry puts here.
|
|
33
|
+
*/
|
|
34
|
+
localMukey: string | null
|
|
35
|
+
/**
|
|
36
|
+
* The map unit Soil Data Access puts here.
|
|
37
|
+
*/
|
|
38
|
+
serviceMukey: string | null
|
|
39
|
+
result: "agree" | "disagree" | "boundary_tolerance"
|
|
40
|
+
/**
|
|
41
|
+
* Meters from the point to the nearest edge of the delineation the artifact matched.
|
|
42
|
+
*
|
|
43
|
+
* Included on every row because it separates a real defect from two channels
|
|
44
|
+
* rendering the same edge differently.
|
|
45
|
+
*/
|
|
46
|
+
nearestEdgeMetres: number | null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The negative half: a point the authority's published surveys do not reach.
|
|
51
|
+
*/
|
|
52
|
+
export interface SoilOutsideRow {
|
|
53
|
+
label: string
|
|
54
|
+
latitude: number
|
|
55
|
+
longitude: number
|
|
56
|
+
kind: SoilReadingKind
|
|
57
|
+
/**
|
|
58
|
+
* True when the artifact answered `unknown`, the only acceptable reading outside the built survey areas.
|
|
59
|
+
*/
|
|
60
|
+
passed: boolean
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface VerifySoilResult {
|
|
64
|
+
agreement: SoilAgreementRow[]
|
|
65
|
+
agreed: number
|
|
66
|
+
disagreed: number
|
|
67
|
+
boundaryTolerance: number
|
|
68
|
+
outside: SoilOutsideRow[]
|
|
69
|
+
outsidePassed: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Points outside the pilot region.
|
|
74
|
+
*
|
|
75
|
+
* Every neighboring state is included because a footprint clipped to "the
|
|
76
|
+
* Midwest" would pass a one-state check.
|
|
77
|
+
*/
|
|
78
|
+
export const OUTSIDE_PILOT_POINTS: ReadonlyArray<{ label: string; latitude: number; longitude: number }> = [
|
|
79
|
+
{ label: "Lincoln, Nebraska", latitude: 40.8136, longitude: -96.7026 },
|
|
80
|
+
{ label: "Omaha, Nebraska (Iowa border)", latitude: 41.2565, longitude: -95.9345 },
|
|
81
|
+
{ label: "Minneapolis, Minnesota", latitude: 44.9778, longitude: -93.265 },
|
|
82
|
+
{ label: "Albert Lea, Minnesota (Iowa border)", latitude: 43.6478, longitude: -93.3683 },
|
|
83
|
+
{ label: "Madison, Wisconsin", latitude: 43.0731, longitude: -89.4012 },
|
|
84
|
+
{ label: "Rockford, Illinois", latitude: 42.2711, longitude: -89.094 },
|
|
85
|
+
{ label: "Kansas City, Missouri", latitude: 39.0997, longitude: -94.5786 },
|
|
86
|
+
{ label: "Sioux Falls, South Dakota", latitude: 43.5446, longitude: -96.7311 },
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One meter, far below the median delineation, so a disagreement within it is the two
|
|
91
|
+
* channels rendering the same edge differently rather than a conversion defect.
|
|
92
|
+
*/
|
|
93
|
+
const BOUNDARY_TOLERANCE_METRES = 1
|
|
94
|
+
|
|
95
|
+
export interface VerifySoilOptions {
|
|
96
|
+
databasePath: PathBuilderLike
|
|
97
|
+
client: Pick<SoilDataAccessClient, "mukeyAtPoint">
|
|
98
|
+
/**
|
|
99
|
+
* Points to re-ask the service about, sampled from the artifact — see {@link sampleAgreementPoints}.
|
|
100
|
+
*/
|
|
101
|
+
points: ReadonlyArray<{ label: string; latitude: number; longitude: number }>
|
|
102
|
+
outsidePoints?: ReadonlyArray<{ label: string; latitude: number; longitude: number }>
|
|
103
|
+
onProgress?: (message: string) => void
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Run both halves.
|
|
108
|
+
*/
|
|
109
|
+
export async function verifySoilDatabase(options: VerifySoilOptions): Promise<VerifySoilResult> {
|
|
110
|
+
const database = new DatabaseClient<SoilDatabase>(options.databasePath, { readOnly: true })
|
|
111
|
+
const lookup = new SoilCapabilityLookup({ databasePath: options.databasePath })
|
|
112
|
+
|
|
113
|
+
// Read once: the stored index is mixed-resolution, so a probe that assumed one
|
|
114
|
+
// resolution would read every row at the others as an absence.
|
|
115
|
+
const resolutions = (
|
|
116
|
+
database.prepare("SELECT DISTINCT resolution FROM soil_map_unit_cell ORDER BY resolution").all() as Array<{
|
|
117
|
+
resolution: number
|
|
118
|
+
}>
|
|
119
|
+
).map((row) => row.resolution)
|
|
120
|
+
|
|
121
|
+
const { indexResolution } = lookup.identity
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
const agreement: SoilAgreementRow[] = []
|
|
125
|
+
|
|
126
|
+
for (const point of options.points) {
|
|
127
|
+
const local = localDelineationAt(database, resolutions, indexResolution, point.latitude, point.longitude)
|
|
128
|
+
const serviceMukey = (await options.client.mukeyAtPoint(point.latitude, point.longitude)) ?? null
|
|
129
|
+
|
|
130
|
+
const nearEdge = local.nearestEdgeMetres !== null && local.nearestEdgeMetres <= BOUNDARY_TOLERANCE_METRES
|
|
131
|
+
|
|
132
|
+
agreement.push({
|
|
133
|
+
...point,
|
|
134
|
+
localMukey: local.mukey,
|
|
135
|
+
serviceMukey,
|
|
136
|
+
result: local.mukey === serviceMukey ? "agree" : nearEdge ? "boundary_tolerance" : "disagree",
|
|
137
|
+
nearestEdgeMetres: local.nearestEdgeMetres,
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
options.onProgress?.(`${agreement.length}/${options.points.length} points compared`)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const outside: SoilOutsideRow[] = []
|
|
144
|
+
|
|
145
|
+
for (const point of options.outsidePoints ?? OUTSIDE_PILOT_POINTS) {
|
|
146
|
+
const reading = lookup.lookup(point.latitude, point.longitude)
|
|
147
|
+
|
|
148
|
+
outside.push({ ...point, kind: reading.kind, passed: reading.kind === SoilReadingKind.Unknown })
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
agreement,
|
|
153
|
+
agreed: agreement.filter((row) => row.result === "agree").length,
|
|
154
|
+
disagreed: agreement.filter((row) => row.result === "disagree").length,
|
|
155
|
+
boundaryTolerance: agreement.filter((row) => row.result === "boundary_tolerance").length,
|
|
156
|
+
outside,
|
|
157
|
+
outsidePassed: outside.filter((row) => row.passed).length,
|
|
158
|
+
}
|
|
159
|
+
} finally {
|
|
160
|
+
lookup[Symbol.dispose]()
|
|
161
|
+
await database.destroy()
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The candidate delineations reaching a point, found through the cell index
|
|
167
|
+
* because a bounding-box scan over `soil_map_unit_area` is a full table scan. every
|
|
168
|
+
* stored resolution is probed since the tier is compacted parent-ward.
|
|
169
|
+
*/
|
|
170
|
+
function candidateDelineations(
|
|
171
|
+
database: DatabaseClient<SoilDatabase>,
|
|
172
|
+
resolutions: readonly number[],
|
|
173
|
+
indexResolution: number,
|
|
174
|
+
latitude: number,
|
|
175
|
+
longitude: number
|
|
176
|
+
): Array<{ mukey: string; rings: Uint8Array }> {
|
|
177
|
+
const selectCandidates = database.prepare(
|
|
178
|
+
"SELECT c.area_id AS area_id, a.mukey AS mukey, a.rings AS rings FROM soil_map_unit_cell c " +
|
|
179
|
+
"JOIN soil_map_unit_area a ON a.area_id = c.area_id WHERE c.h3_cell = ?"
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
const indexCell = latLngToCell(latitude, longitude, indexResolution) as H3Cell
|
|
183
|
+
const seen = new Set<string>()
|
|
184
|
+
const candidates: Array<{ mukey: string; rings: Uint8Array }> = []
|
|
185
|
+
|
|
186
|
+
for (const resolution of resolutions) {
|
|
187
|
+
const cell = resolution === indexResolution ? indexCell : (cellToParent(indexCell, resolution) as H3Cell)
|
|
188
|
+
|
|
189
|
+
for (const row of selectCandidates.all(shortCellToInt(cell)) as Array<{
|
|
190
|
+
area_id: string
|
|
191
|
+
mukey: string
|
|
192
|
+
rings: Uint8Array
|
|
193
|
+
}>) {
|
|
194
|
+
// Dedupe on the delineation rather than its map unit, since two different delineations
|
|
195
|
+
// of one map unit cover different ground and both must be tested.
|
|
196
|
+
if (seen.has(row.area_id)) continue
|
|
197
|
+
|
|
198
|
+
seen.add(row.area_id)
|
|
199
|
+
candidates.push({ mukey: row.mukey, rings: row.rings })
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return candidates
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The map unit that the artifact's geometry places at a point.
|
|
208
|
+
*
|
|
209
|
+
* Also records the distance to that delineation's nearest edge.
|
|
210
|
+
*/
|
|
211
|
+
function localDelineationAt(
|
|
212
|
+
database: DatabaseClient<SoilDatabase>,
|
|
213
|
+
resolutions: readonly number[],
|
|
214
|
+
indexResolution: number,
|
|
215
|
+
latitude: number,
|
|
216
|
+
longitude: number
|
|
217
|
+
): { mukey: string | null; nearestEdgeMetres: number | null } {
|
|
218
|
+
const candidates = candidateDelineations(database, resolutions, indexResolution, latitude, longitude)
|
|
219
|
+
|
|
220
|
+
let mukey: string | null = null
|
|
221
|
+
let nearest = Infinity
|
|
222
|
+
|
|
223
|
+
for (const candidate of candidates) {
|
|
224
|
+
const distance = nearestEdgeDistance(candidate.rings, longitude, latitude)
|
|
225
|
+
|
|
226
|
+
if (distance < nearest) {
|
|
227
|
+
nearest = distance
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!mukey && pointInEncodedRings(candidate.rings, longitude, latitude)) {
|
|
231
|
+
mukey = candidate.mukey
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return { mukey, nearestEdgeMetres: Number.isFinite(nearest) ? nearest : null }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Meters from a point to the nearest edge of an encoded ring set.
|
|
240
|
+
*
|
|
241
|
+
* This function decodes here because it runs once per verification rather than once per geocode.
|
|
242
|
+
*/
|
|
243
|
+
function nearestEdgeDistance(blob: Uint8Array, lon: number, lat: number): number {
|
|
244
|
+
const { polygons } = decodeRings(blob)
|
|
245
|
+
|
|
246
|
+
let nearest = Infinity
|
|
247
|
+
|
|
248
|
+
for (const rings of polygons) {
|
|
249
|
+
for (const ring of rings) {
|
|
250
|
+
for (let index = 2; index < ring.length; index += 2) {
|
|
251
|
+
const distance = segmentDistanceMetres(
|
|
252
|
+
lon,
|
|
253
|
+
lat,
|
|
254
|
+
[ring[index - 2]!, ring[index - 1]!],
|
|
255
|
+
[ring[index]!, ring[index + 1]!]
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if (distance < nearest) {
|
|
259
|
+
nearest = distance
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return nearest
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Draw a reproducible sample of points from the artifact, a deterministic stride
|
|
270
|
+
* over the primary key so a re-run compares the same points.
|
|
271
|
+
*/
|
|
272
|
+
export function sampleAgreementPoints(
|
|
273
|
+
databasePath: PathBuilderLike,
|
|
274
|
+
options: { count?: number } = {}
|
|
275
|
+
): Array<{ label: string; latitude: number; longitude: number }> {
|
|
276
|
+
const count = options.count ?? 60
|
|
277
|
+
using database = new DatabaseClient<SoilDatabase>(databasePath, { readOnly: true })
|
|
278
|
+
|
|
279
|
+
const total = (database.prepare("SELECT count(*) AS n FROM soil_map_unit_area").get() as { n: number }).n
|
|
280
|
+
const stride = Math.max(1, Math.floor(total / Math.max(1, count)))
|
|
281
|
+
|
|
282
|
+
// Use one offset probe per sample point instead of materializing a key list.
|
|
283
|
+
// That list would retain every primary key to select sixty points.
|
|
284
|
+
const selectByOffset = database.prepare(
|
|
285
|
+
"SELECT area_id, mukey, min_lat, min_lon, max_lat, max_lon, rings FROM soil_map_unit_area ORDER BY area_id LIMIT 1 OFFSET ?"
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
const points: Array<{ label: string; latitude: number; longitude: number }> = []
|
|
289
|
+
|
|
290
|
+
for (let index = 0; index < total && points.length < count; index += stride) {
|
|
291
|
+
const area = selectByOffset.get(index) as
|
|
292
|
+
| {
|
|
293
|
+
area_id: string
|
|
294
|
+
mukey: string
|
|
295
|
+
min_lat: number
|
|
296
|
+
min_lon: number
|
|
297
|
+
max_lat: number
|
|
298
|
+
max_lon: number
|
|
299
|
+
rings: Uint8Array
|
|
300
|
+
}
|
|
301
|
+
| undefined
|
|
302
|
+
|
|
303
|
+
if (!area) continue
|
|
304
|
+
|
|
305
|
+
const interior = interiorPointOfEncodedRings(area, 7)
|
|
306
|
+
|
|
307
|
+
if (!interior) continue
|
|
308
|
+
|
|
309
|
+
points.push({ label: `map unit ${area.mukey} delineation ${area.area_id}`, ...interior })
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return points
|
|
313
|
+
}
|
package/lib/sdk/client.ts
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Soil Data Access — NRCS's live SQL service, and the two things this layer asks it: which survey areas
|
|
7
|
-
* exist with what version date, and which map unit covers a point.
|
|
8
|
-
*
|
|
9
|
-
* THIS IS AN API REQUEST AND IT GOES THROUGH {@linkcode APIClient}. Small bodies, repeated calls, a
|
|
10
|
-
* third-party host with a server-side query timeout and no published rate limit — the pacing, bounded
|
|
11
|
-
* retry, response caching and `ResourceError` mapping are exactly what it needs. The survey-area
|
|
12
|
-
* ARCHIVES are not: they are 13 to 41 MB file transfers, they stream to disk on raw `fetch`, and
|
|
13
|
-
* `download.ts` says so in place.
|
|
14
|
-
*
|
|
15
|
-
* FAILURES COME BACK AS XML, INCLUDING ON A TIMEOUT, AND A JSON-ONLY PARSER MIS-READS THEM. A bad column,
|
|
16
|
-
* a blocked query and a query that exceeded the server's own timeout all return an OGC
|
|
17
|
-
* `ServiceExceptionReport` document. Measured messages: `Invalid query: Invalid column name
|
|
18
|
-
* 'nosuchcolumn'.` (HTTP 400), `Invalid query - access denied.`, and `Your query timed out.` — and the
|
|
19
|
-
* last one arrives on an HTTP 200. So every response is read as TEXT and checked for the report before
|
|
20
|
-
* anything tries to parse it as JSON. A client that branched on the status code alone would read a
|
|
21
|
-
* timeout as a successful empty answer, which is the exact shape of lie this program keeps writing down.
|
|
22
|
-
*
|
|
23
|
-
* SCHEMA INTROSPECTION IS REFUSED, SO THE COLUMN NAMES ARE THE PUBLISHED DATA DICTIONARY'S.
|
|
24
|
-
* `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS` answers `Invalid query - access denied.` The
|
|
25
|
-
* columns this file names were each verified by querying them successfully.
|
|
26
|
-
*
|
|
27
|
-
* FRESHNESS IS `sacatalog.saverest` AND NEVER A LENGTH PROBE. The download host answers `HEAD` with HTTP
|
|
28
|
-
* 405 and IGNORES `Range` — a request with `Range: bytes=0-0` returned HTTP 200 and transferred the whole
|
|
29
|
-
* 27,598,377 bytes — so "just check the size" starts a real download. The tabular service answers the
|
|
30
|
-
* freshness question directly instead, and the version date it returns is what the archive's filename
|
|
31
|
-
* embeds.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
import { APIClient, type APIClientConfig, type ClockLike, assertNoOGCServiceException } from "@mailwoman/core/api"
|
|
35
|
-
import { buildDiskStorage } from "@mailwoman/core/api/disk-storage"
|
|
36
|
-
import { dataRootPath } from "@mailwoman/core/data-root"
|
|
37
|
-
import { parseJSONStrict } from "@mailwoman/core/json"
|
|
38
|
-
|
|
39
|
-
import { saverestToISODate } from "#sdk/tabular"
|
|
40
|
-
|
|
41
|
-
// Re-exported so a caller branching on this client's failures needs exactly one import.
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The tabular endpoint. Anonymous: no key, no account, and no rate-limit header on any observed response.
|
|
45
|
-
*/
|
|
46
|
-
export const SDA_POST_REST_URL = "https://sdmdataaccess.nrcs.usda.gov/Tabular/post.rest"
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Minimum spacing between Soil Data Access requests, in milliseconds.
|
|
50
|
-
*
|
|
51
|
-
* NRCS publishes no rate limit for this service and returned no rate-limit header on any request, so this is courtesy
|
|
52
|
-
* pacing rather than a published ceiling — stated as such rather than dressed up as a measured limit. It costs an
|
|
53
|
-
* acquisition run nothing: a whole-state build makes one catalogue call, and the verification's per-point calls are
|
|
54
|
-
* measured at 1.8 s each anyway.
|
|
55
|
-
*/
|
|
56
|
-
export const SDA_MIN_REQUEST_INTERVAL_MS = 500
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* How long a cached Soil Data Access response stays fresh.
|
|
60
|
-
*
|
|
61
|
-
* Twelve hours, chosen against the product's cadence rather than a wall-clock intuition: NRCS performs ONE coordinated
|
|
62
|
-
* Annual Soils Refresh, on October 1. Grouping `sacatalog` by year of `saverest` returns 2016: 1, 2025: 3,323, 2026: 56
|
|
63
|
-
* — 98.3% of survey areas carry a single version date from one refresh rather than a per-area drift. A shorter TTL buys
|
|
64
|
-
* nothing.
|
|
65
|
-
*/
|
|
66
|
-
const SDA_CACHE_TTL_MS = 12 * 60 * 60 * 1000
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* One published survey area, as the catalogue reports it.
|
|
70
|
-
*/
|
|
71
|
-
export interface SurveyAreaCatalogEntry {
|
|
72
|
-
areasymbol: string
|
|
73
|
-
areaname: string
|
|
74
|
-
/**
|
|
75
|
-
* The version-established date as an ISO date — what the archive's filename embeds.
|
|
76
|
-
*/
|
|
77
|
-
saverest: string
|
|
78
|
-
saversion: number
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* A client for Soil Data Access.
|
|
83
|
-
*/
|
|
84
|
-
export class SoilDataAccessClient extends APIClient<APIClientConfig> {
|
|
85
|
-
/**
|
|
86
|
-
* Run one query and return its rows.
|
|
87
|
-
*
|
|
88
|
-
* @throws {OGCServiceError} When the service answers with an exception report — including on an HTTP 200, which is
|
|
89
|
-
* what a server-side timeout does.
|
|
90
|
-
*/
|
|
91
|
-
public async query(sql: string): Promise<string[][]> {
|
|
92
|
-
const { data } = await this.fetch<string>({
|
|
93
|
-
method: "POST",
|
|
94
|
-
url: SDA_POST_REST_URL,
|
|
95
|
-
// TEXT, not JSON, and that is the whole trap. A JSON response type hands a failure body to a JSON parser,
|
|
96
|
-
// which either throws something unrelated to what went wrong or — on a 200 — yields nothing at all.
|
|
97
|
-
responseType: "text",
|
|
98
|
-
headers: { "Content-Type": "application/json" },
|
|
99
|
-
data: { SERVICE: "query", FORMAT: "JSON", QUERY: sql },
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
assertNoOGCServiceException(data, `soil data access (query: ${sql.slice(0, 200)})`)
|
|
103
|
-
|
|
104
|
-
const parsed = parseJSONStrict<{ Table?: unknown }>(data)
|
|
105
|
-
|
|
106
|
-
// An answer with NO rows is `{}` rather than `{"Table":[]}`, so an absent `Table` is a real empty result and not a
|
|
107
|
-
// read failure — the exception check above has already separated the two.
|
|
108
|
-
if (parsed.Table === undefined) return []
|
|
109
|
-
|
|
110
|
-
if (!Array.isArray(parsed.Table)) {
|
|
111
|
-
throw new TypeError(
|
|
112
|
-
`soil data access: the service answered with a Table that is not an array (${typeof parsed.Table}) — the response format changed`
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return parsed.Table.map((row) => (row as unknown[]).map((value) => (value === null ? "" : String(value))))
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* The published survey areas whose symbol starts with `prefix` — a state code for a state-scoped build, or a whole
|
|
121
|
-
* symbol for the single-area rung.
|
|
122
|
-
*
|
|
123
|
-
* @throws {Error} When the catalogue returns nothing. An empty catalogue for a prefix a caller named is either a typo
|
|
124
|
-
* or a service change, and building zero survey areas while reporting success is the shape this refuses.
|
|
125
|
-
*/
|
|
126
|
-
public async readSurveyAreaCatalog(prefix: string): Promise<SurveyAreaCatalogEntry[]> {
|
|
127
|
-
const escaped = prefix.replaceAll("'", "''")
|
|
128
|
-
|
|
129
|
-
const rows = await this.query(
|
|
130
|
-
`SELECT areasymbol, areaname, saverest, saversion FROM sacatalog WHERE areasymbol LIKE '${escaped}%' ORDER BY areasymbol`
|
|
131
|
-
)
|
|
132
|
-
|
|
133
|
-
if (!rows.length) {
|
|
134
|
-
throw new Error(
|
|
135
|
-
`soil data access: the catalogue holds no survey area whose symbol starts with ${JSON.stringify(prefix)} — a build over an empty set would report success having written nothing`
|
|
136
|
-
)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return rows.map((row) => ({
|
|
140
|
-
areasymbol: row[0]!,
|
|
141
|
-
areaname: row[1]!,
|
|
142
|
-
saverest: saverestToISODate(row[2]!),
|
|
143
|
-
saversion: Number(row[3]),
|
|
144
|
-
}))
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Which map unit the service's OWN geometry assigns at a point, or `undefined` where it assigns none.
|
|
149
|
-
*
|
|
150
|
-
* This is the second path the built artifact is checked against: same authority, different distribution channel, and
|
|
151
|
-
* geometry this package has never touched. Measured at 1.807 s per point, so a few hundred points is minutes.
|
|
152
|
-
*/
|
|
153
|
-
public async mukeyAtPoint(latitude: number, longitude: number): Promise<string | undefined> {
|
|
154
|
-
const rows = await this.query(
|
|
155
|
-
`SELECT mukey FROM SDA_Get_Mukey_from_intersection_with_WktWgs84('point(${longitude} ${latitude})')`
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
return rows[0]?.[0] || undefined
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export interface CreateSoilDataAccessClientOptions {
|
|
163
|
-
clock?: ClockLike
|
|
164
|
-
cacheDirectory?: string
|
|
165
|
-
minRequestIntervalMs?: number
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Build a {@link SoilDataAccessClient} with the disk cache and pacing this package's acquisition path expects.
|
|
170
|
-
*/
|
|
171
|
-
export function createSoilDataAccessClient(options: CreateSoilDataAccessClientOptions = {}): SoilDataAccessClient {
|
|
172
|
-
return new SoilDataAccessClient({
|
|
173
|
-
displayName: "SoilDataAccess",
|
|
174
|
-
minRequestIntervalMs: options.minRequestIntervalMs ?? SDA_MIN_REQUEST_INTERVAL_MS,
|
|
175
|
-
retry: true,
|
|
176
|
-
...(options.clock ? { clock: options.clock } : {}),
|
|
177
|
-
caching: {
|
|
178
|
-
ttl: SDA_CACHE_TTL_MS,
|
|
179
|
-
storage: buildDiskStorage({
|
|
180
|
-
directory: options.cacheDirectory ?? String(dataRootPath("soil", "cache", "http")),
|
|
181
|
-
}),
|
|
182
|
-
},
|
|
183
|
-
})
|
|
184
|
-
}
|
package/lib/sdk/download.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* Acquire one survey area's published archive — 13 to 41 MB streamed to disk and unzipped.
|
|
7
|
-
*
|
|
8
|
-
* THE TRANSFER ITSELF LIVES IN `@mailwoman/core/utils`, and `streamToDisk` carries why a file transfer of
|
|
9
|
-
* this size keeps raw `fetch` instead of going through `APIClient`, plus the `.part`-rename rule. What is
|
|
10
|
-
* soil's, and stays here, is the URL shape, the cache key, and the two facts below that the shared
|
|
11
|
-
* transfer is told rather than assumes: the progress stride and what a 400 means. The METADATA reads
|
|
12
|
-
* around this one do go through `APIClient` — see `client.ts`.
|
|
13
|
-
*
|
|
14
|
-
* FRESHNESS IS `sacatalog.saverest`, NEVER A LENGTH PROBE, AND THE HOST LEAVES NO CHOICE. It answers `HEAD`
|
|
15
|
-
* with HTTP 405 (`allow: GET`) and IGNORES `Range`: a request with `Range: bytes=0-0` returned HTTP 200 and
|
|
16
|
-
* transferred the whole 27,598,377 bytes in 7.23 s. So "check the size first" starts a real download. The
|
|
17
|
-
* cache is keyed on the version date the tabular service reports instead, and a vintage already on disk is
|
|
18
|
-
* never re-fetched. The `Range` behaviour is PATH-SPECIFIC rather than host-wide — `/DataAvailability/`
|
|
19
|
-
* does answer 206 — so a client must probe per path rather than conclude from one.
|
|
20
|
-
*
|
|
21
|
-
* THE FILENAME EMBEDS THE VERSION DATE AND A WRONG ONE IS AN HTTP 400. Not a 404: asking for a date the
|
|
22
|
-
* host does not hold reads as a malformed request rather than a missing file, which is why the date comes
|
|
23
|
-
* from the catalogue rather than from a guess. The square brackets must be sent literally, so the URL is
|
|
24
|
-
* built with them percent-encoded.
|
|
25
|
-
*
|
|
26
|
-
* TWO CACHE VARIANTS EXIST AND THE BARE ONE IS WANTED. `wss_SSA_IA153_[2025-09-09].zip` is 25,474,922 bytes;
|
|
27
|
-
* `wss_SSA_IA153_soildb_IA_2003_[2025-09-09].zip` is 27,598,377 and differs only by an EMPTY Microsoft Access
|
|
28
|
-
* template container for a workflow this program does not use. Confirmed on a second area (`IA015`:
|
|
29
|
-
* 38,981,269 against 41,104,724 bytes) and on a third that ships no template at all (`TX299`, 13,455,641
|
|
30
|
-
* bytes, 97 files, no `.mdb`).
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import { tryStat } from "@mailwoman/core/fs/readers"
|
|
34
|
-
import { makeDirectories } from "@mailwoman/core/fs/writers"
|
|
35
|
-
import { runFile } from "@mailwoman/core/process"
|
|
36
|
-
import { streamToDisk } from "@mailwoman/core/utils"
|
|
37
|
-
import { join } from "path-ts"
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The download service's survey-area cache. Documented at `https://websoilsurvey.sc.egov.usda.gov/DSD/Download/help`,
|
|
41
|
-
* which lists `GET /{CacheName}/{FileName}`.
|
|
42
|
-
*/
|
|
43
|
-
export const WSS_SSA_CACHE_URL = "https://websoilsurvey.sc.egov.usda.gov/DSD/Download/Cache/SSA"
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The archive URL for one survey area at one version date.
|
|
47
|
-
*
|
|
48
|
-
* The brackets are percent-encoded rather than sent raw: they are not valid in a URL path, and a client that sends them
|
|
49
|
-
* literally depends on the fetcher tolerating them.
|
|
50
|
-
*/
|
|
51
|
-
export function surveyAreaArchiveURL(areaSymbol: string, versionDate: string): string {
|
|
52
|
-
return `${WSS_SSA_CACHE_URL}/wss_SSA_${areaSymbol}_%5B${versionDate}%5D.zip`
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface DownloadSurveyAreaOptions {
|
|
56
|
-
areaSymbol: string
|
|
57
|
-
/**
|
|
58
|
-
* The version date from `sacatalog.saverest`, as `YYYY-MM-DD`.
|
|
59
|
-
*/
|
|
60
|
-
versionDate: string
|
|
61
|
-
/**
|
|
62
|
-
* Where vintages are kept. Each version date gets its own directory, so a new refresh never overwrites the old one in
|
|
63
|
-
* place and a re-run against the same vintage never re-transfers.
|
|
64
|
-
*/
|
|
65
|
-
cacheRoot: string
|
|
66
|
-
onProgress?: (message: string) => void
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Bytes between progress reports. Smaller than the shared default because these archives are 13–41 MB, and the default
|
|
71
|
-
* stride would leave the smallest of them reporting once.
|
|
72
|
-
*/
|
|
73
|
-
const PROGRESS_STRIDE_BYTES = 8 * 1024 * 1024
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* What this host answers for a version date it does not hold. NOT a 404: it reads as a malformed request rather than a
|
|
77
|
-
* missing file, which is why the message below says so and why the date comes from the catalogue rather than a guess.
|
|
78
|
-
*/
|
|
79
|
-
const UNKNOWN_VERSION_STATUS = 400
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* What one acquired survey area is, on disk.
|
|
83
|
-
*/
|
|
84
|
-
export interface SurveyAreaArchive {
|
|
85
|
-
areaSymbol: string
|
|
86
|
-
versionDate: string
|
|
87
|
-
/**
|
|
88
|
-
* The extracted `<areasymbol>/` directory, holding `spatial/` and `tabular/`.
|
|
89
|
-
*/
|
|
90
|
-
root: string
|
|
91
|
-
spatialDirectory: string
|
|
92
|
-
tabularDirectory: string
|
|
93
|
-
/**
|
|
94
|
-
* The archive as transferred. Kept so a re-run costs nothing and so the bytes are re-checkable.
|
|
95
|
-
*/
|
|
96
|
-
archivePath: string
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Download and unzip one survey area, returning where its pieces landed.
|
|
101
|
-
*
|
|
102
|
-
* Downloads to a `.part` file and renames only on a clean finish, so an interrupted transfer never presents as a
|
|
103
|
-
* complete archive — the same discipline the database build uses, for the same reason.
|
|
104
|
-
*
|
|
105
|
-
* @throws {Error} When the host answers anything but 200, or when the extracted tree does not hold the two directories
|
|
106
|
-
* every survey area publishes.
|
|
107
|
-
*/
|
|
108
|
-
export async function downloadSurveyArea(options: DownloadSurveyAreaOptions): Promise<SurveyAreaArchive> {
|
|
109
|
-
const vintageDirectory = join(options.cacheRoot, options.versionDate)
|
|
110
|
-
const root = join(vintageDirectory, options.areaSymbol)
|
|
111
|
-
const archivePath = join(vintageDirectory, `wss_SSA_${options.areaSymbol}.zip`)
|
|
112
|
-
|
|
113
|
-
if (!(await tryStat(root))) {
|
|
114
|
-
await makeDirectories(vintageDirectory)
|
|
115
|
-
|
|
116
|
-
if (await tryStat(archivePath)) {
|
|
117
|
-
options.onProgress?.(`${options.areaSymbol}: archive for ${options.versionDate} already downloaded`)
|
|
118
|
-
} else {
|
|
119
|
-
await streamToDisk({
|
|
120
|
-
url: surveyAreaArchiveURL(options.areaSymbol, options.versionDate),
|
|
121
|
-
destination: archivePath,
|
|
122
|
-
context: "soil download",
|
|
123
|
-
progressStrideBytes: PROGRESS_STRIDE_BYTES,
|
|
124
|
-
describeStatus: (status) =>
|
|
125
|
-
status === UNKNOWN_VERSION_STATUS
|
|
126
|
-
? " — this host answers 400 rather than 404 for a version date it does not hold, so check the date against sacatalog.saverest"
|
|
127
|
-
: undefined,
|
|
128
|
-
// Every progress line names the area, because a full acquisition interleaves hundreds of them.
|
|
129
|
-
...(options.onProgress
|
|
130
|
-
? { onProgress: (message: string) => options.onProgress?.(`${options.areaSymbol}: ${message}`) }
|
|
131
|
-
: {}),
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// The archive holds its files under an `<AREASYMBOL>/` root already, so it unzips into the vintage directory
|
|
136
|
-
// rather than into a directory named for itself.
|
|
137
|
-
await runFile("unzip", ["-o", "-q", archivePath, "-d", vintageDirectory])
|
|
138
|
-
} else {
|
|
139
|
-
options.onProgress?.(`${options.areaSymbol}: already extracted for ${options.versionDate}`)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const spatialDirectory = join(root, "spatial")
|
|
143
|
-
const tabularDirectory = join(root, "tabular")
|
|
144
|
-
|
|
145
|
-
for (const directory of [spatialDirectory, tabularDirectory]) {
|
|
146
|
-
if (!(await tryStat(directory))) {
|
|
147
|
-
throw new Error(
|
|
148
|
-
`soil download: ${options.areaSymbol} extracted without a ${directory} directory — every survey area publishes both spatial/ and tabular/, so this archive is not the product`
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return {
|
|
154
|
-
areaSymbol: options.areaSymbol,
|
|
155
|
-
versionDate: options.versionDate,
|
|
156
|
-
root,
|
|
157
|
-
spatialDirectory,
|
|
158
|
-
tabularDirectory,
|
|
159
|
-
archivePath,
|
|
160
|
-
}
|
|
161
|
-
}
|
package/lib/sdk/index.ts
DELETED
|
@@ -1,20 +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 NRCS SSURGO soil-capability layer. The READER is the
|
|
6
|
-
* package root.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export * from "#sdk/acquire"
|
|
10
|
-
export * from "#sdk/build-soil"
|
|
11
|
-
export * from "#sdk/cell-tiers"
|
|
12
|
-
export * from "#sdk/cells"
|
|
13
|
-
export * from "#sdk/client"
|
|
14
|
-
export * from "#sdk/download"
|
|
15
|
-
export * from "#sdk/ingest/index"
|
|
16
|
-
export * from "#sdk/measure-resolutions"
|
|
17
|
-
export * from "#sdk/reduce"
|
|
18
|
-
export * from "#sdk/survey-area"
|
|
19
|
-
export * from "#sdk/tabular"
|
|
20
|
-
export * from "#sdk/verify"
|