@@ -6,19 +6,18 @@ import { AllGeoJSON, isObject } from "@turf/helpers";
66import { simplify as simplifyJS } from "./lib/simplify.js" ;
77
88/**
9- * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of
10- * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.
11- *
9+ * Simplifies the geometries in a GeoJSON object. Uses the 2d version of
10+ * [simplify-js](https://mourner.github.io/simplify-js/).
1211 *
1312 * @function
14- * @param {GeoJSON } geojson object to be simplified
13+ * @param {GeoJSON } geojson GeoJSON object to be simplified
1514 * @param {Object } [options={}] Optional parameters
16- * @param {number } [options.tolerance=1] simplification tolerance
17- * @param {boolean } [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
18- * @param {boolean } [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
19- * @returns {GeoJSON } a simplified GeoJSON
15+ * @param {number } [options.tolerance=1] Simplification tolerance
16+ * @param {boolean } [options.highQuality=false] Produce a higher-quality simplification using a slower algorithm
17+ * @param {boolean } [options.mutate=false] Allow GeoJSON input to be mutated (significant performance improvement if true)
18+ * @returns {GeoJSON } Simplified GeoJSON
2019 * @example
21- * var geojson = turf.polygon([[
20+ * const geojson = turf.polygon([[
2221 * [-70.603637, -33.399918],
2322 * [-70.614624, -33.395332],
2423 * [-70.639343, -33.392466],
@@ -40,11 +39,11 @@ import { simplify as simplifyJS } from "./lib/simplify.js";
4039 * [-70.594711, -33.406224],
4140 * [-70.603637, -33.399918]
4241 * ]]);
43- * var options = {tolerance: 0.01, highQuality: false} ;
44- * var simplified = turf.simplify(geojson, options );
42+ * const result0_01 = turf.simplify(geojson, {tolerance: 0.01 }) ;
43+ * const result0_005 = turf.simplify(geojson, {tolerance: 0.005 } );
4544 *
4645 * //addToMap
47- * var addToMap = [geojson, simplified ]
46+ * const addToMap = [geojson, result0_01, result0_005 ]
4847 */
4948function simplify < T extends AllGeoJSON > (
5049 geojson : T ,
@@ -147,11 +146,20 @@ function simplifyPolygon(
147146 }
148147 let ringTolerance = tolerance ;
149148 let simpleRing = simplifyJS ( ring , ringTolerance , highQuality ) ;
150- // remove 1 percent of tolerance until enough points to make a triangle
151- while ( ! checkValidity ( simpleRing ) ) {
149+
150+ // If simplified ring isn't valid (has been over simplified) reduce the
151+ // tolerance by 1% and try again.
152+ while ( ! checkValidity ( simpleRing ) && ringTolerance >= Number . EPSILON ) {
152153 ringTolerance -= ringTolerance * 0.01 ;
153154 simpleRing = simplifyJS ( ring , ringTolerance , highQuality ) ;
154155 }
156+
157+ // If ring wasn't able to be simplified in a valid way, return it unchanged.
158+ if ( ! checkValidity ( simpleRing ) ) {
159+ return ring ;
160+ }
161+
162+ // Close the ring if it wasn't already.
155163 if (
156164 simpleRing [ simpleRing . length - 1 ] [ 0 ] !== simpleRing [ 0 ] [ 0 ] ||
157165 simpleRing [ simpleRing . length - 1 ] [ 1 ] !== simpleRing [ 0 ] [ 1 ]
0 commit comments