-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
46 lines (37 loc) · 1.22 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
var geojsonCoords = require('@mapbox/geojson-coords'),
traverse = require('neotraverse/legacy'),
extent = require('@mapbox/extent');
var geojsonTypesByDataAttributes = {
features: ['FeatureCollection'],
coordinates: ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'],
geometry: ['Feature'],
geometries: ['GeometryCollection']
}
var dataAttributes = Object.keys(geojsonTypesByDataAttributes);
module.exports = function(_) {
return getExtent(_).bbox();
};
module.exports.polygon = function(_) {
return getExtent(_).polygon();
};
module.exports.bboxify = function(_) {
return traverse(_).map(function(value) {
if (!value) return ;
var isValid = dataAttributes.some(function(attribute){
if(value[attribute]) {
return geojsonTypesByDataAttributes[attribute].indexOf(value.type) !== -1;
}
return false;
});
if(isValid){
value.bbox = getExtent(value).bbox();
this.update(value);
}
});
};
function getExtent(_) {
var ext = extent(),
coords = geojsonCoords(_);
for (var i = 0; i < coords.length; i++) ext.include(coords[i]);
return ext;
}