PostGISBounding Box

ST_MakeBox2D

What is ST_MakeBox2D?

ST_MakeBox2D is a PostGIS constructor that builds a box2d value from two corner points. The two points represent the lower-left and upper-right corners of the bounding rectangle.

SQL
ST_MakeBox2D(geometry pointLowLeft, geometry pointUpRight) → box2d

When would you use ST_MakeBox2D?

Use ST_MakeBox2D to programmatically build bounding boxes from user-provided coordinates — for example, when translating a map viewport from a web client into a box2d you can use with the && operator to filter rows by a GiST index.

SQL
1SELECT id
2FROM parcels
3WHERE geom && ST_MakeBox2D(
4    ST_Point(-9.20, 38.70),
5    ST_Point(-9.10, 38.75)
6);

FAQs

Must the points be lower-left and upper-right?

Yes — ST_MakeBox2D assumes the first argument has the smaller X and Y and the second has the larger. Swapping the order or passing arbitrary corners can produce degenerate or unexpected boxes. Normalize your corners with LEAST/GREATEST if you are unsure.

What SRID does the resulting box2d carry?

box2d does not carry an SRID — it is a coordinate rectangle. Any SRID-awareness must come from your query context. If you need an SRIDed geometry, use ST_MakeEnvelope(xmin, ymin, xmax, ymax, srid) instead.

How is ST_MakeBox2D different from ST_MakeEnvelope?

ST_MakeBox2D returns a compact box2d scalar from two point geometries. ST_MakeEnvelope takes four numeric coordinates (plus optional SRID) and returns a full geometry polygon. Use ST_MakeBox2D for index filtering and ST_MakeEnvelope when you need a stored or visual geometry.

Is ST_MakeBox2D index-accelerated?

The function itself is a constructor. It becomes useful in combination with the bbox operator && against a GiST index — that is where indexing happens. parcels.geom && ST_MakeBox2D(...) is the canonical viewport query.