@turf/concave 7.0.0-alpha.1 → 7.0.0-alpha.111

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
@@ -57,26 +57,21 @@ Returns **([Feature][8]<([Polygon][9] | [MultiPolygon][10])> | null)** a concave
57
57
 
58
58
  [10]: https://tools.ietf.org/html/rfc7946#section-3.1.7
59
59
 
60
- <!-- This file is automatically generated. Please don't edit it directly:
61
- if you find an error, edit the source file (likely index.js), and re-run
62
- ./scripts/generate-readmes in the turf project. -->
60
+ <!-- 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. -->
63
61
 
64
62
  ---
65
63
 
66
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
67
- module collection dedicated to geographic algorithms. It is maintained in the
68
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
69
- PRs and issues.
64
+ 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.
70
65
 
71
66
  ### Installation
72
67
 
73
- Install this module individually:
68
+ Install this single module individually:
74
69
 
75
70
  ```sh
76
71
  $ npm install @turf/concave
77
72
  ```
78
73
 
79
- Or install the Turf module that includes it as a function:
74
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
80
75
 
81
76
  ```sh
82
77
  $ npm install @turf/turf
@@ -0,0 +1,209 @@
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 _distance = require('@turf/distance');
6
+ var _helpers = require('@turf/helpers');
7
+ var _meta = require('@turf/meta');
8
+ var _tin = require('@turf/tin');
9
+
10
+ // lib/turf-dissolve.ts
11
+ var _clone = require('@turf/clone');
12
+
13
+ var _invariant = require('@turf/invariant');
14
+
15
+
16
+ // lib/turf-line-dissolve.ts
17
+
18
+
19
+
20
+
21
+ function lineDissolve(geojson, options = {}) {
22
+ options = options || {};
23
+ if (!_helpers.isObject.call(void 0, options)) {
24
+ throw new Error("options is invalid");
25
+ }
26
+ const mutate = options.mutate;
27
+ if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
28
+ throw new Error("geojson must be a FeatureCollection");
29
+ }
30
+ if (!geojson.features.length) {
31
+ throw new Error("geojson is empty");
32
+ }
33
+ if (mutate === false || mutate === void 0) {
34
+ geojson = _clone.clone.call(void 0, geojson);
35
+ }
36
+ const result = [];
37
+ const lastLine = _meta.lineReduce.call(void 0,
38
+ geojson,
39
+ (previousLine, currentLine) => {
40
+ const merged = mergeLineStrings(previousLine, currentLine);
41
+ if (merged) {
42
+ return merged;
43
+ } else {
44
+ result.push(previousLine);
45
+ return currentLine;
46
+ }
47
+ }
48
+ );
49
+ if (lastLine) {
50
+ result.push(lastLine);
51
+ }
52
+ if (!result.length) {
53
+ return null;
54
+ } else if (result.length === 1) {
55
+ return result[0];
56
+ } else {
57
+ return _helpers.multiLineString.call(void 0,
58
+ result.map((line) => {
59
+ return line.coordinates;
60
+ })
61
+ );
62
+ }
63
+ }
64
+ __name(lineDissolve, "lineDissolve");
65
+ function coordId(coord) {
66
+ return coord[0].toString() + "," + coord[1].toString();
67
+ }
68
+ __name(coordId, "coordId");
69
+ function mergeLineStrings(a, b) {
70
+ const coords1 = a.geometry.coordinates;
71
+ const coords2 = b.geometry.coordinates;
72
+ const s1 = coordId(coords1[0]);
73
+ const e1 = coordId(coords1[coords1.length - 1]);
74
+ const s2 = coordId(coords2[0]);
75
+ const e2 = coordId(coords2[coords2.length - 1]);
76
+ let coords;
77
+ if (s1 === e2) {
78
+ coords = coords2.concat(coords1.slice(1));
79
+ } else if (s2 === e1) {
80
+ coords = coords1.concat(coords2.slice(1));
81
+ } else if (s1 === s2) {
82
+ coords = coords1.slice(1).reverse().concat(coords2);
83
+ } else if (e1 === e2) {
84
+ coords = coords1.concat(coords2.reverse().slice(1));
85
+ } else {
86
+ return null;
87
+ }
88
+ return _helpers.lineString.call(void 0, coords);
89
+ }
90
+ __name(mergeLineStrings, "mergeLineStrings");
91
+
92
+ // lib/turf-polygon-dissolve.ts
93
+
94
+
95
+
96
+
97
+ var _topojsonclient = require('topojson-client');
98
+ var _topojsonserver = require('topojson-server');
99
+ function polygonDissolve(geojson, options = {}) {
100
+ if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
101
+ throw new Error("geojson must be a FeatureCollection");
102
+ }
103
+ if (!geojson.features.length) {
104
+ throw new Error("geojson is empty");
105
+ }
106
+ if (options.mutate === false || options.mutate === void 0) {
107
+ geojson = _clone.clone.call(void 0, geojson);
108
+ }
109
+ const geoms = [];
110
+ _meta.flattenEach.call(void 0, geojson, (feature2) => {
111
+ geoms.push(feature2.geometry);
112
+ });
113
+ const topo = _topojsonserver.topology.call(void 0, { geoms: _helpers.geometryCollection.call(void 0, geoms).geometry });
114
+ const merged = _topojsonclient.merge.call(void 0, topo, topo.objects.geoms.geometries);
115
+ return merged;
116
+ }
117
+ __name(polygonDissolve, "polygonDissolve");
118
+
119
+ // lib/turf-dissolve.ts
120
+ function dissolve(geojson, options = {}) {
121
+ options = options || {};
122
+ if (!_helpers.isObject.call(void 0, options)) {
123
+ throw new Error("options is invalid");
124
+ }
125
+ const mutate = options.mutate;
126
+ if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
127
+ throw new Error("geojson must be a FeatureCollection");
128
+ }
129
+ if (!geojson.features.length) {
130
+ throw new Error("geojson is empty");
131
+ }
132
+ if (mutate === false || mutate === void 0) {
133
+ geojson = _clone.clone.call(void 0, geojson);
134
+ }
135
+ const type = getHomogenousType(geojson);
136
+ if (!type) {
137
+ throw new Error("geojson must be homogenous");
138
+ }
139
+ const data = geojson;
140
+ switch (type) {
141
+ case "LineString":
142
+ return lineDissolve(data, options);
143
+ case "Polygon":
144
+ return polygonDissolve(data, options);
145
+ default:
146
+ throw new Error(type + " is not supported");
147
+ }
148
+ }
149
+ __name(dissolve, "dissolve");
150
+ function getHomogenousType(geojson) {
151
+ const types = {};
152
+ _meta.flattenEach.call(void 0, geojson, (feature2) => {
153
+ types[feature2.geometry.type] = true;
154
+ });
155
+ const keys = Object.keys(types);
156
+ if (keys.length === 1) {
157
+ return keys[0];
158
+ }
159
+ return null;
160
+ }
161
+ __name(getHomogenousType, "getHomogenousType");
162
+
163
+ // index.ts
164
+ function concave(points, options = {}) {
165
+ const maxEdge = options.maxEdge || Infinity;
166
+ const cleaned = removeDuplicates(points);
167
+ const tinPolys = _tin.tin.call(void 0, cleaned);
168
+ tinPolys.features = tinPolys.features.filter((triangle) => {
169
+ const pt1 = triangle.geometry.coordinates[0][0];
170
+ const pt2 = triangle.geometry.coordinates[0][1];
171
+ const pt3 = triangle.geometry.coordinates[0][2];
172
+ const dist1 = _distance.distance.call(void 0, pt1, pt2, options);
173
+ const dist2 = _distance.distance.call(void 0, pt2, pt3, options);
174
+ const dist3 = _distance.distance.call(void 0, pt1, pt3, options);
175
+ return dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge;
176
+ });
177
+ if (tinPolys.features.length < 1) {
178
+ return null;
179
+ }
180
+ const dissolved = dissolve(tinPolys);
181
+ if (dissolved.coordinates.length === 1) {
182
+ dissolved.coordinates = dissolved.coordinates[0];
183
+ dissolved.type = "Polygon";
184
+ }
185
+ return _helpers.feature.call(void 0, dissolved);
186
+ }
187
+ __name(concave, "concave");
188
+ function removeDuplicates(points) {
189
+ const cleaned = [];
190
+ const existing = {};
191
+ _meta.featureEach.call(void 0, points, (pt) => {
192
+ if (!pt.geometry) {
193
+ return;
194
+ }
195
+ const key = pt.geometry.coordinates.join("-");
196
+ if (!Object.prototype.hasOwnProperty.call(existing, key)) {
197
+ cleaned.push(pt);
198
+ existing[key] = true;
199
+ }
200
+ });
201
+ return _helpers.featureCollection.call(void 0, cleaned);
202
+ }
203
+ __name(removeDuplicates, "removeDuplicates");
204
+ var turf_concave_default = concave;
205
+
206
+
207
+
208
+ exports.concave = concave; exports.default = turf_concave_default;
209
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts","../../lib/turf-dissolve.ts","../../lib/turf-line-dissolve.ts","../../lib/turf-polygon-dissolve.ts"],"names":["clone","isObject","getType","flattenEach","feature"],"mappings":";;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,SAAS,yBAAyB;AAS3C,SAAS,mBAAmB;AAC5B,SAAS,WAAW;;;ACHpB,SAAS,SAAAA,cAAa;AACtB,SAAS,YAAAC,iBAAgB;AACzB,SAAS,WAAAC,gBAAe;AACxB,SAAS,eAAAC,oBAAmB;;;ACL5B,SAAS,aAAa;AACtB,SAAS,UAAU,YAAY,uBAAuB;AACtD,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAW3B,SAAS,aACP,SACA,UAAgC,CAAC,GACa;AAE9C,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO,GAAG;AACtB,UAAM,IAAI,MAAM,oBAAoB;AAAA,EACtC;AACA,QAAM,SAAS,QAAQ;AAGvB,MAAI,QAAQ,OAAO,MAAM,qBAAqB;AAC5C,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACvD;AACA,MAAI,CAAC,QAAQ,SAAS,QAAQ;AAC5B,UAAM,IAAI,MAAM,kBAAkB;AAAA,EACpC;AAGA,MAAI,WAAW,SAAS,WAAW,QAAW;AAC5C,cAAU,MAAM,OAAO;AAAA,EACzB;AAEA,QAAM,SAAgB,CAAC;AACvB,QAAM,WAAW;AAAA,IACf;AAAA,IACA,CAAC,cAAmB,gBAAqB;AAGvC,YAAM,SAAS,iBAAiB,cAAc,WAAW;AAGzD,UAAI,QAAQ;AACV,eAAO;AAAA,MAET,OAAO;AACL,eAAO,KAAK,YAAY;AACxB,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAEA,MAAI,UAAU;AACZ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AAO;AAC1B,QAAI,CAAC,GAAG,UAAU;AAChB;AAAA,IACF;AACA,UAAM,MAAM,GAAG,SAAS,YAAY,KAAK,GAAG;AAC5C,QAAI,CAAC,OAAO,UAAU,eAAe,KAAK,UAAU,GAAG,GAAG;AACxD,cAAQ,KAAK,EAAE;AACf,eAAS,GAAG,IAAI;AAAA,IAClB;AAAA,EACF,CAAC;AACD,SAAO,kBAAkB,OAAO;AAClC;AAjBS;AAoBT,IAAO,uBAAQ","sourcesContent":["import { distance } from \"@turf/distance\";\nimport { feature, featureCollection } from \"@turf/helpers\";\nimport {\n Feature,\n FeatureCollection,\n MultiPolygon,\n Point,\n Polygon,\n} from \"geojson\";\nimport { Units } from \"@turf/helpers\";\nimport { featureEach } from \"@turf/meta\";\nimport { tin } from \"@turf/tin\";\nimport { dissolve } from \"./lib/turf-dissolve\";\n\n/**\n * Takes a set of {@link Point|points} and returns a concave hull Polygon or MultiPolygon.\n * Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries.\n *\n * @name concave\n * @param {FeatureCollection<Point>} points input points\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.maxEdge=Infinity] the length (in 'units') of an edge necessary for part of the\n * hull to become concave.\n * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers\n * @returns {Feature<(Polygon|MultiPolygon)>|null} a concave hull (null value is returned if unable to compute hull)\n * @example\n * var points = turf.featureCollection([\n * turf.point([-63.601226, 44.642643]),\n * turf.point([-63.591442, 44.651436]),\n * turf.point([-63.580799, 44.648749]),\n * turf.point([-63.573589, 44.641788]),\n * turf.point([-63.587665, 44.64533]),\n * turf.point([-63.595218, 44.64765])\n * ]);\n * var options = {units: 'miles', maxEdge: 1};\n *\n * var hull = turf.concave(points, options);\n *\n * //addToMap\n * var addToMap = [points, hull]\n */\nfunction concave(\n points: FeatureCollection<Point>,\n options: { maxEdge?: number; units?: Units } = {}\n): Feature<Polygon | MultiPolygon> | null {\n const maxEdge = options.maxEdge || Infinity;\n\n const cleaned = removeDuplicates(points);\n\n const tinPolys = tin(cleaned);\n // calculate length of all edges and area of all triangles\n // and remove triangles that fail the max length test\n tinPolys.features = tinPolys.features.filter((triangle) => {\n const pt1 = triangle.geometry.coordinates[0][0];\n const pt2 = triangle.geometry.coordinates[0][1];\n const pt3 = triangle.geometry.coordinates[0][2];\n const dist1 = distance(pt1, pt2, options);\n const dist2 = distance(pt2, pt3, options);\n const dist3 = distance(pt1, pt3, options);\n return dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge;\n });\n\n if (tinPolys.features.length < 1) {\n return null;\n }\n\n // merge the adjacent triangles\n const dissolved: any = dissolve(tinPolys);\n\n // geojson-dissolve always returns a MultiPolygon\n if (dissolved.coordinates.length === 1) {\n dissolved.coordinates = dissolved.coordinates[0];\n dissolved.type = \"Polygon\";\n }\n return feature(dissolved);\n}\n\n/**\n * Removes duplicated points in a collection returning a new collection\n *\n * @private\n * @param {FeatureCollection<Point>} points to be cleaned\n * @returns {FeatureCollection<Point>} cleaned set of points\n */\nfunction removeDuplicates(\n points: FeatureCollection<Point>\n): FeatureCollection<Point> {\n const cleaned: Array<Feature<Point>> = [];\n const existing: { [key: string]: boolean } = {};\n\n featureEach(points, (pt) => {\n if (!pt.geometry) {\n return;\n }\n const key = pt.geometry.coordinates.join(\"-\");\n if (!Object.prototype.hasOwnProperty.call(existing, key)) {\n cleaned.push(pt);\n existing[key] = true;\n }\n });\n return featureCollection(cleaned);\n}\n\nexport { concave };\nexport default concave;\n","import {\n Feature,\n FeatureCollection,\n LineString,\n MultiLineString,\n MultiPolygon,\n Polygon,\n} from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { isObject } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { flattenEach } from \"@turf/meta\";\nimport { lineDissolve } from \"./turf-line-dissolve\";\nimport { polygonDissolve } from \"./turf-polygon-dissolve\";\n\n/**\n * Transform function: attempts to dissolve geojson objects where possible\n * [GeoJSON] -> GeoJSON geometry\n *\n * @private\n * @param {FeatureCollection<LineString|MultiLineString|Polygon|MultiPolygon>} geojson Features to dissolved\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<MultiLineString|MultiPolygon>} Dissolved Features\n */\nfunction dissolve(\n geojson: FeatureCollection<\n LineString | MultiLineString | Polygon | MultiPolygon\n >,\n options: {\n mutate?: boolean;\n } = {}\n): Feature<LineString | MultiLineString | Polygon | MultiPolygon> | null {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) {\n throw new Error(\"options is invalid\");\n }\n const mutate = options.mutate;\n\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n // Topojson modifies in place, so we need to deep clone first\n if (mutate === false || mutate === undefined) {\n geojson = clone(geojson);\n }\n\n // Assert homogenity\n const type = getHomogenousType(geojson);\n if (!type) {\n throw new Error(\"geojson must be homogenous\");\n }\n\n // Data => Typescript hack\n const data: any = geojson;\n\n switch (type) {\n case \"LineString\":\n return lineDissolve(data, options);\n case \"Polygon\":\n return polygonDissolve(data, options);\n default:\n throw new Error(type + \" is not supported\");\n }\n}\n\n/**\n * Checks if GeoJSON is Homogenous\n *\n * @private\n * @param {GeoJSON} geojson GeoJSON\n * @returns {string|null} Homogenous type or null if multiple types\n */\nfunction getHomogenousType(geojson: any) {\n const types: { [key: string]: boolean } = {};\n flattenEach(geojson, (feature) => {\n types[feature.geometry.type] = true;\n });\n const keys = Object.keys(types);\n if (keys.length === 1) {\n return keys[0];\n }\n return null;\n}\n\nexport { dissolve };\nexport default dissolve;\n","import {\n Feature,\n FeatureCollection,\n LineString,\n MultiLineString,\n} from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { isObject, lineString, multiLineString } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { lineReduce } from \"@turf/meta\";\n\n/**\n * Merges all connected (non-forking, non-junctioning) line strings into single lineStrings.\n * [LineString] -> LineString|MultiLineString\n *\n * @param {FeatureCollection<LineString|MultiLineString>} geojson Lines to dissolve\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<LineString|MultiLineString>} Dissolved lines\n */\nfunction lineDissolve(\n geojson: FeatureCollection<LineString | MultiLineString>,\n options: { mutate?: boolean } = {}\n): Feature<LineString | MultiLineString> | null {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) {\n throw new Error(\"options is invalid\");\n }\n const mutate = options.mutate;\n\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n if (mutate === false || mutate === undefined) {\n geojson = clone(geojson);\n }\n\n const result: any[] = [];\n const lastLine = lineReduce(\n geojson,\n (previousLine: any, currentLine: any) => {\n // Attempt to merge this LineString with the other LineStrings, updating\n // the reference as it is merged with others and grows.\n const merged = mergeLineStrings(previousLine, currentLine);\n\n // Accumulate the merged LineString\n if (merged) {\n return merged;\n // Put the unmerged LineString back into the list\n } else {\n result.push(previousLine);\n return currentLine;\n }\n }\n );\n // Append the last line\n if (lastLine) {\n result.push(lastLine);\n }\n\n // Return null if no lines were dissolved\n if (!result.length) {\n return null;\n // Return LineString if only 1 line was dissolved\n } else if (result.length === 1) {\n return result[0];\n // Return MultiLineString if multiple lines were dissolved with gaps\n } else {\n return multiLineString(\n result.map((line) => {\n return line.coordinates;\n })\n );\n }\n}\n\n// [Number, Number] -> String\nfunction coordId(coord: number[]) {\n return coord[0].toString() + \",\" + coord[1].toString();\n}\n\n/**\n * LineString, LineString -> LineString\n *\n * @private\n * @param {Feature<LineString>} a line1\n * @param {Feature<LineString>} b line2\n * @returns {Feature<LineString>|null} Merged LineString\n */\nfunction mergeLineStrings(a: Feature<LineString>, b: Feature<LineString>) {\n const coords1 = a.geometry.coordinates;\n const coords2 = b.geometry.coordinates;\n\n const s1 = coordId(coords1[0]);\n const e1 = coordId(coords1[coords1.length - 1]);\n const s2 = coordId(coords2[0]);\n const e2 = coordId(coords2[coords2.length - 1]);\n\n // TODO: handle case where more than one of these is true!\n let coords;\n if (s1 === e2) {\n coords = coords2.concat(coords1.slice(1));\n } else if (s2 === e1) {\n coords = coords1.concat(coords2.slice(1));\n } else if (s1 === s2) {\n coords = coords1.slice(1).reverse().concat(coords2);\n } else if (e1 === e2) {\n coords = coords1.concat(coords2.reverse().slice(1));\n } else {\n return null;\n }\n\n return lineString(coords);\n}\n\nexport { lineDissolve };\nexport default lineDissolve;\n","import { Feature, FeatureCollection, MultiPolygon, Polygon } from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { geometryCollection } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { flattenEach } from \"@turf/meta\";\nimport { merge } from \"topojson-client\";\nimport { topology } from \"topojson-server\";\n\n/**\n * Dissolves all overlapping (Multi)Polygon\n *\n * @param {FeatureCollection<Polygon|MultiPolygon>} geojson Polygons to dissolve\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<Polygon|MultiPolygon>} Dissolved Polygons\n */\nfunction polygonDissolve(\n geojson: FeatureCollection<Polygon | MultiPolygon>,\n options: { mutate?: boolean } = {}\n): Feature<Polygon | MultiPolygon> | null {\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n // Topojson modifies in place, so we need to deep clone first\n if (options.mutate === false || options.mutate === undefined) {\n geojson = clone(geojson);\n }\n\n const geoms: any[] = [];\n flattenEach(geojson, (feature) => {\n geoms.push(feature.geometry);\n });\n const topo: any = topology({ geoms: geometryCollection(geoms).geometry });\n const merged: any = merge(topo, topo.objects.geoms.geometries);\n return merged;\n}\n\nexport { polygonDissolve };\nexport default polygonDissolve;\n"]}
@@ -1,5 +1,6 @@
1
- import { Feature, FeatureCollection, MultiPolygon, Point, Polygon } from "geojson";
2
- import { Units } from "@turf/helpers";
1
+ import { FeatureCollection, Point, Feature, Polygon, MultiPolygon } from 'geojson';
2
+ import { Units } from '@turf/helpers';
3
+
3
4
  /**
4
5
  * Takes a set of {@link Point|points} and returns a concave hull Polygon or MultiPolygon.
5
6
  * Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries.
@@ -31,4 +32,5 @@ declare function concave(points: FeatureCollection<Point>, options?: {
31
32
  maxEdge?: number;
32
33
  units?: Units;
33
34
  }): Feature<Polygon | MultiPolygon> | null;
34
- export default concave;
35
+
36
+ export { concave, concave as default };
@@ -0,0 +1,36 @@
1
+ import { FeatureCollection, Point, Feature, Polygon, MultiPolygon } from 'geojson';
2
+ import { Units } from '@turf/helpers';
3
+
4
+ /**
5
+ * Takes a set of {@link Point|points} and returns a concave hull Polygon or MultiPolygon.
6
+ * Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries.
7
+ *
8
+ * @name concave
9
+ * @param {FeatureCollection<Point>} points input points
10
+ * @param {Object} [options={}] Optional parameters
11
+ * @param {number} [options.maxEdge=Infinity] the length (in 'units') of an edge necessary for part of the
12
+ * hull to become concave.
13
+ * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers
14
+ * @returns {Feature<(Polygon|MultiPolygon)>|null} a concave hull (null value is returned if unable to compute hull)
15
+ * @example
16
+ * var points = turf.featureCollection([
17
+ * turf.point([-63.601226, 44.642643]),
18
+ * turf.point([-63.591442, 44.651436]),
19
+ * turf.point([-63.580799, 44.648749]),
20
+ * turf.point([-63.573589, 44.641788]),
21
+ * turf.point([-63.587665, 44.64533]),
22
+ * turf.point([-63.595218, 44.64765])
23
+ * ]);
24
+ * var options = {units: 'miles', maxEdge: 1};
25
+ *
26
+ * var hull = turf.concave(points, options);
27
+ *
28
+ * //addToMap
29
+ * var addToMap = [points, hull]
30
+ */
31
+ declare function concave(points: FeatureCollection<Point>, options?: {
32
+ maxEdge?: number;
33
+ units?: Units;
34
+ }): Feature<Polygon | MultiPolygon> | null;
35
+
36
+ export { concave, concave as default };
@@ -0,0 +1,209 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // index.ts
5
+ import { distance } from "@turf/distance";
6
+ import { feature, featureCollection } from "@turf/helpers";
7
+ import { featureEach } from "@turf/meta";
8
+ import { tin } from "@turf/tin";
9
+
10
+ // lib/turf-dissolve.ts
11
+ import { clone as clone3 } from "@turf/clone";
12
+ import { isObject as isObject2 } from "@turf/helpers";
13
+ import { getType as getType3 } from "@turf/invariant";
14
+ import { flattenEach as flattenEach2 } from "@turf/meta";
15
+
16
+ // lib/turf-line-dissolve.ts
17
+ import { clone } from "@turf/clone";
18
+ import { isObject, lineString, multiLineString } from "@turf/helpers";
19
+ import { getType } from "@turf/invariant";
20
+ import { lineReduce } from "@turf/meta";
21
+ function lineDissolve(geojson, options = {}) {
22
+ options = options || {};
23
+ if (!isObject(options)) {
24
+ throw new Error("options is invalid");
25
+ }
26
+ const mutate = options.mutate;
27
+ if (getType(geojson) !== "FeatureCollection") {
28
+ throw new Error("geojson must be a FeatureCollection");
29
+ }
30
+ if (!geojson.features.length) {
31
+ throw new Error("geojson is empty");
32
+ }
33
+ if (mutate === false || mutate === void 0) {
34
+ geojson = clone(geojson);
35
+ }
36
+ const result = [];
37
+ const lastLine = lineReduce(
38
+ geojson,
39
+ (previousLine, currentLine) => {
40
+ const merged = mergeLineStrings(previousLine, currentLine);
41
+ if (merged) {
42
+ return merged;
43
+ } else {
44
+ result.push(previousLine);
45
+ return currentLine;
46
+ }
47
+ }
48
+ );
49
+ if (lastLine) {
50
+ result.push(lastLine);
51
+ }
52
+ if (!result.length) {
53
+ return null;
54
+ } else if (result.length === 1) {
55
+ return result[0];
56
+ } else {
57
+ return multiLineString(
58
+ result.map((line) => {
59
+ return line.coordinates;
60
+ })
61
+ );
62
+ }
63
+ }
64
+ __name(lineDissolve, "lineDissolve");
65
+ function coordId(coord) {
66
+ return coord[0].toString() + "," + coord[1].toString();
67
+ }
68
+ __name(coordId, "coordId");
69
+ function mergeLineStrings(a, b) {
70
+ const coords1 = a.geometry.coordinates;
71
+ const coords2 = b.geometry.coordinates;
72
+ const s1 = coordId(coords1[0]);
73
+ const e1 = coordId(coords1[coords1.length - 1]);
74
+ const s2 = coordId(coords2[0]);
75
+ const e2 = coordId(coords2[coords2.length - 1]);
76
+ let coords;
77
+ if (s1 === e2) {
78
+ coords = coords2.concat(coords1.slice(1));
79
+ } else if (s2 === e1) {
80
+ coords = coords1.concat(coords2.slice(1));
81
+ } else if (s1 === s2) {
82
+ coords = coords1.slice(1).reverse().concat(coords2);
83
+ } else if (e1 === e2) {
84
+ coords = coords1.concat(coords2.reverse().slice(1));
85
+ } else {
86
+ return null;
87
+ }
88
+ return lineString(coords);
89
+ }
90
+ __name(mergeLineStrings, "mergeLineStrings");
91
+
92
+ // lib/turf-polygon-dissolve.ts
93
+ import { clone as clone2 } from "@turf/clone";
94
+ import { geometryCollection } from "@turf/helpers";
95
+ import { getType as getType2 } from "@turf/invariant";
96
+ import { flattenEach } from "@turf/meta";
97
+ import { merge } from "topojson-client";
98
+ import { topology } from "topojson-server";
99
+ function polygonDissolve(geojson, options = {}) {
100
+ if (getType2(geojson) !== "FeatureCollection") {
101
+ throw new Error("geojson must be a FeatureCollection");
102
+ }
103
+ if (!geojson.features.length) {
104
+ throw new Error("geojson is empty");
105
+ }
106
+ if (options.mutate === false || options.mutate === void 0) {
107
+ geojson = clone2(geojson);
108
+ }
109
+ const geoms = [];
110
+ flattenEach(geojson, (feature2) => {
111
+ geoms.push(feature2.geometry);
112
+ });
113
+ const topo = topology({ geoms: geometryCollection(geoms).geometry });
114
+ const merged = merge(topo, topo.objects.geoms.geometries);
115
+ return merged;
116
+ }
117
+ __name(polygonDissolve, "polygonDissolve");
118
+
119
+ // lib/turf-dissolve.ts
120
+ function dissolve(geojson, options = {}) {
121
+ options = options || {};
122
+ if (!isObject2(options)) {
123
+ throw new Error("options is invalid");
124
+ }
125
+ const mutate = options.mutate;
126
+ if (getType3(geojson) !== "FeatureCollection") {
127
+ throw new Error("geojson must be a FeatureCollection");
128
+ }
129
+ if (!geojson.features.length) {
130
+ throw new Error("geojson is empty");
131
+ }
132
+ if (mutate === false || mutate === void 0) {
133
+ geojson = clone3(geojson);
134
+ }
135
+ const type = getHomogenousType(geojson);
136
+ if (!type) {
137
+ throw new Error("geojson must be homogenous");
138
+ }
139
+ const data = geojson;
140
+ switch (type) {
141
+ case "LineString":
142
+ return lineDissolve(data, options);
143
+ case "Polygon":
144
+ return polygonDissolve(data, options);
145
+ default:
146
+ throw new Error(type + " is not supported");
147
+ }
148
+ }
149
+ __name(dissolve, "dissolve");
150
+ function getHomogenousType(geojson) {
151
+ const types = {};
152
+ flattenEach2(geojson, (feature2) => {
153
+ types[feature2.geometry.type] = true;
154
+ });
155
+ const keys = Object.keys(types);
156
+ if (keys.length === 1) {
157
+ return keys[0];
158
+ }
159
+ return null;
160
+ }
161
+ __name(getHomogenousType, "getHomogenousType");
162
+
163
+ // index.ts
164
+ function concave(points, options = {}) {
165
+ const maxEdge = options.maxEdge || Infinity;
166
+ const cleaned = removeDuplicates(points);
167
+ const tinPolys = tin(cleaned);
168
+ tinPolys.features = tinPolys.features.filter((triangle) => {
169
+ const pt1 = triangle.geometry.coordinates[0][0];
170
+ const pt2 = triangle.geometry.coordinates[0][1];
171
+ const pt3 = triangle.geometry.coordinates[0][2];
172
+ const dist1 = distance(pt1, pt2, options);
173
+ const dist2 = distance(pt2, pt3, options);
174
+ const dist3 = distance(pt1, pt3, options);
175
+ return dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge;
176
+ });
177
+ if (tinPolys.features.length < 1) {
178
+ return null;
179
+ }
180
+ const dissolved = dissolve(tinPolys);
181
+ if (dissolved.coordinates.length === 1) {
182
+ dissolved.coordinates = dissolved.coordinates[0];
183
+ dissolved.type = "Polygon";
184
+ }
185
+ return feature(dissolved);
186
+ }
187
+ __name(concave, "concave");
188
+ function removeDuplicates(points) {
189
+ const cleaned = [];
190
+ const existing = {};
191
+ featureEach(points, (pt) => {
192
+ if (!pt.geometry) {
193
+ return;
194
+ }
195
+ const key = pt.geometry.coordinates.join("-");
196
+ if (!Object.prototype.hasOwnProperty.call(existing, key)) {
197
+ cleaned.push(pt);
198
+ existing[key] = true;
199
+ }
200
+ });
201
+ return featureCollection(cleaned);
202
+ }
203
+ __name(removeDuplicates, "removeDuplicates");
204
+ var turf_concave_default = concave;
205
+ export {
206
+ concave,
207
+ turf_concave_default as default
208
+ };
209
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts","../../lib/turf-dissolve.ts","../../lib/turf-line-dissolve.ts","../../lib/turf-polygon-dissolve.ts"],"sourcesContent":["import { distance } from \"@turf/distance\";\nimport { feature, featureCollection } from \"@turf/helpers\";\nimport {\n Feature,\n FeatureCollection,\n MultiPolygon,\n Point,\n Polygon,\n} from \"geojson\";\nimport { Units } from \"@turf/helpers\";\nimport { featureEach } from \"@turf/meta\";\nimport { tin } from \"@turf/tin\";\nimport { dissolve } from \"./lib/turf-dissolve\";\n\n/**\n * Takes a set of {@link Point|points} and returns a concave hull Polygon or MultiPolygon.\n * Internally, this uses [turf-tin](https://github.com/Turfjs/turf-tin) to generate geometries.\n *\n * @name concave\n * @param {FeatureCollection<Point>} points input points\n * @param {Object} [options={}] Optional parameters\n * @param {number} [options.maxEdge=Infinity] the length (in 'units') of an edge necessary for part of the\n * hull to become concave.\n * @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers\n * @returns {Feature<(Polygon|MultiPolygon)>|null} a concave hull (null value is returned if unable to compute hull)\n * @example\n * var points = turf.featureCollection([\n * turf.point([-63.601226, 44.642643]),\n * turf.point([-63.591442, 44.651436]),\n * turf.point([-63.580799, 44.648749]),\n * turf.point([-63.573589, 44.641788]),\n * turf.point([-63.587665, 44.64533]),\n * turf.point([-63.595218, 44.64765])\n * ]);\n * var options = {units: 'miles', maxEdge: 1};\n *\n * var hull = turf.concave(points, options);\n *\n * //addToMap\n * var addToMap = [points, hull]\n */\nfunction concave(\n points: FeatureCollection<Point>,\n options: { maxEdge?: number; units?: Units } = {}\n): Feature<Polygon | MultiPolygon> | null {\n const maxEdge = options.maxEdge || Infinity;\n\n const cleaned = removeDuplicates(points);\n\n const tinPolys = tin(cleaned);\n // calculate length of all edges and area of all triangles\n // and remove triangles that fail the max length test\n tinPolys.features = tinPolys.features.filter((triangle) => {\n const pt1 = triangle.geometry.coordinates[0][0];\n const pt2 = triangle.geometry.coordinates[0][1];\n const pt3 = triangle.geometry.coordinates[0][2];\n const dist1 = distance(pt1, pt2, options);\n const dist2 = distance(pt2, pt3, options);\n const dist3 = distance(pt1, pt3, options);\n return dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge;\n });\n\n if (tinPolys.features.length < 1) {\n return null;\n }\n\n // merge the adjacent triangles\n const dissolved: any = dissolve(tinPolys);\n\n // geojson-dissolve always returns a MultiPolygon\n if (dissolved.coordinates.length === 1) {\n dissolved.coordinates = dissolved.coordinates[0];\n dissolved.type = \"Polygon\";\n }\n return feature(dissolved);\n}\n\n/**\n * Removes duplicated points in a collection returning a new collection\n *\n * @private\n * @param {FeatureCollection<Point>} points to be cleaned\n * @returns {FeatureCollection<Point>} cleaned set of points\n */\nfunction removeDuplicates(\n points: FeatureCollection<Point>\n): FeatureCollection<Point> {\n const cleaned: Array<Feature<Point>> = [];\n const existing: { [key: string]: boolean } = {};\n\n featureEach(points, (pt) => {\n if (!pt.geometry) {\n return;\n }\n const key = pt.geometry.coordinates.join(\"-\");\n if (!Object.prototype.hasOwnProperty.call(existing, key)) {\n cleaned.push(pt);\n existing[key] = true;\n }\n });\n return featureCollection(cleaned);\n}\n\nexport { concave };\nexport default concave;\n","import {\n Feature,\n FeatureCollection,\n LineString,\n MultiLineString,\n MultiPolygon,\n Polygon,\n} from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { isObject } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { flattenEach } from \"@turf/meta\";\nimport { lineDissolve } from \"./turf-line-dissolve\";\nimport { polygonDissolve } from \"./turf-polygon-dissolve\";\n\n/**\n * Transform function: attempts to dissolve geojson objects where possible\n * [GeoJSON] -> GeoJSON geometry\n *\n * @private\n * @param {FeatureCollection<LineString|MultiLineString|Polygon|MultiPolygon>} geojson Features to dissolved\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<MultiLineString|MultiPolygon>} Dissolved Features\n */\nfunction dissolve(\n geojson: FeatureCollection<\n LineString | MultiLineString | Polygon | MultiPolygon\n >,\n options: {\n mutate?: boolean;\n } = {}\n): Feature<LineString | MultiLineString | Polygon | MultiPolygon> | null {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) {\n throw new Error(\"options is invalid\");\n }\n const mutate = options.mutate;\n\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n // Topojson modifies in place, so we need to deep clone first\n if (mutate === false || mutate === undefined) {\n geojson = clone(geojson);\n }\n\n // Assert homogenity\n const type = getHomogenousType(geojson);\n if (!type) {\n throw new Error(\"geojson must be homogenous\");\n }\n\n // Data => Typescript hack\n const data: any = geojson;\n\n switch (type) {\n case \"LineString\":\n return lineDissolve(data, options);\n case \"Polygon\":\n return polygonDissolve(data, options);\n default:\n throw new Error(type + \" is not supported\");\n }\n}\n\n/**\n * Checks if GeoJSON is Homogenous\n *\n * @private\n * @param {GeoJSON} geojson GeoJSON\n * @returns {string|null} Homogenous type or null if multiple types\n */\nfunction getHomogenousType(geojson: any) {\n const types: { [key: string]: boolean } = {};\n flattenEach(geojson, (feature) => {\n types[feature.geometry.type] = true;\n });\n const keys = Object.keys(types);\n if (keys.length === 1) {\n return keys[0];\n }\n return null;\n}\n\nexport { dissolve };\nexport default dissolve;\n","import {\n Feature,\n FeatureCollection,\n LineString,\n MultiLineString,\n} from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { isObject, lineString, multiLineString } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { lineReduce } from \"@turf/meta\";\n\n/**\n * Merges all connected (non-forking, non-junctioning) line strings into single lineStrings.\n * [LineString] -> LineString|MultiLineString\n *\n * @param {FeatureCollection<LineString|MultiLineString>} geojson Lines to dissolve\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<LineString|MultiLineString>} Dissolved lines\n */\nfunction lineDissolve(\n geojson: FeatureCollection<LineString | MultiLineString>,\n options: { mutate?: boolean } = {}\n): Feature<LineString | MultiLineString> | null {\n // Optional parameters\n options = options || {};\n if (!isObject(options)) {\n throw new Error(\"options is invalid\");\n }\n const mutate = options.mutate;\n\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n if (mutate === false || mutate === undefined) {\n geojson = clone(geojson);\n }\n\n const result: any[] = [];\n const lastLine = lineReduce(\n geojson,\n (previousLine: any, currentLine: any) => {\n // Attempt to merge this LineString with the other LineStrings, updating\n // the reference as it is merged with others and grows.\n const merged = mergeLineStrings(previousLine, currentLine);\n\n // Accumulate the merged LineString\n if (merged) {\n return merged;\n // Put the unmerged LineString back into the list\n } else {\n result.push(previousLine);\n return currentLine;\n }\n }\n );\n // Append the last line\n if (lastLine) {\n result.push(lastLine);\n }\n\n // Return null if no lines were dissolved\n if (!result.length) {\n return null;\n // Return LineString if only 1 line was dissolved\n } else if (result.length === 1) {\n return result[0];\n // Return MultiLineString if multiple lines were dissolved with gaps\n } else {\n return multiLineString(\n result.map((line) => {\n return line.coordinates;\n })\n );\n }\n}\n\n// [Number, Number] -> String\nfunction coordId(coord: number[]) {\n return coord[0].toString() + \",\" + coord[1].toString();\n}\n\n/**\n * LineString, LineString -> LineString\n *\n * @private\n * @param {Feature<LineString>} a line1\n * @param {Feature<LineString>} b line2\n * @returns {Feature<LineString>|null} Merged LineString\n */\nfunction mergeLineStrings(a: Feature<LineString>, b: Feature<LineString>) {\n const coords1 = a.geometry.coordinates;\n const coords2 = b.geometry.coordinates;\n\n const s1 = coordId(coords1[0]);\n const e1 = coordId(coords1[coords1.length - 1]);\n const s2 = coordId(coords2[0]);\n const e2 = coordId(coords2[coords2.length - 1]);\n\n // TODO: handle case where more than one of these is true!\n let coords;\n if (s1 === e2) {\n coords = coords2.concat(coords1.slice(1));\n } else if (s2 === e1) {\n coords = coords1.concat(coords2.slice(1));\n } else if (s1 === s2) {\n coords = coords1.slice(1).reverse().concat(coords2);\n } else if (e1 === e2) {\n coords = coords1.concat(coords2.reverse().slice(1));\n } else {\n return null;\n }\n\n return lineString(coords);\n}\n\nexport { lineDissolve };\nexport default lineDissolve;\n","import { Feature, FeatureCollection, MultiPolygon, Polygon } from \"geojson\";\nimport { clone } from \"@turf/clone\";\nimport { geometryCollection } from \"@turf/helpers\";\nimport { getType } from \"@turf/invariant\";\nimport { flattenEach } from \"@turf/meta\";\nimport { merge } from \"topojson-client\";\nimport { topology } from \"topojson-server\";\n\n/**\n * Dissolves all overlapping (Multi)Polygon\n *\n * @param {FeatureCollection<Polygon|MultiPolygon>} geojson Polygons to dissolve\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] Prevent input mutation\n * @returns {Feature<Polygon|MultiPolygon>} Dissolved Polygons\n */\nfunction polygonDissolve(\n geojson: FeatureCollection<Polygon | MultiPolygon>,\n options: { mutate?: boolean } = {}\n): Feature<Polygon | MultiPolygon> | null {\n // Validation\n if (getType(geojson) !== \"FeatureCollection\") {\n throw new Error(\"geojson must be a FeatureCollection\");\n }\n if (!geojson.features.length) {\n throw new Error(\"geojson is empty\");\n }\n\n // Clone geojson to avoid side effects\n // Topojson modifies in place, so we need to deep clone first\n if (options.mutate === false || options.mutate === undefined) {\n geojson = clone(geojson);\n }\n\n const geoms: any[] = [];\n flattenEach(geojson, (feature) => {\n geoms.push(feature.geometry);\n });\n const topo: any = topology({ geoms: geometryCollection(geoms).geometry });\n const merged: any = merge(topo, topo.objects.geoms.geometries);\n return merged;\n}\n\nexport { polygonDissolve };\nexport default polygonDissolve;\n"],"mappings":";;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,SAAS,yBAAyB;AAS3C,SAAS,mBAAmB;AAC5B,SAAS,WAAW;;;ACHpB,SAAS,SAAAA,cAAa;AACtB,SAAS,YAAAC,iBAAgB;AACzB,SAAS,WAAAC,gBAAe;AACxB,SAAS,eAAAC,oBAAmB;;;ACL5B,SAAS,aAAa;AACtB,SAAS,UAAU,YAAY,uBAAuB;AACtD,SAAS,eAAe;AACxB,SAAS,kBAAkB;AAW3B,SAAS,aACP,SACA,UAAgC,CAAC,GACa;AAE9C,YAAU,WAAW,CAAC;AACtB,MAAI,CAAC,SAAS,OAAO,GAAG;AACtB,UAAM,IAAI,MAAM,oBAAoB;AAAA,EACtC;AACA,QAAM,SAAS,QAAQ;AAGvB,MAAI,QAAQ,OAAO,MAAM,qBAAqB;AAC5C,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACvD;AACA,MAAI,CAAC,QAAQ,SAAS,QAAQ;AAC5B,UAAM,IAAI,MAAM,kBAAkB;AAAA,EACpC;AAGA,MAAI,WAAW,SAAS,WAAW,QAAW;AAC5C,cAAU,MAAM,OAAO;AAAA,EACzB;AAEA,QAAM,SAAgB,CAAC;AACvB,QAAM,WAAW;AAAA,IACf;AAAA,IACA,CAAC,cAAmB,gBAAqB;AAGvC,YAA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AU,OAAO;AAAA,EACnB;AACA,SAAO,QAAQ,SAAS;AAC1B;AAlCS;AA2CT,SAAS,iBACP,QAC0B;AAC1B,QAAM,UAAiC,CAAC;AACxC,QAAM,WAAuC,CAAC;AAE9C,cAAY,QAAQ,CAAC,OAAO;AAC1B,QAAI,CAAC,GAAG,UAAU;AAChB;AAAA,IACF;AACA,UAAM,MAAM,GAAG,SAAS,YAAY,KAAK,GAAG;AAC5C,QAAI,CAAC,OAAO,UAAU,eAAe,KAAK,UAAU,GAAG,GAAG;AACxD,cAAQ,KAAK,EAAE;AACf,eAAS,GAAG,IAAI;AAAA,IAClB;AAAA,EACF,CAAC;AACD,SAAO,kBAAkB,OAAO;AAClC;AAjBS;AAoBT,IAAO,uBAAQ;","names":["clone","isObject","getType","flattenEach","clone","getType","getType","clone","feature","isObject","getType","clone","flattenEach","feature"]}