UNPKG

898 BJavaScriptView Raw
1var 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 = turf.lineString([[-74, 40], [-78, 42], [-82, 35]]);
11 * var bbox = turf.bbox(line);
12 * var bboxPolygon = turf.bboxPolygon(bbox);
13 *
14 * //addToMap
15 * var addToMap = [line, bboxPolygon]
16 */
17module.exports = function (geojson) {
18 var bbox = [Infinity, Infinity, -Infinity, -Infinity];
19 coordEach(geojson, function (coord) {
20 if (bbox[0] > coord[0]) bbox[0] = coord[0];
21 if (bbox[1] > coord[1]) bbox[1] = coord[1];
22 if (bbox[2] < coord[0]) bbox[2] = coord[0];
23 if (bbox[3] < coord[1]) bbox[3] = coord[1];
24 });
25 return bbox;
26};