Functions / PostGIS / Box2D
PostGISBounding Box

Box2D

What is Box2D?

Box2D is a PostGIS function that returns the 2D bounding box of a geometry as the internal box2d type — an axis-aligned rectangle given by its minimum and maximum X and Y coordinates.

SQL
Box2D(geometry geom) → box2d

The box2d type is a lightweight bounding-box representation that PostGIS uses internally for indexing and comparison. Casting it to text yields a string like BOX(xmin ymin, xmax ymax).

When would you use Box2D?

Use Box2D when you need the minimum bounding rectangle of a geometry as a cheap, fixed-size value — for indexing, coarse spatial filters, or computing viewports for maps. It is also useful when you want to feed a bounding box to functions such as ST_MakeEnvelope or build custom tile queries.

SQL
1SELECT id, Box2D(geom) AS bbox
2FROM parcels
3WHERE city = 'Lisbon';

FAQs

How is Box2D different from ST_Envelope?

ST_Envelope returns a fully-fledged geometry polygon representing the bounding rectangle, which you can store, index, and render. Box2D returns a compact box2d scalar that is cheaper to compute and store but isn't a geometry. Use Box2D for internal computation and ST_Envelope when you need a geometry column.

Does Box2D include Z values?

No — Box2D is strictly 2D and ignores Z and M coordinates. For 3D bounding boxes use Box3D, and for an XY-only geometry polygon use ST_Envelope.

What does Box2D return for an empty geometry?

An empty geometry has no extent, and Box2D on such an input returns NULL. Guard against this case with NOT ST_IsEmpty(geom) when aggregating, or use COALESCE on the result.

Can I use Box2D in a GiST index?

Yes, indirectly. PostGIS GiST indexes on the geometry column already use bounding-box geometry for their internal nodes, so you rarely need to materialise Box2D yourself. Computing Box2D explicitly is most useful when you need to expose the extent in application code or compare it against other boxes.