@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
64 lines • 3.06 kB
JavaScript
import { roundDecimal } from "../../helpers/index.js";
import { createMetric } from "../../metrics/index.js";
/**
* Converts an array of geoblaze raster StatsObject to an array of Metrics
* @param statsObjects
* @param sketchId
* @param extra
*/
export const rasterStatsToMetrics = (statsObjects, options = {}) => {
const { metricId, metricIdPrefix = "", metricPartial = {}, truncate = true, bandMetricProperty = "groupId", bandMetricValues = [
...Array.from({ length: statsObjects.length }).keys(),
].map((x) => `band-${x}`), categorical = false, categoryMetricProperty = "classId", categoryMetricValues, } = options;
const metrics = [];
if (bandMetricProperty === categoryMetricProperty)
throw new Error("bandMetricProperty and categoryMetricProperty cannot be the same");
for (const [band, curStats] of statsObjects.entries()) {
const statNames = Object.keys(curStats);
for (const statName of statNames) {
const value = curStats[statName];
if (categorical) {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
categoryMetricValues
? categoryMetricValues.forEach((category) => {
metrics.push(createMetric({
metricId: metricId ?? `${metricIdPrefix}valid`,
value: truncate
? roundDecimal(value[category], 6, {
keepSmallValues: true,
})
: value[category],
...metricPartial,
[bandMetricProperty]: bandMetricValues[band],
[categoryMetricProperty]: category,
}));
})
: Object.keys(value).forEach((category) => {
metrics.push(createMetric({
metricId: metricId ?? `${metricIdPrefix}valid`,
value: truncate
? roundDecimal(value[category], 6, {
keepSmallValues: true,
})
: value[category],
...metricPartial,
[bandMetricProperty]: bandMetricValues[band],
[categoryMetricProperty]: category,
}));
});
}
else {
metrics.push(createMetric({
metricId: metricId ?? `${metricIdPrefix}${statName}`,
value: truncate
? roundDecimal(value, 6, { keepSmallValues: true })
: value,
...metricPartial,
[bandMetricProperty]: bandMetricValues[band],
}));
}
}
}
return metrics;
};
//# sourceMappingURL=rasterStatsToMetrics.js.map