@turf/polygon-smooth 7.0.0-alpha.1 → 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 +9 -14
- package/dist/cjs/index.cjs +121 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +24 -0
- package/dist/esm/index.d.mts +24 -0
- package/dist/esm/index.mjs +121 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +34 -25
- package/dist/es/index.js +0 -157
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -160
- package/index.d.ts +0 -11
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Warning: may create degenerate polygons.
|
|
|
9
9
|
|
|
10
10
|
### Parameters
|
|
11
11
|
|
|
12
|
-
* `inputPolys` **([FeatureCollection][4] | [Feature][
|
|
12
|
+
* `inputPolys` **([FeatureCollection][4]<([Polygon][5] | [MultiPolygon][6])> | [Feature][7]<([Polygon][5] | [MultiPolygon][6])> | [Polygon][5] | [MultiPolygon][6])** (Multi)Polygon(s) to smooth
|
|
13
13
|
* `options` **[Object][8]** Optional parameters (optional, default `{}`)
|
|
14
14
|
|
|
15
15
|
* `options.iterations` **[string][9]** The number of times to smooth the polygon. A higher value means a smoother polygon. (optional, default `1`)
|
|
@@ -25,7 +25,7 @@ var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
|
|
|
25
25
|
var addToMap = [smoothed, polygon];
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
Returns **[FeatureCollection][4]<[Polygon][6]>** FeatureCollection containing the smoothed polygon/
|
|
28
|
+
Returns **[FeatureCollection][4]<([Polygon][5] | [MultiPolygon][6])>** FeatureCollection containing the smoothed polygon/multipoylgons
|
|
29
29
|
|
|
30
30
|
[1]: https://tools.ietf.org/html/rfc7946#section-3.1.6
|
|
31
31
|
|
|
@@ -35,36 +35,31 @@ Returns **[FeatureCollection][4]<[Polygon][6]>** FeatureCollection containing th
|
|
|
35
35
|
|
|
36
36
|
[4]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
37
37
|
|
|
38
|
-
[5]: https://tools.ietf.org/html/rfc7946#section-3.
|
|
38
|
+
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.6
|
|
39
39
|
|
|
40
|
-
[6]: https://tools.ietf.org/html/rfc7946#section-3.1.
|
|
40
|
+
[6]: https://tools.ietf.org/html/rfc7946#section-3.1.7
|
|
41
41
|
|
|
42
|
-
[7]: https://tools.ietf.org/html/rfc7946#section-3.
|
|
42
|
+
[7]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
43
43
|
|
|
44
44
|
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
45
45
|
|
|
46
46
|
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
47
47
|
|
|
48
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
49
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
50
|
-
./scripts/generate-readmes in the turf project. -->
|
|
48
|
+
<!-- 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. -->
|
|
51
49
|
|
|
52
50
|
---
|
|
53
51
|
|
|
54
|
-
This module is part of the [Turfjs project](
|
|
55
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
56
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
57
|
-
PRs and issues.
|
|
52
|
+
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.
|
|
58
53
|
|
|
59
54
|
### Installation
|
|
60
55
|
|
|
61
|
-
Install this module individually:
|
|
56
|
+
Install this single module individually:
|
|
62
57
|
|
|
63
58
|
```sh
|
|
64
59
|
$ npm install @turf/polygon-smooth
|
|
65
60
|
```
|
|
66
61
|
|
|
67
|
-
Or install the
|
|
62
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
68
63
|
|
|
69
64
|
```sh
|
|
70
65
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,121 @@
|
|
|
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 _helpers = require('@turf/helpers');
|
|
6
|
+
var _meta = require('@turf/meta');
|
|
7
|
+
function polygonSmooth(inputPolys, options) {
|
|
8
|
+
options = options || {};
|
|
9
|
+
options.iterations = options.iterations || 1;
|
|
10
|
+
const { iterations } = options;
|
|
11
|
+
const outPolys = [];
|
|
12
|
+
if (!inputPolys)
|
|
13
|
+
throw new Error("inputPolys is required");
|
|
14
|
+
_meta.geomEach.call(void 0, inputPolys, function(geom, geomIndex, properties) {
|
|
15
|
+
if (geom.type === "Polygon") {
|
|
16
|
+
let outCoords = [[]];
|
|
17
|
+
for (let i = 0; i < iterations; i++) {
|
|
18
|
+
let tempOutput = [];
|
|
19
|
+
let poly = geom;
|
|
20
|
+
if (i > 0) {
|
|
21
|
+
poly = _helpers.polygon.call(void 0, outCoords).geometry;
|
|
22
|
+
}
|
|
23
|
+
processPolygon(poly, tempOutput);
|
|
24
|
+
outCoords = tempOutput.slice(0);
|
|
25
|
+
}
|
|
26
|
+
outPolys.push(_helpers.polygon.call(void 0, outCoords, properties));
|
|
27
|
+
} else if (geom.type === "MultiPolygon") {
|
|
28
|
+
let outCoords = [[[]]];
|
|
29
|
+
for (let y = 0; y < iterations; y++) {
|
|
30
|
+
let tempOutput = [];
|
|
31
|
+
let poly = geom;
|
|
32
|
+
if (y > 0) {
|
|
33
|
+
poly = _helpers.multiPolygon.call(void 0, outCoords).geometry;
|
|
34
|
+
}
|
|
35
|
+
processMultiPolygon(poly, tempOutput);
|
|
36
|
+
outCoords = tempOutput.slice(0);
|
|
37
|
+
}
|
|
38
|
+
outPolys.push(_helpers.multiPolygon.call(void 0, outCoords, properties));
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error("geometry is invalid, must be Polygon or MultiPolygon");
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return _helpers.featureCollection.call(void 0, outPolys);
|
|
44
|
+
}
|
|
45
|
+
__name(polygonSmooth, "polygonSmooth");
|
|
46
|
+
function processPolygon(poly, tempOutput) {
|
|
47
|
+
var previousCoord;
|
|
48
|
+
var previousGeometryIndex;
|
|
49
|
+
_meta.coordEach.call(void 0,
|
|
50
|
+
poly,
|
|
51
|
+
function(currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
|
|
52
|
+
if (previousGeometryIndex !== geometryIndex) {
|
|
53
|
+
tempOutput.push([]);
|
|
54
|
+
} else {
|
|
55
|
+
var p0x = previousCoord[0];
|
|
56
|
+
var p0y = previousCoord[1];
|
|
57
|
+
var p1x = currentCoord[0];
|
|
58
|
+
var p1y = currentCoord[1];
|
|
59
|
+
tempOutput[geometryIndex].push([
|
|
60
|
+
0.75 * p0x + 0.25 * p1x,
|
|
61
|
+
0.75 * p0y + 0.25 * p1y
|
|
62
|
+
]);
|
|
63
|
+
tempOutput[geometryIndex].push([
|
|
64
|
+
0.25 * p0x + 0.75 * p1x,
|
|
65
|
+
0.25 * p0y + 0.75 * p1y
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
previousCoord = currentCoord;
|
|
69
|
+
previousGeometryIndex = geometryIndex;
|
|
70
|
+
},
|
|
71
|
+
false
|
|
72
|
+
);
|
|
73
|
+
tempOutput.forEach(function(ring) {
|
|
74
|
+
ring.push(ring[0]);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
__name(processPolygon, "processPolygon");
|
|
78
|
+
function processMultiPolygon(poly, tempOutput) {
|
|
79
|
+
let previousCoord;
|
|
80
|
+
let previousMultiFeatureIndex;
|
|
81
|
+
let previousGeometryIndex;
|
|
82
|
+
_meta.coordEach.call(void 0,
|
|
83
|
+
poly,
|
|
84
|
+
function(currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
|
|
85
|
+
if (previousMultiFeatureIndex !== multiFeatureIndex) {
|
|
86
|
+
tempOutput.push([[]]);
|
|
87
|
+
} else if (previousGeometryIndex !== geometryIndex) {
|
|
88
|
+
tempOutput[multiFeatureIndex].push([]);
|
|
89
|
+
} else {
|
|
90
|
+
var p0x = previousCoord[0];
|
|
91
|
+
var p0y = previousCoord[1];
|
|
92
|
+
var p1x = currentCoord[0];
|
|
93
|
+
var p1y = currentCoord[1];
|
|
94
|
+
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
95
|
+
0.75 * p0x + 0.25 * p1x,
|
|
96
|
+
0.75 * p0y + 0.25 * p1y
|
|
97
|
+
]);
|
|
98
|
+
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
99
|
+
0.25 * p0x + 0.75 * p1x,
|
|
100
|
+
0.25 * p0y + 0.75 * p1y
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
previousCoord = currentCoord;
|
|
104
|
+
previousMultiFeatureIndex = multiFeatureIndex;
|
|
105
|
+
previousGeometryIndex = geometryIndex;
|
|
106
|
+
},
|
|
107
|
+
false
|
|
108
|
+
);
|
|
109
|
+
tempOutput.forEach(function(poly2) {
|
|
110
|
+
poly2.forEach(function(ring) {
|
|
111
|
+
ring.push(ring[0]);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
__name(processMultiPolygon, "processMultiPolygon");
|
|
116
|
+
var turf_polygon_smooth_default = polygonSmooth;
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
exports.default = turf_polygon_smooth_default; exports.polygonSmooth = polygonSmooth;
|
|
121
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":["poly"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A,QACtB,CAAC;AAAA,MACH;AACA,sBAAgB;AAChB,kCAA4B;AAC5B,8BAAwB;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACA,aAAW,QAAQ,SAAUA,OAAM;AACjC,IAAAA,MAAK,QAAQ,SAAU,MAAM;AAC3B,WAAK,KAAK,KAAK,CAAC,CAAC;AAAA,IACnB,CAAC;AAAA,EACH,CAAC;AACH;AA3CS;AA8CT,IAAO,8BAAQ","sourcesContent":["import type {\n Feature,\n FeatureCollection,\n Polygon,\n Position,\n MultiPolygon,\n} from \"geojson\";\nimport { featureCollection, multiPolygon, polygon } from \"@turf/helpers\";\nimport { coordEach, geomEach } from \"@turf/meta\";\n\n/**\n * Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).\n * Warning: may create degenerate polygons.\n *\n * @name polygonSmooth\n * @param {FeatureCollection<Polygon|MultiPolygon>|Feature<Polygon|MultiPolygon>|Polygon|MultiPolygon} inputPolys (Multi)Polygon(s) to smooth\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.\n * @returns {FeatureCollection<Polygon|MultiPolygon>} FeatureCollection containing the smoothed polygon/multipoylgons\n * @example\n * var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);\n *\n * var smoothed = turf.polygonSmooth(polygon, {iterations: 3})\n *\n * //addToMap\n * var addToMap = [smoothed, polygon];\n */\nfunction polygonSmooth(\n inputPolys:\n | FeatureCollection<Polygon | MultiPolygon>\n | Feature<Polygon | MultiPolygon>\n | Polygon\n | MultiPolygon,\n options?: {\n iterations?: number;\n }\n): FeatureCollection<Polygon | MultiPolygon> {\n // Optional parameters\n options = options || {};\n options.iterations = options.iterations || 1;\n\n const { iterations } = options;\n\n const outPolys: Feature<Polygon | MultiPolygon>[] = [];\n if (!inputPolys) throw new Error(\"inputPolys is required\");\n\n geomEach(inputPolys, function (geom, geomIndex, properties) {\n if (geom.type === \"Polygon\") {\n let outCoords: Position[][] = [[]];\n for (let i = 0; i < iterations; i++) {\n let tempOutput: Position[][] = [];\n let poly = geom;\n if (i > 0) {\n poly = polygon(outCoords).geometry;\n }\n processPolygon(poly, tempOutput);\n outCoords = tempOutput.slice(0);\n }\n outPolys.push(polygon(outCoords, properties));\n } else if (geom.type === \"MultiPolygon\") {\n let outCoords: Position[][][] = [[[]]];\n for (let y = 0; y < iterations; y++) {\n let tempOutput: Position[][][] = [];\n let poly = geom;\n if (y > 0) {\n poly = multiPolygon(outCoords).geometry;\n }\n processMultiPolygon(poly, tempOutput);\n outCoords = tempOutput.slice(0);\n }\n outPolys.push(multiPolygon(outCoords, properties));\n } else {\n throw new Error(\"geometry is invalid, must be Polygon or MultiPolygon\");\n }\n });\n\n return featureCollection(outPolys);\n}\n\n/**\n * @param {poly} poly to process\n * @param {poly} tempOutput to place the results in\n * @private\n */\nfunction processPolygon(poly: Polygon, tempOutput: Position[][]) {\n var previousCoord: Position;\n var previousGeometryIndex: number;\n\n coordEach(\n poly,\n function (\n currentCoord,\n coordIndex,\n featureIndex,\n multiFeatureIndex,\n geometryIndex\n ) {\n if (previousGeometryIndex !== geometryIndex) {\n tempOutput.push([]);\n } else {\n var p0x = previousCoord[0];\n var p0y = previousCoord[1];\n var p1x = currentCoord[0];\n var p1y = currentCoord[1];\n tempOutput[geometryIndex].push([\n 0.75 * p0x + 0.25 * p1x,\n 0.75 * p0y + 0.25 * p1y,\n ]);\n tempOutput[geometryIndex].push([\n 0.25 * p0x + 0.75 * p1x,\n 0.25 * p0y + 0.75 * p1y,\n ]);\n }\n previousCoord = currentCoord;\n previousGeometryIndex = geometryIndex;\n },\n false\n );\n tempOutput.forEach(function (ring) {\n ring.push(ring[0]);\n });\n}\n\n/**\n * @param {poly} poly to process\n * @param {poly} tempOutput to place the results in\n * @private\n */\nfunction processMultiPolygon(poly: MultiPolygon, tempOutput: Position[][][]) {\n let previousCoord: Position;\n let previousMultiFeatureIndex: number;\n let previousGeometryIndex: number;\n\n coordEach(\n poly,\n function (\n currentCoord,\n coordIndex,\n featureIndex,\n multiFeatureIndex,\n geometryIndex\n ) {\n if (previousMultiFeatureIndex !== multiFeatureIndex) {\n tempOutput.push([[]]);\n } else if (previousGeometryIndex !== geometryIndex) {\n tempOutput[multiFeatureIndex].push([]);\n } else {\n var p0x = previousCoord[0];\n var p0y = previousCoord[1];\n var p1x = currentCoord[0];\n var p1y = currentCoord[1];\n tempOutput[multiFeatureIndex][geometryIndex].push([\n 0.75 * p0x + 0.25 * p1x,\n 0.75 * p0y + 0.25 * p1y,\n ]);\n tempOutput[multiFeatureIndex][geometryIndex].push([\n 0.25 * p0x + 0.75 * p1x,\n 0.25 * p0y + 0.75 * p1y,\n ]);\n }\n previousCoord = currentCoord;\n previousMultiFeatureIndex = multiFeatureIndex;\n previousGeometryIndex = geometryIndex;\n },\n false\n );\n tempOutput.forEach(function (poly) {\n poly.forEach(function (ring) {\n ring.push(ring[0]);\n });\n });\n}\n\nexport { polygonSmooth };\nexport default polygonSmooth;\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { FeatureCollection, Polygon, MultiPolygon, Feature } from 'geojson';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).
|
|
5
|
+
* Warning: may create degenerate polygons.
|
|
6
|
+
*
|
|
7
|
+
* @name polygonSmooth
|
|
8
|
+
* @param {FeatureCollection<Polygon|MultiPolygon>|Feature<Polygon|MultiPolygon>|Polygon|MultiPolygon} inputPolys (Multi)Polygon(s) to smooth
|
|
9
|
+
* @param {Object} [options={}] Optional parameters
|
|
10
|
+
* @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
|
|
11
|
+
* @returns {FeatureCollection<Polygon|MultiPolygon>} FeatureCollection containing the smoothed polygon/multipoylgons
|
|
12
|
+
* @example
|
|
13
|
+
* var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
|
|
14
|
+
*
|
|
15
|
+
* var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
|
|
16
|
+
*
|
|
17
|
+
* //addToMap
|
|
18
|
+
* var addToMap = [smoothed, polygon];
|
|
19
|
+
*/
|
|
20
|
+
declare function polygonSmooth(inputPolys: FeatureCollection<Polygon | MultiPolygon> | Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon, options?: {
|
|
21
|
+
iterations?: number;
|
|
22
|
+
}): FeatureCollection<Polygon | MultiPolygon>;
|
|
23
|
+
|
|
24
|
+
export { polygonSmooth as default, polygonSmooth };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { FeatureCollection, Polygon, MultiPolygon, Feature } from 'geojson';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).
|
|
5
|
+
* Warning: may create degenerate polygons.
|
|
6
|
+
*
|
|
7
|
+
* @name polygonSmooth
|
|
8
|
+
* @param {FeatureCollection<Polygon|MultiPolygon>|Feature<Polygon|MultiPolygon>|Polygon|MultiPolygon} inputPolys (Multi)Polygon(s) to smooth
|
|
9
|
+
* @param {Object} [options={}] Optional parameters
|
|
10
|
+
* @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
|
|
11
|
+
* @returns {FeatureCollection<Polygon|MultiPolygon>} FeatureCollection containing the smoothed polygon/multipoylgons
|
|
12
|
+
* @example
|
|
13
|
+
* var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
|
|
14
|
+
*
|
|
15
|
+
* var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
|
|
16
|
+
*
|
|
17
|
+
* //addToMap
|
|
18
|
+
* var addToMap = [smoothed, polygon];
|
|
19
|
+
*/
|
|
20
|
+
declare function polygonSmooth(inputPolys: FeatureCollection<Polygon | MultiPolygon> | Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon, options?: {
|
|
21
|
+
iterations?: number;
|
|
22
|
+
}): FeatureCollection<Polygon | MultiPolygon>;
|
|
23
|
+
|
|
24
|
+
export { polygonSmooth as default, polygonSmooth };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { featureCollection, multiPolygon, polygon } from "@turf/helpers";
|
|
6
|
+
import { coordEach, geomEach } from "@turf/meta";
|
|
7
|
+
function polygonSmooth(inputPolys, options) {
|
|
8
|
+
options = options || {};
|
|
9
|
+
options.iterations = options.iterations || 1;
|
|
10
|
+
const { iterations } = options;
|
|
11
|
+
const outPolys = [];
|
|
12
|
+
if (!inputPolys)
|
|
13
|
+
throw new Error("inputPolys is required");
|
|
14
|
+
geomEach(inputPolys, function(geom, geomIndex, properties) {
|
|
15
|
+
if (geom.type === "Polygon") {
|
|
16
|
+
let outCoords = [[]];
|
|
17
|
+
for (let i = 0; i < iterations; i++) {
|
|
18
|
+
let tempOutput = [];
|
|
19
|
+
let poly = geom;
|
|
20
|
+
if (i > 0) {
|
|
21
|
+
poly = polygon(outCoords).geometry;
|
|
22
|
+
}
|
|
23
|
+
processPolygon(poly, tempOutput);
|
|
24
|
+
outCoords = tempOutput.slice(0);
|
|
25
|
+
}
|
|
26
|
+
outPolys.push(polygon(outCoords, properties));
|
|
27
|
+
} else if (geom.type === "MultiPolygon") {
|
|
28
|
+
let outCoords = [[[]]];
|
|
29
|
+
for (let y = 0; y < iterations; y++) {
|
|
30
|
+
let tempOutput = [];
|
|
31
|
+
let poly = geom;
|
|
32
|
+
if (y > 0) {
|
|
33
|
+
poly = multiPolygon(outCoords).geometry;
|
|
34
|
+
}
|
|
35
|
+
processMultiPolygon(poly, tempOutput);
|
|
36
|
+
outCoords = tempOutput.slice(0);
|
|
37
|
+
}
|
|
38
|
+
outPolys.push(multiPolygon(outCoords, properties));
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error("geometry is invalid, must be Polygon or MultiPolygon");
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return featureCollection(outPolys);
|
|
44
|
+
}
|
|
45
|
+
__name(polygonSmooth, "polygonSmooth");
|
|
46
|
+
function processPolygon(poly, tempOutput) {
|
|
47
|
+
var previousCoord;
|
|
48
|
+
var previousGeometryIndex;
|
|
49
|
+
coordEach(
|
|
50
|
+
poly,
|
|
51
|
+
function(currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
|
|
52
|
+
if (previousGeometryIndex !== geometryIndex) {
|
|
53
|
+
tempOutput.push([]);
|
|
54
|
+
} else {
|
|
55
|
+
var p0x = previousCoord[0];
|
|
56
|
+
var p0y = previousCoord[1];
|
|
57
|
+
var p1x = currentCoord[0];
|
|
58
|
+
var p1y = currentCoord[1];
|
|
59
|
+
tempOutput[geometryIndex].push([
|
|
60
|
+
0.75 * p0x + 0.25 * p1x,
|
|
61
|
+
0.75 * p0y + 0.25 * p1y
|
|
62
|
+
]);
|
|
63
|
+
tempOutput[geometryIndex].push([
|
|
64
|
+
0.25 * p0x + 0.75 * p1x,
|
|
65
|
+
0.25 * p0y + 0.75 * p1y
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
previousCoord = currentCoord;
|
|
69
|
+
previousGeometryIndex = geometryIndex;
|
|
70
|
+
},
|
|
71
|
+
false
|
|
72
|
+
);
|
|
73
|
+
tempOutput.forEach(function(ring) {
|
|
74
|
+
ring.push(ring[0]);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
__name(processPolygon, "processPolygon");
|
|
78
|
+
function processMultiPolygon(poly, tempOutput) {
|
|
79
|
+
let previousCoord;
|
|
80
|
+
let previousMultiFeatureIndex;
|
|
81
|
+
let previousGeometryIndex;
|
|
82
|
+
coordEach(
|
|
83
|
+
poly,
|
|
84
|
+
function(currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
|
|
85
|
+
if (previousMultiFeatureIndex !== multiFeatureIndex) {
|
|
86
|
+
tempOutput.push([[]]);
|
|
87
|
+
} else if (previousGeometryIndex !== geometryIndex) {
|
|
88
|
+
tempOutput[multiFeatureIndex].push([]);
|
|
89
|
+
} else {
|
|
90
|
+
var p0x = previousCoord[0];
|
|
91
|
+
var p0y = previousCoord[1];
|
|
92
|
+
var p1x = currentCoord[0];
|
|
93
|
+
var p1y = currentCoord[1];
|
|
94
|
+
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
95
|
+
0.75 * p0x + 0.25 * p1x,
|
|
96
|
+
0.75 * p0y + 0.25 * p1y
|
|
97
|
+
]);
|
|
98
|
+
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
99
|
+
0.25 * p0x + 0.75 * p1x,
|
|
100
|
+
0.25 * p0y + 0.75 * p1y
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
previousCoord = currentCoord;
|
|
104
|
+
previousMultiFeatureIndex = multiFeatureIndex;
|
|
105
|
+
previousGeometryIndex = geometryIndex;
|
|
106
|
+
},
|
|
107
|
+
false
|
|
108
|
+
);
|
|
109
|
+
tempOutput.forEach(function(poly2) {
|
|
110
|
+
poly2.forEach(function(ring) {
|
|
111
|
+
ring.push(ring[0]);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
__name(processMultiPolygon, "processMultiPolygon");
|
|
116
|
+
var turf_polygon_smooth_default = polygonSmooth;
|
|
117
|
+
export {
|
|
118
|
+
turf_polygon_smooth_default as default,
|
|
119
|
+
polygonSmooth
|
|
120
|
+
};
|
|
121
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import type {\n Feature,\n FeatureCollection,\n Polygon,\n Position,\n MultiPolygon,\n} from \"geojson\";\nimport { featureCollection, multiPolygon, polygon } from \"@turf/helpers\";\nimport { coordEach, geomEach } from \"@turf/meta\";\n\n/**\n * Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).\n * Warning: may create degenerate polygons.\n *\n * @name polygonSmooth\n * @param {FeatureCollection<Polygon|MultiPolygon>|Feature<Polygon|MultiPolygon>|Polygon|MultiPolygon} inputPolys (Multi)Polygon(s) to smooth\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.\n * @returns {FeatureCollection<Polygon|MultiPolygon>} FeatureCollection containing the smoothed polygon/multipoylgons\n * @example\n * var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);\n *\n * var smoothed = turf.polygonSmooth(polygon, {iterations: 3})\n *\n * //addToMap\n * var addToMap = [smoothed, polygon];\n */\nfunction polygonSmooth(\n inputPolys:\n | FeatureCollection<Polygon | MultiPolygon>\n | Feature<Polygon | MultiPolygon>\n | Polygon\n | MultiPolygon,\n options?: {\n iterations?: number;\n }\n): FeatureCollection<Polygon | MultiPolygon> {\n // Optional parameters\n options = options || {};\n options.iterations = options.iterations || 1;\n\n const { iterations } = options;\n\n const outPolys: Feature<Polygon | MultiPolygon>[] = [];\n if (!inputPolys) throw new Error(\"inputPolys is required\");\n\n geomEach(inputPolys, function (geom, geomIndex, properties) {\n if (geom.type === \"Polygon\") {\n let outCoords: Position[][] = [[]];\n for (let i = 0; i < iterations; i++) {\n let tempOutput: Position[][] = [];\n let poly = geom;\n if (i > 0) {\n poly = polygon(outCoords).geometry;\n }\n processPolygon(poly, tempOutput);\n outCoords = tempOutput.slice(0);\n }\n outPolys.push(polygon(outCoords, properties));\n } else if (geom.type === \"MultiPolygon\") {\n let outCoords: Position[][][] = [[[]]];\n for (let y = 0; y < iterations; y++) {\n let tempOutput: Position[][][] = [];\n let poly = geom;\n if (y > 0) {\n poly = multiPolygon(outCoords).geometry;\n }\n processMultiPolygon(poly, tempOutput);\n outCoords = tempOutput.slice(0);\n }\n outPolys.push(multiPolygon(outCoords, properties));\n } else {\n throw new Error(\"geometry is invalid, must be Polygon or MultiPolygon\");\n }\n });\n\n return featureCollection(outPolys);\n}\n\n/**\n * @param {poly} poly to process\n * @param {poly} tempOutput to place the results in\n * @private\n */\nfunction processPolygon(poly: Polygon, tempOutput: Position[][]) {\n var previousCoord: Position;\n var previousGeometryIndex: number;\n\n coordEach(\n poly,\n function (\n currentCoord,\n coordIndex,\n featureIndex,\n multiFeatureIndex,\n geometryIndex\n ) {\n if (previousGeometryIndex !== geometryIndex) {\n tempOutput.push([]);\n } else {\n var p0x = previousCoord[0];\n var p0y = previousCoord[1];\n var p1x = currentCoord[0];\n var p1y = currentCoord[1];\n tempOutput[geometryIndex].push([\n 0.75 * p0x + 0.25 * p1x,\n 0.75 * p0y + 0.25 * p1y,\n ]);\n tempOutput[geometryIndex].push([\n 0.25 * p0x + 0.75 * p1x,\n 0.25 * p0y + 0.75 * p1y,\n ]);\n }\n previousCoord = currentCoord;\n previousGeometryIndex = geometryIndex;\n },\n false\n );\n tempOutput.forEach(function (ring) {\n ring.push(ring[0]);\n });\n}\n\n/**\n * @param {poly} poly to process\n * @param {poly} tempOutput to place the results in\n * @private\n */\nfunction processMultiPolygon(poly: MultiPolygon, tempOutput: Position[][][]) {\n let previousCoord: Position;\n let previousMultiFeatureIndex: number;\n let previousGeometryIndex: number;\n\n coordEach(\n poly,\n function (\n currentCoord,\n coordIndex,\n featureIndex,\n multiFeatureIndex,\n geometryIndex\n ) {\n if (previousMultiFeatureIndex !== multiFeatureIndex) {\n tempOutput.push([[]]);\n } else if (previousGeometryIndex !== geometryIndex) {\n tempOutput[multiFeatureIndex].push([]);\n } else {\n var p0x = previousCoord[0];\n var p0y = previousCoord[1];\n var p1x = currentCoord[0];\n var p1y = currentCoord[1];\n tempOutput[multiFeatureIndex][geometryIndex].push([\n 0.75 * p0x + 0.25 * p1x,\n 0.75 * p0y + 0.25 * p1y,\n ]);\n tempOutput[multiFeatureIndex][geometryIndex].push([\n 0.25 * p0x + 0.75 * p1x,\n 0.25 * p0y + 0.75 * p1y,\n ]);\n }\n previousCoord = currentCoord;\n previousMultiFeatureIndex = multiFeatureIndex;\n previousGeometryIndex = geometryIndex;\n },\n false\n );\n tempOutput.forEach(function (poly) {\n poly.forEach(function (ring) {\n ring.push(ring[0]);\n });\n });\n}\n\nexport { polygonSmooth };\nexport default polygonSmooth;\n"],"mappings":";;;;AAOA,SAAS,mBAAmB,cAAc,eAAe;AACzD,SAAS,WAAW,gBAAgB;AAmBpC,SAAS,cACP,YAKA,SAG2C;AAE3C,YAAU,WAAW,CAAC;AACtB,UAAQ,aAAa,QAAQ,cAAc;AAE3C,QAAM,EAAE,WAAW,IAAI;AAEvB,QAAM,WAA8C,CAAC;AACrD,MAAI,CAAC;AAAY,UAAM,IAAI,MAAM,wBAAwB;AAEzD,WAAS,YAAY,SAAU,MAAM,WAAW,YAAY;AAC1D,QAAI,KAAK,SAAS,WAAW;AAC3B,UAAI,YAA0B,CAAC,CAAC,CAAC;AACjC,eAAS,IAAI,GAAG,IAAI,YAAY,KAAK;AACnC,YAAI,aAA2B,CAAC;AAChC,YAAI,OAAO;AACX,YAAI,IAAI,GAAG;AACT,iBAAO,QAAQ,SAAS,EAAE;AAAA,QAC5B;AACA,uBAAe,MAAM,UAAU;AAC/B,oBAAY,WAAW,MAAM,CAAC;AAAA,MAChC;AACA,eAAS,KAAK,QAAQ,WAAW,UAAU,CAAC;AAAA,IAC9C,WAAW,K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,IACE;AAAA,IACA,SACE,cACA,YACA,cACA,mBACA,eACA;AACA,UAAI,8BAA8B,mBAAmB;AACnD,mBAAW,KAAK,CAAC,CAAC,CAAC,CAAC;AAAA,MACtB,WAAW,0BAA0B,eAAe;AAClD,mBAAW,iBAAiB,EAAE,KAAK,CAAC,CAAC;AAAA,MACvC,OAAO;AACL,YAAI,MAAM,cAAc,CAAC;AACzB,YAAI,MAAM,cAAc,CAAC;AACzB,YAAI,MAAM,aAAa,CAAC;AACxB,YAAI,MAAM,aAAa,CAAC;AACxB,mBAAW,iBAAiB,EAAE,aAAa,EAAE,KAAK;AAAA,UAChD,OAAO,MAAM,OAAO;AAAA,UACpB,OAAO,MAAM,OAAO;AAAA,QACtB,CAAC;AACD,mBAAW,iBAAiB,EAAE,aAAa,EAAE,KAAK;AAAA,UAChD,OAAO,MAAM,OAAO;AAAA,UACpB,OAAO,MAAM,OAAO;AAAA,QACtB,CAAC;AAAA,MACH;AACA,sBAAgB;AAChB,kCAA4B;AAC5B,8BAAwB;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACA,aAAW,QAAQ,SAAUA,OAAM;AACjC,IAAAA,MAAK,QAAQ,SAAU,MAAM;AAC3B,WAAK,KAAK,KAAK,CAAC,CAAC;AAAA,IACnB,CAAC;AAAA,EACH,CAAC;AACH;AA3CS;AA8CT,IAAO,8BAAQ;","names":["poly"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/polygon-smooth",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.110+1411d63a7",
|
|
4
4
|
"description": "turf polygon smooth module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -24,43 +24,52 @@
|
|
|
24
24
|
"geojson",
|
|
25
25
|
"polygon"
|
|
26
26
|
],
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"type": "commonjs",
|
|
28
|
+
"main": "dist/cjs/index.cjs",
|
|
29
|
+
"module": "dist/esm/index.mjs",
|
|
30
|
+
"types": "dist/cjs/index.d.ts",
|
|
29
31
|
"exports": {
|
|
30
32
|
"./package.json": "./package.json",
|
|
31
33
|
".": {
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/esm/index.d.mts",
|
|
36
|
+
"default": "./dist/esm/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/cjs/index.d.ts",
|
|
40
|
+
"default": "./dist/cjs/index.cjs"
|
|
41
|
+
}
|
|
35
42
|
}
|
|
36
43
|
},
|
|
37
|
-
"types": "index.d.ts",
|
|
38
44
|
"sideEffects": false,
|
|
39
45
|
"files": [
|
|
40
|
-
"dist"
|
|
41
|
-
"index.d.ts"
|
|
46
|
+
"dist"
|
|
42
47
|
],
|
|
43
48
|
"scripts": {
|
|
44
|
-
"bench": "tsx bench.
|
|
45
|
-
"build": "
|
|
46
|
-
"docs": "tsx ../../scripts/generate-readmes",
|
|
47
|
-
"test": "npm-run-all test:*",
|
|
48
|
-
"test:tape": "tsx test.
|
|
49
|
+
"bench": "tsx bench.ts",
|
|
50
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
51
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
52
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
53
|
+
"test:tape": "tsx test.ts",
|
|
49
54
|
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
50
55
|
},
|
|
51
56
|
"devDependencies": {
|
|
52
|
-
"benchmark": "
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
57
|
+
"@types/benchmark": "^2.1.5",
|
|
58
|
+
"@types/tape": "^4.2.32",
|
|
59
|
+
"benchmark": "^2.1.4",
|
|
60
|
+
"glob": "^10.3.10",
|
|
61
|
+
"load-json-file": "^7.0.1",
|
|
62
|
+
"npm-run-all": "^4.1.5",
|
|
63
|
+
"tape": "^5.7.2",
|
|
64
|
+
"tsup": "^8.0.1",
|
|
65
|
+
"tsx": "^4.6.2",
|
|
66
|
+
"typescript": "^5.2.2",
|
|
67
|
+
"write-json-file": "^5.0.0"
|
|
60
68
|
},
|
|
61
69
|
"dependencies": {
|
|
62
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
63
|
-
"@turf/meta": "^7.0.0-alpha.
|
|
70
|
+
"@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
|
|
71
|
+
"@turf/meta": "^7.0.0-alpha.110+1411d63a7",
|
|
72
|
+
"tslib": "^2.6.2"
|
|
64
73
|
},
|
|
65
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
|
|
66
75
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { multiPolygon, polygon, featureCollection } from '@turf/helpers';
|
|
2
|
-
import { geomEach, coordEach } from '@turf/meta';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).
|
|
6
|
-
* Warning: may create degenerate polygons.
|
|
7
|
-
*
|
|
8
|
-
* @name polygonSmooth
|
|
9
|
-
* @param {FeatureCollection|Feature<Polygon|MultiPolygon>} inputPolys (Multi)Polygon(s) to smooth
|
|
10
|
-
* @param {Object} [options={}] Optional parameters
|
|
11
|
-
* @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
|
|
12
|
-
* @returns {FeatureCollection<Polygon>} FeatureCollection containing the smoothed polygon/poylgons
|
|
13
|
-
* @example
|
|
14
|
-
* var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
|
|
15
|
-
*
|
|
16
|
-
* var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
|
|
17
|
-
*
|
|
18
|
-
* //addToMap
|
|
19
|
-
* var addToMap = [smoothed, polygon];
|
|
20
|
-
*/
|
|
21
|
-
function polygonSmooth(inputPolys, options) {
|
|
22
|
-
options = options || {};
|
|
23
|
-
var outPolys = [];
|
|
24
|
-
// Optional parameters
|
|
25
|
-
var iterations = options.iterations || 1;
|
|
26
|
-
if (!inputPolys) throw new Error("inputPolys is required");
|
|
27
|
-
|
|
28
|
-
geomEach(inputPolys, function (geom, geomIndex, properties) {
|
|
29
|
-
var outCoords;
|
|
30
|
-
var poly;
|
|
31
|
-
var tempOutput;
|
|
32
|
-
|
|
33
|
-
switch (geom.type) {
|
|
34
|
-
case "Polygon":
|
|
35
|
-
outCoords = [[]];
|
|
36
|
-
for (var i = 0; i < iterations; i++) {
|
|
37
|
-
tempOutput = [];
|
|
38
|
-
poly = geom;
|
|
39
|
-
if (i > 0) poly = polygon(outCoords).geometry;
|
|
40
|
-
processPolygon(poly, tempOutput);
|
|
41
|
-
outCoords = tempOutput.slice(0);
|
|
42
|
-
}
|
|
43
|
-
outPolys.push(polygon(outCoords, properties));
|
|
44
|
-
break;
|
|
45
|
-
case "MultiPolygon":
|
|
46
|
-
outCoords = [[[]]];
|
|
47
|
-
for (var y = 0; y < iterations; y++) {
|
|
48
|
-
tempOutput = [];
|
|
49
|
-
poly = geom;
|
|
50
|
-
if (y > 0) poly = multiPolygon(outCoords).geometry;
|
|
51
|
-
processMultiPolygon(poly, tempOutput);
|
|
52
|
-
outCoords = tempOutput.slice(0);
|
|
53
|
-
}
|
|
54
|
-
outPolys.push(multiPolygon(outCoords, properties));
|
|
55
|
-
break;
|
|
56
|
-
default:
|
|
57
|
-
throw new Error("geometry is invalid, must be Polygon or MultiPolygon");
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
return featureCollection(outPolys);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @param {poly} poly to process
|
|
65
|
-
* @param {poly} tempOutput to place the results in
|
|
66
|
-
* @private
|
|
67
|
-
*/
|
|
68
|
-
function processPolygon(poly, tempOutput) {
|
|
69
|
-
var previousCoord = null;
|
|
70
|
-
var previousGeometryIndex = null;
|
|
71
|
-
|
|
72
|
-
coordEach(
|
|
73
|
-
poly,
|
|
74
|
-
function (
|
|
75
|
-
currentCoord,
|
|
76
|
-
coordIndex,
|
|
77
|
-
featureIndex,
|
|
78
|
-
multiFeatureIndex,
|
|
79
|
-
geometryIndex
|
|
80
|
-
) {
|
|
81
|
-
if (previousGeometryIndex !== geometryIndex) {
|
|
82
|
-
tempOutput.push([]);
|
|
83
|
-
} else {
|
|
84
|
-
var p0x = previousCoord[0];
|
|
85
|
-
var p0y = previousCoord[1];
|
|
86
|
-
var p1x = currentCoord[0];
|
|
87
|
-
var p1y = currentCoord[1];
|
|
88
|
-
tempOutput[geometryIndex].push([
|
|
89
|
-
0.75 * p0x + 0.25 * p1x,
|
|
90
|
-
0.75 * p0y + 0.25 * p1y,
|
|
91
|
-
]);
|
|
92
|
-
tempOutput[geometryIndex].push([
|
|
93
|
-
0.25 * p0x + 0.75 * p1x,
|
|
94
|
-
0.25 * p0y + 0.75 * p1y,
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
previousCoord = currentCoord;
|
|
98
|
-
previousGeometryIndex = geometryIndex;
|
|
99
|
-
},
|
|
100
|
-
false
|
|
101
|
-
);
|
|
102
|
-
tempOutput.forEach(function (ring) {
|
|
103
|
-
ring.push(ring[0]);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* @param {poly} poly to process
|
|
109
|
-
* @param {poly} tempOutput to place the results in
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
function processMultiPolygon(poly, tempOutput) {
|
|
113
|
-
var previousCoord = null;
|
|
114
|
-
var previousMultiFeatureIndex = null;
|
|
115
|
-
var previousGeometryIndex = null;
|
|
116
|
-
|
|
117
|
-
coordEach(
|
|
118
|
-
poly,
|
|
119
|
-
function (
|
|
120
|
-
currentCoord,
|
|
121
|
-
coordIndex,
|
|
122
|
-
featureIndex,
|
|
123
|
-
multiFeatureIndex,
|
|
124
|
-
geometryIndex
|
|
125
|
-
) {
|
|
126
|
-
if (previousMultiFeatureIndex !== multiFeatureIndex) {
|
|
127
|
-
tempOutput.push([[]]);
|
|
128
|
-
} else if (previousGeometryIndex !== geometryIndex) {
|
|
129
|
-
tempOutput[multiFeatureIndex].push([]);
|
|
130
|
-
} else {
|
|
131
|
-
var p0x = previousCoord[0];
|
|
132
|
-
var p0y = previousCoord[1];
|
|
133
|
-
var p1x = currentCoord[0];
|
|
134
|
-
var p1y = currentCoord[1];
|
|
135
|
-
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
136
|
-
0.75 * p0x + 0.25 * p1x,
|
|
137
|
-
0.75 * p0y + 0.25 * p1y,
|
|
138
|
-
]);
|
|
139
|
-
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
140
|
-
0.25 * p0x + 0.75 * p1x,
|
|
141
|
-
0.25 * p0y + 0.75 * p1y,
|
|
142
|
-
]);
|
|
143
|
-
}
|
|
144
|
-
previousCoord = currentCoord;
|
|
145
|
-
previousMultiFeatureIndex = multiFeatureIndex;
|
|
146
|
-
previousGeometryIndex = geometryIndex;
|
|
147
|
-
},
|
|
148
|
-
false
|
|
149
|
-
);
|
|
150
|
-
tempOutput.forEach(function (poly) {
|
|
151
|
-
poly.forEach(function (ring) {
|
|
152
|
-
ring.push(ring[0]);
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export default polygonSmooth;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var helpers = require('@turf/helpers');
|
|
4
|
-
var meta = require('@turf/meta');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Smooths a {@link Polygon} or {@link MultiPolygon}. Based on [Chaikin's algorithm](http://graphics.cs.ucdavis.edu/education/CAGDNotes/Chaikins-Algorithm/Chaikins-Algorithm.html).
|
|
8
|
-
* Warning: may create degenerate polygons.
|
|
9
|
-
*
|
|
10
|
-
* @name polygonSmooth
|
|
11
|
-
* @param {FeatureCollection|Feature<Polygon|MultiPolygon>} inputPolys (Multi)Polygon(s) to smooth
|
|
12
|
-
* @param {Object} [options={}] Optional parameters
|
|
13
|
-
* @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
|
|
14
|
-
* @returns {FeatureCollection<Polygon>} FeatureCollection containing the smoothed polygon/poylgons
|
|
15
|
-
* @example
|
|
16
|
-
* var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
|
|
17
|
-
*
|
|
18
|
-
* var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
|
|
19
|
-
*
|
|
20
|
-
* //addToMap
|
|
21
|
-
* var addToMap = [smoothed, polygon];
|
|
22
|
-
*/
|
|
23
|
-
function polygonSmooth(inputPolys, options) {
|
|
24
|
-
options = options || {};
|
|
25
|
-
var outPolys = [];
|
|
26
|
-
// Optional parameters
|
|
27
|
-
var iterations = options.iterations || 1;
|
|
28
|
-
if (!inputPolys) throw new Error("inputPolys is required");
|
|
29
|
-
|
|
30
|
-
meta.geomEach(inputPolys, function (geom, geomIndex, properties) {
|
|
31
|
-
var outCoords;
|
|
32
|
-
var poly;
|
|
33
|
-
var tempOutput;
|
|
34
|
-
|
|
35
|
-
switch (geom.type) {
|
|
36
|
-
case "Polygon":
|
|
37
|
-
outCoords = [[]];
|
|
38
|
-
for (var i = 0; i < iterations; i++) {
|
|
39
|
-
tempOutput = [];
|
|
40
|
-
poly = geom;
|
|
41
|
-
if (i > 0) poly = helpers.polygon(outCoords).geometry;
|
|
42
|
-
processPolygon(poly, tempOutput);
|
|
43
|
-
outCoords = tempOutput.slice(0);
|
|
44
|
-
}
|
|
45
|
-
outPolys.push(helpers.polygon(outCoords, properties));
|
|
46
|
-
break;
|
|
47
|
-
case "MultiPolygon":
|
|
48
|
-
outCoords = [[[]]];
|
|
49
|
-
for (var y = 0; y < iterations; y++) {
|
|
50
|
-
tempOutput = [];
|
|
51
|
-
poly = geom;
|
|
52
|
-
if (y > 0) poly = helpers.multiPolygon(outCoords).geometry;
|
|
53
|
-
processMultiPolygon(poly, tempOutput);
|
|
54
|
-
outCoords = tempOutput.slice(0);
|
|
55
|
-
}
|
|
56
|
-
outPolys.push(helpers.multiPolygon(outCoords, properties));
|
|
57
|
-
break;
|
|
58
|
-
default:
|
|
59
|
-
throw new Error("geometry is invalid, must be Polygon or MultiPolygon");
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return helpers.featureCollection(outPolys);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* @param {poly} poly to process
|
|
67
|
-
* @param {poly} tempOutput to place the results in
|
|
68
|
-
* @private
|
|
69
|
-
*/
|
|
70
|
-
function processPolygon(poly, tempOutput) {
|
|
71
|
-
var previousCoord = null;
|
|
72
|
-
var previousGeometryIndex = null;
|
|
73
|
-
|
|
74
|
-
meta.coordEach(
|
|
75
|
-
poly,
|
|
76
|
-
function (
|
|
77
|
-
currentCoord,
|
|
78
|
-
coordIndex,
|
|
79
|
-
featureIndex,
|
|
80
|
-
multiFeatureIndex,
|
|
81
|
-
geometryIndex
|
|
82
|
-
) {
|
|
83
|
-
if (previousGeometryIndex !== geometryIndex) {
|
|
84
|
-
tempOutput.push([]);
|
|
85
|
-
} else {
|
|
86
|
-
var p0x = previousCoord[0];
|
|
87
|
-
var p0y = previousCoord[1];
|
|
88
|
-
var p1x = currentCoord[0];
|
|
89
|
-
var p1y = currentCoord[1];
|
|
90
|
-
tempOutput[geometryIndex].push([
|
|
91
|
-
0.75 * p0x + 0.25 * p1x,
|
|
92
|
-
0.75 * p0y + 0.25 * p1y,
|
|
93
|
-
]);
|
|
94
|
-
tempOutput[geometryIndex].push([
|
|
95
|
-
0.25 * p0x + 0.75 * p1x,
|
|
96
|
-
0.25 * p0y + 0.75 * p1y,
|
|
97
|
-
]);
|
|
98
|
-
}
|
|
99
|
-
previousCoord = currentCoord;
|
|
100
|
-
previousGeometryIndex = geometryIndex;
|
|
101
|
-
},
|
|
102
|
-
false
|
|
103
|
-
);
|
|
104
|
-
tempOutput.forEach(function (ring) {
|
|
105
|
-
ring.push(ring[0]);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @param {poly} poly to process
|
|
111
|
-
* @param {poly} tempOutput to place the results in
|
|
112
|
-
* @private
|
|
113
|
-
*/
|
|
114
|
-
function processMultiPolygon(poly, tempOutput) {
|
|
115
|
-
var previousCoord = null;
|
|
116
|
-
var previousMultiFeatureIndex = null;
|
|
117
|
-
var previousGeometryIndex = null;
|
|
118
|
-
|
|
119
|
-
meta.coordEach(
|
|
120
|
-
poly,
|
|
121
|
-
function (
|
|
122
|
-
currentCoord,
|
|
123
|
-
coordIndex,
|
|
124
|
-
featureIndex,
|
|
125
|
-
multiFeatureIndex,
|
|
126
|
-
geometryIndex
|
|
127
|
-
) {
|
|
128
|
-
if (previousMultiFeatureIndex !== multiFeatureIndex) {
|
|
129
|
-
tempOutput.push([[]]);
|
|
130
|
-
} else if (previousGeometryIndex !== geometryIndex) {
|
|
131
|
-
tempOutput[multiFeatureIndex].push([]);
|
|
132
|
-
} else {
|
|
133
|
-
var p0x = previousCoord[0];
|
|
134
|
-
var p0y = previousCoord[1];
|
|
135
|
-
var p1x = currentCoord[0];
|
|
136
|
-
var p1y = currentCoord[1];
|
|
137
|
-
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
138
|
-
0.75 * p0x + 0.25 * p1x,
|
|
139
|
-
0.75 * p0y + 0.25 * p1y,
|
|
140
|
-
]);
|
|
141
|
-
tempOutput[multiFeatureIndex][geometryIndex].push([
|
|
142
|
-
0.25 * p0x + 0.75 * p1x,
|
|
143
|
-
0.25 * p0y + 0.75 * p1y,
|
|
144
|
-
]);
|
|
145
|
-
}
|
|
146
|
-
previousCoord = currentCoord;
|
|
147
|
-
previousMultiFeatureIndex = multiFeatureIndex;
|
|
148
|
-
previousGeometryIndex = geometryIndex;
|
|
149
|
-
},
|
|
150
|
-
false
|
|
151
|
-
);
|
|
152
|
-
tempOutput.forEach(function (poly) {
|
|
153
|
-
poly.forEach(function (ring) {
|
|
154
|
-
ring.push(ring[0]);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
module.exports = polygonSmooth;
|
|
160
|
-
module.exports.default = polygonSmooth;
|
package/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Feature, FeatureCollection, Polygon, MultiPolygon } from "geojson";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* http://turfjs.org/docs/#polygonSmooth
|
|
5
|
-
*/
|
|
6
|
-
export default function <T extends Polygon | MultiPolygon>(
|
|
7
|
-
polygon: FeatureCollection<T> | Feature<T> | T,
|
|
8
|
-
options?: {
|
|
9
|
-
iterations?: number;
|
|
10
|
-
}
|
|
11
|
-
): FeatureCollection<T>;
|