Skip to content
Open
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
44 changes: 17 additions & 27 deletions packages/turf-clean-coords/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# @turf/clean-coords

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

## cleanCoords

Removes redundant coordinates from any GeoJSON Geometry.

### Parameters

* `geojson` **([Geometry][1] | [Feature][2])** Feature or Geometry
* `options` **[Object][3]** Optional parameters (optional, default `{}`)

* `options.mutate` **[boolean][4]** allows GeoJSON input to be mutated (optional, default `false`)

### Examples

```javascript
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->## CleanCoordsResultGeneric response type for cleanCoordsType: any## cleanCoords### Parameters* `geojson` **any**&#x20;
* `options` **{mutate: [boolean][1]?, epsilon: [number][2]?}?**&#x20;Returns **any** **Meta*** **deprecated**: loosely typed version deprecated. Will be removed in next major version## cleanCoordsRemoves redundant coordinates from any GeoJSON Type.Always returns the same geojson type that it receives.When operating on MultiPoint geometries will remove co-incident points.Obeys consistent structural sharing rules based on the `mutate` option:* Feature and Geometry (nested and top-level) objects will *always* be new
when mutate is false (default) and *always* return the provided object
when mutate is true (i.e., ===).
* bbox, id, and properties members on Features, and bbox on Geometries and
FeatureCollections will *always* be === to the original. Note that this
does not copy across other forrign members when mutate is false.
* Members other than the geometry(ies) or features members will always be
\=== to the original.
* Geometry(ies) and features members and their nested arrays will be reused
from the passed object or generated new based on performance and
simplicity in the implementation - and therefore will not be deep clones.### Parameters* `geojson` **[GeoJSON][3]** Any valid GeoJSON type
* `options` **[Object][4]** Optional parameters (optional, default `{}`)

* `options.mutate` **[boolean][1]** allows GeoJSON input to be mutated (optional, default `false`)
* `options.epsilon` **[number][2]?** Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points### Examples```javascript
var line = turf.lineString([[0, 0], [0, 2], [0, 5], [0, 8], [0, 8], [0, 10]]);
var multiPoint = turf.multiPoint([[0, 0], [0, 0], [2, 2]]);

Expand All @@ -24,17 +24,7 @@ turf.cleanCoords(line).geometry.coordinates;

turf.cleanCoords(multiPoint).geometry.coordinates;
//= [[0, 0], [2, 2]]
```

Returns **([Geometry][1] | [Feature][2])** the cleaned input Feature/Geometry

[1]: https://tools.ietf.org/html/rfc7946#section-3.1

[2]: https://tools.ietf.org/html/rfc7946#section-3.2

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
```Returns **([Geometry][5] | [Feature][6])** the cleaned input Feature/Geometry[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number[3]: https://tools.ietf.org/html/rfc7946#section-3[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object[5]: https://tools.ietf.org/html/rfc7946#section-3.1[6]: https://tools.ietf.org/html/rfc7946#section-3.2

<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->

Expand Down
96 changes: 41 additions & 55 deletions packages/turf-clean-coords/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,63 @@ import { fileURLToPath } from "url";
import { glob } from "glob";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { GeoJSON } from "geojson";
import { cleanCoords } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* Benchmark Results
*
* geometry: 0.675ms
* multiline: 0.044ms
* multipoint: 0.291ms
* multipolygon: 0.062ms
* point: 0.010ms
* polygon-with-hole: 0.017ms
* polygon: 0.010ms
* simple-line: 0.008ms
* triangle: 0.020ms
* Note - the original author benchmarked alternative methods of avoiding
* mutation and concluded that @turf/clone and JSON.parse were ~10x slower than
* direct use of geometry/features.
*
* // mutate=false (using geometry/feature)
* geometry x 1,524,640 ops/sec ±4.60% (74 runs sampled)
* multiline x 1,511,608 ops/sec ±8.79% (72 runs sampled)
* multipoint x 382,429 ops/sec ±3.56% (84 runs sampled)
* multipolygon x 808,277 ops/sec ±2.84% (82 runs sampled)
* point x 14,675,464 ops/sec ±4.42% (80 runs sampled)
* polygon-with-hole x 1,493,507 ops/sec ±5.53% (72 runs sampled)
* polygon x 2,386,278 ops/sec ±1.27% (86 runs sampled)
* simple-line x 4,195,499 ops/sec ±2.88% (86 runs sampled)
* triangle x 2,254,753 ops/sec ±1.10% (88 runs sampled)
* // mutate=false
* triplicate-issue1255 x 3,062,616 ops/sec ±0.39% (94 runs sampled)
* triangle x 2,082,533 ops/sec ±0.34% (96 runs sampled)
* simple-line x 3,779,713 ops/sec ±0.40% (95 runs sampled)
* segment x 6,582,228 ops/sec ±1.30% (82 runs sampled)
* polygon x 2,585,401 ops/sec ±0.25% (99 runs sampled)
* polygon-with-hole x 1,518,417 ops/sec ±0.34% (93 runs sampled)
* point x 26,543,371 ops/sec ±0.88% (89 runs sampled)
* multipolygon x 974,694 ops/sec ±1.27% (97 runs sampled)
* multipoint x 2,537,537 ops/sec ±0.56% (95 runs sampled)
* multiline x 1,974,913 ops/sec ±0.39% (93 runs sampled)
* line-3-coords x 6,701,852 ops/sec ±0.54% (97 runs sampled)
* geometry x 1,582,544 ops/sec ±1.38% (93 runs sampled)
* geometry-collection x 1,015,123 ops/sec ±0.30% (98 runs sampled)
* feature-collection x 945,614 ops/sec ±0.23% (95 runs sampled)
* closed-linestring x 8,822,010 ops/sec ±0.89% (93 runs sampled)
* clean-segment x 18,363,540 ops/sec ±0.68% (94 runs sampled)
*
* // mutate=false (using @turf/clone)
* geometry x 202,410 ops/sec ±1.43% (88 runs sampled)
* multiline x 235,421 ops/sec ±3.48% (86 runs sampled)
* multipoint x 280,757 ops/sec ±1.59% (87 runs sampled)
* multipolygon x 127,353 ops/sec ±1.35% (88 runs sampled)
* point x 18,233,987 ops/sec ±1.34% (86 runs sampled)
* polygon-with-hole x 199,203 ops/sec ±2.61% (84 runs sampled)
* polygon x 355,616 ops/sec ±1.58% (86 runs sampled)
* simple-line x 515,430 ops/sec ±2.40% (83 runs sampled)
* triangle x 286,315 ops/sec ±1.64% (86 runs sampled)
*
* // mutate=false (using JSON.parse + JSON.stringify)
* geometry x 93,681 ops/sec ±7.66% (75 runs sampled)
* multiline x 112,836 ops/sec ±4.60% (82 runs sampled)
* multipoint x 113,937 ops/sec ±1.09% (90 runs sampled)
* multipolygon x 71,131 ops/sec ±1.32% (90 runs sampled)
* point x 18,181,612 ops/sec ±1.36% (91 runs sampled)
* polygon-with-hole x 100,011 ops/sec ±1.14% (85 runs sampled)
* polygon x 154,331 ops/sec ±1.31% (89 runs sampled)
* simple-line x 193,304 ops/sec ±1.33% (90 runs sampled)
* triangle x 130,921 ops/sec ±3.37% (87 runs sampled)
*
* // mutate=true
* geometry x 2,016,365 ops/sec ±1.83% (85 runs sampled)
* multiline x 2,266,787 ops/sec ±3.69% (79 runs sampled)
* multipoint x 411,246 ops/sec ±0.81% (89 runs sampled)
* multipolygon x 1,011,846 ops/sec ±1.34% (85 runs sampled)
* point x 17,635,961 ops/sec ±1.47% (89 runs sampled)
* polygon-with-hole x 2,110,166 ops/sec ±1.59% (89 runs sampled)
* polygon x 2,887,298 ops/sec ±1.75% (86 runs sampled)
* simple-line x 7,109,682 ops/sec ±1.52% (87 runs sampled)
* triangle x 3,116,940 ops/sec ±0.71% (87 runs sampled)
* triplicate-issue1255 x 5,643,789 ops/sec ±0.45% (95 runs sampled)
* triangle x 6,429,094 ops/sec ±0.47% (95 runs sampled)
* simple-line x 22,923,304 ops/sec ±0.84% (90 runs sampled)
* segment x 22,916,879 ops/sec ±0.86% (92 runs sampled)
* polygon x 4,867,685 ops/sec ±0.55% (92 runs sampled)
* polygon-with-hole x 2,746,040 ops/sec ±0.69% (95 runs sampled)
* point x 32,975,988 ops/sec ±1.20% (92 runs sampled)
* multipolygon x 2,022,668 ops/sec ±0.47% (96 runs sampled)
* multipoint x 2,549,962 ops/sec ±1.48% (92 runs sampled)
* multiline x 19,285,512 ops/sec ±1.20% (91 runs sampled)
* line-3-coords x 22,508,235 ops/sec ±1.52% (90 runs sampled)
* geometry x 2,925,115 ops/sec ±0.35% (96 runs sampled)
* geometry-collection x 2,052,061 ops/sec ±0.60% (96 runs sampled)
* feature-collection x 1,914,939 ops/sec ±0.33% (97 runs sampled)
* closed-linestring x 10,339,559 ops/sec ±0.56% (93 runs sampled)
* clean-segment x 22,354,825 ops/sec ±0.84% (95 runs sampled)
*/
const suite = new Benchmark.Suite("turf-clean-coords");
glob
.sync(path.join(__dirname, "test", "in", "*.geojson"))
.forEach((filepath) => {
const { name } = path.parse(filepath);
const geojson = loadJsonFileSync(filepath);
console.time(name);
cleanCoords(geojson);
console.timeEnd(name);
suite.add(name, () => cleanCoords(geojson, true));
const geojson = loadJsonFileSync(filepath) as GeoJSON;
suite.add(name, () => cleanCoords(geojson));
//suite.add(name, () => cleanCoords(geojson, { mutate: true }));
});

suite.on("cycle", (e) => console.log(String(e.target))).run();
suite.on("cycle", (e: any) => console.log(String(e.target))).run();
Loading