@mailwoman/soil 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 +221 -0
- package/lib/index.ts +474 -0
- package/lib/schema.ts +493 -0
- package/lib/scripts/ingest-chunk.ts +47 -0
- package/lib/sdk/acquire.ts +124 -0
- package/lib/sdk/build-soil.ts +778 -0
- package/lib/sdk/cell-tiers.ts +310 -0
- package/lib/sdk/cells.ts +224 -0
- package/lib/sdk/client.ts +273 -0
- package/lib/sdk/download.ts +161 -0
- package/lib/sdk/index.ts +20 -0
- package/lib/sdk/ingest-chunk.ts +182 -0
- package/lib/sdk/ingest.ts +271 -0
- package/lib/sdk/measure-resolutions.ts +81 -0
- package/lib/sdk/reduce.ts +375 -0
- package/lib/sdk/survey-area.ts +467 -0
- package/lib/sdk/tabular.ts +296 -0
- package/lib/sdk/verify.ts +369 -0
- package/lib/test-kit.ts +255 -0
- package/lib/vocabulary.ts +241 -0
- package/out/index.d.ts +199 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +255 -0
- package/out/index.js.map +1 -0
- package/out/schema.d.ts +369 -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 +42 -0
- package/out/scripts/ingest-chunk.js.map +1 -0
- package/out/sdk/acquire.d.ts +60 -0
- package/out/sdk/acquire.d.ts.map +1 -0
- package/out/sdk/acquire.js +74 -0
- package/out/sdk/acquire.js.map +1 -0
- package/out/sdk/build-soil.d.ts +197 -0
- package/out/sdk/build-soil.d.ts.map +1 -0
- package/out/sdk/build-soil.js +434 -0
- package/out/sdk/build-soil.js.map +1 -0
- package/out/sdk/cell-tiers.d.ts +47 -0
- package/out/sdk/cell-tiers.d.ts.map +1 -0
- package/out/sdk/cell-tiers.js +194 -0
- package/out/sdk/cell-tiers.js.map +1 -0
- package/out/sdk/cells.d.ts +118 -0
- package/out/sdk/cells.d.ts.map +1 -0
- package/out/sdk/cells.js +140 -0
- package/out/sdk/cells.js.map +1 -0
- package/out/sdk/client.d.ts +120 -0
- package/out/sdk/client.d.ts.map +1 -0
- package/out/sdk/client.js +214 -0
- package/out/sdk/client.js.map +1 -0
- package/out/sdk/download.d.ts +84 -0
- package/out/sdk/download.d.ts.map +1 -0
- package/out/sdk/download.js +117 -0
- package/out/sdk/download.js.map +1 -0
- package/out/sdk/index.d.ts +20 -0
- package/out/sdk/index.d.ts.map +1 -0
- package/out/sdk/index.js +20 -0
- package/out/sdk/index.js.map +1 -0
- package/out/sdk/ingest-chunk.d.ts +73 -0
- package/out/sdk/ingest-chunk.d.ts.map +1 -0
- package/out/sdk/ingest-chunk.js +98 -0
- package/out/sdk/ingest-chunk.js.map +1 -0
- package/out/sdk/ingest.d.ts +132 -0
- package/out/sdk/ingest.d.ts.map +1 -0
- package/out/sdk/ingest.js +163 -0
- package/out/sdk/ingest.js.map +1 -0
- package/out/sdk/measure-resolutions.d.ts +39 -0
- package/out/sdk/measure-resolutions.d.ts.map +1 -0
- package/out/sdk/measure-resolutions.js +52 -0
- package/out/sdk/measure-resolutions.js.map +1 -0
- package/out/sdk/reduce.d.ts +122 -0
- package/out/sdk/reduce.d.ts.map +1 -0
- package/out/sdk/reduce.js +266 -0
- package/out/sdk/reduce.js.map +1 -0
- package/out/sdk/survey-area.d.ts +105 -0
- package/out/sdk/survey-area.d.ts.map +1 -0
- package/out/sdk/survey-area.js +346 -0
- package/out/sdk/survey-area.js.map +1 -0
- package/out/sdk/tabular.d.ts +106 -0
- package/out/sdk/tabular.d.ts.map +1 -0
- package/out/sdk/tabular.js +194 -0
- package/out/sdk/tabular.js.map +1 -0
- package/out/sdk/verify.d.ts +135 -0
- package/out/sdk/verify.d.ts.map +1 -0
- package/out/sdk/verify.js +221 -0
- package/out/sdk/verify.js.map +1 -0
- package/out/test-kit.d.ts +80 -0
- package/out/test-kit.d.ts.map +1 -0
- package/out/test-kit.js +228 -0
- package/out/test-kit.js.map +1 -0
- package/out/vocabulary.d.ts +179 -0
- package/out/vocabulary.d.ts.map +1 -0
- package/out/vocabulary.js +215 -0
- package/out/vocabulary.js.map +1 -0
- package/package.json +276 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The reduction: the containment index, read once, into one per-cell distribution both consumers share.
|
|
7
|
+
*
|
|
8
|
+
* A DISTRIBUTION, NEVER A WINNER, AND THAT IS FORCED BY MEASUREMENT. 84.0% of the 339,191 national map
|
|
9
|
+
* units hold two or more components; in 16.8% the largest component covers under half the map unit; and
|
|
10
|
+
* 85.4% of `IA153`'s delineations are smaller than one resolution-9 cell. No affordable cell size removes
|
|
11
|
+
* the mixture — it is a property of the survey, whose own `mukind` says so: 128,499 map units (38.0%) are
|
|
12
|
+
* complexes, associations or undifferentiated groups, which is NRCS stating that the soils are
|
|
13
|
+
* intermingled and cannot be separated at the mapping scale. A winner class would satisfy the result-level
|
|
14
|
+
* consumer and starve the signal consumer, which needs a magnitude to vary over.
|
|
15
|
+
*
|
|
16
|
+
* THE WEIGHT IS A UNIFORM-AREA LATTICE OVER THE CELL, AND THE GRAIN IS CHOSEN AGAINST THE AUTHORITY'S OWN.
|
|
17
|
+
* A cell's children at {@link WEIGHT_LATTICE_DEPTH} levels finer have equal area by construction, so
|
|
18
|
+
* counting which delineation covers each child centre estimates covered area without a polygon clip. At
|
|
19
|
+
* depth 2 that is 49 children — 2.04% per child, which is the finest share NRCS's own
|
|
20
|
+
* `muaggatt.niccdcdpct` ever reports (observed minimum: 2%). Resolving finer than the authority publishes
|
|
21
|
+
* would be precision this layer cannot source.
|
|
22
|
+
*
|
|
23
|
+
* A WHOLE CELL SKIPS THE LATTICE ENTIRELY, and that is exact rather than an optimization: a cell lying
|
|
24
|
+
* wholly inside one delineation is covered by that delineation and by nothing else, so its distribution is
|
|
25
|
+
* that map unit's component split and its `mapped_share` is 1.
|
|
26
|
+
*
|
|
27
|
+
* `mapped_share` EXISTS BECAUSE A SURVEY-AREA EDGE CELL IS PARTLY OUTSIDE EVERY DELINEATION. Without it,
|
|
28
|
+
* the unmapped remainder would silently deflate every class share — an absence represented as a small
|
|
29
|
+
* number, which is the one thing this schema exists to prevent. The five shares are normalized over the
|
|
30
|
+
* mapped part, so they sum to 1 exactly, and `mapped_share` says how much of the cell that was.
|
|
31
|
+
*
|
|
32
|
+
* CLASS 8 IS A CLASS SHARE, NOT AN ABSENCE. It is a determination — the survey looked and rated the land
|
|
33
|
+
* as precluding commercial plant production, and 67,547 national components carry it. Folding it in with
|
|
34
|
+
* `NOTCOM`, a water body and an unrated series would produce a well-formed wrong answer, and separating
|
|
35
|
+
* the four absences from the one positive negative is the whole reason this table has five columns rather
|
|
36
|
+
* than one.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { parseJSONStrict } from "@mailwoman/core/objects"
|
|
40
|
+
import { pointInEncodedRings, type H3Cell } from "@mailwoman/spatial"
|
|
41
|
+
import { cellToChildren, cellToLatLng } from "h3-js"
|
|
42
|
+
|
|
43
|
+
import type { SoilCapabilityCellTable, SoilComponentTable, SoilMapUnitTable } from "#schema"
|
|
44
|
+
import { SOIL_SHARE_WEIGHTING } from "#vocabulary"
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* How many resolution levels finer than the index the weighting lattice runs.
|
|
48
|
+
*
|
|
49
|
+
* Two, giving 49 children per cell and a 2.04% share granularity. That is deliberately matched to the finest share the
|
|
50
|
+
* authority itself publishes — `muaggatt.niccdcdpct`'s observed minimum is 2% — because a lattice finer than the
|
|
51
|
+
* source's own reporting grain buys precision this layer cannot source, at 7× the cost per level.
|
|
52
|
+
*/
|
|
53
|
+
export const WEIGHT_LATTICE_DEPTH = 2
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Class shares below this are folded into `other_share` rather than stored.
|
|
57
|
+
*
|
|
58
|
+
* One percent, which sits BELOW the lattice's own 2.04% granularity, so nothing a single child cell produces is
|
|
59
|
+
* truncated — what lands here is the long tail that component percentages create inside a child (a 1%-weight component
|
|
60
|
+
* inside one child cell contributes 0.02%). Truncating a long tail is legitimate; doing it silently is not, which is
|
|
61
|
+
* why the remainder is stored explicitly and the shares still sum to 1.
|
|
62
|
+
*/
|
|
63
|
+
export const CLASS_SHARE_FLOOR = 0.01
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* One delineation reaching a cell, as the reduction needs it.
|
|
67
|
+
*/
|
|
68
|
+
export interface CellCandidate {
|
|
69
|
+
areaID: string
|
|
70
|
+
mukey: string
|
|
71
|
+
containment: string
|
|
72
|
+
minLat: number
|
|
73
|
+
minLon: number
|
|
74
|
+
maxLat: number
|
|
75
|
+
maxLon: number
|
|
76
|
+
rings: Uint8Array
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* What a map unit contributes per unit of area — computed once per map unit and reused for every cell it reaches.
|
|
81
|
+
*/
|
|
82
|
+
export interface MapUnitProfile {
|
|
83
|
+
/**
|
|
84
|
+
* Class code → share of the map unit, summing to 1 across classes and the three mapped-absence buckets.
|
|
85
|
+
*/
|
|
86
|
+
classShares: ReadonlyMap<string, number>
|
|
87
|
+
unrated: number
|
|
88
|
+
notRateable: number
|
|
89
|
+
noData: number
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Turn one map unit and its components into the per-unit-area profile the reduction folds in.
|
|
94
|
+
*
|
|
95
|
+
* A `no_mapping` map unit contributes wholly to `nodata` and NEVER to a class: it is a polygon the authority drew with
|
|
96
|
+
* no soil mapping behind it, and reading it as a low class would be the reassuring wrong number §3.2 of the survey is
|
|
97
|
+
* about.
|
|
98
|
+
*
|
|
99
|
+
* The split across components is by `comppct_r`, the component's representative percentage of its map unit, normalized
|
|
100
|
+
* by the total actually present rather than assumed to be 100 — measured on `IA153` all 152 map units sum to exactly
|
|
101
|
+
* 100, and a national build must not depend on that holding everywhere.
|
|
102
|
+
*/
|
|
103
|
+
export function mapUnitProfile(
|
|
104
|
+
mapUnit: Pick<SoilMapUnitTable, "no_mapping">,
|
|
105
|
+
components: ReadonlyArray<Pick<SoilComponentTable, "comppct_r" | "compkind" | "nirrcapcl">>
|
|
106
|
+
): MapUnitProfile {
|
|
107
|
+
if (mapUnit.no_mapping) {
|
|
108
|
+
return { classShares: new Map(), unrated: 0, notRateable: 0, noData: 1 }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let total = 0
|
|
112
|
+
|
|
113
|
+
for (const component of components) {
|
|
114
|
+
total += component.comppct_r
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// A map unit whose components carry no weight at all publishes no readable proportion, so nothing can be apportioned
|
|
118
|
+
// from it. It is marked `no_mapping` upstream for exactly this reason; reaching here with a zero total means the
|
|
119
|
+
// upstream check and this one disagree, and answering with an empty distribution would silently drop the delineation's
|
|
120
|
+
// area out of every share.
|
|
121
|
+
if (total <= 0) {
|
|
122
|
+
return { classShares: new Map(), unrated: 0, notRateable: 0, noData: 1 }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const classShares = new Map<string, number>()
|
|
126
|
+
|
|
127
|
+
let unrated = 0
|
|
128
|
+
let notRateable = 0
|
|
129
|
+
|
|
130
|
+
for (const component of components) {
|
|
131
|
+
const weight = component.comppct_r / total
|
|
132
|
+
|
|
133
|
+
if (weight <= 0) continue
|
|
134
|
+
|
|
135
|
+
if (component.nirrcapcl) {
|
|
136
|
+
classShares.set(component.nirrcapcl, (classShares.get(component.nirrcapcl) ?? 0) + weight)
|
|
137
|
+
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// A NULL rating means the survey did not rate this component, and WHY it did not is what separates the two buckets.
|
|
142
|
+
// A miscellaneous area is a non-soil area — rock outcrop, water — that the capability rating does not apply to; a
|
|
143
|
+
// named soil with no rating is one the survey chose not to rate. Read as one number they would both say "not
|
|
144
|
+
// arable", which neither of them says.
|
|
145
|
+
if (component.compkind === "Miscellaneous area") {
|
|
146
|
+
notRateable += weight
|
|
147
|
+
} else {
|
|
148
|
+
unrated += weight
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { classShares, unrated, notRateable, noData: 0 }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* What one cell's reduction produced, plus the diagnostics the receipt reports.
|
|
157
|
+
*/
|
|
158
|
+
export interface ReducedCell {
|
|
159
|
+
row: SoilCapabilityCellTable
|
|
160
|
+
/**
|
|
161
|
+
* True when the top class covers less than half the cell — the §4.7 number, counted here so it comes off the artifact
|
|
162
|
+
* rather than out of a separate harness.
|
|
163
|
+
*/
|
|
164
|
+
topClassUnderHalf: boolean
|
|
165
|
+
/**
|
|
166
|
+
* True when the lattice was used rather than the whole-cell fast path.
|
|
167
|
+
*/
|
|
168
|
+
sampled: boolean
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Reduce one cell.
|
|
173
|
+
*
|
|
174
|
+
* @throws {Error} When a candidate names a map unit the profile map does not hold. A missing profile means the
|
|
175
|
+
* attribute join is short, and answering with the remaining candidates would report a well-formed distribution over
|
|
176
|
+
* part of the cell.
|
|
177
|
+
*/
|
|
178
|
+
export function reduceCell(
|
|
179
|
+
cell: H3Cell,
|
|
180
|
+
resolution: number,
|
|
181
|
+
candidates: ReadonlyArray<CellCandidate>,
|
|
182
|
+
profiles: ReadonlyMap<string, MapUnitProfile>,
|
|
183
|
+
h3Cell: number
|
|
184
|
+
): ReducedCell {
|
|
185
|
+
const weights = new Map<string, number>()
|
|
186
|
+
let sampled = false
|
|
187
|
+
let mappedShare = 1
|
|
188
|
+
|
|
189
|
+
const whole = candidates.length === 1 ? candidates.find((candidate) => candidate.containment === "whole") : undefined
|
|
190
|
+
|
|
191
|
+
if (whole) {
|
|
192
|
+
// Exactly one delineation, and it covers the cell entirely. Nothing else can reach it, so the lattice would return
|
|
193
|
+
// the same answer at 49 times the cost.
|
|
194
|
+
weights.set(whole.mukey, 1)
|
|
195
|
+
} else {
|
|
196
|
+
sampled = true
|
|
197
|
+
|
|
198
|
+
const children = cellToChildren(cell, resolution + WEIGHT_LATTICE_DEPTH)
|
|
199
|
+
let covered = 0
|
|
200
|
+
|
|
201
|
+
for (const child of children) {
|
|
202
|
+
const [latitude, longitude] = cellToLatLng(child)
|
|
203
|
+
const owner = candidateAt(candidates, latitude, longitude)
|
|
204
|
+
|
|
205
|
+
if (!owner) continue
|
|
206
|
+
|
|
207
|
+
covered++
|
|
208
|
+
weights.set(owner.mukey, (weights.get(owner.mukey) ?? 0) + 1)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (!covered) {
|
|
212
|
+
// Every child centre fell outside every delineation reaching the cell. The cell IS touched — the index says so —
|
|
213
|
+
// but no lattice point landed inside, which happens when a sliver clips a corner. Reporting shares over nothing
|
|
214
|
+
// would divide by zero; reporting a mapped share of zero is the truthful answer, and the row is dropped by the
|
|
215
|
+
// caller rather than stored as an all-zero distribution.
|
|
216
|
+
return {
|
|
217
|
+
row: emptyRow(h3Cell, candidates.length),
|
|
218
|
+
topClassUnderHalf: false,
|
|
219
|
+
sampled,
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
mappedShare = covered / children.length
|
|
224
|
+
|
|
225
|
+
for (const [mukey, count] of weights) {
|
|
226
|
+
weights.set(mukey, count / covered)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return assembleRow(h3Cell, weights, profiles, mappedShare, candidates.length, sampled)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The delineation covering a point, or `undefined` where none does.
|
|
235
|
+
*
|
|
236
|
+
* The bounding box is the prefilter the geometry table stores precisely so the ray cast runs on the few delineations
|
|
237
|
+
* that could contain the point rather than on every delineation reaching the cell.
|
|
238
|
+
*/
|
|
239
|
+
function candidateAt(
|
|
240
|
+
candidates: ReadonlyArray<CellCandidate>,
|
|
241
|
+
latitude: number,
|
|
242
|
+
longitude: number
|
|
243
|
+
): CellCandidate | undefined {
|
|
244
|
+
for (const candidate of candidates) {
|
|
245
|
+
if (
|
|
246
|
+
longitude < candidate.minLon ||
|
|
247
|
+
longitude > candidate.maxLon ||
|
|
248
|
+
latitude < candidate.minLat ||
|
|
249
|
+
latitude > candidate.maxLat
|
|
250
|
+
) {
|
|
251
|
+
continue
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (pointInEncodedRings(candidate.rings, longitude, latitude)) return candidate
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return undefined
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Fold the per-map-unit weights through their profiles into the stored row.
|
|
262
|
+
*/
|
|
263
|
+
function assembleRow(
|
|
264
|
+
h3Cell: number,
|
|
265
|
+
weights: ReadonlyMap<string, number>,
|
|
266
|
+
profiles: ReadonlyMap<string, MapUnitProfile>,
|
|
267
|
+
mappedShare: number,
|
|
268
|
+
delineations: number,
|
|
269
|
+
sampled: boolean
|
|
270
|
+
): ReducedCell {
|
|
271
|
+
const classShares = new Map<string, number>()
|
|
272
|
+
|
|
273
|
+
let unrated = 0
|
|
274
|
+
let notRateable = 0
|
|
275
|
+
let noData = 0
|
|
276
|
+
|
|
277
|
+
for (const [mukey, weight] of weights) {
|
|
278
|
+
const profile = profiles.get(mukey)
|
|
279
|
+
|
|
280
|
+
if (!profile) {
|
|
281
|
+
throw new Error(
|
|
282
|
+
`soil reduce: cell ${h3Cell} names map unit ${mukey}, which the attribute join does not hold — a missing profile means the join is short, and reducing the remaining candidates would report a well-formed distribution over part of the cell`
|
|
283
|
+
)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (const [code, share] of profile.classShares) {
|
|
287
|
+
classShares.set(code, (classShares.get(code) ?? 0) + share * weight)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
unrated += profile.unrated * weight
|
|
291
|
+
notRateable += profile.notRateable * weight
|
|
292
|
+
noData += profile.noData * weight
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// The floor truncates the long tail component percentages create inside a lattice child. The remainder is stored
|
|
296
|
+
// rather than dropped, so the five shares sum to 1 and a reader can see how much was folded away.
|
|
297
|
+
let other = 0
|
|
298
|
+
const kept: Array<[string, number]> = []
|
|
299
|
+
|
|
300
|
+
for (const [code, share] of classShares) {
|
|
301
|
+
if (share < CLASS_SHARE_FLOOR) {
|
|
302
|
+
other += share
|
|
303
|
+
} else {
|
|
304
|
+
kept.push([code, share])
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
kept.sort((left, right) => right[1] - left[1] || (left[0] < right[0] ? -1 : 1))
|
|
309
|
+
|
|
310
|
+
const top = kept[0]
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
row: {
|
|
314
|
+
h3_cell: h3Cell,
|
|
315
|
+
class_shares: JSON.stringify(Object.fromEntries(kept.map(([code, share]) => [code, round(share)]))),
|
|
316
|
+
unrated_share: round(unrated),
|
|
317
|
+
notrateable_share: round(notRateable),
|
|
318
|
+
nodata_share: round(noData),
|
|
319
|
+
other_share: round(other),
|
|
320
|
+
mapped_share: round(mappedShare),
|
|
321
|
+
top_class: top ? top[0] : null,
|
|
322
|
+
top_class_share: top ? round(top[1]) : null,
|
|
323
|
+
weighting: SOIL_SHARE_WEIGHTING,
|
|
324
|
+
delineations,
|
|
325
|
+
},
|
|
326
|
+
topClassUnderHalf: !top || top[1] < 0.5,
|
|
327
|
+
sampled,
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* A cell no lattice point landed inside. `mapped_share` zero says exactly that, and the caller drops it rather than
|
|
333
|
+
* storing an all-zero distribution that would read as a surveyed cell holding nothing.
|
|
334
|
+
*/
|
|
335
|
+
function emptyRow(h3Cell: number, delineations: number): SoilCapabilityCellTable {
|
|
336
|
+
return {
|
|
337
|
+
h3_cell: h3Cell,
|
|
338
|
+
class_shares: "{}",
|
|
339
|
+
unrated_share: 0,
|
|
340
|
+
notrateable_share: 0,
|
|
341
|
+
nodata_share: 0,
|
|
342
|
+
other_share: 0,
|
|
343
|
+
mapped_share: 0,
|
|
344
|
+
top_class: null,
|
|
345
|
+
top_class_share: null,
|
|
346
|
+
weighting: SOIL_SHARE_WEIGHTING,
|
|
347
|
+
delineations,
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Six decimals — a millionth of a cell, far below the lattice's own 2% granularity, and enough that the stored shares
|
|
353
|
+
* still sum to 1 within a rounding error a reader can see is rounding.
|
|
354
|
+
*/
|
|
355
|
+
const SHARE_DECIMALS = 6
|
|
356
|
+
|
|
357
|
+
function round(value: number): number {
|
|
358
|
+
return Number(value.toFixed(SHARE_DECIMALS))
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The sum of a stored row's five shares. Exported because the invariant it checks — that they sum to 1 — is what makes
|
|
363
|
+
* `other_share` required rather than decorative, and a test that could not state the sum could not pin it.
|
|
364
|
+
*/
|
|
365
|
+
export function shareTotal(row: SoilCapabilityCellTable): number {
|
|
366
|
+
const classes = parseJSONStrict<Record<string, number>>(row.class_shares)
|
|
367
|
+
|
|
368
|
+
let total = row.unrated_share + row.notrateable_share + row.nodata_share + row.other_share
|
|
369
|
+
|
|
370
|
+
for (const share of Object.values(classes)) {
|
|
371
|
+
total += share
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return total
|
|
375
|
+
}
|