@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
85 lines (75 loc) • 1.91 kB
text/typescript
import { describe, test, expect, vi, afterEach, assert } from "vitest";
import { clipToLand } from "./clipToLand.js";
import { area, polygon } from "@turf/turf";
const landFeature = polygon([
[
[],
[],
[],
[],
[],
],
]);
// fully outside landFeature
const outside = polygon([
[
[],
[],
[],
[],
[],
],
]);
// half inside landFeature
const halfInside = polygon([
[
[],
[],
[],
[],
[],
],
]);
// fully inside landFeature
const inside = polygon([
[
[],
[],
[],
[],
[],
],
]);
describe("clipToLand", () => {
afterEach(() => {
vi.restoreAllMocks();
});
// Mock loadFgb method to return landFeature
// @ts-ignore
vi.mock(import("@seasketch/geoprocessing"), async (importOriginal) => {
const actual = await importOriginal();
const loadFgb = vi.fn(() => [landFeature]);
return { ...actual, loadFgb };
});
test("clipToLand - feature outside of land should throw", async () => {
try {
await clipToLand(outside);
} catch (error: unknown) {
if (error instanceof Error) {
expect(error.message).toBe("Feature is outside of land boundary");
return;
}
}
assert.fail("This should not be reached");
});
test("clipToLand - feature inside of land boundary should return entire", async () => {
const result = await clipToLand(inside);
expect(result).toEqual(inside);
expect(area(result)).toBe(area(inside));
});
test("clipToLand - feature partially inside of land boundary should return clipped", async () => {
const result = await clipToLand(halfInside);
// Expect half outside land to be clipped off and land-side polygon with approximately half the area to be returned
expect(area(result)).toBeCloseTo(area(inside));
});
});