35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
var each = require('@turf/meta').coordEach;
|
|
|
|
/**
|
|
* Takes a set of features, calculates the bbox of all input features, and returns a bounding box.
|
|
*
|
|
* @name bbox
|
|
* @param {(Feature|FeatureCollection)} geojson input features
|
|
* @returns {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
|
|
* @addToMap features, bboxPolygon
|
|
* @example
|
|
* var pt1 = turf.point([114.175329, 22.2524])
|
|
* var pt2 = turf.point([114.170007, 22.267969])
|
|
* var pt3 = turf.point([114.200649, 22.274641])
|
|
* var pt4 = turf.point([114.200649, 22.274641])
|
|
* var pt5 = turf.point([114.186744, 22.265745])
|
|
* var features = turf.featureCollection([pt1, pt2, pt3, pt4, pt5])
|
|
*
|
|
* var bbox = turf.bbox(features);
|
|
*
|
|
* var bboxPolygon = turf.bboxPolygon(bbox);
|
|
*
|
|
* //=bbox
|
|
*
|
|
* //=bboxPolygon
|
|
*/
|
|
module.exports = function (geojson) {
|
|
var bbox = [Infinity, Infinity, -Infinity, -Infinity];
|
|
each(geojson, function (coord) {
|
|
if (bbox[0] > coord[0]) bbox[0] = coord[0];
|
|
if (bbox[1] > coord[1]) bbox[1] = coord[1];
|
|
if (bbox[2] < coord[0]) bbox[2] = coord[0];
|
|
if (bbox[3] < coord[1]) bbox[3] = coord[1];
|
|
});
|
|
return bbox;
|
|
};
|