Skip to content

Commit

Permalink
Fixed issue where a polygon that got cleaned down to a single line se…
Browse files Browse the repository at this point in the history
…gment was causing errors - started checking number of points before doing other tests. Replaced our own booleanEquals with external geojsonEquality. booleanEquals uses cleanCoords, which we're using to check ... cleanCoords. Fixed a couple of linting issues.
  • Loading branch information
smallsaucepan committed Dec 21, 2024
1 parent c454a03 commit 3ed5b23
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/turf-boolean-disjoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ function booleanDisjoint(
function disjoint(geom1: any, geom2: any, ignoreSelfIntersections: boolean) {
switch (geom1.type) {
case "Point":
/* eslint-disable @typescript-eslint/no-unused-vars */
switch (geom2.type) {
case "Point":
return !compareCoords(geom1.coordinates, geom2.coordinates);
Expand Down
8 changes: 4 additions & 4 deletions packages/turf-clean-coords/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function cleanLine(line: Position[], type: string) {
if (
booleanPointOnLine(
newPoints[0],
lineString([newPoints[1], points[newPoints.length - 2]])
lineString([newPoints[1], newPoints[newPoints.length - 2]])
)
) {
newPoints.shift(); // Discard starting point.
Expand All @@ -158,12 +158,12 @@ function cleanLine(line: Position[], type: string) {
}

// (Multi)Polygons must have at least 4 points and be closed.
if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {
throw new Error("invalid polygon, first and last points not equal");
}
if (newPoints.length < 4) {
throw new Error("invalid polygon, fewer than 4 points");
}
if (!equals(newPoints[0], newPoints[newPoints.length - 1])) {
throw new Error("invalid polygon, first and last points not equal");
}
}

return newPoints;
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-clean-coords/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
},
"devDependencies": {
"@turf/boolean-equal": "workspace:^",
"@turf/truncate": "workspace:^",
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.13.4",
"benchmark": "^2.1.4",
"geojson-equality-ts": "^1.0.2",
"load-json-file": "^7.0.1",
"npm-run-all": "^4.1.5",
"tape": "^5.9.0",
Expand Down
38 changes: 35 additions & 3 deletions packages/turf-clean-coords/test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from "fs";
import test from "tape";
import path from "path";
import { geojsonEquality } from "geojson-equality-ts";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { truncate } from "@turf/truncate";
import { booleanEqual } from "@turf/boolean-equal";
import {
point,
multiPoint,
Expand Down Expand Up @@ -40,7 +40,7 @@ test("turf-clean-coords", (t) => {
if (process.env.REGEN)
writeJsonFileSync(directories.out + filename, results);
t.true(
booleanEqual(results, loadJsonFileSync(directories.out + filename)),
geojsonEquality(results, loadJsonFileSync(directories.out + filename)),
name
);
});
Expand Down Expand Up @@ -257,7 +257,7 @@ test("turf-clean-coords - overly aggressive removal - issue 2740", (t) => {

test("turf-clean-coords - start point protected - issue 2406", (t) => {
t.true(
booleanEqual(
geojsonEquality(
cleanCoords(
polygon([
[
Expand Down Expand Up @@ -286,3 +286,35 @@ test("turf-clean-coords - start point protected - issue 2406", (t) => {

t.end();
});

test("turf-clean-coords - multipolygon - issue #918", (t) => {
// Copied from turf-simplify as (at heart) it's cleanCoords that's being
// tested here.
// simplify hangs on this input #918
t.throws(
() =>
cleanCoords(
multiPolygon([
[
[
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
[0, 90],
],
],
])
),
/invalid polygon/,
"invalid polygon"
);

t.end();
});
2 changes: 1 addition & 1 deletion packages/turf-meta/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ test("null geometries", (t) => {
0,
"coordReduce"
);
/* eslint-enable no-return-assign */

t.end();
});

Expand Down
74 changes: 69 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ed5b23

Please sign in to comment.