@turf/mask 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
@@ -6,12 +6,12 @@
6
6
 
7
7
  Takes any type of [polygon][1] and an optional mask and returns a [polygon][1] exterior ring with holes.
8
8
 
9
- **Parameters**
9
+ ### Parameters
10
10
 
11
- - `polygon` **([FeatureCollection][2] \| [Feature][3]<([Polygon][4] \| [MultiPolygon][5])>)** GeoJSON Polygon used as interior rings or holes.
12
- - `mask` **[Feature][3]<[Polygon][4]>?** GeoJSON Polygon used as the exterior ring (if undefined, the world extent is used)
11
+ * `polygon` **([FeatureCollection][2] | [Feature][3]<([Polygon][4] | [MultiPolygon][5])>)** GeoJSON Polygon used as interior rings or holes.
12
+ * `mask` **[Feature][3]<[Polygon][4]>?** GeoJSON Polygon used as the exterior ring (if undefined, the world extent is used)
13
13
 
14
- **Examples**
14
+ ### Examples
15
15
 
16
16
  ```javascript
17
17
  var polygon = turf.polygon([[[112, -21], [116, -36], [146, -39], [153, -24], [133, -10], [112, -21]]]);
@@ -23,7 +23,7 @@ var masked = turf.mask(polygon, mask);
23
23
  var addToMap = [masked]
24
24
  ```
25
25
 
26
- Returns **[Feature][3]&lt;[Polygon][4]>** Masked Polygon (exterior ring with holes).
26
+ Returns **[Feature][3]<[Polygon][4]>** Masked Polygon (exterior ring with holes).
27
27
 
28
28
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.6
29
29
 
package/dist/es/index.js CHANGED
@@ -1,8 +1,5 @@
1
- import rbush from 'rbush';
2
- import union from '@turf/union';
3
- import { polygon, featureCollection } from '@turf/helpers';
4
- import turfBBox from '@turf/bbox';
5
- import { flattenEach } from '@turf/meta';
1
+ import { polygon, multiPolygon } from '@turf/helpers';
2
+ import polygonClipping from 'polygon-clipping';
6
3
 
7
4
  /**
8
5
  * Takes any type of {@link Polygon|polygon} and an optional mask and returns a {@link Polygon|polygon} exterior ring with holes.
@@ -20,67 +17,42 @@ import { flattenEach } from '@turf/meta';
20
17
  * //addToMap
21
18
  * var addToMap = [masked]
22
19
  */
23
- function mask(polygon$$1, mask) {
20
+ function mask(polygon, mask) {
24
21
  // Define mask
25
22
  var maskPolygon = createMask(mask);
26
23
 
27
- // Define polygon
28
- var separated = separatePolygons(polygon$$1);
29
- var polygonOuters = separated[0];
30
- var polygonInners = separated[1];
24
+ var polygonOuters = null;
25
+ if (polygon.type === "FeatureCollection") polygonOuters = unionFc(polygon);
26
+ else
27
+ polygonOuters = createGeomFromPolygonClippingOutput(
28
+ polygonClipping.union(polygon.geometry.coordinates)
29
+ );
31
30
 
32
- // Union Outers & Inners
33
- polygonOuters = unionPolygons(polygonOuters);
34
- polygonInners = unionPolygons(polygonInners);
31
+ polygonOuters.geometry.coordinates.forEach(function (contour) {
32
+ maskPolygon.geometry.coordinates.push(contour[0]);
33
+ });
35
34
 
36
- // Create masked area
37
- var masked = buildMask(maskPolygon, polygonOuters, polygonInners);
38
- return masked;
35
+ return maskPolygon;
39
36
  }
40
37
 
41
- /**
42
- * Build Mask
43
- *
44
- * @private
45
- * @param {Feature<Polygon>} maskPolygon Mask Outer
46
- * @param {FeatureCollection<Polygon>} polygonOuters Polygon Outers
47
- * @param {FeatureCollection<Polygon>} polygonInners Polygon Inners
48
- * @returns {Feature<Polygon>} Feature Polygon
49
- */
50
- function buildMask(maskPolygon, polygonOuters, polygonInners) {
51
- var coordinates = [];
52
- coordinates.push(maskPolygon.geometry.coordinates[0]);
53
-
54
- flattenEach(polygonOuters, function (feature) {
55
- coordinates.push(feature.geometry.coordinates[0]);
56
- });
57
-
58
- flattenEach(polygonInners, function (feature) {
59
- coordinates.push(feature.geometry.coordinates[0]);
60
- });
61
- return polygon(coordinates);
38
+ function unionFc(fc) {
39
+ var unioned =
40
+ fc.features.length === 2
41
+ ? polygonClipping.union(
42
+ fc.features[0].geometry.coordinates,
43
+ fc.features[1].geometry.coordinates
44
+ )
45
+ : polygonClipping.union.apply(
46
+ polygonClipping,
47
+ fc.features.map(function (f) {
48
+ return f.geometry.coordinates;
49
+ })
50
+ );
51
+ return createGeomFromPolygonClippingOutput(unioned);
62
52
  }
63
53
 
64
- /**
65
- * Separate Polygons to inners & outers
66
- *
67
- * @private
68
- * @param {FeatureCollection|Feature<Polygon|MultiPolygon>} poly GeoJSON Feature
69
- * @returns {Array<FeatureCollection<Polygon>, FeatureCollection<Polygon>>} Outer & Inner lines
70
- */
71
- function separatePolygons(poly) {
72
- var outers = [];
73
- var inners = [];
74
- flattenEach(poly, function (feature) {
75
- var coordinates = feature.geometry.coordinates;
76
- var featureOuter = coordinates[0];
77
- var featureInner = coordinates.slice(1);
78
- outers.push(polygon([featureOuter]));
79
- featureInner.forEach(function (inner) {
80
- inners.push(polygon([inner]));
81
- });
82
- });
83
- return [featureCollection(outers), featureCollection(inners)];
54
+ function createGeomFromPolygonClippingOutput(unioned) {
55
+ return multiPolygon(unioned);
84
56
  }
85
57
 
86
58
  /**
@@ -104,92 +76,4 @@ function createMask(mask) {
104
76
  return polygon(coordinates);
105
77
  }
106
78
 
107
- /**
108
- * Union Polygons
109
- *
110
- * @private
111
- * @param {FeatureCollection<Polygon>} polygons collection of polygons
112
- * @returns {FeatureCollection<Polygon>} polygons only apply union if they collide
113
- */
114
- function unionPolygons(polygons) {
115
- if (polygons.features.length <= 1) return polygons;
116
-
117
- var tree = createIndex(polygons);
118
- var results = [];
119
- var removed = {};
120
-
121
- flattenEach(polygons, function (currentFeature, currentIndex) {
122
- // Exclude any removed features
123
- if (removed[currentIndex]) return true;
124
-
125
- // Don't search for itself
126
- tree.remove({ index: currentIndex }, filterByIndex);
127
- removed[currentIndex] = true;
128
-
129
- // Keep applying the union operation until no more overlapping features
130
- while (true) {
131
- var bbox = turfBBox(currentFeature);
132
- var search = tree.search({
133
- minX: bbox[0],
134
- minY: bbox[1],
135
- maxX: bbox[2],
136
- maxY: bbox[3],
137
- });
138
- if (search.length > 0) {
139
- var polys = search.map(function (item) {
140
- removed[item.index] = true;
141
- tree.remove({ index: item.index }, filterByIndex);
142
- return item.geojson;
143
- });
144
-
145
- for (var i = 0, l = polys.length; i < l; i++) {
146
- currentFeature = union(currentFeature, polys[i]);
147
- }
148
- }
149
- // Done
150
- if (search.length === 0) break;
151
- }
152
- results.push(currentFeature);
153
- });
154
-
155
- return featureCollection(results);
156
- }
157
-
158
- /**
159
- * Filter by Index - RBush helper function
160
- *
161
- * @private
162
- * @param {Object} a remove item
163
- * @param {Object} b search item
164
- * @returns {boolean} true if matches
165
- */
166
- function filterByIndex(a, b) {
167
- return a.index === b.index;
168
- }
169
-
170
- /**
171
- * Create RBush Tree Index
172
- *
173
- * @private
174
- * @param {FeatureCollection<any>} features GeoJSON FeatureCollection
175
- * @returns {RBush} RBush Tree
176
- */
177
- function createIndex(features) {
178
- var tree = rbush();
179
- var load = [];
180
- flattenEach(features, function (feature, index) {
181
- var bbox = turfBBox(feature);
182
- load.push({
183
- minX: bbox[0],
184
- minY: bbox[1],
185
- maxX: bbox[2],
186
- maxY: bbox[3],
187
- geojson: feature,
188
- index: index,
189
- });
190
- });
191
- tree.load(load);
192
- return tree;
193
- }
194
-
195
79
  export default mask;
package/dist/js/index.js CHANGED
@@ -1,12 +1,11 @@
1
1
  'use strict';
2
2
 
3
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
-
5
- var rbush = _interopDefault(require('rbush'));
6
- var union = _interopDefault(require('@turf/union'));
7
3
  var helpers = require('@turf/helpers');
8
- var turfBBox = _interopDefault(require('@turf/bbox'));
9
- var meta = require('@turf/meta');
4
+ var polygonClipping = require('polygon-clipping');
5
+
6
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
+
8
+ var polygonClipping__default = /*#__PURE__*/_interopDefaultLegacy(polygonClipping);
10
9
 
11
10
  /**
12
11
  * Takes any type of {@link Polygon|polygon} and an optional mask and returns a {@link Polygon|polygon} exterior ring with holes.
@@ -28,63 +27,38 @@ function mask(polygon, mask) {
28
27
  // Define mask
29
28
  var maskPolygon = createMask(mask);
30
29
 
31
- // Define polygon
32
- var separated = separatePolygons(polygon);
33
- var polygonOuters = separated[0];
34
- var polygonInners = separated[1];
30
+ var polygonOuters = null;
31
+ if (polygon.type === "FeatureCollection") polygonOuters = unionFc(polygon);
32
+ else
33
+ polygonOuters = createGeomFromPolygonClippingOutput(
34
+ polygonClipping__default['default'].union(polygon.geometry.coordinates)
35
+ );
35
36
 
36
- // Union Outers & Inners
37
- polygonOuters = unionPolygons(polygonOuters);
38
- polygonInners = unionPolygons(polygonInners);
37
+ polygonOuters.geometry.coordinates.forEach(function (contour) {
38
+ maskPolygon.geometry.coordinates.push(contour[0]);
39
+ });
39
40
 
40
- // Create masked area
41
- var masked = buildMask(maskPolygon, polygonOuters, polygonInners);
42
- return masked;
41
+ return maskPolygon;
43
42
  }
44
43
 
45
- /**
46
- * Build Mask
47
- *
48
- * @private
49
- * @param {Feature<Polygon>} maskPolygon Mask Outer
50
- * @param {FeatureCollection<Polygon>} polygonOuters Polygon Outers
51
- * @param {FeatureCollection<Polygon>} polygonInners Polygon Inners
52
- * @returns {Feature<Polygon>} Feature Polygon
53
- */
54
- function buildMask(maskPolygon, polygonOuters, polygonInners) {
55
- var coordinates = [];
56
- coordinates.push(maskPolygon.geometry.coordinates[0]);
57
-
58
- meta.flattenEach(polygonOuters, function (feature) {
59
- coordinates.push(feature.geometry.coordinates[0]);
60
- });
61
-
62
- meta.flattenEach(polygonInners, function (feature) {
63
- coordinates.push(feature.geometry.coordinates[0]);
64
- });
65
- return helpers.polygon(coordinates);
44
+ function unionFc(fc) {
45
+ var unioned =
46
+ fc.features.length === 2
47
+ ? polygonClipping__default['default'].union(
48
+ fc.features[0].geometry.coordinates,
49
+ fc.features[1].geometry.coordinates
50
+ )
51
+ : polygonClipping__default['default'].union.apply(
52
+ polygonClipping__default['default'],
53
+ fc.features.map(function (f) {
54
+ return f.geometry.coordinates;
55
+ })
56
+ );
57
+ return createGeomFromPolygonClippingOutput(unioned);
66
58
  }
67
59
 
68
- /**
69
- * Separate Polygons to inners & outers
70
- *
71
- * @private
72
- * @param {FeatureCollection|Feature<Polygon|MultiPolygon>} poly GeoJSON Feature
73
- * @returns {Array<FeatureCollection<Polygon>, FeatureCollection<Polygon>>} Outer & Inner lines
74
- */
75
- function separatePolygons(poly) {
76
- var outers = [];
77
- var inners = [];
78
- meta.flattenEach(poly, function (feature) {
79
- var coordinates = feature.geometry.coordinates;
80
- var featureOuter = coordinates[0];
81
- var featureInner = coordinates.slice(1);
82
- outers.push(helpers.polygon([featureOuter]));
83
- featureInner.forEach(function (inner) {
84
- inners.push(helpers.polygon([inner]));
85
- });
86
- });
87
- return [helpers.featureCollection(outers), helpers.featureCollection(inners)];
60
+ function createGeomFromPolygonClippingOutput(unioned) {
61
+ return helpers.multiPolygon(unioned);
88
62
  }
89
63
 
90
64
  /**
@@ -108,92 +82,5 @@ function createMask(mask) {
108
82
  return helpers.polygon(coordinates);
109
83
  }
110
84
 
111
- /**
112
- * Union Polygons
113
- *
114
- * @private
115
- * @param {FeatureCollection<Polygon>} polygons collection of polygons
116
- * @returns {FeatureCollection<Polygon>} polygons only apply union if they collide
117
- */
118
- function unionPolygons(polygons) {
119
- if (polygons.features.length <= 1) return polygons;
120
-
121
- var tree = createIndex(polygons);
122
- var results = [];
123
- var removed = {};
124
-
125
- meta.flattenEach(polygons, function (currentFeature, currentIndex) {
126
- // Exclude any removed features
127
- if (removed[currentIndex]) return true;
128
-
129
- // Don't search for itself
130
- tree.remove({ index: currentIndex }, filterByIndex);
131
- removed[currentIndex] = true;
132
-
133
- // Keep applying the union operation until no more overlapping features
134
- while (true) {
135
- var bbox = turfBBox(currentFeature);
136
- var search = tree.search({
137
- minX: bbox[0],
138
- minY: bbox[1],
139
- maxX: bbox[2],
140
- maxY: bbox[3],
141
- });
142
- if (search.length > 0) {
143
- var polys = search.map(function (item) {
144
- removed[item.index] = true;
145
- tree.remove({ index: item.index }, filterByIndex);
146
- return item.geojson;
147
- });
148
-
149
- for (var i = 0, l = polys.length; i < l; i++) {
150
- currentFeature = union(currentFeature, polys[i]);
151
- }
152
- }
153
- // Done
154
- if (search.length === 0) break;
155
- }
156
- results.push(currentFeature);
157
- });
158
-
159
- return helpers.featureCollection(results);
160
- }
161
-
162
- /**
163
- * Filter by Index - RBush helper function
164
- *
165
- * @private
166
- * @param {Object} a remove item
167
- * @param {Object} b search item
168
- * @returns {boolean} true if matches
169
- */
170
- function filterByIndex(a, b) {
171
- return a.index === b.index;
172
- }
173
-
174
- /**
175
- * Create RBush Tree Index
176
- *
177
- * @private
178
- * @param {FeatureCollection<any>} features GeoJSON FeatureCollection
179
- * @returns {RBush} RBush Tree
180
- */
181
- function createIndex(features) {
182
- var tree = rbush();
183
- var load = [];
184
- meta.flattenEach(features, function (feature, index) {
185
- var bbox = turfBBox(feature);
186
- load.push({
187
- minX: bbox[0],
188
- minY: bbox[1],
189
- maxX: bbox[2],
190
- maxY: bbox[3],
191
- geojson: feature,
192
- index: index,
193
- });
194
- });
195
- tree.load(load);
196
- return tree;
197
- }
198
-
199
85
  module.exports = mask;
86
+ module.exports.default = mask;
package/index.d.ts CHANGED
@@ -1,9 +1,4 @@
1
- import {
2
- Feature,
3
- Polygon,
4
- MultiPolygon,
5
- FeatureCollection,
6
- } from "@turf/helpers";
1
+ import { Feature, Polygon, MultiPolygon, FeatureCollection } from "geojson";
7
2
 
8
3
  /**
9
4
  * http://turfjs.org/docs/#mask
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/mask",
3
- "version": "6.4.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf mask module",
5
5
  "author": "Turf Authors",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "type": "git",
13
13
  "url": "git://github.com/Turfjs/turf.git"
14
14
  },
15
+ "funding": "https://opencollective.com/turf",
15
16
  "publishConfig": {
16
17
  "access": "public"
17
18
  },
@@ -41,7 +42,7 @@
41
42
  "docs": "node ../../scripts/generate-readmes",
42
43
  "test": "npm-run-all test:*",
43
44
  "test:tape": "node -r esm test.js",
44
- "test:types": "tsc --esModuleInterop --noEmit types.ts"
45
+ "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
45
46
  },
46
47
  "devDependencies": {
47
48
  "benchmark": "*",
@@ -53,11 +54,8 @@
53
54
  "write-json-file": "*"
54
55
  },
55
56
  "dependencies": {
56
- "@turf/bbox": "^6.4.0",
57
- "@turf/helpers": "^6.4.0",
58
- "@turf/meta": "^6.4.0",
59
- "@turf/union": "^6.4.0",
60
- "rbush": "^2.0.1"
57
+ "@turf/helpers": "^7.0.0-alpha.0",
58
+ "polygon-clipping": "^0.15.3"
61
59
  },
62
- "gitHead": "1e62773cfc88c627cca8effcb5c14cfb65a905ac"
60
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
63
61
  }