-
Notifications
You must be signed in to change notification settings - Fork 953
/
Copy pathindex.ts
103 lines (99 loc) · 3.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
GeoJsonProperties,
FeatureCollection,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
} from "geojson";
import { feature, featureCollection } from "@turf/helpers";
import { featureEach } from "@turf/meta";
/**
* Combines a {@link FeatureCollection} of {@link Point}, {@link LineString}, or {@link Polygon} features
* into {@link MultiPoint}, {@link MultiLineString}, or {@link MultiPolygon} features.
*
* @function
* @param {FeatureCollection<Point|LineString|Polygon>} fc a FeatureCollection of any type
* @returns {FeatureCollection<MultiPoint|MultiLineString|MultiPolygon>} a FeatureCollection of corresponding type to input
* @example
* var fc = turf.featureCollection([
* turf.point([19.026432, 47.49134]),
* turf.point([19.074497, 47.509548])
* ]);
*
* var combined = turf.combine(fc);
*
* //addToMap
* var addToMap = [combined]
*/
function combine(
fc: FeatureCollection<
Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
>
) {
var groups = {
MultiPoint: {
coordinates: [] as number[][],
properties: [] as GeoJsonProperties[],
},
MultiLineString: {
coordinates: [] as number[][][],
properties: [] as GeoJsonProperties[],
},
MultiPolygon: {
coordinates: [] as number[][][][],
properties: [] as GeoJsonProperties[],
},
};
featureEach(fc, (feature) => {
switch (feature.geometry?.type) {
case "Point":
groups.MultiPoint.coordinates.push(feature.geometry.coordinates);
groups.MultiPoint.properties.push(feature.properties);
break;
case "MultiPoint":
groups.MultiPoint.coordinates.push(...feature.geometry.coordinates);
groups.MultiPoint.properties.push(feature.properties);
break;
case "LineString":
groups.MultiLineString.coordinates.push(feature.geometry.coordinates);
groups.MultiLineString.properties.push(feature.properties);
break;
case "MultiLineString":
groups.MultiLineString.coordinates.push(
...feature.geometry.coordinates
);
groups.MultiLineString.properties.push(feature.properties);
break;
case "Polygon":
groups.MultiPolygon.coordinates.push(feature.geometry.coordinates);
groups.MultiPolygon.properties.push(feature.properties);
break;
case "MultiPolygon":
groups.MultiPolygon.coordinates.push(...feature.geometry.coordinates);
groups.MultiPolygon.properties.push(feature.properties);
break;
default:
break;
}
});
return featureCollection(
(Object.keys(groups) as (keyof typeof groups)[])
.filter(function (key) {
return groups[key].coordinates.length;
})
.sort()
.map(function (key) {
var geometry = { type: key, coordinates: groups[key].coordinates } as
| MultiPoint
| MultiLineString
| MultiPolygon;
var properties = { collectedProperties: groups[key].properties };
return feature(geometry, properties);
})
);
}
export { combine };
export default combine;