@turf/polygon-smooth 6.4.0 → 7.0.0-alpha.0

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
@@ -7,13 +7,14 @@
7
7
  Smooths a [Polygon][1] or [MultiPolygon][2]. Based on [Chaikin's algorithm][3].
8
8
  Warning: may create degenerate polygons.
9
9
 
10
- **Parameters**
10
+ ### Parameters
11
11
 
12
- - `inputPolys` **([FeatureCollection][4] \| [Feature][5]<([Polygon][6] \| [MultiPolygon][7])>)** (Multi)Polygon(s) to smooth
13
- - `options` **[Object][8]** Optional parameters (optional, default `{}`)
14
- - `options.iterations` **[string][9]** THe number of times to smooth the polygon. A higher value means a smoother polygon. (optional, default `1`)
12
+ * `inputPolys` **([FeatureCollection][4] | [Feature][5]<([Polygon][6] | [MultiPolygon][7])>)** (Multi)Polygon(s) to smooth
13
+ * `options` **[Object][8]** Optional parameters (optional, default `{}`)
15
14
 
16
- **Examples**
15
+ * `options.iterations` **[string][9]** The number of times to smooth the polygon. A higher value means a smoother polygon. (optional, default `1`)
16
+
17
+ ### Examples
17
18
 
18
19
  ```javascript
19
20
  var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
@@ -24,7 +25,7 @@ var smoothed = turf.polygonSmooth(polygon, {iterations: 3})
24
25
  var addToMap = [smoothed, polygon];
25
26
  ```
26
27
 
27
- Returns **[FeatureCollection][4]&lt;[Polygon][6]>** FeatureCollection containing the smoothed polygon/poylgons
28
+ Returns **[FeatureCollection][4]<[Polygon][6]>** FeatureCollection containing the smoothed polygon/poylgons
28
29
 
29
30
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.6
30
31
 
package/dist/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { coordEach, geomEach } from '@turf/meta';
2
- import { featureCollection, polygon, multiPolygon } from '@turf/helpers';
1
+ import { multiPolygon, polygon, featureCollection } from '@turf/helpers';
2
+ import { geomEach, coordEach } from '@turf/meta';
3
3
 
4
4
  /**
5
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).
@@ -8,7 +8,7 @@ import { featureCollection, polygon, multiPolygon } from '@turf/helpers';
8
8
  * @name polygonSmooth
9
9
  * @param {FeatureCollection|Feature<Polygon|MultiPolygon>} inputPolys (Multi)Polygon(s) to smooth
10
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.
11
+ * @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
12
12
  * @returns {FeatureCollection<Polygon>} FeatureCollection containing the smoothed polygon/poylgons
13
13
  * @example
14
14
  * var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
@@ -19,6 +19,7 @@ import { featureCollection, polygon, multiPolygon } from '@turf/helpers';
19
19
  * var addToMap = [smoothed, polygon];
20
20
  */
21
21
  function polygonSmooth(inputPolys, options) {
22
+ options = options || {};
22
23
  var outPolys = [];
23
24
  // Optional parameters
24
25
  var iterations = options.iterations || 1;
@@ -33,7 +34,7 @@ function polygonSmooth(inputPolys, options) {
33
34
  case "Polygon":
34
35
  outCoords = [[]];
35
36
  for (var i = 0; i < iterations; i++) {
36
- tempOutput = [[]];
37
+ tempOutput = [];
37
38
  poly = geom;
38
39
  if (i > 0) poly = polygon(outCoords).geometry;
39
40
  processPolygon(poly, tempOutput);
@@ -44,7 +45,7 @@ function polygonSmooth(inputPolys, options) {
44
45
  case "MultiPolygon":
45
46
  outCoords = [[[]]];
46
47
  for (var y = 0; y < iterations; y++) {
47
- tempOutput = [[[]]];
48
+ tempOutput = [];
48
49
  poly = geom;
49
50
  if (y > 0) poly = multiPolygon(outCoords).geometry;
50
51
  processMultiPolygon(poly, tempOutput);
@@ -65,8 +66,8 @@ function polygonSmooth(inputPolys, options) {
65
66
  * @private
66
67
  */
67
68
  function processPolygon(poly, tempOutput) {
68
- var prevGeomIndex = 0;
69
- var subtractCoordIndex = 0;
69
+ var previousCoord = null;
70
+ var previousGeometryIndex = null;
70
71
 
71
72
  coordEach(
72
73
  poly,
@@ -77,27 +78,26 @@ function processPolygon(poly, tempOutput) {
77
78
  multiFeatureIndex,
78
79
  geometryIndex
79
80
  ) {
80
- if (geometryIndex > prevGeomIndex) {
81
- prevGeomIndex = geometryIndex;
82
- subtractCoordIndex = coordIndex;
81
+ if (previousGeometryIndex !== geometryIndex) {
83
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
+ ]);
84
96
  }
85
- var realCoordIndex = coordIndex - subtractCoordIndex;
86
- var p1 = poly.coordinates[geometryIndex][realCoordIndex + 1];
87
- var p0x = currentCoord[0];
88
- var p0y = currentCoord[1];
89
- var p1x = p1[0];
90
- var p1y = p1[1];
91
- tempOutput[geometryIndex].push([
92
- 0.75 * p0x + 0.25 * p1x,
93
- 0.75 * p0y + 0.25 * p1y,
94
- ]);
95
- tempOutput[geometryIndex].push([
96
- 0.25 * p0x + 0.75 * p1x,
97
- 0.25 * p0y + 0.75 * p1y,
98
- ]);
97
+ previousCoord = currentCoord;
98
+ previousGeometryIndex = geometryIndex;
99
99
  },
100
- true
100
+ false
101
101
  );
102
102
  tempOutput.forEach(function (ring) {
103
103
  ring.push(ring[0]);
@@ -110,9 +110,9 @@ function processPolygon(poly, tempOutput) {
110
110
  * @private
111
111
  */
112
112
  function processMultiPolygon(poly, tempOutput) {
113
- var prevGeomIndex = 0;
114
- var subtractCoordIndex = 0;
115
- var prevMultiIndex = 0;
113
+ var previousCoord = null;
114
+ var previousMultiFeatureIndex = null;
115
+ var previousGeometryIndex = null;
116
116
 
117
117
  coordEach(
118
118
  poly,
@@ -123,35 +123,30 @@ function processMultiPolygon(poly, tempOutput) {
123
123
  multiFeatureIndex,
124
124
  geometryIndex
125
125
  ) {
126
- if (multiFeatureIndex > prevMultiIndex) {
127
- prevMultiIndex = multiFeatureIndex;
128
- subtractCoordIndex = coordIndex;
126
+ if (previousMultiFeatureIndex !== multiFeatureIndex) {
129
127
  tempOutput.push([[]]);
130
- }
131
- if (geometryIndex > prevGeomIndex) {
132
- prevGeomIndex = geometryIndex;
133
- subtractCoordIndex = coordIndex;
128
+ } else if (previousGeometryIndex !== geometryIndex) {
134
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
+ ]);
135
143
  }
136
- var realCoordIndex = coordIndex - subtractCoordIndex;
137
- var p1 =
138
- poly.coordinates[multiFeatureIndex][geometryIndex][realCoordIndex + 1];
139
- var p0x = currentCoord[0];
140
- var p0y = currentCoord[1];
141
- var p1x = p1[0];
142
- var p1y = p1[1];
143
- tempOutput[multiFeatureIndex][geometryIndex].push([
144
- 0.75 * p0x + 0.25 * p1x,
145
- 0.75 * p0y + 0.25 * p1y,
146
- ]);
147
- tempOutput[multiFeatureIndex][geometryIndex].push([
148
- 0.25 * p0x + 0.75 * p1x,
149
- 0.25 * p0y + 0.75 * p1y,
150
- ]);
144
+ previousCoord = currentCoord;
145
+ previousMultiFeatureIndex = multiFeatureIndex;
146
+ previousGeometryIndex = geometryIndex;
151
147
  },
152
- true
148
+ false
153
149
  );
154
-
155
150
  tempOutput.forEach(function (poly) {
156
151
  poly.forEach(function (ring) {
157
152
  ring.push(ring[0]);
package/dist/js/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var meta = require('@turf/meta');
4
3
  var helpers = require('@turf/helpers');
4
+ var meta = require('@turf/meta');
5
5
 
6
6
  /**
7
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).
@@ -10,7 +10,7 @@ var helpers = require('@turf/helpers');
10
10
  * @name polygonSmooth
11
11
  * @param {FeatureCollection|Feature<Polygon|MultiPolygon>} inputPolys (Multi)Polygon(s) to smooth
12
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.
13
+ * @param {string} [options.iterations=1] The number of times to smooth the polygon. A higher value means a smoother polygon.
14
14
  * @returns {FeatureCollection<Polygon>} FeatureCollection containing the smoothed polygon/poylgons
15
15
  * @example
16
16
  * var polygon = turf.polygon([[[11, 0], [22, 4], [31, 0], [31, 11], [21, 15], [11, 11], [11, 0]]]);
@@ -21,6 +21,7 @@ var helpers = require('@turf/helpers');
21
21
  * var addToMap = [smoothed, polygon];
22
22
  */
23
23
  function polygonSmooth(inputPolys, options) {
24
+ options = options || {};
24
25
  var outPolys = [];
25
26
  // Optional parameters
26
27
  var iterations = options.iterations || 1;
@@ -35,7 +36,7 @@ function polygonSmooth(inputPolys, options) {
35
36
  case "Polygon":
36
37
  outCoords = [[]];
37
38
  for (var i = 0; i < iterations; i++) {
38
- tempOutput = [[]];
39
+ tempOutput = [];
39
40
  poly = geom;
40
41
  if (i > 0) poly = helpers.polygon(outCoords).geometry;
41
42
  processPolygon(poly, tempOutput);
@@ -46,7 +47,7 @@ function polygonSmooth(inputPolys, options) {
46
47
  case "MultiPolygon":
47
48
  outCoords = [[[]]];
48
49
  for (var y = 0; y < iterations; y++) {
49
- tempOutput = [[[]]];
50
+ tempOutput = [];
50
51
  poly = geom;
51
52
  if (y > 0) poly = helpers.multiPolygon(outCoords).geometry;
52
53
  processMultiPolygon(poly, tempOutput);
@@ -67,8 +68,8 @@ function polygonSmooth(inputPolys, options) {
67
68
  * @private
68
69
  */
69
70
  function processPolygon(poly, tempOutput) {
70
- var prevGeomIndex = 0;
71
- var subtractCoordIndex = 0;
71
+ var previousCoord = null;
72
+ var previousGeometryIndex = null;
72
73
 
73
74
  meta.coordEach(
74
75
  poly,
@@ -79,27 +80,26 @@ function processPolygon(poly, tempOutput) {
79
80
  multiFeatureIndex,
80
81
  geometryIndex
81
82
  ) {
82
- if (geometryIndex > prevGeomIndex) {
83
- prevGeomIndex = geometryIndex;
84
- subtractCoordIndex = coordIndex;
83
+ if (previousGeometryIndex !== geometryIndex) {
85
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
+ ]);
86
98
  }
87
- var realCoordIndex = coordIndex - subtractCoordIndex;
88
- var p1 = poly.coordinates[geometryIndex][realCoordIndex + 1];
89
- var p0x = currentCoord[0];
90
- var p0y = currentCoord[1];
91
- var p1x = p1[0];
92
- var p1y = p1[1];
93
- tempOutput[geometryIndex].push([
94
- 0.75 * p0x + 0.25 * p1x,
95
- 0.75 * p0y + 0.25 * p1y,
96
- ]);
97
- tempOutput[geometryIndex].push([
98
- 0.25 * p0x + 0.75 * p1x,
99
- 0.25 * p0y + 0.75 * p1y,
100
- ]);
99
+ previousCoord = currentCoord;
100
+ previousGeometryIndex = geometryIndex;
101
101
  },
102
- true
102
+ false
103
103
  );
104
104
  tempOutput.forEach(function (ring) {
105
105
  ring.push(ring[0]);
@@ -112,9 +112,9 @@ function processPolygon(poly, tempOutput) {
112
112
  * @private
113
113
  */
114
114
  function processMultiPolygon(poly, tempOutput) {
115
- var prevGeomIndex = 0;
116
- var subtractCoordIndex = 0;
117
- var prevMultiIndex = 0;
115
+ var previousCoord = null;
116
+ var previousMultiFeatureIndex = null;
117
+ var previousGeometryIndex = null;
118
118
 
119
119
  meta.coordEach(
120
120
  poly,
@@ -125,35 +125,30 @@ function processMultiPolygon(poly, tempOutput) {
125
125
  multiFeatureIndex,
126
126
  geometryIndex
127
127
  ) {
128
- if (multiFeatureIndex > prevMultiIndex) {
129
- prevMultiIndex = multiFeatureIndex;
130
- subtractCoordIndex = coordIndex;
128
+ if (previousMultiFeatureIndex !== multiFeatureIndex) {
131
129
  tempOutput.push([[]]);
132
- }
133
- if (geometryIndex > prevGeomIndex) {
134
- prevGeomIndex = geometryIndex;
135
- subtractCoordIndex = coordIndex;
130
+ } else if (previousGeometryIndex !== geometryIndex) {
136
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
+ ]);
137
145
  }
138
- var realCoordIndex = coordIndex - subtractCoordIndex;
139
- var p1 =
140
- poly.coordinates[multiFeatureIndex][geometryIndex][realCoordIndex + 1];
141
- var p0x = currentCoord[0];
142
- var p0y = currentCoord[1];
143
- var p1x = p1[0];
144
- var p1y = p1[1];
145
- tempOutput[multiFeatureIndex][geometryIndex].push([
146
- 0.75 * p0x + 0.25 * p1x,
147
- 0.75 * p0y + 0.25 * p1y,
148
- ]);
149
- tempOutput[multiFeatureIndex][geometryIndex].push([
150
- 0.25 * p0x + 0.75 * p1x,
151
- 0.25 * p0y + 0.75 * p1y,
152
- ]);
146
+ previousCoord = currentCoord;
147
+ previousMultiFeatureIndex = multiFeatureIndex;
148
+ previousGeometryIndex = geometryIndex;
153
149
  },
154
- true
150
+ false
155
151
  );
156
-
157
152
  tempOutput.forEach(function (poly) {
158
153
  poly.forEach(function (ring) {
159
154
  ring.push(ring[0]);
@@ -162,3 +157,4 @@ function processMultiPolygon(poly, tempOutput) {
162
157
  }
163
158
 
164
159
  module.exports = polygonSmooth;
160
+ module.exports.default = polygonSmooth;
package/index.d.ts CHANGED
@@ -1,9 +1,4 @@
1
- import {
2
- Feature,
3
- FeatureCollection,
4
- Polygon,
5
- MultiPolygon,
6
- } from "@turf/helpers";
1
+ import { Feature, FeatureCollection, Polygon, MultiPolygon } from "geojson";
7
2
 
8
3
  /**
9
4
  * http://turfjs.org/docs/#polygonSmooth
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/polygon-smooth",
3
- "version": "6.4.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf polygon smooth module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -15,6 +15,7 @@
15
15
  "type": "git",
16
16
  "url": "git://github.com/Turfjs/turf.git"
17
17
  },
18
+ "funding": "https://opencollective.com/turf",
18
19
  "publishConfig": {
19
20
  "access": "public"
20
21
  },
@@ -44,7 +45,7 @@
44
45
  "docs": "node ../../scripts/generate-readmes",
45
46
  "test": "npm-run-all test:*",
46
47
  "test:tape": "node -r esm test.js",
47
- "test:types": "tsc --esModuleInterop --noEmit types.ts"
48
+ "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
48
49
  },
49
50
  "devDependencies": {
50
51
  "benchmark": "*",
@@ -56,8 +57,8 @@
56
57
  "write-json-file": "*"
57
58
  },
58
59
  "dependencies": {
59
- "@turf/helpers": "^6.4.0",
60
- "@turf/meta": "^6.4.0"
60
+ "@turf/helpers": "^7.0.0-alpha.0",
61
+ "@turf/meta": "^7.0.0-alpha.0"
61
62
  },
62
- "gitHead": "1e62773cfc88c627cca8effcb5c14cfb65a905ac"
63
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
63
64
  }