Calculates bounding boxes for any GeoJSON object including FeatureCollection
npx @tessl/cli install tessl/npm-turf--bbox@7.2.00
# @turf/bbox
1
2
@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.
3
4
## Package Information
5
6
- **Package Name**: @turf/bbox
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @turf/bbox`
10
11
## Core Imports
12
13
```typescript
14
import { bbox } from "@turf/bbox";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { bbox } = require("@turf/bbox");
21
```
22
23
Default import:
24
25
```typescript
26
import bbox from "@turf/bbox";
27
```
28
29
## Basic Usage
30
31
```typescript
32
import { bbox } from "@turf/bbox";
33
import { lineString, polygon, featureCollection } from "@turf/helpers";
34
35
// Calculate bbox for a LineString
36
const line = lineString([[-74, 40], [-78, 42], [-82, 35]]);
37
const lineBbox = bbox(line);
38
// Result: [-82, 35, -74, 42]
39
40
// Calculate bbox for a Polygon
41
const poly = polygon([[
42
[101.0, 0.0],
43
[101.0, 1.0],
44
[100.0, 1.0],
45
[100.0, 0.0],
46
[101.0, 0.0]
47
]]);
48
const polyBbox = bbox(poly);
49
// Result: [100, 0, 101, 1]
50
51
// Calculate bbox for a FeatureCollection
52
const fc = featureCollection([line, poly]);
53
const fcBbox = bbox(fc);
54
// Result: [-82, 0, -74, 42]
55
```
56
57
## Capabilities
58
59
### Bounding Box Calculation
60
61
Calculates the bounding box for any GeoJSON object, including FeatureCollection. Uses existing bbox property if available, or computes from coordinates.
62
63
```typescript { .api }
64
/**
65
* Calculates the bounding box for any GeoJSON object, including FeatureCollection.
66
* Uses geojson.bbox if available and options.recompute is not set.
67
* @param geojson - Any GeoJSON object (Feature, FeatureCollection, Geometry, GeometryCollection)
68
* @param options - Optional parameters
69
* @returns bbox extent in [minX, minY, maxX, maxY] order
70
*/
71
function bbox(
72
geojson: AllGeoJSON,
73
options?: {
74
recompute?: boolean;
75
}
76
): BBox;
77
```
78
79
**Parameters:**
80
- `geojson` (AllGeoJSON) - Any GeoJSON object to calculate bounds for
81
- `options.recompute` (boolean, optional) - Whether to ignore existing bbox property and force recalculation. Default: false
82
83
**Returns:** BBox array representing [minX, minY, maxX, maxY] coordinate extents
84
85
**Usage Examples:**
86
87
```typescript
88
import { bbox } from "@turf/bbox";
89
import { point, multiPolygon } from "@turf/helpers";
90
91
// Point bbox
92
const pt = point([102.0, 0.5]);
93
const ptBbox = bbox(pt);
94
// Result: [102, 0.5, 102, 0.5]
95
96
// Force recalculation even if bbox exists
97
const geojsonWithBbox = { ...pt, bbox: [0, 0, 0, 0] };
98
const recomputedBbox = bbox(geojsonWithBbox, { recompute: true });
99
// Result: [102, 0.5, 102, 0.5] (ignores existing bbox)
100
101
// Use existing bbox for performance
102
const existingBbox = bbox(geojsonWithBbox);
103
// Result: [0, 0, 0, 0] (uses existing bbox property)
104
105
// Empty bbox arrays are returned as-is
106
const emptyBboxGeojson = { ...pt, bbox: [] };
107
const emptyResult = bbox(emptyBboxGeojson);
108
// Result: [] (returns existing empty bbox)
109
```
110
111
**Error Handling:**
112
- Throws error for unknown geometry types (delegated to coordEach from @turf/meta)
113
- Returns `[Infinity, Infinity, -Infinity, -Infinity]` for null geometries
114
115
## Types
116
117
```typescript { .api }
118
/**
119
* Union type for all possible GeoJSON objects
120
*/
121
type AllGeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
122
123
/**
124
* Bounding box represented as [minX, minY, maxX, maxY]
125
*/
126
type BBox = [number, number, number, number];
127
128
/**
129
* GeoJSON properties object
130
*/
131
type GeoJsonProperties = { [name: string]: any } | null;
132
133
/**
134
* GeoJSON Feature object
135
*/
136
interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
137
type: "Feature";
138
geometry: G;
139
properties: P;
140
bbox?: BBox;
141
}
142
143
/**
144
* Collection of GeoJSON Feature objects
145
*/
146
interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> {
147
type: "FeatureCollection";
148
features: Feature<G, P>[];
149
bbox?: BBox;
150
}
151
152
/**
153
* Base interface for all geometry types
154
*/
155
interface Geometry {
156
type: string;
157
bbox?: BBox;
158
}
159
160
/**
161
* Collection of geometry objects
162
*/
163
interface GeometryCollection {
164
type: "GeometryCollection";
165
geometries: Geometry[];
166
bbox?: BBox;
167
}
168
```