@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
519 lines (509 loc) • 17.4 kB
text/typescript
import { clipToGeography } from "./clipToGeography.js";
import project from "../../project/projectClient.js";
import { bbox, area } from "@turf/turf";
import {
Polygon,
Sketch,
genSampleSketch,
genSketchCollection,
} from "@seasketch/geoprocessing";
import { describe, test, expect } from "vitest";
const sketch: Sketch<Polygon> = genSampleSketch<Polygon>(
{
type: "Polygon",
coordinates: [
[
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
],
[
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
],
[
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
],
],
},
"foo",
);
const noOverlapSketch = genSampleSketch<Polygon>({
type: "Polygon",
coordinates: [
[
[],
[],
[],
[],
[],
],
],
});
describe("clipToGeography", () => {
test("clipToGeography - with world polygon should not change the polygon", async () => {
const curGeography = project.getGeographyById("world");
const sketchArea = area(sketch);
const sketchBox = sketch.bbox || bbox(sketch);
const clippedSketch = await clipToGeography(sketch, curGeography);
const clippedSketchArea = area(clippedSketch);
const clippedSketchBox = clippedSketch.bbox || bbox(clippedSketch);
expect(clippedSketchArea).toEqual(sketchArea);
expect(sketchBox).toEqual(clippedSketchBox);
}, 10_000);
test("clipToGeography - no overlap", async () => {
const curGeography = project.getGeographyById("world");
const sketchArea = area(noOverlapSketch);
const clippedSketch = await clipToGeography(noOverlapSketch, curGeography);
const clippedSketchArea = area(clippedSketch);
const clippedSketchBox = clippedSketch.bbox || bbox(clippedSketch);
// Clipped sketch should be zero-ed
expect(sketchArea === clippedSketchArea).toBe(false);
expect(clippedSketchArea).toEqual(0);
expect(clippedSketchBox.every((v) => v === 0)).toBe(true);
}, 60_000);
test("clipToGeography - sketch collection", async () => {
// Sketch collection with overlapping and non-overlapping polygons
const curGeography = project.getGeographyById("world");
const sketchArea = area(sketch);
const sketchBox = sketch.bbox || bbox(sketch);
const sketchCollection = genSketchCollection([sketch, noOverlapSketch]);
const clippedSketchCollection = await clipToGeography(
sketchCollection,
curGeography,
);
const clippedSketchCollectionArea = area(clippedSketchCollection);
const clippedSketchCollectionBox =
clippedSketchCollection.bbox || bbox(clippedSketchCollection);
expect(sketchArea === clippedSketchCollectionArea).toBe(true);
for (const [i, bboxCoord] of sketchBox.entries())
expect(bboxCoord).toBeCloseTo(clippedSketchCollectionBox[i]);
});
});