ST_MakeBox
Returns the bounding box of a geometry or trajectory, optionally restricted to a time range.
Syntax
-- Geometry only
boxndf ST_MakeBox(geometry geom)
-- Trajectory only (full trajectory)
boxndf ST_MakeBox(trajectory traj)
-- Geometry + time range
boxndf ST_MakeBox(geometry geom, timestamp ts, timestamp te)
-- Trajectory + time range (sub-trajectory)
boxndf ST_MakeBox(trajectory traj, timestamp ts, timestamp te)
-- Time range only
boxndf ST_MakeBox(timestamp ts, timestamp te)Parameters
| Parameter | Description |
|---|---|
geom | The geometry whose bounding box to compute. |
traj | The trajectory whose bounding box to compute. |
ts | The start of the time range. |
te | The end of the time range. |
Description
A bounding box is the minimal multi-dimensional rectangle that encloses a spatio-temporal object.
Geometry only — returns a spatial bounding box based on the dimensions of the object (two-dimensional or three-dimensional).
Geometry + time range — extends the spatial bounding box with the specified time range, producing a spatio-temporal bounding box.
Trajectory only — returns the bounding box that covers the complete trajectory.
Trajectory + time range — returns the bounding box of the sub-trajectory within the specified time range.
Bounding boxes are stored as FLOAT values. The result may be slightly larger than the input: the lower bound can be marginally smaller, and the upper bound marginally larger, than the actual geometry or trajectory bounds.
Examples
Geometry bounding box
-- No time range: returns a 2D spatial bounding box (BOX2D)
-- With time range: extends the bounding box to include the time dimension (BOX2DT)
WITH geom AS (
SELECT (
'POLYGON((12.7243236691148 4.35238368367118,12.9102992732078 1.49748113937676,'
|| '12.5926592946053 1.67643963359296,12.0197574747333 3.19258554889152,'
|| '12.7243236691148 4.35238368367118))'
)::geometry AS a
)
SELECT
ST_MakeBox(a),
ST_MakeBox(a, '2000-01-01 00:00:10'::timestamp, '2000-01-01 02:13:20'::timestamp)
FROM geom;Expected output:
st_makebox | st_makebox
-----------------------------------------------------+---------------------------------------------------------------------------------
BOX2D(12.0197572708 1.49748110771,12.9102993011 4.35238409042) | BOX2DT(12.0197572708 1.49748110771 2000-01-01 00:00:09.999999,12.9102993011 4.35238409042 2000-01-01 02:13:20.000381)The FLOAT precision effect is visible here: the returned time range is 00:00:09.999999 to 02:13:20.000381 rather than exactly 00:00:10 to 02:13:20.
Trajectory bounding box
-- No time range: covers the full trajectory extent
-- With time range: covers only the sub-trajectory within '1999-12-31 23:00:00' to '2000-01-01 00:00:30'
WITH traj AS (
SELECT ST_makeTrajectory(
'STPOINT',
'LINESTRING(0 0, 50 50, 100 100)'::geometry,
tsrange('2000-01-01 00:00:00'::timestamp, '2000-01-01 00:01:40'::timestamp),
'{"leafcount":3,"attributes":{"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120,130,140]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120,130,140]},"acceleration":{"type":"string","length":20,"nullable":true,"value":["120","130","140"]},"active":{"type":"timestamp","nullable":false,"value":["Fri Jan 01 11:35:00 2010","Fri Jan 01 12:35:00 2010","Fri Jan 01 13:30:00 2010"]}},"events":[{"2":"Fri Jan 02 15:00:00 2010"},{"3":"Fri Jan 02 15:30:00 2010"}]}'
) AS a
)
SELECT
ST_MakeBox(a),
ST_MakeBox(a, '1999-12-31 23:00:00'::timestamp, '2000-01-01 00:00:30'::timestamp)
FROM traj;Expected output:
st_makebox | st_makebox
---------------------------------------------------------+------------------------------------------------------------------
BOX2DT(0 0 2000-01-01 00:00:00,100 100 2000-01-01 00:01:40) | BOX2DT(0 0 2000-01-01 00:00:00,30 30 2000-01-01 00:00:30.000001)The time-bounded call clips the trajectory to the point at 00:00:30, returning a smaller spatial extent (30 30 instead of 100 100).