diff --git a/packages/turf-nearest-neighbor-analysis/README.md b/packages/turf-nearest-neighbor-analysis/README.md index 0536682bd..25121b22a 100644 --- a/packages/turf-nearest-neighbor-analysis/README.md +++ b/packages/turf-nearest-neighbor-analysis/README.md @@ -30,7 +30,7 @@ Type: [object][1] ## nearestNeighborAnalysis -Nearest Neighbor Analysis calculates an index based the average distances +Nearest Neighbor Analysis calculates an index based on the average distances between points in the dataset, thereby providing inference as to whether the data is clustered, dispersed, or randomly distributed within the study area. diff --git a/packages/turf-simplify/README.md b/packages/turf-simplify/README.md index 42e533702..c8349ea97 100644 --- a/packages/turf-simplify/README.md +++ b/packages/turf-simplify/README.md @@ -4,22 +4,22 @@ ## simplify -Takes a [GeoJSON][1] object and returns a simplified version. Internally uses the 2d version of -[simplify-js][2] to perform simplification using the Ramer-Douglas-Peucker algorithm. +Simplifies the geometries in a GeoJSON object. Uses the 2d version of +[simplify-js][1]. ### Parameters -* `geojson` **[GeoJSON][1]** object to be simplified +* `geojson` **[GeoJSON][2]** GeoJSON object to be simplified * `options` **[Object][3]** Optional parameters (optional, default `{}`) - * `options.tolerance` **[number][4]** simplification tolerance (optional, default `1`) - * `options.highQuality` **[boolean][5]** whether or not to spend more time to create a higher-quality simplification with a different algorithm (optional, default `false`) - * `options.mutate` **[boolean][5]** allows GeoJSON input to be mutated (significant performance increase if true) (optional, default `false`) + * `options.tolerance` **[number][4]** Simplification tolerance (optional, default `1`) + * `options.highQuality` **[boolean][5]** Produce a higher-quality simplification using a slower algorithm (optional, default `false`) + * `options.mutate` **[boolean][5]** Allow GeoJSON input to be mutated (significant performance improvement if true) (optional, default `false`) ### Examples ```javascript -var geojson = turf.polygon([[ +const geojson = turf.polygon([[ [-70.603637, -33.399918], [-70.614624, -33.395332], [-70.639343, -33.392466], @@ -41,18 +41,18 @@ var geojson = turf.polygon([[ [-70.594711, -33.406224], [-70.603637, -33.399918] ]]); -var options = {tolerance: 0.01, highQuality: false}; -var simplified = turf.simplify(geojson, options); +const result0_01 = turf.simplify(geojson, {tolerance: 0.01 }); +const result0_005 = turf.simplify(geojson, {tolerance: 0.005 }); //addToMap -var addToMap = [geojson, simplified] +const addToMap = [geojson, result0_01, result0_005] ``` -Returns **[GeoJSON][1]** a simplified GeoJSON +Returns **[GeoJSON][2]** Simplified GeoJSON -[1]: https://tools.ietf.org/html/rfc7946#section-3 +[1]: https://mourner.github.io/simplify-js/ -[2]: http://mourner.github.io/simplify-js/ +[2]: https://tools.ietf.org/html/rfc7946#section-3 [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object diff --git a/packages/turf-simplify/index.ts b/packages/turf-simplify/index.ts index 0556207ee..0efa19e46 100644 --- a/packages/turf-simplify/index.ts +++ b/packages/turf-simplify/index.ts @@ -6,19 +6,18 @@ import { AllGeoJSON, isObject } from "@turf/helpers"; import { simplify as simplifyJS } from "./lib/simplify.js"; /** - * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of - * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm. - * + * Simplifies the geometries in a GeoJSON object. Uses the 2d version of + * [simplify-js](https://mourner.github.io/simplify-js/). * * @function - * @param {GeoJSON} geojson object to be simplified + * @param {GeoJSON} geojson GeoJSON object to be simplified * @param {Object} [options={}] Optional parameters - * @param {number} [options.tolerance=1] simplification tolerance - * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm - * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true) - * @returns {GeoJSON} a simplified GeoJSON + * @param {number} [options.tolerance=1] Simplification tolerance + * @param {boolean} [options.highQuality=false] Produce a higher-quality simplification using a slower algorithm + * @param {boolean} [options.mutate=false] Allow GeoJSON input to be mutated (significant performance improvement if true) + * @returns {GeoJSON} Simplified GeoJSON * @example - * var geojson = turf.polygon([[ + * const geojson = turf.polygon([[ * [-70.603637, -33.399918], * [-70.614624, -33.395332], * [-70.639343, -33.392466], @@ -40,11 +39,11 @@ import { simplify as simplifyJS } from "./lib/simplify.js"; * [-70.594711, -33.406224], * [-70.603637, -33.399918] * ]]); - * var options = {tolerance: 0.01, highQuality: false}; - * var simplified = turf.simplify(geojson, options); + * const result0_01 = turf.simplify(geojson, {tolerance: 0.01 }); + * const result0_005 = turf.simplify(geojson, {tolerance: 0.005 }); * * //addToMap - * var addToMap = [geojson, simplified] + * const addToMap = [geojson, result0_01, result0_005] */ function simplify( geojson: T, @@ -147,11 +146,20 @@ function simplifyPolygon( } let ringTolerance = tolerance; let simpleRing = simplifyJS(ring, ringTolerance, highQuality); - // remove 1 percent of tolerance until enough points to make a triangle - while (!checkValidity(simpleRing)) { + + // If simplified ring isn't valid (has been over simplified) reduce the + // tolerance by 1% and try again. + while (!checkValidity(simpleRing) && ringTolerance >= Number.EPSILON) { ringTolerance -= ringTolerance * 0.01; simpleRing = simplifyJS(ring, ringTolerance, highQuality); } + + // If ring wasn't able to be simplified in a valid way, return it unchanged. + if (!checkValidity(simpleRing)) { + return ring; + } + + // Close the ring if it wasn't already. if ( simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] || simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1] diff --git a/packages/turf-simplify/test.ts b/packages/turf-simplify/test.ts index e033c573a..feed0c2dd 100644 --- a/packages/turf-simplify/test.ts +++ b/packages/turf-simplify/test.ts @@ -214,3 +214,60 @@ test("simplify -- issue #918", (t) => { ); t.end(); }); + +test("simplify - issue 1788 - infinite loop", (t) => { + // For particularly small polygons simplify was getting stuck in an + // infinite loop. + + // https://github.com/Turfjs/turf/issues/1788#issue-521052548 + const poly1 = polygon([ + [ + [11.662180661499999, 50.1081498005], + [11.662192661499999, 50.108041800500004], + [11.6621866615, 50.1080958005], + [11.662180661499999, 50.1081498005], + ], + ]); + simplify(poly1, { + tolerance: 0.000001, + highQuality: true, + mutate: false, + }); + t.pass("issue 1788 test 1 didn't hang"); + + // https://github.com/Turfjs/turf/issues/1788#issuecomment-951109683 + const poly2 = polygon([ + [ + [4.0881641, 6.8121605], + [4.0881639, 6.8121607], + [4.0881638, 6.8121608], + [4.0881641, 6.8121605], + ], + ]); + simplify(poly2); + t.pass("issue 1788 test 2 didn't hang"); + + // https://github.com/Turfjs/turf/issues/1788#issuecomment-1362073785 + const poly3 = multiPolygon([ + [ + [ + [-10.06762725, -17.428977611], + [-10.067626613, -17.428977323], + [-10.067625943, -17.428976957], + [-10.06762725, -17.428977611], + ], + ], + [ + [ + [-10.067625943, -17.428976957], + [-10.067599027, -17.428963499], + [-10.067625941, -17.428976956], + [-10.067625943, -17.428976957], + ], + ], + ]); + simplify(poly3, { tolerance: 0.00001, highQuality: true }); + t.pass("issue 1788 test 3 didn't hang"); + + t.end(); +});