From 3ed5b230649738fc70606f11f2ca2d930b21aa7d Mon Sep 17 00:00:00 2001 From: James Beard Date: Sat, 21 Dec 2024 18:13:02 +1100 Subject: [PATCH] Fixed issue where a polygon that got cleaned down to a single line segment 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. --- packages/turf-boolean-disjoint/index.ts | 1 - packages/turf-clean-coords/index.ts | 8 +-- packages/turf-clean-coords/package.json | 2 +- packages/turf-clean-coords/test.ts | 38 ++++++++++++- packages/turf-meta/test.ts | 2 +- pnpm-lock.yaml | 74 +++++++++++++++++++++++-- 6 files changed, 110 insertions(+), 15 deletions(-) diff --git a/packages/turf-boolean-disjoint/index.ts b/packages/turf-boolean-disjoint/index.ts index 60e89c23ff..4bcc07c426 100644 --- a/packages/turf-boolean-disjoint/index.ts +++ b/packages/turf-boolean-disjoint/index.ts @@ -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); diff --git a/packages/turf-clean-coords/index.ts b/packages/turf-clean-coords/index.ts index 8e02add71b..9c8660252c 100644 --- a/packages/turf-clean-coords/index.ts +++ b/packages/turf-clean-coords/index.ts @@ -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. @@ -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; diff --git a/packages/turf-clean-coords/package.json b/packages/turf-clean-coords/package.json index 3af6e79fb0..72f767e74b 100644 --- a/packages/turf-clean-coords/package.json +++ b/packages/turf-clean-coords/package.json @@ -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", diff --git a/packages/turf-clean-coords/test.ts b/packages/turf-clean-coords/test.ts index 8244d62c28..8bd99215f9 100644 --- a/packages/turf-clean-coords/test.ts +++ b/packages/turf-clean-coords/test.ts @@ -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, @@ -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 ); }); @@ -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([ [ @@ -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(); +}); diff --git a/packages/turf-meta/test.ts b/packages/turf-meta/test.ts index a8589fe0e3..8c59c0e0bd 100644 --- a/packages/turf-meta/test.ts +++ b/packages/turf-meta/test.ts @@ -532,7 +532,7 @@ test("null geometries", (t) => { 0, "coordReduce" ); - /* eslint-enable no-return-assign */ + t.end(); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e92dab0aea..ca8de70d22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2073,9 +2073,6 @@ importers: specifier: ^2.8.1 version: 2.8.1 devDependencies: - '@turf/boolean-equal': - specifier: workspace:^ - version: link:../turf-boolean-equal '@turf/truncate': specifier: workspace:^ version: link:../turf-truncate @@ -2088,6 +2085,9 @@ importers: benchmark: specifier: ^2.1.4 version: 2.1.4 + geojson-equality-ts: + specifier: ^1.0.2 + version: 1.0.2 load-json-file: specifier: ^7.0.1 version: 7.0.1 @@ -2099,7 +2099,7 @@ importers: version: 5.9.0 tsup: specifier: ^8.3.5 - version: 8.3.5(postcss@8.4.40)(tsx@4.19.2)(typescript@5.5.4) + version: 8.3.5(tsx@4.19.2)(typescript@5.5.4) tsx: specifier: ^4.19.2 version: 4.19.2 @@ -12103,7 +12103,6 @@ packages: resolution: {integrity: sha512-h3Ryq+0mCSN/7yLs0eDgrZhvc9af23o/QuC4aTiuuzP/MRCtd6mf5rLsLRY44jX0RPUfM8c4GqERQmlUxPGPoQ==} dependencies: '@types/geojson': 7946.0.14 - dev: false /geojson-polygon-self-intersections@1.2.1: resolution: {integrity: sha512-/QM1b5u2d172qQVO//9CGRa49jEmclKEsYOQmWP9ooEjj63tBM51m2805xsbxkzlEELQ2REgTf700gUhhlegxA==} @@ -15416,6 +15415,28 @@ packages: tsx: 4.19.2 dev: true + /postcss-load-config@6.0.1(tsx@4.19.2): + resolution: {integrity: sha512-oPtTM4oerL+UXmx+93ytZVN82RrlY/wPUV8IeDxFrzIjXOLF1pN+EmKPLbubvKHT2HC20xXsCAH2Z+CKV6Oz/g==} + engines: {node: '>= 18'} + peerDependencies: + jiti: '>=1.21.0' + postcss: '>=8.0.9' + tsx: ^4.8.1 + yaml: ^2.4.2 + peerDependenciesMeta: + jiti: + optional: true + postcss: + optional: true + tsx: + optional: true + yaml: + optional: true + dependencies: + lilconfig: 3.1.3 + tsx: 4.19.2 + dev: true + /postcss-selector-parser@6.1.2: resolution: {integrity: sha512-Q8qQfPiZ+THO/3ZrOrO0cJJKfpYCagtMUkXbnEfmgUjwXg6z/WBeOyS9APBBPCTSiDV+s4SwQGu8yFsiMRIudg==} engines: {node: '>=4'} @@ -17030,6 +17051,49 @@ packages: - yaml dev: true + /tsup@8.3.5(tsx@4.19.2)(typescript@5.5.4): + resolution: {integrity: sha512-Tunf6r6m6tnZsG9GYWndg0z8dEV7fD733VBFzFJ5Vcm1FtlXB8xBD/rtrBi2a3YKEV7hHtxiZtW5EAVADoe1pA==} + engines: {node: '>=18'} + hasBin: true + peerDependencies: + '@microsoft/api-extractor': ^7.36.0 + '@swc/core': ^1 + postcss: ^8.4.12 + typescript: '>=4.5.0' + peerDependenciesMeta: + '@microsoft/api-extractor': + optional: true + '@swc/core': + optional: true + postcss: + optional: true + typescript: + optional: true + dependencies: + bundle-require: 5.0.0(esbuild@0.24.0) + cac: 6.7.14 + chokidar: 4.0.1 + consola: 3.2.3 + debug: 4.3.7 + esbuild: 0.24.0 + joycon: 3.1.1 + picocolors: 1.1.1 + postcss-load-config: 6.0.1(tsx@4.19.2) + resolve-from: 5.0.0 + rollup: 4.28.0 + source-map: 0.8.0-beta.0 + sucrase: 3.35.0 + tinyexec: 0.3.1 + tinyglobby: 0.2.10 + tree-kill: 1.2.2 + typescript: 5.5.4 + transitivePeerDependencies: + - jiti + - supports-color + - tsx + - yaml + dev: true + /tsx@4.19.2: resolution: {integrity: sha512-pOUl6Vo2LUq/bSa8S5q7b91cgNSjctn9ugq/+Mvow99qW6x/UZYwzxy/3NmqoT66eHYfCVvFvACC58UBPFf28g==} engines: {node: '>=18.0.0'}