fix(geometry): preserve polygon holes

This commit is contained in:
t8y2
2026-06-25 00:37:59 +08:00
parent 5f7a6096a9
commit a0df43c391
2 changed files with 57 additions and 1 deletions
@@ -0,0 +1,54 @@
import { describe, expect, it } from "vitest";
import { wktToGeoJson } from "@/lib/geometryPreview";
describe("wktToGeoJson", () => {
it("preserves polygon interior rings", () => {
const geometry = wktToGeoJson("POLYGON((0 0,10 0,10 10,0 10,0 0),(2 2,8 2,8 8,2 8,2 2))");
expect(geometry).toEqual({
type: "Polygon",
coordinates: [
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0],
],
[
[2, 2],
[8, 2],
[8, 8],
[2, 8],
[2, 2],
],
],
});
});
it("preserves multipolygon interior rings", () => {
const geometry = wktToGeoJson("MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(2 2,8 2,8 8,2 8,2 2)))");
expect(geometry).toEqual({
type: "MultiPolygon",
coordinates: [
[
[
[0, 0],
[10, 0],
[10, 10],
[0, 10],
[0, 0],
],
[
[2, 2],
[8, 2],
[8, 8],
[2, 8],
[2, 2],
],
],
],
});
});
});
+3 -1
View File
@@ -73,8 +73,10 @@ function parseGroups(tokens: (string | number)[], start: number): { groups: Geom
i = inner.end;
}
if (i < tokens.length && tokens[i] === ")") i++;
} else if (tokens[i] === ")" || tokens[i] === ",") {
} else if (tokens[i] === ")") {
break;
} else if (tokens[i] === ",") {
i++;
} else if (typeof tokens[i] === "number") {
const seq = parseCoordSequence(tokens, i);
groups.push({ coords: seq.coords, children: [] });