-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
61 lines (51 loc) · 1.66 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
50
51
52
53
54
55
56
57
58
59
60
61
var bbox2extent = require('bbox2extent')
module.exports = function (collection, callback) {
// fallback if features passed directly
var features = collection.features ? collection.features : collection
var bbox = [Infinity, Infinity, -Infinity, -Infinity]
features.forEach(function (f, i) {
if (!f.geometry) return
var isPoint = f.geometry.type === 'Point'
var isMultiPoint = f.geometry.type === 'MultiPoint'
var isLine = f.geometry.type === 'LineString'
var isMultiLine = f.geometry.type === 'MultiLineString'
var isPolygon = f.geometry.type === 'Polygon'
var isMultiPolygon = f.geometry.type === 'MultiPolygon'
var coords = f.geometry.coordinates
if (isPoint || isMultiPoint) {
if(Number.isNaN(coords[0]) || Number.isNaN(coords[1])){
console.warn(`Feature without geometries: ${JSON.stringify(f,null, 4)}`);
}
if (isPoint) coords = [[[coords]]]
else coords = [[coords]]
loop(bbox, coords)
}
if (isLine || isMultiLine) {
if (isLine) coords = [[coords]]
else coords = [coords]
loop(bbox, coords)
}
if (isPolygon || isMultiPolygon) {
if (isPolygon) coords = [coords]
loop(bbox, coords)
}
})
var extent = bbox2extent(bbox)
if (callback) callback(null, extent)
else return extent
}
function loop (bbox, coords) {
coords.forEach(function (r) {
r.forEach(function (inner) {
inner.forEach(function (c) {
extend(bbox, c)
})
})
})
}
function extend (bbox, coord) {
bbox[0] = Math.min(bbox[0], coord[0])
bbox[1] = Math.min(bbox[1], coord[1])
bbox[2] = Math.max(bbox[2], coord[0])
bbox[3] = Math.max(bbox[3], coord[1])
}