1 | export default function flatten(gj) {
|
2 | switch ((gj && gj.type) || null) {
|
3 | case "FeatureCollection":
|
4 | gj.features = gj.features.reduce(function(mem, feature) {
|
5 | return mem.concat(flatten(feature));
|
6 | }, []);
|
7 | return gj;
|
8 | case "Feature":
|
9 | if (!gj.geometry) return [gj];
|
10 | return flatten(gj.geometry).map(function(geom) {
|
11 | var data = {
|
12 | type: "Feature",
|
13 | properties: JSON.parse(JSON.stringify(gj.properties)),
|
14 | geometry: geom
|
15 | };
|
16 | if (gj.id !== undefined) {
|
17 | data.id = gj.id;
|
18 | }
|
19 | return data;
|
20 | });
|
21 | case "MultiPoint":
|
22 | return gj.coordinates.map(function(_) {
|
23 | return { type: "Point", coordinates: _ };
|
24 | });
|
25 | case "MultiPolygon":
|
26 | return gj.coordinates.map(function(_) {
|
27 | return { type: "Polygon", coordinates: _ };
|
28 | });
|
29 | case "MultiLineString":
|
30 | return gj.coordinates.map(function(_) {
|
31 | return { type: "LineString", coordinates: _ };
|
32 | });
|
33 | case "GeometryCollection":
|
34 | return gj.geometries.map(flatten).reduce(function(memo, geoms) {
|
35 | return memo.concat(geoms);
|
36 | }, []);
|
37 | case "Point":
|
38 | case "Polygon":
|
39 | case "LineString":
|
40 | return [gj];
|
41 | }
|
42 | }
|