Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/turf-nearest-neighbor-analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was already in the index.ts comment, just not populated into the readme. I thought we had pre-commit hooks to make sure that didn't happen. Oh well.

between points in the dataset, thereby providing inference as to whether the
data is clustered, dispersed, or randomly distributed within the study area.

Expand Down
26 changes: 13 additions & 13 deletions packages/turf-simplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down
36 changes: 22 additions & 14 deletions packages/turf-simplify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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<T extends AllGeoJSON>(
geojson: T,
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about this leaving impossibly tiny polygons with a large number of points, but EPSILON is so much smaller than any meaningful coordinate delta that this is probably a nonissue.

> area({type: "Polygon", coordinates: [[0,0], [0, Number.EPSILON], [Number.EPSILON, Number.EPSILON], [Number.EPSILON, 0], [0, 0]]})
0

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]
Expand Down
57 changes: 57 additions & 0 deletions packages/turf-simplify/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});