@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.
Files changed (97) hide show
  1. package/README.md +221 -0
  2. package/lib/index.ts +474 -0
  3. package/lib/schema.ts +493 -0
  4. package/lib/scripts/ingest-chunk.ts +47 -0
  5. package/lib/sdk/acquire.ts +124 -0
  6. package/lib/sdk/build-soil.ts +778 -0
  7. package/lib/sdk/cell-tiers.ts +310 -0
  8. package/lib/sdk/cells.ts +224 -0
  9. package/lib/sdk/client.ts +273 -0
  10. package/lib/sdk/download.ts +161 -0
  11. package/lib/sdk/index.ts +20 -0
  12. package/lib/sdk/ingest-chunk.ts +182 -0
  13. package/lib/sdk/ingest.ts +271 -0
  14. package/lib/sdk/measure-resolutions.ts +81 -0
  15. package/lib/sdk/reduce.ts +375 -0
  16. package/lib/sdk/survey-area.ts +467 -0
  17. package/lib/sdk/tabular.ts +296 -0
  18. package/lib/sdk/verify.ts +369 -0
  19. package/lib/test-kit.ts +255 -0
  20. package/lib/vocabulary.ts +241 -0
  21. package/out/index.d.ts +199 -0
  22. package/out/index.d.ts.map +1 -0
  23. package/out/index.js +255 -0
  24. package/out/index.js.map +1 -0
  25. package/out/schema.d.ts +369 -0
  26. package/out/schema.d.ts.map +1 -0
  27. package/out/schema.js +186 -0
  28. package/out/schema.js.map +1 -0
  29. package/out/scripts/ingest-chunk.d.ts +11 -0
  30. package/out/scripts/ingest-chunk.d.ts.map +1 -0
  31. package/out/scripts/ingest-chunk.js +42 -0
  32. package/out/scripts/ingest-chunk.js.map +1 -0
  33. package/out/sdk/acquire.d.ts +60 -0
  34. package/out/sdk/acquire.d.ts.map +1 -0
  35. package/out/sdk/acquire.js +74 -0
  36. package/out/sdk/acquire.js.map +1 -0
  37. package/out/sdk/build-soil.d.ts +197 -0
  38. package/out/sdk/build-soil.d.ts.map +1 -0
  39. package/out/sdk/build-soil.js +434 -0
  40. package/out/sdk/build-soil.js.map +1 -0
  41. package/out/sdk/cell-tiers.d.ts +47 -0
  42. package/out/sdk/cell-tiers.d.ts.map +1 -0
  43. package/out/sdk/cell-tiers.js +194 -0
  44. package/out/sdk/cell-tiers.js.map +1 -0
  45. package/out/sdk/cells.d.ts +118 -0
  46. package/out/sdk/cells.d.ts.map +1 -0
  47. package/out/sdk/cells.js +140 -0
  48. package/out/sdk/cells.js.map +1 -0
  49. package/out/sdk/client.d.ts +120 -0
  50. package/out/sdk/client.d.ts.map +1 -0
  51. package/out/sdk/client.js +214 -0
  52. package/out/sdk/client.js.map +1 -0
  53. package/out/sdk/download.d.ts +84 -0
  54. package/out/sdk/download.d.ts.map +1 -0
  55. package/out/sdk/download.js +117 -0
  56. package/out/sdk/download.js.map +1 -0
  57. package/out/sdk/index.d.ts +20 -0
  58. package/out/sdk/index.d.ts.map +1 -0
  59. package/out/sdk/index.js +20 -0
  60. package/out/sdk/index.js.map +1 -0
  61. package/out/sdk/ingest-chunk.d.ts +73 -0
  62. package/out/sdk/ingest-chunk.d.ts.map +1 -0
  63. package/out/sdk/ingest-chunk.js +98 -0
  64. package/out/sdk/ingest-chunk.js.map +1 -0
  65. package/out/sdk/ingest.d.ts +132 -0
  66. package/out/sdk/ingest.d.ts.map +1 -0
  67. package/out/sdk/ingest.js +163 -0
  68. package/out/sdk/ingest.js.map +1 -0
  69. package/out/sdk/measure-resolutions.d.ts +39 -0
  70. package/out/sdk/measure-resolutions.d.ts.map +1 -0
  71. package/out/sdk/measure-resolutions.js +52 -0
  72. package/out/sdk/measure-resolutions.js.map +1 -0
  73. package/out/sdk/reduce.d.ts +122 -0
  74. package/out/sdk/reduce.d.ts.map +1 -0
  75. package/out/sdk/reduce.js +266 -0
  76. package/out/sdk/reduce.js.map +1 -0
  77. package/out/sdk/survey-area.d.ts +105 -0
  78. package/out/sdk/survey-area.d.ts.map +1 -0
  79. package/out/sdk/survey-area.js +346 -0
  80. package/out/sdk/survey-area.js.map +1 -0
  81. package/out/sdk/tabular.d.ts +106 -0
  82. package/out/sdk/tabular.d.ts.map +1 -0
  83. package/out/sdk/tabular.js +194 -0
  84. package/out/sdk/tabular.js.map +1 -0
  85. package/out/sdk/verify.d.ts +135 -0
  86. package/out/sdk/verify.d.ts.map +1 -0
  87. package/out/sdk/verify.js +221 -0
  88. package/out/sdk/verify.js.map +1 -0
  89. package/out/test-kit.d.ts +80 -0
  90. package/out/test-kit.d.ts.map +1 -0
  91. package/out/test-kit.js +228 -0
  92. package/out/test-kit.js.map +1 -0
  93. package/out/vocabulary.d.ts +179 -0
  94. package/out/vocabulary.d.ts.map +1 -0
  95. package/out/vocabulary.js +215 -0
  96. package/out/vocabulary.js.map +1 -0
  97. package/package.json +276 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The streaming pass — every delineation into `soil_map_unit_area` and into the build's touch table — as a
7
+ * unit of work that can run over PART of one survey area.
8
+ *
9
+ * WHY THIS IS A CHUNK RATHER THAN A WHOLE FILE. h3's WASM heap cannot be reset from JavaScript, and it does
10
+ * not survive an unbounded number of polyfill calls: over the flood layer's real product, runs died after
11
+ * roughly 510,000 and 798,000 features on geometry that classifies in milliseconds in a fresh process. A
12
+ * build that completes only when fragmentation happens to stay low is not a reproducible build, so the
13
+ * classification is bounded by construction — {@linkcode buildSoilDatabase} runs one of these per range of
14
+ * the shapefile's own FIDs, each in its own process, and each therefore against a heap that starts empty.
15
+ * Iowa's 99 survey areas hold far more delineations together than any one of them does, which is why the
16
+ * bound is per RANGE rather than per area.
17
+ *
18
+ * THE CHUNK OWNS NO ARTIFACT. It appends rows to a database the parent created and will seal, and returns
19
+ * counts the parent adds up. Chunks run one at a time against that file, so there is no concurrent writer
20
+ * and no locking to reason about.
21
+ */
22
+ import { addCoverageCells, encodeRings, ringAreaReadings, ringsBoundingBox, shortCellToInt } from "@mailwoman/spatial";
23
+ import { beginBatched } from "@mailwoman/sqlite/batched";
24
+ import { classifyDelineationCells } from "#sdk/cells";
25
+ /**
26
+ * Rows per bulk-insert transaction. Chosen for the geometry table, whose rows carry a blob: a larger transaction grows
27
+ * the write-ahead file without improving throughput.
28
+ */
29
+ const INSERT_TRANSACTION_ROWS = 5000;
30
+ /**
31
+ * Delineations between progress reports.
32
+ */
33
+ const PROGRESS_STRIDE = 20_000;
34
+ /**
35
+ * Stream one chunk of one survey area into `database`.
36
+ *
37
+ * @throws {Error} On a delineation the classifier refuses — which includes the allocator's silent zero-cell answer.
38
+ */
39
+ export async function ingestSoilChunk(database, options) {
40
+ const insertArea = database.prepare("INSERT INTO soil_map_unit_area (area_id, mukey, areasymbol, min_lat, min_lon, max_lat, max_lon, rings) " +
41
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
42
+ const insertTouch = database.prepare("INSERT INTO build_cell_touch (h3_cell, resolution, area_id, is_full) VALUES (?, ?, ?, ?)");
43
+ const observedByCoverageCell = new Map();
44
+ const mappedByCoverageCell = new Map();
45
+ let delineations = 0;
46
+ let coarsened = 0;
47
+ let nestedArea = 0;
48
+ let allExteriorArea = 0;
49
+ const batch = beginBatched(database, { rowsPerCommit: INSERT_TRANSACTION_ROWS });
50
+ try {
51
+ for await (const delineation of options.source.delineations()) {
52
+ const bbox = ringsBoundingBox(delineation.polygons);
53
+ const areas = ringAreaReadings(delineation.polygons);
54
+ nestedArea += areas.nested;
55
+ allExteriorArea += areas.allExterior;
56
+ insertArea.run(delineation.areaID, delineation.mukey, delineation.areasymbol, bbox.minLat, bbox.minLon, bbox.maxLat, bbox.maxLon, encodeRings(delineation.polygons));
57
+ const classified = classifyDelineationCells(delineation.polygons, options.indexResolution, delineation.areaID);
58
+ if (classified.resolution !== options.indexResolution) {
59
+ coarsened++;
60
+ }
61
+ const coverageCells = new Set();
62
+ for (const cell of classified.whole) {
63
+ insertTouch.run(shortCellToInt(cell), classified.resolution, delineation.areaID, 1);
64
+ addCoverageCells(coverageCells, cell, classified.resolution, options.coverageResolution);
65
+ }
66
+ for (const cell of classified.partial) {
67
+ insertTouch.run(shortCellToInt(cell), classified.resolution, delineation.areaID, 0);
68
+ addCoverageCells(coverageCells, cell, classified.resolution, options.coverageResolution);
69
+ }
70
+ const hasMapping = !options.noMappingMukeys.has(delineation.mukey);
71
+ for (const coverageCell of coverageCells) {
72
+ observedByCoverageCell.set(coverageCell, (observedByCoverageCell.get(coverageCell) ?? 0) + 1);
73
+ if (hasMapping) {
74
+ mappedByCoverageCell.set(coverageCell, (mappedByCoverageCell.get(coverageCell) ?? 0) + 1);
75
+ }
76
+ }
77
+ delineations++;
78
+ batch.rowWritten();
79
+ if (delineations % PROGRESS_STRIDE === 0) {
80
+ options.onProgress?.(`${delineations.toLocaleString()} delineations in this chunk`);
81
+ }
82
+ }
83
+ batch.commit();
84
+ }
85
+ catch (error) {
86
+ batch.rollbackQuietly();
87
+ throw error;
88
+ }
89
+ return {
90
+ areaSymbol: options.source.areaSymbol,
91
+ delineations,
92
+ coarsened,
93
+ observedByCoverageCell: [...observedByCoverageCell],
94
+ mappedByCoverageCell: [...mappedByCoverageCell],
95
+ area: { nestedM2: nestedArea, allExteriorM2: allExteriorArea },
96
+ };
97
+ }
98
+ //# sourceMappingURL=ingest-chunk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest-chunk.js","sourceRoot":"","sources":["../../lib/sdk/ingest-chunk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACtH,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAIxD,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAA;AAGrD;;;GAGG;AACH,MAAM,uBAAuB,GAAG,IAAI,CAAA;AAEpC;;GAEG;AACH,MAAM,eAAe,GAAG,MAAM,CAAA;AA2C9B;;;;GAIG;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AC;YACF,CAAC;YAED,YAAY,EAAE,CAAA;YAEd,KAAK,CAAC,UAAU,EAAE,CAAA;YAElB,IAAI,YAAY,GAAG,eAAe,KAAK,CAAC,EAAE,CAAC;gBAC1C,OAAO,CAAC,UAAU,EAAE,CAAC,GAAG,YAAY,CAAC,cAAc,EAAE,6BAA6B,CAAC,CAAA;YACpF,CAAC;QACF,CAAC;QAED,KAAK,CAAC,MAAM,EAAE,CAAA;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,KAAK,CAAC,eAAe,EAAE,CAAA;QAEvB,MAAM,KAAK,CAAA;IACZ,CAAC;IAED,OAAO;QACN,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,UAAU;QACrC,YAAY;QACZ,SAAS;QACT,sBAAsB,EAAE,CAAC,GAAG,sBAAsB,CAAC;QACnD,oBAAoB,EAAE,CAAC,GAAG,oBAAoB,CAAC;QAC/C,IAAI,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,eAAe,EAAE;KAC9D,CAAA;AACF,CAAC"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Read a survey area's published shapefiles as a stream of WGS84 delineations, through ogr2ogr.
7
+ *
8
+ * OGR IS BUILD TOOLING, NEVER A SERVE DEPENDENCY (SCOPE invariant 6). It converts the authority's geometry
9
+ * into the structure the runtime probes, and nothing downstream of this module knows GDAL exists.
10
+ *
11
+ * THE SOURCE IS ALREADY IN WGS84, AND CHECKING IT IS STILL THE CHECK. Each `.prj` is an ESRI WKT reading
12
+ * `GEOGCS["GCS_WGS_1984",…]`, which GDAL resolves to EPSG:4326 — so no reprojection is needed before H3.
13
+ * The authority code is asserted anyway before a single feature is read, and the reprojected stream is
14
+ * asserted against the layer's own declared extent, which is the check that catches a coordinate-order
15
+ * mistake the projection check cannot see.
16
+ *
17
+ * THE DATUM GUARD RUNS EVEN THOUGH THE ANSWER IS THE IDENTITY, AND THAT IS THE POINT. PROJ substitutes a
18
+ * ballpark datum shift SILENTLY when the accurate grid is missing — measured on the flood layer at 3.4 m
19
+ * over an entire country, visible only as eight disagreements out of 59 against the authority's own
20
+ * service. For an EPSG:4326 source `projinfo` answers `Null geographic offset from WGS 84 to WGS 84, 0 m,
21
+ * World.` and the guard passes in one process. Skipping it on the reasoning that this source needs no
22
+ * shift is how the guard comes to be missing on the day a source arrives that does.
23
+ *
24
+ * THE ID IS THE SHAPEFILE'S OWN FID, AND IT HAS TO BE, because SSURGO publishes no per-delineation key:
25
+ * `MUKEY` names the MAP UNIT and one map unit has many delineations — `IA153` holds 17,966 delineations
26
+ * across 152 map units. So `area_id` is `<areasymbol>:<fid>`, which is stable across runs and is what makes
27
+ * a bounded chunk name the same features every time.
28
+ */
29
+ import { type MultiPolygonRings } from "@mailwoman/spatial";
30
+ /**
31
+ * The shapefile holding a survey area's map-unit polygons — the delineations this layer stores.
32
+ */
33
+ export declare function mapUnitShapefile(spatialDirectory: string, areaSymbol: string): string;
34
+ /**
35
+ * The shapefile holding a survey area's own OUTLINE. The footprint comes from HERE and never from the union of the
36
+ * rated polygons — `NOTCOM` and access-denied map units are inside the footprint and carry no rating, so a footprint
37
+ * derived from the rated set would report them as unmapped when the authority has declared exactly what they are.
38
+ */
39
+ export declare function surveyAreaShapefile(spatialDirectory: string, areaSymbol: string): string;
40
+ /**
41
+ * One map-unit delineation, reprojected to WGS84.
42
+ */
43
+ export interface SoilDelineation {
44
+ /**
45
+ * `<areasymbol>:<fid>`.
46
+ */
47
+ areaID: string;
48
+ mukey: string;
49
+ areasymbol: string;
50
+ polygons: MultiPolygonRings;
51
+ }
52
+ /**
53
+ * What a shapefile says about itself, read before any feature is.
54
+ */
55
+ export interface SoilSourceIdentity {
56
+ epsg: number;
57
+ featureCount: number;
58
+ layer: string;
59
+ /**
60
+ * The layer's own declared extent, `[minLon, minLat, maxLon, maxLat]`.
61
+ */
62
+ bbox: readonly [number, number, number, number];
63
+ }
64
+ export interface SoilIngestOptions {
65
+ shapefilePath: string;
66
+ /**
67
+ * Layer inside it. Defaults to the shapefile's base name, which is what the ESRI driver reports.
68
+ */
69
+ layer?: string;
70
+ /**
71
+ * The EPSG code the source must declare.
72
+ */
73
+ expectEPSG?: number;
74
+ /**
75
+ * Read only the shapefile's own FIDs in `[fidFrom, fidTo]`, inclusive — what makes a bounded chunk possible.
76
+ */
77
+ fidFrom?: number;
78
+ fidTo?: number;
79
+ /**
80
+ * Stop after this many features. The fixture and smoke rungs use it; a full build does not set it.
81
+ */
82
+ limit?: number;
83
+ }
84
+ /**
85
+ * Read what the shapefile declares about itself, and refuse a projection this ingest was not written for.
86
+ *
87
+ * @throws {Error} When the layer is missing, declares no EPSG authority code, declares one other than `expectEPSG`, or
88
+ * reports no feature count.
89
+ */
90
+ export declare function readSoilSourceIdentity(options: SoilIngestOptions): Promise<SoilSourceIdentity>;
91
+ /**
92
+ * Stream the map-unit delineations as WGS84 features.
93
+ *
94
+ * Every feature is checked against the declared extent as it passes. A swapped coordinate order survives a projection
95
+ * check — both axes are still numbers in a plausible range — and shows up here immediately.
96
+ *
97
+ * @throws {Error} When ogr2ogr fails, when a feature carries no geometry or no `MUKEY`, or when a vertex falls outside
98
+ * the declared extent.
99
+ */
100
+ export declare function readSoilDelineations(options: SoilIngestOptions & {
101
+ bbox: readonly [number, number, number, number];
102
+ }): AsyncGenerator<SoilDelineation>;
103
+ /**
104
+ * Where a build's delineations come from, and what the source declares about itself.
105
+ *
106
+ * The builder takes ONE of these rather than a path, which is what makes the fixture rung possible: hand-built geometry
107
+ * with no network and no GDAL still exercises the whole database half — the domain check, the cell classification, the
108
+ * reduction, the coverage rows, the manifest and the seal.
109
+ */
110
+ export interface SoilFeatureSource {
111
+ areaSymbol: string;
112
+ /**
113
+ * What the source says it holds. The build compares its own streamed total against this, so a short read throws
114
+ * instead of building a smaller county.
115
+ */
116
+ declaredFeatureCount: number;
117
+ layer: string;
118
+ epsg: number;
119
+ /**
120
+ * A description of where these delineations came from, for the receipt.
121
+ */
122
+ origin: string;
123
+ delineations: () => AsyncIterable<SoilDelineation>;
124
+ }
125
+ /**
126
+ * One survey area's map-unit shapefile as a feature source — identity read up front, features streamed on demand.
127
+ */
128
+ export declare function createShapefileFeatureSource(options: SoilIngestOptions & {
129
+ areaSymbol: string;
130
+ declaredFeatureCount?: number;
131
+ }): Promise<SoilFeatureSource>;
132
+ //# sourceMappingURL=ingest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest.d.ts","sourceRoot":"","sources":["../../lib/sdk/ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAiD,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAsB1G;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,gBAAgB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAErF;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,gBAAgB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAExF;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,iBAAiB,CAAA;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;CAC/C;AAED,MAAM,WAAW,iBAAiB;IACjC,aAAa,EAAE,MAAM,CAAA;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAepG;AAyBD;;;;;;;;GAQG;AACH,wBAAuB,oBAAoB,CAC1C,OAAO,EAAE,iBAAiB,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAC9E,cAAc,CAAC,eAAe,CAAC,CAqBjC;AAqCD;;;;;;GAMG;AACH,MAAM,WAAW,iBAAiB;IACjC,UAAU,EAAE,MAAM,CAAA;IAClB;;;OAGG;IACH,oBAAoB,EAAE,MAAM,CAAA;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,aAAa,CAAC,eAAe,CAAC,CAAA;CAClD;AAED;;GAEG;AACH,wBAAsB,4BAA4B,CACjD,OAAO,EAAE,iBAAiB,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,oBAAoB,CAAC,EAAE,MAAM,CAAA;CAAE,GAChF,OAAO,CAAC,iBAAiB,CAAC,CAa5B"}
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Read a survey area's published shapefiles as a stream of WGS84 delineations, through ogr2ogr.
7
+ *
8
+ * OGR IS BUILD TOOLING, NEVER A SERVE DEPENDENCY (SCOPE invariant 6). It converts the authority's geometry
9
+ * into the structure the runtime probes, and nothing downstream of this module knows GDAL exists.
10
+ *
11
+ * THE SOURCE IS ALREADY IN WGS84, AND CHECKING IT IS STILL THE CHECK. Each `.prj` is an ESRI WKT reading
12
+ * `GEOGCS["GCS_WGS_1984",…]`, which GDAL resolves to EPSG:4326 — so no reprojection is needed before H3.
13
+ * The authority code is asserted anyway before a single feature is read, and the reprojected stream is
14
+ * asserted against the layer's own declared extent, which is the check that catches a coordinate-order
15
+ * mistake the projection check cannot see.
16
+ *
17
+ * THE DATUM GUARD RUNS EVEN THOUGH THE ANSWER IS THE IDENTITY, AND THAT IS THE POINT. PROJ substitutes a
18
+ * ballpark datum shift SILENTLY when the accurate grid is missing — measured on the flood layer at 3.4 m
19
+ * over an entire country, visible only as eight disagreements out of 59 against the authority's own
20
+ * service. For an EPSG:4326 source `projinfo` answers `Null geographic offset from WGS 84 to WGS 84, 0 m,
21
+ * World.` and the guard passes in one process. Skipping it on the reasoning that this source needs no
22
+ * shift is how the guard comes to be missing on the day a source arrives that does.
23
+ *
24
+ * THE ID IS THE SHAPEFILE'S OWN FID, AND IT HAS TO BE, because SSURGO publishes no per-delineation key:
25
+ * `MUKEY` names the MAP UNIT and one map unit has many delineations — `IA153` holds 17,966 delineations
26
+ * across 152 map units. So `area_id` is `<areasymbol>:<fid>`, which is stable across runs and is what makes
27
+ * a bounded chunk name the same features every time.
28
+ */
29
+ import { assertRingsInsideExtent, requireArealPolygons } from "@mailwoman/spatial";
30
+ import { readOGRLayerIdentity } from "@mailwoman/spatial/tools/ogr";
31
+ import { ogr2ogrGeoJSONSeq } from "@mailwoman/spatial/tools/ogr-stream";
32
+ import { basename, join } from "path-ts";
33
+ import { SSURGO_SOURCE_EPSG } from "#vocabulary";
34
+ /**
35
+ * Coordinate decimals ogr2ogr writes into the stream. Nine is well past the source's own precision — the metadata
36
+ * states compilation to base maps meeting National Map Accuracy Standards at 1 inch = 1,000 feet — and is chosen so the
37
+ * round trip contributes nothing measurable to the area cross-check.
38
+ */
39
+ const COORDINATE_PRECISION = 9;
40
+ /**
41
+ * How far outside the layer's declared extent a vertex may fall before the ingest refuses.
42
+ *
43
+ * A tenth of a degree is about 11 km — small enough that an unprojected or axis-swapped read, which lands whole
44
+ * hemispheres away, still fails, and loose enough that a rounded declared extent is not brittle.
45
+ */
46
+ const BBOX_MARGIN_DEGREES = 0.1;
47
+ /**
48
+ * The shapefile holding a survey area's map-unit polygons — the delineations this layer stores.
49
+ */
50
+ export function mapUnitShapefile(spatialDirectory, areaSymbol) {
51
+ return join(spatialDirectory, `soilmu_a_${areaSymbol.toLowerCase()}.shp`);
52
+ }
53
+ /**
54
+ * The shapefile holding a survey area's own OUTLINE. The footprint comes from HERE and never from the union of the
55
+ * rated polygons — `NOTCOM` and access-denied map units are inside the footprint and carry no rating, so a footprint
56
+ * derived from the rated set would report them as unmapped when the authority has declared exactly what they are.
57
+ */
58
+ export function surveyAreaShapefile(spatialDirectory, areaSymbol) {
59
+ return join(spatialDirectory, `soilsa_a_${areaSymbol.toLowerCase()}.shp`);
60
+ }
61
+ /**
62
+ * Read what the shapefile declares about itself, and refuse a projection this ingest was not written for.
63
+ *
64
+ * @throws {Error} When the layer is missing, declares no EPSG authority code, declares one other than `expectEPSG`, or
65
+ * reports no feature count.
66
+ */
67
+ export async function readSoilSourceIdentity(options) {
68
+ const identity = await readOGRLayerIdentity({
69
+ path: options.shapefilePath,
70
+ layer: options.layer ?? basename(options.shapefilePath, ".shp"),
71
+ expectEPSG: options.expectEPSG ?? SSURGO_SOURCE_EPSG,
72
+ context: "soil ingest",
73
+ requireExtent: true,
74
+ messages: {
75
+ noAuthorityCode: "the projection cannot be checked, and reading one datum's coordinates as another's is silent",
76
+ epsgMismatch: "SSURGO publishes geographic WGS84, so a different code is a product change rather than a variation to absorb",
77
+ },
78
+ });
79
+ return { epsg: identity.epsg, featureCount: identity.featureCount, layer: identity.layer, bbox: identity.extent };
80
+ }
81
+ /**
82
+ * The ingest's `SELECT`, with the FID range applied when one is asked for.
83
+ */
84
+ function delineationSelectSQL(layer, options) {
85
+ const select = `SELECT FID AS fid, MUKEY AS mukey, AREASYMBOL AS areasymbol FROM "${layer}"`;
86
+ const bounds = [];
87
+ if (options.fidFrom !== undefined) {
88
+ bounds.push(`FID >= ${options.fidFrom}`);
89
+ }
90
+ if (options.fidTo !== undefined) {
91
+ bounds.push(`FID <= ${options.fidTo}`);
92
+ }
93
+ return bounds.length ? `${select} WHERE ${bounds.join(" AND ")}` : select;
94
+ }
95
+ /**
96
+ * Stream the map-unit delineations as WGS84 features.
97
+ *
98
+ * Every feature is checked against the declared extent as it passes. A swapped coordinate order survives a projection
99
+ * check — both axes are still numbers in a plausible range — and shows up here immediately.
100
+ *
101
+ * @throws {Error} When ogr2ogr fails, when a feature carries no geometry or no `MUKEY`, or when a vertex falls outside
102
+ * the declared extent.
103
+ */
104
+ export async function* readSoilDelineations(options) {
105
+ const layer = options.layer ?? basename(options.shapefilePath, ".shp");
106
+ const [minLon, minLat, maxLon, maxLat] = options.bbox;
107
+ const args = [
108
+ "-f",
109
+ "GeoJSONSeq",
110
+ "/vsistdout/",
111
+ "-t_srs",
112
+ `EPSG:${options.expectEPSG ?? SSURGO_SOURCE_EPSG}`,
113
+ "-lco",
114
+ `COORDINATE_PRECISION=${COORDINATE_PRECISION}`,
115
+ ...(options.limit === undefined ? [] : ["-limit", String(options.limit)]),
116
+ "-sql",
117
+ delineationSelectSQL(layer, options),
118
+ options.shapefilePath,
119
+ ];
120
+ for await (const raw of ogr2ogrGeoJSONSeq(args, "soil ingest")) {
121
+ yield toDelineation(raw, { minLon, minLat, maxLon, maxLat });
122
+ }
123
+ }
124
+ /**
125
+ * Validate one raw GeoJSON feature and narrow it. Split out so the generator body stays a loop.
126
+ */
127
+ function toDelineation(raw, extent) {
128
+ const { properties, geometry } = raw;
129
+ if (!geometry) {
130
+ throw new Error(`soil ingest: delineation ${properties.fid} carries no geometry`);
131
+ }
132
+ if (properties.mukey === null || properties.mukey === "") {
133
+ throw new Error(`soil ingest: delineation ${properties.fid} carries no MUKEY — a delineation with no map unit joins to nothing and would read downstream as unmapped ground`);
134
+ }
135
+ if (!properties.areasymbol) {
136
+ throw new Error(`soil ingest: delineation ${properties.fid} carries no AREASYMBOL`);
137
+ }
138
+ const polygons = requireArealPolygons(geometry, `delineation ${properties.fid}`, "soil ingest");
139
+ assertRingsInsideExtent(polygons, `delineation ${properties.fid}`, extent, BBOX_MARGIN_DEGREES, "soil ingest");
140
+ return {
141
+ areaID: `${properties.areasymbol}:${properties.fid}`,
142
+ mukey: String(properties.mukey),
143
+ areasymbol: properties.areasymbol,
144
+ polygons,
145
+ };
146
+ }
147
+ /**
148
+ * One survey area's map-unit shapefile as a feature source — identity read up front, features streamed on demand.
149
+ */
150
+ export async function createShapefileFeatureSource(options) {
151
+ const identity = await readSoilSourceIdentity(options);
152
+ return {
153
+ areaSymbol: options.areaSymbol,
154
+ // A RANGE's own count is supplied by the caller, because `ogrinfo` reports the layer's total and nothing narrower.
155
+ // The whole-file total is still checked: the builder sums what its chunks streamed and compares that.
156
+ declaredFeatureCount: options.declaredFeatureCount ?? options.limit ?? identity.featureCount,
157
+ layer: identity.layer,
158
+ epsg: identity.epsg,
159
+ origin: options.shapefilePath,
160
+ delineations: () => readSoilDelineations({ ...options, bbox: identity.bbox }),
161
+ };
162
+ }
163
+ //# sourceMappingURL=ingest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingest.js","sourceRoot":"","sources":["../../lib/sdk/ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAA0B,MAAM,oBAAoB,CAAA;AAC1G,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AACvE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAExC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAEhD;;;;GAIG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,CAAC,UAAU;QAC9B,mHAAmH;QACnH,sGAAsG;QACtG,oBAAoB,EAAE,OAAO,CAAC,oBAAoB,IAAI,OAAO,CAAC,KAAK,IAAI,QAAQ,CAAC,YAAY;QAC5F,KAAK,EAAE,QAAQ,CAAC,KAAK;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,MAAM,EAAE,OAAO,CAAC,aAAa;QAC7B,YAAY,EAAE,GAAG,EAAE,CAAC,oBAAoB,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC;KAC7E,CAAA;AACF,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The index resolution is a MEASUREMENT this layer takes, not a number argued to.
7
+ *
8
+ * ONE STREAM, EVERY RESOLUTION. Re-reading a survey area's shapefile per candidate buys nothing — the
9
+ * classification is per delineation, so every candidate index folds the same delineation in turn. The cost
10
+ * is memory: each resolution holds its own cell sets, and the finest candidate dominates.
11
+ *
12
+ * THIS INSTRUMENT REPORTS THE FIRST OF THE TWO NUMBERS §4.7 NAMES — the `partial` cell share, plus the mean
13
+ * delineations per cell that drives it. The SECOND number, the share of cells whose top class holds less
14
+ * than half the cell, is not measurable here: it needs the attribute join and the area weighting, which
15
+ * are the build. So it comes off the SHIPPING ARTIFACT instead — {@linkcode buildSoilDatabase} counts it
16
+ * while it writes the rows, and the build receipt reports it. That is the flood layer's lesson applied:
17
+ * the number that describes the artifact is the one taken from the artifact.
18
+ */
19
+ import type { ResolutionMeasurementOptions } from "@mailwoman/core/layers";
20
+ import { type SoilCellIndexMeasurement } from "#sdk/cells";
21
+ import { type SoilIngestOptions } from "#sdk/ingest";
22
+ export interface MeasureSoilResolutionsOptions extends SoilIngestOptions, ResolutionMeasurementOptions {
23
+ }
24
+ export interface SoilResolutionReport {
25
+ delineations: number;
26
+ /**
27
+ * The count the shapefile declares for itself. A run whose streamed total differs read a truncated file.
28
+ */
29
+ declaredFeatureCount: number;
30
+ measurements: SoilCellIndexMeasurement[];
31
+ }
32
+ /**
33
+ * Measure every candidate resolution over one survey area's real delineations.
34
+ *
35
+ * @throws {Error} When the streamed count does not match the count the shapefile declares. A short read produces a
36
+ * well-formed table describing a smaller county, which is the partial result that must throw.
37
+ */
38
+ export declare function measureSoilCellResolutions(options: MeasureSoilResolutionsOptions): Promise<SoilResolutionReport>;
39
+ //# sourceMappingURL=measure-resolutions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"measure-resolutions.d.ts","sourceRoot":"","sources":["../../lib/sdk/measure-resolutions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,wBAAwB,CAAA;AAE1E,OAAO,EAA2C,KAAK,wBAAwB,EAAE,MAAM,YAAY,CAAA;AACnG,OAAO,EAAgD,KAAK,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAElG,MAAM,WAAW,6BAA8B,SAAQ,iBAAiB,EAAE,4BAA4B;CAAG;AAEzG,MAAM,WAAW,oBAAoB;IACpC,YAAY,EAAE,MAAM,CAAA;IACpB;;OAEG;IACH,oBAAoB,EAAE,MAAM,CAAA;IAC5B,YAAY,EAAE,wBAAwB,EAAE,CAAA;CACxC;AAID;;;;;GAKG;AACH,wBAAsB,0BAA0B,CAC/C,OAAO,EAAE,6BAA6B,GACpC,OAAO,CAAC,oBAAoB,CAAC,CAmC/B"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * The index resolution is a MEASUREMENT this layer takes, not a number argued to.
7
+ *
8
+ * ONE STREAM, EVERY RESOLUTION. Re-reading a survey area's shapefile per candidate buys nothing — the
9
+ * classification is per delineation, so every candidate index folds the same delineation in turn. The cost
10
+ * is memory: each resolution holds its own cell sets, and the finest candidate dominates.
11
+ *
12
+ * THIS INSTRUMENT REPORTS THE FIRST OF THE TWO NUMBERS §4.7 NAMES — the `partial` cell share, plus the mean
13
+ * delineations per cell that drives it. The SECOND number, the share of cells whose top class holds less
14
+ * than half the cell, is not measurable here: it needs the attribute join and the area weighting, which
15
+ * are the build. So it comes off the SHIPPING ARTIFACT instead — {@linkcode buildSoilDatabase} counts it
16
+ * while it writes the rows, and the build receipt reports it. That is the flood layer's lesson applied:
17
+ * the number that describes the artifact is the one taken from the artifact.
18
+ */
19
+ import { classifyDelineationCells, SoilCellIndex } from "#sdk/cells";
20
+ import { readSoilDelineations, readSoilSourceIdentity } from "#sdk/ingest";
21
+ const DEFAULT_PROGRESS_EVERY = 5000;
22
+ /**
23
+ * Measure every candidate resolution over one survey area's real delineations.
24
+ *
25
+ * @throws {Error} When the streamed count does not match the count the shapefile declares. A short read produces a
26
+ * well-formed table describing a smaller county, which is the partial result that must throw.
27
+ */
28
+ export async function measureSoilCellResolutions(options) {
29
+ const identity = await readSoilSourceIdentity(options);
30
+ const indexes = options.resolutions.map((resolution) => new SoilCellIndex(resolution));
31
+ const progressEvery = options.progressEvery ?? DEFAULT_PROGRESS_EVERY;
32
+ let delineations = 0;
33
+ for await (const delineation of readSoilDelineations({ ...options, bbox: identity.bbox })) {
34
+ for (const index of indexes) {
35
+ index.add(delineation.areaID, classifyDelineationCells(delineation.polygons, index.resolution, delineation.areaID));
36
+ }
37
+ delineations++;
38
+ if (delineations % progressEvery === 0) {
39
+ options.onProgress?.(`${delineations.toLocaleString()} delineations classified`);
40
+ }
41
+ }
42
+ const expected = options.limit ?? identity.featureCount;
43
+ if (delineations !== expected) {
44
+ throw new Error(`soil measure: streamed ${delineations} delineations, the shapefile declares ${expected} — a short read reports a smaller survey area rather than failing`);
45
+ }
46
+ return {
47
+ delineations,
48
+ declaredFeatureCount: identity.featureCount,
49
+ measurements: indexes.map((index) => index.finish()),
50
+ };
51
+ }
52
+ //# sourceMappingURL=meas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Q,GAAG,OAAO,CAAC,KAAK,IAAI,QAAQ,CAAC,YAAY,CAAA;IAEvD,IAAI,YAAY,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CACd,0BAA0B,YAAY,yCAAyC,QAAQ,mEAAmE,CAC1J,CAAA;IACF,CAAC;IAED,OAAO;QACN,YAAY;QACZ,oBAAoB,EAAE,QAAQ,CAAC,YAAY;QAC3C,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;KACpD,CAAA;AACF,CAAC"}
@@ -0,0 +1,122 @@
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
+ import { type H3Cell } from "@mailwoman/spatial";
39
+ import type { SoilCapabilityCellTable, SoilComponentTable, SoilMapUnitTable } from "#schema";
40
+ /**
41
+ * How many resolution levels finer than the index the weighting lattice runs.
42
+ *
43
+ * Two, giving 49 children per cell and a 2.04% share granularity. That is deliberately matched to the finest share the
44
+ * authority itself publishes — `muaggatt.niccdcdpct`'s observed minimum is 2% — because a lattice finer than the
45
+ * source's own reporting grain buys precision this layer cannot source, at 7× the cost per level.
46
+ */
47
+ export declare const WEIGHT_LATTICE_DEPTH = 2;
48
+ /**
49
+ * Class shares below this are folded into `other_share` rather than stored.
50
+ *
51
+ * One percent, which sits BELOW the lattice's own 2.04% granularity, so nothing a single child cell produces is
52
+ * truncated — what lands here is the long tail that component percentages create inside a child (a 1%-weight component
53
+ * inside one child cell contributes 0.02%). Truncating a long tail is legitimate; doing it silently is not, which is
54
+ * why the remainder is stored explicitly and the shares still sum to 1.
55
+ */
56
+ export declare const CLASS_SHARE_FLOOR = 0.01;
57
+ /**
58
+ * One delineation reaching a cell, as the reduction needs it.
59
+ */
60
+ export interface CellCandidate {
61
+ areaID: string;
62
+ mukey: string;
63
+ containment: string;
64
+ minLat: number;
65
+ minLon: number;
66
+ maxLat: number;
67
+ maxLon: number;
68
+ rings: Uint8Array;
69
+ }
70
+ /**
71
+ * What a map unit contributes per unit of area — computed once per map unit and reused for every cell it reaches.
72
+ */
73
+ export interface MapUnitProfile {
74
+ /**
75
+ * Class code → share of the map unit, summing to 1 across classes and the three mapped-absence buckets.
76
+ */
77
+ classShares: ReadonlyMap<string, number>;
78
+ unrated: number;
79
+ notRateable: number;
80
+ noData: number;
81
+ }
82
+ /**
83
+ * Turn one map unit and its components into the per-unit-area profile the reduction folds in.
84
+ *
85
+ * A `no_mapping` map unit contributes wholly to `nodata` and NEVER to a class: it is a polygon the authority drew with
86
+ * no soil mapping behind it, and reading it as a low class would be the reassuring wrong number §3.2 of the survey is
87
+ * about.
88
+ *
89
+ * The split across components is by `comppct_r`, the component's representative percentage of its map unit, normalized
90
+ * by the total actually present rather than assumed to be 100 — measured on `IA153` all 152 map units sum to exactly
91
+ * 100, and a national build must not depend on that holding everywhere.
92
+ */
93
+ export declare function mapUnitProfile(mapUnit: Pick<SoilMapUnitTable, "no_mapping">, components: ReadonlyArray<Pick<SoilComponentTable, "comppct_r" | "compkind" | "nirrcapcl">>): MapUnitProfile;
94
+ /**
95
+ * What one cell's reduction produced, plus the diagnostics the receipt reports.
96
+ */
97
+ export interface ReducedCell {
98
+ row: SoilCapabilityCellTable;
99
+ /**
100
+ * True when the top class covers less than half the cell — the §4.7 number, counted here so it comes off the artifact
101
+ * rather than out of a separate harness.
102
+ */
103
+ topClassUnderHalf: boolean;
104
+ /**
105
+ * True when the lattice was used rather than the whole-cell fast path.
106
+ */
107
+ sampled: boolean;
108
+ }
109
+ /**
110
+ * Reduce one cell.
111
+ *
112
+ * @throws {Error} When a candidate names a map unit the profile map does not hold. A missing profile means the
113
+ * attribute join is short, and answering with the remaining candidates would report a well-formed distribution over
114
+ * part of the cell.
115
+ */
116
+ export declare function reduceCell(cell: H3Cell, resolution: number, candidates: ReadonlyArray<CellCandidate>, profiles: ReadonlyMap<string, MapUnitProfile>, h3Cell: number): ReducedCell;
117
+ /**
118
+ * The sum of a stored row's five shares. Exported because the invariant it checks — that they sum to 1 — is what makes
119
+ * `other_share` required rather than decorative, and a test that could not state the sum could not pin it.
120
+ */
121
+ export declare function shareTotal(row: SoilCapabilityCellTable): number;
122
+ //# sourceMappingURL=reduce.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reduce.d.ts","sourceRoot":"","sources":["../../lib/sdk/reduce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAGH,OAAO,EAAuB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAGrE,OAAO,KAAK,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAG5F;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,IAAI,CAAA;AAErC;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,OAAO,CAAA;AAErC;;GAEG;AACH,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,UAAU,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B;;OAEG;IACH,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACxC,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;CACd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,IAAI,CAAC,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