@turf/difference 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
@@ -4,14 +4,13 @@
4
4
 
5
5
  ## difference
6
6
 
7
- Finds the difference between two [polygons][1] by clipping the second polygon from the first.
7
+ Finds the difference between multiple [polygons][1] by clipping the subsequent polygon from the first.
8
8
 
9
- **Parameters**
9
+ ### Parameters
10
10
 
11
- - `polygon1` **[Feature][2]<([Polygon][3] \| [MultiPolygon][4])>** input Polygon feature
12
- - `polygon2` **[Feature][2]<([Polygon][3] \| [MultiPolygon][4])>** Polygon feature to difference from polygon1
11
+ * `features` **[FeatureCollection][2]<([Polygon][3] | [MultiPolygon][4])>** input Polygon features
13
12
 
14
- **Examples**
13
+ ### Examples
15
14
 
16
15
  ```javascript
17
16
  var polygon1 = turf.polygon([[
@@ -35,22 +34,24 @@ var polygon2 = turf.polygon([[
35
34
  "fill-opacity": 0.1
36
35
  });
37
36
 
38
- var difference = turf.difference(polygon1, polygon2);
37
+ var difference = turf.difference(turf.featureCollection([polygon1, polygon2]));
39
38
 
40
39
  //addToMap
41
40
  var addToMap = [polygon1, polygon2, difference];
42
41
  ```
43
42
 
44
- Returns **([Feature][2]&lt;([Polygon][3] \| [MultiPolygon][4])> | null)** a Polygon or MultiPolygon feature showing the area of `polygon1` excluding the area of `polygon2` (if empty returns `null`)
43
+ Returns **([Feature][5]<([Polygon][3] | [MultiPolygon][4])> | null)** a Polygon or MultiPolygon feature showing the area of `polygon1` excluding the area of `polygon2` (if empty returns `null`)
45
44
 
46
45
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.6
47
46
 
48
- [2]: https://tools.ietf.org/html/rfc7946#section-3.2
47
+ [2]: https://tools.ietf.org/html/rfc7946#section-3.3
49
48
 
50
49
  [3]: https://tools.ietf.org/html/rfc7946#section-3.1.6
51
50
 
52
51
  [4]: https://tools.ietf.org/html/rfc7946#section-3.1.7
53
52
 
53
+ [5]: https://tools.ietf.org/html/rfc7946#section-3.2
54
+
54
55
  <!-- This file is automatically generated. Please don't edit it directly:
55
56
  if you find an error, edit the source file (likely index.js), and re-run
56
57
  ./scripts/generate-readmes in the turf project. -->
package/dist/es/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import polygonClipping from 'polygon-clipping';
2
2
  import { polygon, multiPolygon } from '@turf/helpers';
3
- import { getGeom } from '@turf/invariant';
3
+ import { geomEach } from '@turf/meta';
4
4
 
5
5
  /**
6
- * Finds the difference between two {@link Polygon|polygons} by clipping the second polygon from the first.
6
+ * Finds the difference between multiple {@link Polygon|polygons} by clipping the subsequent polygon from the first.
7
7
  *
8
8
  * @name difference
9
- * @param {Feature<Polygon|MultiPolygon>} polygon1 input Polygon feature
10
- * @param {Feature<Polygon|MultiPolygon>} polygon2 Polygon feature to difference from polygon1
9
+ * @param {FeatureCollection<Polygon|MultiPolygon>} features input Polygon features
11
10
  * @returns {Feature<Polygon|MultiPolygon>|null} a Polygon or MultiPolygon feature showing the area of `polygon1` excluding the area of `polygon2` (if empty returns `null`)
12
11
  * @example
13
12
  * var polygon1 = turf.polygon([[
@@ -31,20 +30,25 @@ import { getGeom } from '@turf/invariant';
31
30
  * "fill-opacity": 0.1
32
31
  * });
33
32
  *
34
- * var difference = turf.difference(polygon1, polygon2);
33
+ * var difference = turf.difference(turf.featureCollection([polygon1, polygon2]));
35
34
  *
36
35
  * //addToMap
37
36
  * var addToMap = [polygon1, polygon2, difference];
38
37
  */
39
- function difference(polygon1, polygon2) {
40
- var geom1 = getGeom(polygon1);
41
- var geom2 = getGeom(polygon2);
42
- var properties = polygon1.properties || {};
38
+ function difference(features) {
39
+ const geoms = [];
43
40
 
44
- var differenced = polygonClipping.difference(
45
- geom1.coordinates,
46
- geom2.coordinates
47
- );
41
+ geomEach(features, (geom) => {
42
+ geoms.push(geom.coordinates);
43
+ });
44
+
45
+ if (geoms.length < 2) {
46
+ throw new Error("Must have at least two features");
47
+ }
48
+
49
+ var properties = features.features[0].properties || {};
50
+
51
+ var differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
48
52
  if (differenced.length === 0) return null;
49
53
  if (differenced.length === 1) return polygon(differenced[0], properties);
50
54
  return multiPolygon(differenced, properties);
package/dist/js/index.js CHANGED
@@ -1,17 +1,18 @@
1
1
  'use strict';
2
2
 
3
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
-
5
- var polygonClipping = _interopDefault(require('polygon-clipping'));
3
+ var polygonClipping = require('polygon-clipping');
6
4
  var helpers = require('@turf/helpers');
7
- var invariant = require('@turf/invariant');
5
+ var meta = require('@turf/meta');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
+
9
+ var polygonClipping__default = /*#__PURE__*/_interopDefaultLegacy(polygonClipping);
8
10
 
9
11
  /**
10
- * Finds the difference between two {@link Polygon|polygons} by clipping the second polygon from the first.
12
+ * Finds the difference between multiple {@link Polygon|polygons} by clipping the subsequent polygon from the first.
11
13
  *
12
14
  * @name difference
13
- * @param {Feature<Polygon|MultiPolygon>} polygon1 input Polygon feature
14
- * @param {Feature<Polygon|MultiPolygon>} polygon2 Polygon feature to difference from polygon1
15
+ * @param {FeatureCollection<Polygon|MultiPolygon>} features input Polygon features
15
16
  * @returns {Feature<Polygon|MultiPolygon>|null} a Polygon or MultiPolygon feature showing the area of `polygon1` excluding the area of `polygon2` (if empty returns `null`)
16
17
  * @example
17
18
  * var polygon1 = turf.polygon([[
@@ -35,23 +36,29 @@ var invariant = require('@turf/invariant');
35
36
  * "fill-opacity": 0.1
36
37
  * });
37
38
  *
38
- * var difference = turf.difference(polygon1, polygon2);
39
+ * var difference = turf.difference(turf.featureCollection([polygon1, polygon2]));
39
40
  *
40
41
  * //addToMap
41
42
  * var addToMap = [polygon1, polygon2, difference];
42
43
  */
43
- function difference(polygon1, polygon2) {
44
- var geom1 = invariant.getGeom(polygon1);
45
- var geom2 = invariant.getGeom(polygon2);
46
- var properties = polygon1.properties || {};
47
-
48
- var differenced = polygonClipping.difference(
49
- geom1.coordinates,
50
- geom2.coordinates
51
- );
44
+ function difference(features) {
45
+ const geoms = [];
46
+
47
+ meta.geomEach(features, (geom) => {
48
+ geoms.push(geom.coordinates);
49
+ });
50
+
51
+ if (geoms.length < 2) {
52
+ throw new Error("Must have at least two features");
53
+ }
54
+
55
+ var properties = features.features[0].properties || {};
56
+
57
+ var differenced = polygonClipping__default['default'].difference(geoms[0], ...geoms.slice(1));
52
58
  if (differenced.length === 0) return null;
53
59
  if (differenced.length === 1) return helpers.polygon(differenced[0], properties);
54
60
  return helpers.multiPolygon(differenced, properties);
55
61
  }
56
62
 
57
63
  module.exports = difference;
64
+ module.exports.default = difference;
package/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { Polygon, MultiPolygon, Feature } from "@turf/helpers";
1
+ import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
2
2
 
3
3
  /**
4
4
  * http://turfjs.org/docs/#difference
5
5
  */
6
6
  export default function difference(
7
- polygon1: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
8
- polygon2: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon
7
+ features: FeatureCollection<Polygon | MultiPolygon>
9
8
  ): Feature<Polygon | MultiPolygon> | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/difference",
3
- "version": "6.4.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf difference 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
  },
@@ -51,9 +52,9 @@
51
52
  "write-json-file": "*"
52
53
  },
53
54
  "dependencies": {
54
- "@turf/helpers": "^6.4.0",
55
- "@turf/invariant": "^6.4.0",
56
- "polygon-clipping": "^0.15.2"
55
+ "@turf/helpers": "^7.0.0-alpha.0",
56
+ "@turf/meta": "^7.0.0-alpha.0",
57
+ "polygon-clipping": "^0.15.3"
57
58
  },
58
- "gitHead": "1e62773cfc88c627cca8effcb5c14cfb65a905ac"
59
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
59
60
  }