@turf/simplify 7.0.0-alpha.0 → 7.0.0-alpha.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## simplify
6
6
 
7
- Takes a [GeoJSON][1] object and returns a simplified version. Internally uses
7
+ Takes a [GeoJSON][1] object and returns a simplified version. Internally uses the 2d version of
8
8
  [simplify-js][2] to perform simplification using the Ramer-Douglas-Peucker algorithm.
9
9
 
10
10
  ### Parameters
@@ -62,26 +62,21 @@ Returns **[GeoJSON][3]** a simplified GeoJSON
62
62
 
63
63
  [6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
64
64
 
65
- <!-- This file is automatically generated. Please don't edit it directly:
66
- if you find an error, edit the source file (likely index.js), and re-run
67
- ./scripts/generate-readmes in the turf project. -->
65
+ <!-- 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. -->
68
66
 
69
67
  ---
70
68
 
71
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
72
- module collection dedicated to geographic algorithms. It is maintained in the
73
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
74
- PRs and issues.
69
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
75
70
 
76
71
  ### Installation
77
72
 
78
- Install this module individually:
73
+ Install this single module individually:
79
74
 
80
75
  ```sh
81
76
  $ npm install @turf/simplify
82
77
  ```
83
78
 
84
- Or install the Turf module that includes it as a function:
79
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
85
80
 
86
81
  ```sh
87
82
  $ npm install @turf/turf
@@ -0,0 +1,168 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ var _cleancoords = require('@turf/clean-coords');
6
+ var _clone = require('@turf/clone');
7
+ var _meta = require('@turf/meta');
8
+ var _helpers = require('@turf/helpers');
9
+
10
+ // lib/simplify.js
11
+ function getSqDist(p1, p2) {
12
+ var dx = p1[0] - p2[0], dy = p1[1] - p2[1];
13
+ return dx * dx + dy * dy;
14
+ }
15
+ __name(getSqDist, "getSqDist");
16
+ function getSqSegDist(p, p1, p2) {
17
+ var x = p1[0], y = p1[1], dx = p2[0] - x, dy = p2[1] - y;
18
+ if (dx !== 0 || dy !== 0) {
19
+ var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);
20
+ if (t > 1) {
21
+ x = p2[0];
22
+ y = p2[1];
23
+ } else if (t > 0) {
24
+ x += dx * t;
25
+ y += dy * t;
26
+ }
27
+ }
28
+ dx = p[0] - x;
29
+ dy = p[1] - y;
30
+ return dx * dx + dy * dy;
31
+ }
32
+ __name(getSqSegDist, "getSqSegDist");
33
+ function simplifyRadialDist(points, sqTolerance) {
34
+ var prevPoint = points[0], newPoints = [prevPoint], point;
35
+ for (var i = 1, len = points.length; i < len; i++) {
36
+ point = points[i];
37
+ if (getSqDist(point, prevPoint) > sqTolerance) {
38
+ newPoints.push(point);
39
+ prevPoint = point;
40
+ }
41
+ }
42
+ if (prevPoint !== point)
43
+ newPoints.push(point);
44
+ return newPoints;
45
+ }
46
+ __name(simplifyRadialDist, "simplifyRadialDist");
47
+ function simplifyDPStep(points, first, last, sqTolerance, simplified) {
48
+ var maxSqDist = sqTolerance, index;
49
+ for (var i = first + 1; i < last; i++) {
50
+ var sqDist = getSqSegDist(points[i], points[first], points[last]);
51
+ if (sqDist > maxSqDist) {
52
+ index = i;
53
+ maxSqDist = sqDist;
54
+ }
55
+ }
56
+ if (maxSqDist > sqTolerance) {
57
+ if (index - first > 1)
58
+ simplifyDPStep(points, first, index, sqTolerance, simplified);
59
+ simplified.push(points[index]);
60
+ if (last - index > 1)
61
+ simplifyDPStep(points, index, last, sqTolerance, simplified);
62
+ }
63
+ }
64
+ __name(simplifyDPStep, "simplifyDPStep");
65
+ function simplifyDouglasPeucker(points, sqTolerance) {
66
+ var last = points.length - 1;
67
+ var simplified = [points[0]];
68
+ simplifyDPStep(points, 0, last, sqTolerance, simplified);
69
+ simplified.push(points[last]);
70
+ return simplified;
71
+ }
72
+ __name(simplifyDouglasPeucker, "simplifyDouglasPeucker");
73
+ function simplify(points, tolerance, highestQuality) {
74
+ if (points.length <= 2)
75
+ return points;
76
+ var sqTolerance = tolerance !== void 0 ? tolerance * tolerance : 1;
77
+ points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
78
+ points = simplifyDouglasPeucker(points, sqTolerance);
79
+ return points;
80
+ }
81
+ __name(simplify, "simplify");
82
+
83
+ // index.ts
84
+ function simplify2(geojson, options = {}) {
85
+ var _a, _b, _c;
86
+ options = options != null ? options : {};
87
+ if (!_helpers.isObject.call(void 0, options))
88
+ throw new Error("options is invalid");
89
+ const tolerance = (_a = options.tolerance) != null ? _a : 1;
90
+ const highQuality = (_b = options.highQuality) != null ? _b : false;
91
+ const mutate = (_c = options.mutate) != null ? _c : false;
92
+ if (!geojson)
93
+ throw new Error("geojson is required");
94
+ if (tolerance && tolerance < 0)
95
+ throw new Error("invalid tolerance");
96
+ if (mutate !== true)
97
+ geojson = _clone.clone.call(void 0, geojson);
98
+ _meta.geomEach.call(void 0, geojson, function(geom) {
99
+ simplifyGeom(geom, tolerance, highQuality);
100
+ });
101
+ return geojson;
102
+ }
103
+ __name(simplify2, "simplify");
104
+ function simplifyGeom(geometry, tolerance, highQuality) {
105
+ const type = geometry.type;
106
+ if (type === "Point" || type === "MultiPoint")
107
+ return geometry;
108
+ _cleancoords.cleanCoords.call(void 0, geometry, { mutate: true });
109
+ if (type !== "GeometryCollection") {
110
+ switch (type) {
111
+ case "LineString":
112
+ geometry.coordinates = simplify(
113
+ geometry.coordinates,
114
+ tolerance,
115
+ highQuality
116
+ );
117
+ break;
118
+ case "MultiLineString":
119
+ geometry.coordinates = geometry.coordinates.map(
120
+ (lines) => simplify(lines, tolerance, highQuality)
121
+ );
122
+ break;
123
+ case "Polygon":
124
+ geometry.coordinates = simplifyPolygon(
125
+ geometry.coordinates,
126
+ tolerance,
127
+ highQuality
128
+ );
129
+ break;
130
+ case "MultiPolygon":
131
+ geometry.coordinates = geometry.coordinates.map(
132
+ (rings) => simplifyPolygon(rings, tolerance, highQuality)
133
+ );
134
+ }
135
+ }
136
+ return geometry;
137
+ }
138
+ __name(simplifyGeom, "simplifyGeom");
139
+ function simplifyPolygon(coordinates, tolerance, highQuality) {
140
+ return coordinates.map(function(ring) {
141
+ if (ring.length < 4) {
142
+ throw new Error("invalid polygon");
143
+ }
144
+ let ringTolerance = tolerance;
145
+ let simpleRing = simplify(ring, ringTolerance, highQuality);
146
+ while (!checkValidity(simpleRing)) {
147
+ ringTolerance -= ringTolerance * 0.01;
148
+ simpleRing = simplify(ring, ringTolerance, highQuality);
149
+ }
150
+ if (simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] || simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]) {
151
+ simpleRing.push(simpleRing[0]);
152
+ }
153
+ return simpleRing;
154
+ });
155
+ }
156
+ __name(simplifyPolygon, "simplifyPolygon");
157
+ function checkValidity(ring) {
158
+ if (ring.length < 3)
159
+ return false;
160
+ return !(ring.length === 3 && ring[2][0] === ring[0][0] && ring[2][1] === ring[0][1]);
161
+ }
162
+ __name(checkValidity, "checkValidity");
163
+ var turf_simplify_default = simplify2;
164
+
165
+
166
+
167
+ exports.default = turf_simplify_default; exports.simplify = simplify2;
168
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts","../../lib/simplify.js"],"names":["simplify"],"mappings":";;;;AACA,SAAS,mBAAmB;AAC5B,SAAS,aAAa;AACtB,SAAS,gBAAgB;AACzB,SAAqB,gBAAgB;;;ACM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O;AAAA,EACT,CAAC;AACH;AAxBS;AAiCT,SAAS,cAAc,MAAkB;AACvC,MAAI,KAAK,SAAS;AAAG,WAAO;AAE5B,SAAO,EACL,KAAK,WAAW,KAChB,KAAK,CAAC,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC,KACxB,KAAK,CAAC,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;AAE5B;AARS;AAWT,IAAO,wBAAQA","sourcesContent":["import { Geometry, Position } from \"geojson\";\nimport { cleanCoords } from \"@turf/clean-coords\";\nimport { clone } from \"@turf/clone\";\nimport { geomEach } from \"@turf/meta\";\nimport { AllGeoJSON, isObject } from \"@turf/helpers\";\nimport { simplify as simplifyJS } from \"./lib/simplify\";\n\n/**\n * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of\n * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.\n *\n *\n * @name simplify\n * @param {GeoJSON} geojson object to be simplified\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.tolerance=1] simplification tolerance\n * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} a simplified GeoJSON\n * @example\n * var geojson = turf.polygon([[\n * [-70.603637, -33.399918],\n * [-70.614624, -33.395332],\n * [-70.639343, -33.392466],\n * [-70.659942, -33.394759],\n * [-70.683975, -33.404504],\n * [-70.697021, -33.419406],\n * [-70.701141, -33.434306],\n * [-70.700454, -33.446339],\n * [-70.694274, -33.458369],\n * [-70.682601, -33.465816],\n * [-70.668869, -33.472117],\n * [-70.646209, -33.473835],\n * [-70.624923, -33.472117],\n * [-70.609817, -33.468107],\n * [-70.595397, -33.458369],\n * [-70.587158, -33.442901],\n * [-70.587158, -33.426283],\n * [-70.590591, -33.414248],\n * [-70.594711, -33.406224],\n * [-70.603637, -33.399918]\n * ]]);\n * var options = {tolerance: 0.01, highQuality: false};\n * var simplified = turf.simplify(geojson, options);\n *\n * //addToMap\n * var addToMap = [geojson, simplified]\n */\nfunction simplify<T extends AllGeoJSON>(\n geojson: T,\n options: {\n tolerance?: number;\n highQuality?: boolean;\n mutate?: boolean;\n } = {}\n): T {\n // Optional parameters\n options = options ?? {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n const tolerance = options.tolerance ?? 1;\n const highQuality = options.highQuality ?? false;\n const mutate = options.mutate ?? false;\n\n if (!geojson) throw new Error(\"geojson is required\");\n if (tolerance && tolerance < 0) throw new Error(\"invalid tolerance\");\n\n // Clone geojson to avoid side effects\n if (mutate !== true) geojson = clone(geojson);\n\n geomEach(geojson, function (geom) {\n simplifyGeom(geom, tolerance, highQuality);\n });\n return geojson;\n}\n\n/**\n * Simplifies a feature's coordinates\n *\n * @private\n * @param {Geometry} geometry to be simplified\n * @param {number} [tolerance=1] simplification tolerance\n * @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm\n * @returns {Geometry} output\n */\nfunction simplifyGeom(\n geometry: Geometry,\n tolerance: number,\n highQuality: boolean\n) {\n const type = geometry.type;\n\n // \"unsimplyfiable\" geometry types\n if (type === \"Point\" || type === \"MultiPoint\") return geometry;\n\n // Remove any extra coordinates\n cleanCoords(geometry, { mutate: true });\n\n if (type !== \"GeometryCollection\") {\n // TODO should this cater for GeometryCollections too?\n switch (type) {\n case \"LineString\":\n geometry.coordinates = simplifyJS(\n geometry.coordinates,\n tolerance,\n highQuality\n );\n break;\n case \"MultiLineString\":\n geometry.coordinates = geometry.coordinates.map((lines) =>\n simplifyJS(lines, tolerance, highQuality)\n );\n break;\n case \"Polygon\":\n geometry.coordinates = simplifyPolygon(\n geometry.coordinates,\n tolerance,\n highQuality\n );\n break;\n case \"MultiPolygon\":\n geometry.coordinates = geometry.coordinates.map((rings) =>\n simplifyPolygon(rings, tolerance, highQuality)\n );\n }\n }\n\n return geometry;\n}\n\n/**\n * Simplifies the coordinates of a Polygon with simplify-js\n *\n * @private\n * @param {Array<number>} coordinates to be processed\n * @param {number} tolerance simplification tolerance\n * @param {boolean} highQuality whether or not to spend more time to create a higher-quality\n * @returns {Array<Array<Array<number>>>} simplified coords\n */\nfunction simplifyPolygon(\n coordinates: Position[][],\n tolerance: number,\n highQuality: boolean\n) {\n return coordinates.map(function (ring) {\n if (ring.length < 4) {\n throw new Error(\"invalid polygon\");\n }\n let ringTolerance = tolerance;\n let simpleRing = simplifyJS(ring, ringTolerance, highQuality);\n // remove 1 percent of tolerance until enough points to make a triangle\n while (!checkValidity(simpleRing)) {\n ringTolerance -= ringTolerance * 0.01;\n simpleRing = simplifyJS(ring, ringTolerance, highQuality);\n }\n if (\n simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||\n simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]\n ) {\n simpleRing.push(simpleRing[0]);\n }\n return simpleRing;\n });\n}\n\n/**\n * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last\n *\n * @private\n * @param {Array<number>} ring coordinates to be checked\n * @returns {boolean} true if valid\n */\nfunction checkValidity(ring: Position[]) {\n if (ring.length < 3) return false;\n //if the last point is the same as the first, it's not a triangle\n return !(\n ring.length === 3 &&\n ring[2][0] === ring[0][0] &&\n ring[2][1] === ring[0][1]\n );\n}\n\nexport { simplify };\nexport default simplify;\n","/*\n (c) 2013, Vladimir Agafonkin\n Simplify.js, a high-performance JS polyline simplification library\n mourner.github.io/simplify-js\n*/\n\n// to suit your point format, run search/replace for '.x' and '.y';\n// for 3D version, see 3d branch (configurability would draw significant performance overhead)\n\n// square distance between 2 points\nfunction getSqDist(p1, p2) {\n var dx = p1[0] - p2[0],\n dy = p1[1] - p2[1];\n\n return dx * dx + dy * dy;\n}\n\n// square distance from a point to a segment\nfunction getSqSegDist(p, p1, p2) {\n var x = p1[0],\n y = p1[1],\n dx = p2[0] - x,\n dy = p2[1] - y;\n\n if (dx !== 0 || dy !== 0) {\n var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);\n\n if (t > 1) {\n x = p2[0];\n y = p2[1];\n } else if (t > 0) {\n x += dx * t;\n y += dy * t;\n }\n }\n\n dx = p[0] - x;\n dy = p[1] - y;\n\n return dx * dx + dy * dy;\n}\n// rest of the code doesn't care about point format\n\n// basic distance-based simplification\nfunction simplifyRadialDist(points, sqTolerance) {\n var prevPoint = points[0],\n newPoints = [prevPoint],\n point;\n\n for (var i = 1, len = points.length; i < len; i++) {\n point = points[i];\n\n if (getSqDist(point, prevPoint) > sqTolerance) {\n newPoints.push(point);\n prevPoint = point;\n }\n }\n\n if (prevPoint !== point) newPoints.push(point);\n\n return newPoints;\n}\n\nfunction simplifyDPStep(points, first, last, sqTolerance, simplified) {\n var maxSqDist = sqTolerance,\n index;\n\n for (var i = first + 1; i < last; i++) {\n var sqDist = getSqSegDist(points[i], points[first], points[last]);\n\n if (sqDist > maxSqDist) {\n index = i;\n maxSqDist = sqDist;\n }\n }\n\n if (maxSqDist > sqTolerance) {\n if (index - first > 1)\n simplifyDPStep(points, first, index, sqTolerance, simplified);\n simplified.push(points[index]);\n if (last - index > 1)\n simplifyDPStep(points, index, last, sqTolerance, simplified);\n }\n}\n\n// simplification using Ramer-Douglas-Peucker algorithm\nfunction simplifyDouglasPeucker(points, sqTolerance) {\n var last = points.length - 1;\n\n var simplified = [points[0]];\n simplifyDPStep(points, 0, last, sqTolerance, simplified);\n simplified.push(points[last]);\n\n return simplified;\n}\n\n// both algorithms combined for awesome performance\nfunction simplify(points, tolerance, highestQuality) {\n if (points.length <= 2) return points;\n\n var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;\n\n points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);\n points = simplifyDouglasPeucker(points, sqTolerance);\n\n return points;\n}\n\nexport { simplify };\nexport default simplify;\n"]}
@@ -0,0 +1,50 @@
1
+ import { AllGeoJSON } from '@turf/helpers';
2
+
3
+ /**
4
+ * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of
5
+ * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.
6
+ *
7
+ *
8
+ * @name simplify
9
+ * @param {GeoJSON} geojson object to be simplified
10
+ * @param {Object} [options={}] Optional parameters
11
+ * @param {number} [options.tolerance=1] simplification tolerance
12
+ * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
13
+ * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
14
+ * @returns {GeoJSON} a simplified GeoJSON
15
+ * @example
16
+ * var geojson = turf.polygon([[
17
+ * [-70.603637, -33.399918],
18
+ * [-70.614624, -33.395332],
19
+ * [-70.639343, -33.392466],
20
+ * [-70.659942, -33.394759],
21
+ * [-70.683975, -33.404504],
22
+ * [-70.697021, -33.419406],
23
+ * [-70.701141, -33.434306],
24
+ * [-70.700454, -33.446339],
25
+ * [-70.694274, -33.458369],
26
+ * [-70.682601, -33.465816],
27
+ * [-70.668869, -33.472117],
28
+ * [-70.646209, -33.473835],
29
+ * [-70.624923, -33.472117],
30
+ * [-70.609817, -33.468107],
31
+ * [-70.595397, -33.458369],
32
+ * [-70.587158, -33.442901],
33
+ * [-70.587158, -33.426283],
34
+ * [-70.590591, -33.414248],
35
+ * [-70.594711, -33.406224],
36
+ * [-70.603637, -33.399918]
37
+ * ]]);
38
+ * var options = {tolerance: 0.01, highQuality: false};
39
+ * var simplified = turf.simplify(geojson, options);
40
+ *
41
+ * //addToMap
42
+ * var addToMap = [geojson, simplified]
43
+ */
44
+ declare function simplify<T extends AllGeoJSON>(geojson: T, options?: {
45
+ tolerance?: number;
46
+ highQuality?: boolean;
47
+ mutate?: boolean;
48
+ }): T;
49
+
50
+ export { simplify as default, simplify };
@@ -0,0 +1,50 @@
1
+ import { AllGeoJSON } from '@turf/helpers';
2
+
3
+ /**
4
+ * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of
5
+ * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.
6
+ *
7
+ *
8
+ * @name simplify
9
+ * @param {GeoJSON} geojson object to be simplified
10
+ * @param {Object} [options={}] Optional parameters
11
+ * @param {number} [options.tolerance=1] simplification tolerance
12
+ * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
13
+ * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
14
+ * @returns {GeoJSON} a simplified GeoJSON
15
+ * @example
16
+ * var geojson = turf.polygon([[
17
+ * [-70.603637, -33.399918],
18
+ * [-70.614624, -33.395332],
19
+ * [-70.639343, -33.392466],
20
+ * [-70.659942, -33.394759],
21
+ * [-70.683975, -33.404504],
22
+ * [-70.697021, -33.419406],
23
+ * [-70.701141, -33.434306],
24
+ * [-70.700454, -33.446339],
25
+ * [-70.694274, -33.458369],
26
+ * [-70.682601, -33.465816],
27
+ * [-70.668869, -33.472117],
28
+ * [-70.646209, -33.473835],
29
+ * [-70.624923, -33.472117],
30
+ * [-70.609817, -33.468107],
31
+ * [-70.595397, -33.458369],
32
+ * [-70.587158, -33.442901],
33
+ * [-70.587158, -33.426283],
34
+ * [-70.590591, -33.414248],
35
+ * [-70.594711, -33.406224],
36
+ * [-70.603637, -33.399918]
37
+ * ]]);
38
+ * var options = {tolerance: 0.01, highQuality: false};
39
+ * var simplified = turf.simplify(geojson, options);
40
+ *
41
+ * //addToMap
42
+ * var addToMap = [geojson, simplified]
43
+ */
44
+ declare function simplify<T extends AllGeoJSON>(geojson: T, options?: {
45
+ tolerance?: number;
46
+ highQuality?: boolean;
47
+ mutate?: boolean;
48
+ }): T;
49
+
50
+ export { simplify as default, simplify };
@@ -0,0 +1,168 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { cleanCoords } from "@turf/clean-coords";
6
+ import { clone } from "@turf/clone";
7
+ import { geomEach } from "@turf/meta";
8
+ import { isObject } from "@turf/helpers";
9
+
10
+ // lib/simplify.js
11
+ function getSqDist(p1, p2) {
12
+ var dx = p1[0] - p2[0], dy = p1[1] - p2[1];
13
+ return dx * dx + dy * dy;
14
+ }
15
+ __name(getSqDist, "getSqDist");
16
+ function getSqSegDist(p, p1, p2) {
17
+ var x = p1[0], y = p1[1], dx = p2[0] - x, dy = p2[1] - y;
18
+ if (dx !== 0 || dy !== 0) {
19
+ var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);
20
+ if (t > 1) {
21
+ x = p2[0];
22
+ y = p2[1];
23
+ } else if (t > 0) {
24
+ x += dx * t;
25
+ y += dy * t;
26
+ }
27
+ }
28
+ dx = p[0] - x;
29
+ dy = p[1] - y;
30
+ return dx * dx + dy * dy;
31
+ }
32
+ __name(getSqSegDist, "getSqSegDist");
33
+ function simplifyRadialDist(points, sqTolerance) {
34
+ var prevPoint = points[0], newPoints = [prevPoint], point;
35
+ for (var i = 1, len = points.length; i < len; i++) {
36
+ point = points[i];
37
+ if (getSqDist(point, prevPoint) > sqTolerance) {
38
+ newPoints.push(point);
39
+ prevPoint = point;
40
+ }
41
+ }
42
+ if (prevPoint !== point)
43
+ newPoints.push(point);
44
+ return newPoints;
45
+ }
46
+ __name(simplifyRadialDist, "simplifyRadialDist");
47
+ function simplifyDPStep(points, first, last, sqTolerance, simplified) {
48
+ var maxSqDist = sqTolerance, index;
49
+ for (var i = first + 1; i < last; i++) {
50
+ var sqDist = getSqSegDist(points[i], points[first], points[last]);
51
+ if (sqDist > maxSqDist) {
52
+ index = i;
53
+ maxSqDist = sqDist;
54
+ }
55
+ }
56
+ if (maxSqDist > sqTolerance) {
57
+ if (index - first > 1)
58
+ simplifyDPStep(points, first, index, sqTolerance, simplified);
59
+ simplified.push(points[index]);
60
+ if (last - index > 1)
61
+ simplifyDPStep(points, index, last, sqTolerance, simplified);
62
+ }
63
+ }
64
+ __name(simplifyDPStep, "simplifyDPStep");
65
+ function simplifyDouglasPeucker(points, sqTolerance) {
66
+ var last = points.length - 1;
67
+ var simplified = [points[0]];
68
+ simplifyDPStep(points, 0, last, sqTolerance, simplified);
69
+ simplified.push(points[last]);
70
+ return simplified;
71
+ }
72
+ __name(simplifyDouglasPeucker, "simplifyDouglasPeucker");
73
+ function simplify(points, tolerance, highestQuality) {
74
+ if (points.length <= 2)
75
+ return points;
76
+ var sqTolerance = tolerance !== void 0 ? tolerance * tolerance : 1;
77
+ points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
78
+ points = simplifyDouglasPeucker(points, sqTolerance);
79
+ return points;
80
+ }
81
+ __name(simplify, "simplify");
82
+
83
+ // index.ts
84
+ function simplify2(geojson, options = {}) {
85
+ var _a, _b, _c;
86
+ options = options != null ? options : {};
87
+ if (!isObject(options))
88
+ throw new Error("options is invalid");
89
+ const tolerance = (_a = options.tolerance) != null ? _a : 1;
90
+ const highQuality = (_b = options.highQuality) != null ? _b : false;
91
+ const mutate = (_c = options.mutate) != null ? _c : false;
92
+ if (!geojson)
93
+ throw new Error("geojson is required");
94
+ if (tolerance && tolerance < 0)
95
+ throw new Error("invalid tolerance");
96
+ if (mutate !== true)
97
+ geojson = clone(geojson);
98
+ geomEach(geojson, function(geom) {
99
+ simplifyGeom(geom, tolerance, highQuality);
100
+ });
101
+ return geojson;
102
+ }
103
+ __name(simplify2, "simplify");
104
+ function simplifyGeom(geometry, tolerance, highQuality) {
105
+ const type = geometry.type;
106
+ if (type === "Point" || type === "MultiPoint")
107
+ return geometry;
108
+ cleanCoords(geometry, { mutate: true });
109
+ if (type !== "GeometryCollection") {
110
+ switch (type) {
111
+ case "LineString":
112
+ geometry.coordinates = simplify(
113
+ geometry.coordinates,
114
+ tolerance,
115
+ highQuality
116
+ );
117
+ break;
118
+ case "MultiLineString":
119
+ geometry.coordinates = geometry.coordinates.map(
120
+ (lines) => simplify(lines, tolerance, highQuality)
121
+ );
122
+ break;
123
+ case "Polygon":
124
+ geometry.coordinates = simplifyPolygon(
125
+ geometry.coordinates,
126
+ tolerance,
127
+ highQuality
128
+ );
129
+ break;
130
+ case "MultiPolygon":
131
+ geometry.coordinates = geometry.coordinates.map(
132
+ (rings) => simplifyPolygon(rings, tolerance, highQuality)
133
+ );
134
+ }
135
+ }
136
+ return geometry;
137
+ }
138
+ __name(simplifyGeom, "simplifyGeom");
139
+ function simplifyPolygon(coordinates, tolerance, highQuality) {
140
+ return coordinates.map(function(ring) {
141
+ if (ring.length < 4) {
142
+ throw new Error("invalid polygon");
143
+ }
144
+ let ringTolerance = tolerance;
145
+ let simpleRing = simplify(ring, ringTolerance, highQuality);
146
+ while (!checkValidity(simpleRing)) {
147
+ ringTolerance -= ringTolerance * 0.01;
148
+ simpleRing = simplify(ring, ringTolerance, highQuality);
149
+ }
150
+ if (simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] || simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]) {
151
+ simpleRing.push(simpleRing[0]);
152
+ }
153
+ return simpleRing;
154
+ });
155
+ }
156
+ __name(simplifyPolygon, "simplifyPolygon");
157
+ function checkValidity(ring) {
158
+ if (ring.length < 3)
159
+ return false;
160
+ return !(ring.length === 3 && ring[2][0] === ring[0][0] && ring[2][1] === ring[0][1]);
161
+ }
162
+ __name(checkValidity, "checkValidity");
163
+ var turf_simplify_default = simplify2;
164
+ export {
165
+ turf_simplify_default as default,
166
+ simplify2 as simplify
167
+ };
168
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts","../../lib/simplify.js"],"sourcesContent":["import { Geometry, Position } from \"geojson\";\nimport { cleanCoords } from \"@turf/clean-coords\";\nimport { clone } from \"@turf/clone\";\nimport { geomEach } from \"@turf/meta\";\nimport { AllGeoJSON, isObject } from \"@turf/helpers\";\nimport { simplify as simplifyJS } from \"./lib/simplify\";\n\n/**\n * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses the 2d version of\n * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.\n *\n *\n * @name simplify\n * @param {GeoJSON} geojson object to be simplified\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.tolerance=1] simplification tolerance\n * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm\n * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)\n * @returns {GeoJSON} a simplified GeoJSON\n * @example\n * var geojson = turf.polygon([[\n * [-70.603637, -33.399918],\n * [-70.614624, -33.395332],\n * [-70.639343, -33.392466],\n * [-70.659942, -33.394759],\n * [-70.683975, -33.404504],\n * [-70.697021, -33.419406],\n * [-70.701141, -33.434306],\n * [-70.700454, -33.446339],\n * [-70.694274, -33.458369],\n * [-70.682601, -33.465816],\n * [-70.668869, -33.472117],\n * [-70.646209, -33.473835],\n * [-70.624923, -33.472117],\n * [-70.609817, -33.468107],\n * [-70.595397, -33.458369],\n * [-70.587158, -33.442901],\n * [-70.587158, -33.426283],\n * [-70.590591, -33.414248],\n * [-70.594711, -33.406224],\n * [-70.603637, -33.399918]\n * ]]);\n * var options = {tolerance: 0.01, highQuality: false};\n * var simplified = turf.simplify(geojson, options);\n *\n * //addToMap\n * var addToMap = [geojson, simplified]\n */\nfunction simplify<T extends AllGeoJSON>(\n geojson: T,\n options: {\n tolerance?: number;\n highQuality?: boolean;\n mutate?: boolean;\n } = {}\n): T {\n // Optional parameters\n options = options ?? {};\n if (!isObject(options)) throw new Error(\"options is invalid\");\n const tolerance = options.tolerance ?? 1;\n const highQuality = options.highQuality ?? false;\n const mutate = options.mutate ?? false;\n\n if (!geojson) throw new Error(\"geojson is required\");\n if (tolerance && tolerance < 0) throw new Error(\"invalid tolerance\");\n\n // Clone geojson to avoid side effects\n if (mutate !== true) geojson = clone(geojson);\n\n geomEach(geojson, function (geom) {\n simplifyGeom(geom, tolerance, highQuality);\n });\n return geojson;\n}\n\n/**\n * Simplifies a feature's coordinates\n *\n * @private\n * @param {Geometry} geometry to be simplified\n * @param {number} [tolerance=1] simplification tolerance\n * @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm\n * @returns {Geometry} output\n */\nfunction simplifyGeom(\n geometry: Geometry,\n tolerance: number,\n highQuality: boolean\n) {\n const type = geometry.type;\n\n // \"unsimplyfiable\" geometry types\n if (type === \"Point\" || type === \"MultiPoint\") return geometry;\n\n // Remove any extra coordinates\n cleanCoords(geometry, { mutate: true });\n\n if (type !== \"GeometryCollection\") {\n // TODO should this cater for GeometryCollections too?\n switch (type) {\n case \"LineString\":\n geometry.coordinates = simplifyJS(\n geometry.coordinates,\n tolerance,\n highQuality\n );\n break;\n case \"MultiLineString\":\n geometry.coordinates = geometry.coordinates.map((lines) =>\n simplifyJS(lines, tolerance, highQuality)\n );\n break;\n case \"Polygon\":\n geometry.coordinates = simplifyPolygon(\n geometry.coordinates,\n tolerance,\n highQuality\n );\n break;\n case \"MultiPolygon\":\n geometry.coordinates = geometry.coordinates.map((rings) =>\n simplifyPolygon(rings, tolerance, highQuality)\n );\n }\n }\n\n return geometry;\n}\n\n/**\n * Simplifies the coordinates of a Polygon with simplify-js\n *\n * @private\n * @param {Array<number>} coordinates to be processed\n * @param {number} tolerance simplification tolerance\n * @param {boolean} highQuality whether or not to spend more time to create a higher-quality\n * @returns {Array<Array<Array<number>>>} simplified coords\n */\nfunction simplifyPolygon(\n coordinates: Position[][],\n tolerance: number,\n highQuality: boolean\n) {\n return coordinates.map(function (ring) {\n if (ring.length < 4) {\n throw new Error(\"invalid polygon\");\n }\n let ringTolerance = tolerance;\n let simpleRing = simplifyJS(ring, ringTolerance, highQuality);\n // remove 1 percent of tolerance until enough points to make a triangle\n while (!checkValidity(simpleRing)) {\n ringTolerance -= ringTolerance * 0.01;\n simpleRing = simplifyJS(ring, ringTolerance, highQuality);\n }\n if (\n simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||\n simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]\n ) {\n simpleRing.push(simpleRing[0]);\n }\n return simpleRing;\n });\n}\n\n/**\n * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last\n *\n * @private\n * @param {Array<number>} ring coordinates to be checked\n * @returns {boolean} true if valid\n */\nfunction checkValidity(ring: Position[]) {\n if (ring.length < 3) return false;\n //if the last point is the same as the first, it's not a triangle\n return !(\n ring.length === 3 &&\n ring[2][0] === ring[0][0] &&\n ring[2][1] === ring[0][1]\n );\n}\n\nexport { simplify };\nexport default simplify;\n","/*\n (c) 2013, Vladimir Agafonkin\n Simplify.js, a high-performance JS polyline simplification library\n mourner.github.io/simplify-js\n*/\n\n// to suit your point format, run search/replace for '.x' and '.y';\n// for 3D version, see 3d branch (configurability would draw significant performance overhead)\n\n// square distance between 2 points\nfunction getSqDist(p1, p2) {\n var dx = p1[0] - p2[0],\n dy = p1[1] - p2[1];\n\n return dx * dx + dy * dy;\n}\n\n// square distance from a point to a segment\nfunction getSqSegDist(p, p1, p2) {\n var x = p1[0],\n y = p1[1],\n dx = p2[0] - x,\n dy = p2[1] - y;\n\n if (dx !== 0 || dy !== 0) {\n var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);\n\n if (t > 1) {\n x = p2[0];\n y = p2[1];\n } else if (t > 0) {\n x += dx * t;\n y += dy * t;\n }\n }\n\n dx = p[0] - x;\n dy = p[1] - y;\n\n return dx * dx + dy * dy;\n}\n// rest of the code doesn't care about point format\n\n// basic distance-based simplification\nfunction simplifyRadialDist(points, sqTolerance) {\n var prevPoint = points[0],\n newPoints = [prevPoint],\n point;\n\n for (var i = 1, len = points.length; i < len; i++) {\n point = points[i];\n\n if (getSqDist(point, prevPoint) > sqTolerance) {\n newPoints.push(point);\n prevPoint = point;\n }\n }\n\n if (prevPoint !== point) newPoints.push(point);\n\n return newPoints;\n}\n\nfunction simplifyDPStep(points, first, last, sqTolerance, simplified) {\n var maxSqDist = sqTolerance,\n index;\n\n for (var i = first + 1; i < last; i++) {\n var sqDist = getSqSegDist(points[i], points[first], points[last]);\n\n if (sqDist > maxSqDist) {\n index = i;\n maxSqDist = sqDist;\n }\n }\n\n if (maxSqDist > sqTolerance) {\n if (index - first > 1)\n simplifyDPStep(points, first, index, sqTolerance, simplified);\n simplified.push(points[index]);\n if (last - index > 1)\n simplifyDPStep(points, index, last, sqTolerance, simplified);\n }\n}\n\n// simplification using Ramer-Douglas-Peucker algorithm\nfunction simplifyDouglasPeucker(points, sqTolerance) {\n var last = points.length - 1;\n\n var simplified = [points[0]];\n simplifyDPStep(points, 0, last, sqTolerance, simplified);\n simplified.push(points[last]);\n\n return simplified;\n}\n\n// both algorithms combined for awesome performance\nfunction simplify(points, tolerance, highestQuality) {\n if (points.length <= 2) return points;\n\n var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;\n\n points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);\n points = simplifyDouglasPeucker(points, sqTolerance);\n\n return points;\n}\n\nexport { simplify };\nexport default simplify;\n"],"mappings":";;;;AACA,SAAS,mBAAmB;AAC5B,SAAS,aAAa;AACtB,SAAS,gBAAgB;AACzB,SAAqB,gBAAgB;;;ACMrC,SAAS,UAAU,IAAI,IAAI;AACzB,MAAI,KAAK,GAAG,CAAC,IAAI,GAAG,CAAC,GACnB,KAAK,GAAG,CAAC,IAAI,GAAG,CAAC;AAEnB,SAAO,KAAK,KAAK,KAAK;AACxB;AALS;AAQT,SAAS,aAAa,GAAG,IAAI,IAAI;AAC/B,MAAI,IAAI,GAAG,CAAC,GACV,IAAI,GAAG,CAAC,GACR,KAAK,GAAG,CAAC,IAAI,GACb,KAAK,GAAG,CAAC,IAAI;AAEf,MAAI,OAAO,KAAK,OAAO,GAAG;AACxB,QAAI,MAAM,EAAE,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC,IAAI,KAAK,OAAO,KAAK,KAAK,KAAK;AAE9D,QAAI,IAAI,GAAG;AACT,UAAI,GAAG,CAAC;AACR,UAAI,GAAG,CAAC;AAA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UACT;AAAA,UACA;AAAA,QACF;AACA;AAAA,MACF,KAAK;AACH,iBAAS,cAAc,SAAS,YAAY;AAAA,UAAI,CAAC,UAC/C,gBAAgB,OAAO,WAAW,WAAW;AAAA,QAC/C;AAAA,IACJ;AAAA,EACF;AAEA,SAAO;AACT;AA3CS;AAsDT,SAAS,gBACP,aACA,WACA,aACA;AACA,SAAO,YAAY,IAAI,SAAU,MAAM;AACrC,QAAI,KAAK,SAAS,GAAG;AACnB,YAAM,IAAI,MAAM,iBAAiB;AAAA,IACnC;AACA,QAAI,gBAAgB;AACpB,QAAI,aAAa,SAAW,MAAM,eAAe,WAAW;AAE5D,WAAO,CAAC,cAAc,UAAU,GAAG;AACjC,uBAAiB,gBAAgB;AACjC,mBAAa,SAAW,MAAM,eAAe,WAAW;AAAA,IAC1D;AACA,QACE,WAAW,WAAW,SAAS,CAAC,EAAE,CAAC,MAAM,WAAW,CAAC,EAAE,CAAC,KACxD,WAAW,WAAW,SAAS,CAAC,EAAE,CAAC,MAAM,WAAW,CAAC,EAAE,CAAC,GACxD;AACA,iBAAW,KAAK,WAAW,CAAC,CAAC;AAAA,IAC/B;AACA,WAAO;AAAA,EACT,CAAC;AACH;AAxBS;AAiCT,SAAS,cAAc,MAAkB;AACvC,MAAI,KAAK,SAAS;AAAG,WAAO;AAE5B,SAAO,EACL,KAAK,WAAW,KAChB,KAAK,CAAC,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC,KACxB,KAAK,CAAC,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;AAE5B;AARS;AAWT,IAAO,wBAAQC;","names":["simplify","simplify"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/simplify",
3
- "version": "7.0.0-alpha.0",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
4
4
  "description": "turf simplify module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -28,43 +28,54 @@
28
28
  "algorithm",
29
29
  "peucker"
30
30
  ],
31
- "main": "dist/js/index.js",
32
- "module": "dist/es/index.js",
31
+ "type": "commonjs",
32
+ "main": "dist/cjs/index.cjs",
33
+ "module": "dist/esm/index.mjs",
34
+ "types": "dist/cjs/index.d.ts",
33
35
  "exports": {
34
36
  "./package.json": "./package.json",
35
37
  ".": {
36
- "import": "./dist/es/index.js",
37
- "require": "./dist/js/index.js"
38
+ "import": {
39
+ "types": "./dist/esm/index.d.mts",
40
+ "default": "./dist/esm/index.mjs"
41
+ },
42
+ "require": {
43
+ "types": "./dist/cjs/index.d.ts",
44
+ "default": "./dist/cjs/index.cjs"
45
+ }
38
46
  }
39
47
  },
40
- "types": "index.d.ts",
41
48
  "sideEffects": false,
42
49
  "files": [
43
- "dist",
44
- "index.d.ts"
50
+ "dist"
45
51
  ],
46
52
  "scripts": {
47
- "bench": "node -r esm bench.js",
48
- "build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json",
49
- "docs": "node ../../scripts/generate-readmes",
50
- "test": "npm-run-all test:*",
51
- "test:tape": "node -r esm test.js",
53
+ "bench": "tsx bench.ts",
54
+ "build": "tsup --config ../../tsup.config.ts",
55
+ "docs": "tsx ../../scripts/generate-readmes.ts",
56
+ "test": "npm-run-all --npm-path npm test:*",
57
+ "test:tape": "tsx test.ts",
52
58
  "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
53
59
  },
54
60
  "devDependencies": {
55
- "@turf/truncate": "^7.0.0-alpha.0",
56
- "benchmark": "*",
57
- "load-json-file": "*",
58
- "npm-run-all": "*",
59
- "rollup": "*",
60
- "tape": "*",
61
- "write-json-file": "*"
61
+ "@turf/truncate": "^7.0.0-alpha.110+1411d63a7",
62
+ "@types/benchmark": "^2.1.5",
63
+ "@types/tape": "^4.2.32",
64
+ "benchmark": "^2.1.4",
65
+ "load-json-file": "^7.0.1",
66
+ "npm-run-all": "^4.1.5",
67
+ "tape": "^5.7.2",
68
+ "tsup": "^8.0.1",
69
+ "tsx": "^4.6.2",
70
+ "typescript": "^5.2.2",
71
+ "write-json-file": "^5.0.0"
62
72
  },
63
73
  "dependencies": {
64
- "@turf/clean-coords": "^7.0.0-alpha.0",
65
- "@turf/clone": "^7.0.0-alpha.0",
66
- "@turf/helpers": "^7.0.0-alpha.0",
67
- "@turf/meta": "^7.0.0-alpha.0"
74
+ "@turf/clean-coords": "^7.0.0-alpha.110+1411d63a7",
75
+ "@turf/clone": "^7.0.0-alpha.110+1411d63a7",
76
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
77
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
78
+ "tslib": "^2.6.2"
68
79
  },
69
- "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
80
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
70
81
  }
package/dist/es/index.js DELETED
@@ -1,300 +0,0 @@
1
- import cleanCoords from '@turf/clean-coords';
2
- import clone from '@turf/clone';
3
- import { geomEach } from '@turf/meta';
4
- import { isObject } from '@turf/helpers';
5
-
6
- /*
7
- (c) 2013, Vladimir Agafonkin
8
- Simplify.js, a high-performance JS polyline simplification library
9
- mourner.github.io/simplify-js
10
- */
11
-
12
- // to suit your point format, run search/replace for '.x' and '.y';
13
- // for 3D version, see 3d branch (configurability would draw significant performance overhead)
14
-
15
- // square distance between 2 points
16
- function getSqDist(p1, p2) {
17
- var dx = p1.x - p2.x,
18
- dy = p1.y - p2.y;
19
-
20
- return dx * dx + dy * dy;
21
- }
22
-
23
- // square distance from a point to a segment
24
- function getSqSegDist(p, p1, p2) {
25
- var x = p1.x,
26
- y = p1.y,
27
- dx = p2.x - x,
28
- dy = p2.y - y;
29
-
30
- if (dx !== 0 || dy !== 0) {
31
- var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
32
-
33
- if (t > 1) {
34
- x = p2.x;
35
- y = p2.y;
36
- } else if (t > 0) {
37
- x += dx * t;
38
- y += dy * t;
39
- }
40
- }
41
-
42
- dx = p.x - x;
43
- dy = p.y - y;
44
-
45
- return dx * dx + dy * dy;
46
- }
47
- // rest of the code doesn't care about point format
48
-
49
- // basic distance-based simplification
50
- function simplifyRadialDist(points, sqTolerance) {
51
- var prevPoint = points[0],
52
- newPoints = [prevPoint],
53
- point;
54
-
55
- for (var i = 1, len = points.length; i < len; i++) {
56
- point = points[i];
57
-
58
- if (getSqDist(point, prevPoint) > sqTolerance) {
59
- newPoints.push(point);
60
- prevPoint = point;
61
- }
62
- }
63
-
64
- if (prevPoint !== point) newPoints.push(point);
65
-
66
- return newPoints;
67
- }
68
-
69
- function simplifyDPStep(points, first, last, sqTolerance, simplified) {
70
- var maxSqDist = sqTolerance,
71
- index;
72
-
73
- for (var i = first + 1; i < last; i++) {
74
- var sqDist = getSqSegDist(points[i], points[first], points[last]);
75
-
76
- if (sqDist > maxSqDist) {
77
- index = i;
78
- maxSqDist = sqDist;
79
- }
80
- }
81
-
82
- if (maxSqDist > sqTolerance) {
83
- if (index - first > 1)
84
- simplifyDPStep(points, first, index, sqTolerance, simplified);
85
- simplified.push(points[index]);
86
- if (last - index > 1)
87
- simplifyDPStep(points, index, last, sqTolerance, simplified);
88
- }
89
- }
90
-
91
- // simplification using Ramer-Douglas-Peucker algorithm
92
- function simplifyDouglasPeucker(points, sqTolerance) {
93
- var last = points.length - 1;
94
-
95
- var simplified = [points[0]];
96
- simplifyDPStep(points, 0, last, sqTolerance, simplified);
97
- simplified.push(points[last]);
98
-
99
- return simplified;
100
- }
101
-
102
- // both algorithms combined for awesome performance
103
- function simplify(points, tolerance, highestQuality) {
104
- if (points.length <= 2) return points;
105
-
106
- var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
107
-
108
- points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
109
- points = simplifyDouglasPeucker(points, sqTolerance);
110
-
111
- return points;
112
- }
113
-
114
- /**
115
- * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses
116
- * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.
117
- *
118
- * @name simplify
119
- * @param {GeoJSON} geojson object to be simplified
120
- * @param {Object} [options={}] Optional parameters
121
- * @param {number} [options.tolerance=1] simplification tolerance
122
- * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
123
- * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
124
- * @returns {GeoJSON} a simplified GeoJSON
125
- * @example
126
- * var geojson = turf.polygon([[
127
- * [-70.603637, -33.399918],
128
- * [-70.614624, -33.395332],
129
- * [-70.639343, -33.392466],
130
- * [-70.659942, -33.394759],
131
- * [-70.683975, -33.404504],
132
- * [-70.697021, -33.419406],
133
- * [-70.701141, -33.434306],
134
- * [-70.700454, -33.446339],
135
- * [-70.694274, -33.458369],
136
- * [-70.682601, -33.465816],
137
- * [-70.668869, -33.472117],
138
- * [-70.646209, -33.473835],
139
- * [-70.624923, -33.472117],
140
- * [-70.609817, -33.468107],
141
- * [-70.595397, -33.458369],
142
- * [-70.587158, -33.442901],
143
- * [-70.587158, -33.426283],
144
- * [-70.590591, -33.414248],
145
- * [-70.594711, -33.406224],
146
- * [-70.603637, -33.399918]
147
- * ]]);
148
- * var options = {tolerance: 0.01, highQuality: false};
149
- * var simplified = turf.simplify(geojson, options);
150
- *
151
- * //addToMap
152
- * var addToMap = [geojson, simplified]
153
- */
154
- function simplify$1(geojson, options) {
155
- // Optional parameters
156
- options = options || {};
157
- if (!isObject(options)) throw new Error("options is invalid");
158
- var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
159
- var highQuality = options.highQuality || false;
160
- var mutate = options.mutate || false;
161
-
162
- if (!geojson) throw new Error("geojson is required");
163
- if (tolerance && tolerance < 0) throw new Error("invalid tolerance");
164
-
165
- // Clone geojson to avoid side effects
166
- if (mutate !== true) geojson = clone(geojson);
167
-
168
- geomEach(geojson, function (geom) {
169
- simplifyGeom(geom, tolerance, highQuality);
170
- });
171
- return geojson;
172
- }
173
-
174
- /**
175
- * Simplifies a feature's coordinates
176
- *
177
- * @private
178
- * @param {Geometry} geometry to be simplified
179
- * @param {number} [tolerance=1] simplification tolerance
180
- * @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
181
- * @returns {Geometry} output
182
- */
183
- function simplifyGeom(geometry, tolerance, highQuality) {
184
- var type = geometry.type;
185
-
186
- // "unsimplyfiable" geometry types
187
- if (type === "Point" || type === "MultiPoint") return geometry;
188
-
189
- // Remove any extra coordinates
190
- cleanCoords(geometry, true);
191
-
192
- var coordinates = geometry.coordinates;
193
- switch (type) {
194
- case "LineString":
195
- geometry["coordinates"] = simplifyLine(
196
- coordinates,
197
- tolerance,
198
- highQuality
199
- );
200
- break;
201
- case "MultiLineString":
202
- geometry["coordinates"] = coordinates.map(function (lines) {
203
- return simplifyLine(lines, tolerance, highQuality);
204
- });
205
- break;
206
- case "Polygon":
207
- geometry["coordinates"] = simplifyPolygon(
208
- coordinates,
209
- tolerance,
210
- highQuality
211
- );
212
- break;
213
- case "MultiPolygon":
214
- geometry["coordinates"] = coordinates.map(function (rings) {
215
- return simplifyPolygon(rings, tolerance, highQuality);
216
- });
217
- }
218
- return geometry;
219
- }
220
-
221
- /**
222
- * Simplifies the coordinates of a LineString with simplify-js
223
- *
224
- * @private
225
- * @param {Array<number>} coordinates to be processed
226
- * @param {number} tolerance simplification tolerance
227
- * @param {boolean} highQuality whether or not to spend more time to create a higher-quality
228
- * @returns {Array<Array<number>>} simplified coords
229
- */
230
- function simplifyLine(coordinates, tolerance, highQuality) {
231
- return simplify(
232
- coordinates.map(function (coord) {
233
- return { x: coord[0], y: coord[1], z: coord[2] };
234
- }),
235
- tolerance,
236
- highQuality
237
- ).map(function (coords) {
238
- return coords.z ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
239
- });
240
- }
241
-
242
- /**
243
- * Simplifies the coordinates of a Polygon with simplify-js
244
- *
245
- * @private
246
- * @param {Array<number>} coordinates to be processed
247
- * @param {number} tolerance simplification tolerance
248
- * @param {boolean} highQuality whether or not to spend more time to create a higher-quality
249
- * @returns {Array<Array<Array<number>>>} simplified coords
250
- */
251
- function simplifyPolygon(coordinates, tolerance, highQuality) {
252
- return coordinates.map(function (ring) {
253
- var pts = ring.map(function (coord) {
254
- return { x: coord[0], y: coord[1] };
255
- });
256
- if (pts.length < 4) {
257
- throw new Error("invalid polygon");
258
- }
259
- var simpleRing = simplify(pts, tolerance, highQuality).map(function (
260
- coords
261
- ) {
262
- return [coords.x, coords.y];
263
- });
264
- //remove 1 percent of tolerance until enough points to make a triangle
265
- while (!checkValidity(simpleRing)) {
266
- tolerance -= tolerance * 0.01;
267
- simpleRing = simplify(pts, tolerance, highQuality).map(function (
268
- coords
269
- ) {
270
- return [coords.x, coords.y];
271
- });
272
- }
273
- if (
274
- simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||
275
- simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]
276
- ) {
277
- simpleRing.push(simpleRing[0]);
278
- }
279
- return simpleRing;
280
- });
281
- }
282
-
283
- /**
284
- * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last
285
- *
286
- * @private
287
- * @param {Array<number>} ring coordinates to be checked
288
- * @returns {boolean} true if valid
289
- */
290
- function checkValidity(ring) {
291
- if (ring.length < 3) return false;
292
- //if the last point is the same as the first, it's not a triangle
293
- return !(
294
- ring.length === 3 &&
295
- ring[2][0] === ring[0][0] &&
296
- ring[2][1] === ring[0][1]
297
- );
298
- }
299
-
300
- export default simplify$1;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,308 +0,0 @@
1
- 'use strict';
2
-
3
- var cleanCoords = require('@turf/clean-coords');
4
- var clone = require('@turf/clone');
5
- var meta = require('@turf/meta');
6
- var helpers = require('@turf/helpers');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var cleanCoords__default = /*#__PURE__*/_interopDefaultLegacy(cleanCoords);
11
- var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
12
-
13
- /*
14
- (c) 2013, Vladimir Agafonkin
15
- Simplify.js, a high-performance JS polyline simplification library
16
- mourner.github.io/simplify-js
17
- */
18
-
19
- // to suit your point format, run search/replace for '.x' and '.y';
20
- // for 3D version, see 3d branch (configurability would draw significant performance overhead)
21
-
22
- // square distance between 2 points
23
- function getSqDist(p1, p2) {
24
- var dx = p1.x - p2.x,
25
- dy = p1.y - p2.y;
26
-
27
- return dx * dx + dy * dy;
28
- }
29
-
30
- // square distance from a point to a segment
31
- function getSqSegDist(p, p1, p2) {
32
- var x = p1.x,
33
- y = p1.y,
34
- dx = p2.x - x,
35
- dy = p2.y - y;
36
-
37
- if (dx !== 0 || dy !== 0) {
38
- var t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
39
-
40
- if (t > 1) {
41
- x = p2.x;
42
- y = p2.y;
43
- } else if (t > 0) {
44
- x += dx * t;
45
- y += dy * t;
46
- }
47
- }
48
-
49
- dx = p.x - x;
50
- dy = p.y - y;
51
-
52
- return dx * dx + dy * dy;
53
- }
54
- // rest of the code doesn't care about point format
55
-
56
- // basic distance-based simplification
57
- function simplifyRadialDist(points, sqTolerance) {
58
- var prevPoint = points[0],
59
- newPoints = [prevPoint],
60
- point;
61
-
62
- for (var i = 1, len = points.length; i < len; i++) {
63
- point = points[i];
64
-
65
- if (getSqDist(point, prevPoint) > sqTolerance) {
66
- newPoints.push(point);
67
- prevPoint = point;
68
- }
69
- }
70
-
71
- if (prevPoint !== point) newPoints.push(point);
72
-
73
- return newPoints;
74
- }
75
-
76
- function simplifyDPStep(points, first, last, sqTolerance, simplified) {
77
- var maxSqDist = sqTolerance,
78
- index;
79
-
80
- for (var i = first + 1; i < last; i++) {
81
- var sqDist = getSqSegDist(points[i], points[first], points[last]);
82
-
83
- if (sqDist > maxSqDist) {
84
- index = i;
85
- maxSqDist = sqDist;
86
- }
87
- }
88
-
89
- if (maxSqDist > sqTolerance) {
90
- if (index - first > 1)
91
- simplifyDPStep(points, first, index, sqTolerance, simplified);
92
- simplified.push(points[index]);
93
- if (last - index > 1)
94
- simplifyDPStep(points, index, last, sqTolerance, simplified);
95
- }
96
- }
97
-
98
- // simplification using Ramer-Douglas-Peucker algorithm
99
- function simplifyDouglasPeucker(points, sqTolerance) {
100
- var last = points.length - 1;
101
-
102
- var simplified = [points[0]];
103
- simplifyDPStep(points, 0, last, sqTolerance, simplified);
104
- simplified.push(points[last]);
105
-
106
- return simplified;
107
- }
108
-
109
- // both algorithms combined for awesome performance
110
- function simplify(points, tolerance, highestQuality) {
111
- if (points.length <= 2) return points;
112
-
113
- var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1;
114
-
115
- points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
116
- points = simplifyDouglasPeucker(points, sqTolerance);
117
-
118
- return points;
119
- }
120
-
121
- /**
122
- * Takes a {@link GeoJSON} object and returns a simplified version. Internally uses
123
- * [simplify-js](http://mourner.github.io/simplify-js/) to perform simplification using the Ramer-Douglas-Peucker algorithm.
124
- *
125
- * @name simplify
126
- * @param {GeoJSON} geojson object to be simplified
127
- * @param {Object} [options={}] Optional parameters
128
- * @param {number} [options.tolerance=1] simplification tolerance
129
- * @param {boolean} [options.highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
130
- * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
131
- * @returns {GeoJSON} a simplified GeoJSON
132
- * @example
133
- * var geojson = turf.polygon([[
134
- * [-70.603637, -33.399918],
135
- * [-70.614624, -33.395332],
136
- * [-70.639343, -33.392466],
137
- * [-70.659942, -33.394759],
138
- * [-70.683975, -33.404504],
139
- * [-70.697021, -33.419406],
140
- * [-70.701141, -33.434306],
141
- * [-70.700454, -33.446339],
142
- * [-70.694274, -33.458369],
143
- * [-70.682601, -33.465816],
144
- * [-70.668869, -33.472117],
145
- * [-70.646209, -33.473835],
146
- * [-70.624923, -33.472117],
147
- * [-70.609817, -33.468107],
148
- * [-70.595397, -33.458369],
149
- * [-70.587158, -33.442901],
150
- * [-70.587158, -33.426283],
151
- * [-70.590591, -33.414248],
152
- * [-70.594711, -33.406224],
153
- * [-70.603637, -33.399918]
154
- * ]]);
155
- * var options = {tolerance: 0.01, highQuality: false};
156
- * var simplified = turf.simplify(geojson, options);
157
- *
158
- * //addToMap
159
- * var addToMap = [geojson, simplified]
160
- */
161
- function simplify$1(geojson, options) {
162
- // Optional parameters
163
- options = options || {};
164
- if (!helpers.isObject(options)) throw new Error("options is invalid");
165
- var tolerance = options.tolerance !== undefined ? options.tolerance : 1;
166
- var highQuality = options.highQuality || false;
167
- var mutate = options.mutate || false;
168
-
169
- if (!geojson) throw new Error("geojson is required");
170
- if (tolerance && tolerance < 0) throw new Error("invalid tolerance");
171
-
172
- // Clone geojson to avoid side effects
173
- if (mutate !== true) geojson = clone__default['default'](geojson);
174
-
175
- meta.geomEach(geojson, function (geom) {
176
- simplifyGeom(geom, tolerance, highQuality);
177
- });
178
- return geojson;
179
- }
180
-
181
- /**
182
- * Simplifies a feature's coordinates
183
- *
184
- * @private
185
- * @param {Geometry} geometry to be simplified
186
- * @param {number} [tolerance=1] simplification tolerance
187
- * @param {boolean} [highQuality=false] whether or not to spend more time to create a higher-quality simplification with a different algorithm
188
- * @returns {Geometry} output
189
- */
190
- function simplifyGeom(geometry, tolerance, highQuality) {
191
- var type = geometry.type;
192
-
193
- // "unsimplyfiable" geometry types
194
- if (type === "Point" || type === "MultiPoint") return geometry;
195
-
196
- // Remove any extra coordinates
197
- cleanCoords__default['default'](geometry, true);
198
-
199
- var coordinates = geometry.coordinates;
200
- switch (type) {
201
- case "LineString":
202
- geometry["coordinates"] = simplifyLine(
203
- coordinates,
204
- tolerance,
205
- highQuality
206
- );
207
- break;
208
- case "MultiLineString":
209
- geometry["coordinates"] = coordinates.map(function (lines) {
210
- return simplifyLine(lines, tolerance, highQuality);
211
- });
212
- break;
213
- case "Polygon":
214
- geometry["coordinates"] = simplifyPolygon(
215
- coordinates,
216
- tolerance,
217
- highQuality
218
- );
219
- break;
220
- case "MultiPolygon":
221
- geometry["coordinates"] = coordinates.map(function (rings) {
222
- return simplifyPolygon(rings, tolerance, highQuality);
223
- });
224
- }
225
- return geometry;
226
- }
227
-
228
- /**
229
- * Simplifies the coordinates of a LineString with simplify-js
230
- *
231
- * @private
232
- * @param {Array<number>} coordinates to be processed
233
- * @param {number} tolerance simplification tolerance
234
- * @param {boolean} highQuality whether or not to spend more time to create a higher-quality
235
- * @returns {Array<Array<number>>} simplified coords
236
- */
237
- function simplifyLine(coordinates, tolerance, highQuality) {
238
- return simplify(
239
- coordinates.map(function (coord) {
240
- return { x: coord[0], y: coord[1], z: coord[2] };
241
- }),
242
- tolerance,
243
- highQuality
244
- ).map(function (coords) {
245
- return coords.z ? [coords.x, coords.y, coords.z] : [coords.x, coords.y];
246
- });
247
- }
248
-
249
- /**
250
- * Simplifies the coordinates of a Polygon with simplify-js
251
- *
252
- * @private
253
- * @param {Array<number>} coordinates to be processed
254
- * @param {number} tolerance simplification tolerance
255
- * @param {boolean} highQuality whether or not to spend more time to create a higher-quality
256
- * @returns {Array<Array<Array<number>>>} simplified coords
257
- */
258
- function simplifyPolygon(coordinates, tolerance, highQuality) {
259
- return coordinates.map(function (ring) {
260
- var pts = ring.map(function (coord) {
261
- return { x: coord[0], y: coord[1] };
262
- });
263
- if (pts.length < 4) {
264
- throw new Error("invalid polygon");
265
- }
266
- var simpleRing = simplify(pts, tolerance, highQuality).map(function (
267
- coords
268
- ) {
269
- return [coords.x, coords.y];
270
- });
271
- //remove 1 percent of tolerance until enough points to make a triangle
272
- while (!checkValidity(simpleRing)) {
273
- tolerance -= tolerance * 0.01;
274
- simpleRing = simplify(pts, tolerance, highQuality).map(function (
275
- coords
276
- ) {
277
- return [coords.x, coords.y];
278
- });
279
- }
280
- if (
281
- simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] ||
282
- simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]
283
- ) {
284
- simpleRing.push(simpleRing[0]);
285
- }
286
- return simpleRing;
287
- });
288
- }
289
-
290
- /**
291
- * Returns true if ring has at least 3 coordinates and its first coordinate is the same as its last
292
- *
293
- * @private
294
- * @param {Array<number>} ring coordinates to be checked
295
- * @returns {boolean} true if valid
296
- */
297
- function checkValidity(ring) {
298
- if (ring.length < 3) return false;
299
- //if the last point is the same as the first, it's not a triangle
300
- return !(
301
- ring.length === 3 &&
302
- ring[2][0] === ring[0][0] &&
303
- ring[2][1] === ring[0][1]
304
- );
305
- }
306
-
307
- module.exports = simplify$1;
308
- module.exports.default = simplify$1;
package/index.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { AllGeoJSON } from "@turf/helpers";
2
-
3
- /**
4
- * http://turfjs.org/docs/#simplify
5
- */
6
- export default function simplify<T extends AllGeoJSON>(
7
- geojson: T,
8
- options?: {
9
- tolerance?: number;
10
- highQuality?: boolean;
11
- mutate?: boolean;
12
- }
13
- ): T;