-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (49 loc) · 1.21 KB
/
index.js
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
//index.js
(function() {
var singles = ['Point', 'LineString', 'Polygon'];
var multies = ['MultiPoint', 'MultiLineString', 'MultiPolygon'];
function explode(g) {
if( multies.indexOf(g.type) > -1) {
return g.coordinates.map(function(part) {
var single = {};
single.type = g.type.replace('Multi','');
single.coordinates = part;
if(g.crs) single.crs = g.crs;
return single;
});
} else {
return false;
}
}
function implode(gs) {
var sameType = gs.every(function(g) {
return singles.indexOf(g.type) > -1;
})
var crs = gs[0].crs || 0;
var sameCrs = gs.every(function(g) {
var gcrs = g.crs || 0;
return gcrs == crs;
});
if(sameType && sameCrs) {
var multi = {};
multi.type = 'Multi' + gs[0].type;
multi.coordinates = [];
if(crs != 0) multi.crs = crs;
gs.forEach(function(g) {
multi.coordinates.push(g.coordinates);
});
return multi;
} else {
return false;
}
};
var multigeojson = {
explode: explode,
implode: implode
};
if(typeof module !== 'undefined' && module.exports) {
module.exports = multigeojson;
} else if(window) {
window.multigeojson = multigeojson;
}
})();