Calculates bounding boxes for any GeoJSON object including FeatureCollection
npx @tessl/cli install tessl/npm-turf--bbox@7.2.0@turf/bbox calculates bounding boxes for any GeoJSON object, including FeatureCollection. It efficiently computes the minimum and maximum coordinate extents in [minX, minY, maxX, maxY] format by iterating through all coordinates in the GeoJSON geometry. The library supports reusing existing bbox properties when available for performance optimization.
npm install @turf/bboximport { bbox } from "@turf/bbox";For CommonJS:
const { bbox } = require("@turf/bbox");Default import:
import bbox from "@turf/bbox";import { bbox } from "@turf/bbox";
import { lineString, polygon, featureCollection } from "@turf/helpers";
// Calculate bbox for a LineString
const line = lineString([[-74, 40], [-78, 42], [-82, 35]]);
const lineBbox = bbox(line);
// Result: [-82, 35, -74, 42]
// Calculate bbox for a Polygon
const poly = polygon([[
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0],
[101.0, 0.0]
]]);
const polyBbox = bbox(poly);
// Result: [100, 0, 101, 1]
// Calculate bbox for a FeatureCollection
const fc = featureCollection([line, poly]);
const fcBbox = bbox(fc);
// Result: [-82, 0, -74, 42]Calculates the bounding box for any GeoJSON object, including FeatureCollection. Uses existing bbox property if available, or computes from coordinates.
/**
* Calculates the bounding box for any GeoJSON object, including FeatureCollection.
* Uses geojson.bbox if available and options.recompute is not set.
* @param geojson - Any GeoJSON object (Feature, FeatureCollection, Geometry, GeometryCollection)
* @param options - Optional parameters
* @returns bbox extent in [minX, minY, maxX, maxY] order
*/
function bbox(
geojson: AllGeoJSON,
options?: {
recompute?: boolean;
}
): BBox;Parameters:
geojson (AllGeoJSON) - Any GeoJSON object to calculate bounds foroptions.recompute (boolean, optional) - Whether to ignore existing bbox property and force recalculation. Default: falseReturns: BBox array representing [minX, minY, maxX, maxY] coordinate extents
Usage Examples:
import { bbox } from "@turf/bbox";
import { point, multiPolygon } from "@turf/helpers";
// Point bbox
const pt = point([102.0, 0.5]);
const ptBbox = bbox(pt);
// Result: [102, 0.5, 102, 0.5]
// Force recalculation even if bbox exists
const geojsonWithBbox = { ...pt, bbox: [0, 0, 0, 0] };
const recomputedBbox = bbox(geojsonWithBbox, { recompute: true });
// Result: [102, 0.5, 102, 0.5] (ignores existing bbox)
// Use existing bbox for performance
const existingBbox = bbox(geojsonWithBbox);
// Result: [0, 0, 0, 0] (uses existing bbox property)
// Empty bbox arrays are returned as-is
const emptyBboxGeojson = { ...pt, bbox: [] };
const emptyResult = bbox(emptyBboxGeojson);
// Result: [] (returns existing empty bbox)Error Handling:
[Infinity, Infinity, -Infinity, -Infinity] for null geometries/**
* Union type for all possible GeoJSON objects
*/
type AllGeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
/**
* Bounding box represented as [minX, minY, maxX, maxY]
*/
type BBox = [number, number, number, number];
/**
* GeoJSON properties object
*/
type GeoJsonProperties = { [name: string]: any } | null;
/**
* GeoJSON Feature object
*/
interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
type: "Feature";
geometry: G;
properties: P;
bbox?: BBox;
}
/**
* Collection of GeoJSON Feature objects
*/
interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
type: "FeatureCollection";
features: Feature<G, P>[];
bbox?: BBox;
}
/**
* Base interface for all geometry types
*/
interface Geometry {
type: string;
bbox?: BBox;
}
/**
* Collection of geometry objects
*/
interface GeometryCollection {
type: "GeometryCollection";
geometries: Geometry[];
bbox?: BBox;
}