ST_MakeBox

更新时间:
复制 MD 格式

Returns the bounding box of a geometry or trajectory, optionally constrained to a time range.

Syntax

-- Spatial bounding box of a geometry (2D or 3D)
boxndf ST_MakeBox(geometry geom);

-- Full spatiotemporal bounding box of a trajectory
boxndf ST_MakeBox(trajectory traj);

-- Spatiotemporal bounding box of a geometry over a time range
boxndf ST_MakeBox(geometry geom, timestamp ts, timestamp te);

-- Bounding box of a sub-trajectory over a time range
boxndf ST_MakeBox(trajectory traj, timestamp ts, timestamp te);

-- Time-only bounding box
boxndf ST_MakeBox(timestamp ts, timestamp te);

Parameters

ParameterDescription
geomThe geometry whose bounding box to compute.
trajThe trajectory whose bounding box to compute.
tsThe start of the time range.
teThe end of the time range.

How it works

A bounding box is the minimal multi-dimensional rectangle that encloses a spatio-temporal object.

InputBehaviorReturn type
Geometry onlyReturns a spatial bounding box based on the object's dimensions (2D or 3D).BOX2D
Geometry + time rangeReturns a spatiotemporal bounding box covering the object over the specified time range.BOX2DT
Trajectory onlyReturns the bounding box of the complete trajectory.BOX2DT
Trajectory + time rangeReturns the bounding box of the sub-trajectory within the time range.BOX2DT

Bounding boxes use the FLOAT data type, so the result may be slightly larger than the actual extent. The lower bound can be marginally smaller, and the upper bound marginally larger, than the true values.

Examples

Geometry bounding box

The following example computes a spatial-only bounding box and a spatiotemporal bounding box for the same polygon.

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 a
)
SELECT
    ST_MakeBox(a),
    ST_MakeBox(a, '2000-01-01 00:00:10'::timestamp, '2000-01-01 02:13:20'::timestamp)
FROM geom;

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 first column returns a BOX2D (spatial only). The second column returns a BOX2DT that adds the time dimension. The timestamps in the output are slightly outside the requested range due to FLOAT precision.

Trajectory bounding box

The following example computes the bounding box of a full trajectory and of a sub-trajectory clipped to a time range. The trajectory is built using ST_makeTrajectory before calling ST_MakeBox.

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"}]}'
    ) a
)
SELECT
    ST_MakeBox(a),
    ST_MakeBox(a, '1999-12-31 23:00:00'::timestamp, '2000-01-01 00:00:30'::timestamp)
FROM traj;

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 first column spans the full trajectory extent. The second column clips to the requested time range — the spatial extent shrinks from (100, 100) to (30, 30) because only the first 30 seconds of movement fall within the range.

See also