1 | var coordEach = require('@turf/meta').coordEach;
|
2 |
|
3 | /**
|
4 | * Takes a set of features, calculates the bbox of all input features, and returns a bounding box.
|
5 | *
|
6 | * @name bbox
|
7 | * @param {FeatureCollection|Feature<any>} geojson input features
|
8 | * @returns {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
|
9 | * @example
|
10 | * var line = {
|
11 | * "type": "Feature",
|
12 | * "properties": {},
|
13 | * "geometry": {
|
14 | * "type": "LineString",
|
15 | * "coordinates": [[-74, 40], [-78, 42], [-82, 35]]
|
16 | * }
|
17 | * }
|
18 | * var bbox = turf.bbox(line);
|
19 | *
|
20 | * //addToMap
|
21 | * var bboxPolygon = turf.bboxPolygon(bbox);
|
22 | * var addToMap = [line, bboxPolygon]
|
23 | */
|
24 | module.exports = function (geojson) {
|
25 | var bbox = [Infinity, Infinity, -Infinity, -Infinity];
|
26 | coordEach(geojson, function (coord) {
|
27 | if (bbox[0] > coord[0]) bbox[0] = coord[0];
|
28 | if (bbox[1] > coord[1]) bbox[1] = coord[1];
|
29 | if (bbox[2] < coord[0]) bbox[2] = coord[0];
|
30 | if (bbox[3] < coord[1]) bbox[3] = coord[1];
|
31 | });
|
32 | return bbox;
|
33 | };
|