@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
84 lines • 3.33 kB
JavaScript
import { isSketchCollection } from "../helpers/index.js";
import { createMetric } from "../metrics/index.js";
import { featureCollection, featureEach, area as turfArea } from "@turf/turf";
import { clip } from "./clip.js";
/**
* Calculates the area of each sketch and collection.
*/
export async function area(
/** single sketch or collection. */
sketch, options = {}) {
const { metricId = "area", includeChildMetrics = true, includePercMetric = false, } = options;
const percMetricId = `${metricId}Perc`;
// if collection - union to remove overlap
const combinedSketch = isSketchCollection(sketch)
? clip(sketch, "union")
: featureCollection([sketch]);
if (!combinedSketch)
throw new Error("Invalid sketch");
const combinedSketchArea = turfArea(combinedSketch);
const sketchMetrics = [];
if (sketch) {
featureEach(sketch, (curSketch) => {
if (!curSketch || !curSketch.properties) {
console.log("Warning: feature or its properties are undefined, skipped");
}
else if (curSketch.geometry) {
const sketchArea = turfArea(curSketch);
sketchMetrics.push(createMetric({
metricId,
sketchId: curSketch.properties.id,
value: sketchArea,
extra: {
sketchName: curSketch.properties.name,
},
}));
if (includePercMetric && isSketchCollection(sketch)) {
sketchMetrics.push(createMetric({
metricId: percMetricId,
sketchId: curSketch.properties.id,
value: sketchArea / combinedSketchArea,
extra: {
sketchName: curSketch.properties.name,
},
}));
}
}
else {
console.log(`Warning: feature is missing geometry, zeroed: sketchId:${curSketch.properties.id}, name:${curSketch.properties.name}`);
sketchMetrics.push(createMetric({
metricId,
sketchId: curSketch.properties.id,
value: 0,
extra: {
sketchName: curSketch.properties.name,
},
}));
if (includePercMetric) {
sketchMetrics.push(createMetric({
metricId: percMetricId,
sketchId: curSketch.properties.id,
value: 0,
extra: {
sketchName: curSketch.properties.name,
},
}));
}
}
});
}
const collMetrics = [];
if (isSketchCollection(sketch)) {
collMetrics.push(createMetric({
metricId,
sketchId: sketch.properties.id,
value: combinedSketchArea,
extra: {
sketchName: sketch.properties.name,
isCollection: true,
},
}));
}
return [...(includeChildMetrics ? sketchMetrics : []), ...collMetrics];
}
//# sourceMappingURL=area.js.map