@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
59 lines • 2.19 kB
JavaScript
import { isSketchCollection, roundDecimal } from "../helpers/index.js";
import { createMetric } from "../metrics/index.js";
import { featureEach } from "@turf/turf";
import { getSum } from "./geoblaze/index.js";
/**
* Returns sum of cells overlapping sketch with raster as a metric object
* If sketch collection, then calculate overlap for all child sketches also
*/
export async function overlapRasterSum(
/** metricId value to assign to each measurement */
metricId,
/** Cloud-optimized geotiff to calculate overlap with, loaded via loadCog or geoblaze.parse() */
raster,
/** single sketch or collection to calculate metrics for. */
sketch, options) {
const newOptions = {
truncate: true,
...options,
};
// Get raster sum for each feature
const sumPromises = [];
const sumFeatures = [];
featureEach(sketch, async (feat) => {
// accumulate geoblaze sum promises and features so we can create metrics later
sumPromises.push(getSum(raster, feat));
sumFeatures.push(feat);
});
// await results and create metrics
const sketchMetrics = [];
for (const [index, curSum] of (await Promise.all(sumPromises)).entries()) {
sketchMetrics.push(createMetric({
metricId,
sketchId: sumFeatures[index].properties.id,
value: newOptions.truncate
? roundDecimal(curSum, 6, { keepSmallValues: true })
: curSum,
extra: {
sketchName: sumFeatures[index].properties.name,
},
}));
}
if (isSketchCollection(sketch)) {
// Push collection with accumulated sumValue
const collSumValue = await getSum(raster, sketch);
sketchMetrics.push(createMetric({
metricId,
sketchId: sketch.properties.id,
value: newOptions.truncate
? roundDecimal(collSumValue, 6, { keepSmallValues: true })
: collSumValue,
extra: {
sketchName: sketch.properties.name,
isCollection: true,
},
}));
}
return sketchMetrics;
}
//# sourceMappingURL=overlapRasterSum.js.map