1

I have the centroid of an operation, the width of the operation (assumed to be on both sides) and the distance of the operation. What is the most efficient way to make a bounding box in Postgres with this data?

For example:

My hypothetical centroid is 0,0. The operation is 4 feet wide on each side. The operation is also 50 feet long. What is the best way for me to create a bounding box from this data?

Jim Jones
  • 18,404
  • 3
  • 35
  • 44

1 Answers1

1

Assuming your hypothetical coordinate pair is encoded in a spatial reference system that has foot as unit, such as NAD83, you just need to create a 4ft buffer and calculate its BBOX using ST_Envelope or ST_Extent:

SELECT ST_Envelope(ST_Buffer('SRID=[YOUR-SRS];POINT (0 0)',4));
SELECT ST_Extent(ST_Buffer('SRID=[YOUR-SRS];POINT (0 0)',4));

Another option is to use the geography data type (metre as unit) to create the buffer and then cast it again to geometry to create the BBOX, e.g.

SELECT ST_Envelope(ST_Buffer('POINT (0 0)'::geography,4*3.2808)::geometry)

You can also use ST_Project to define the corners manually and use ST_MakeBox2D to create your BBOX:

SELECT 
  ST_MakeBox2D(
   ST_Project('POINT(0 0)'::geography,4*3.2808,radians(225.0))::geometry, 
   ST_Project('POINT(0 0)'::geography,4*3.2808,radians(45.0))::geometry
  )::geometry 

Demo: db<>fiddle

WITH j (poi) AS (VALUES ('POINT(-4.50 54.15)'::geography))
-- 1. a random point, e.g. from ST_Centroid.
SELECT poi::geometry FROM j
UNION
-- 2. a 4ft buffer around the point of query 1
SELECT ST_Buffer(poi,4*3.2808) FROM j
UNION
-- 3. an envelope over the buffer created in query 2 (outer bbox)
SELECT ST_Envelope(ST_Buffer(poi,4*3.2808)::geometry) FROM j
UNION
-- 4. Inner bbox created based on a lower left (azimuth 225°) 
-- and a up right (azimuth 45°) corner 4ft away from the POI.
SELECT 
  ST_MakeBox2D(
   ST_Project(poi,4*3.2808,radians(225.0))::geometry, 
   ST_Project(poi,4*3.2808,radians(45.0))::geometry
  )::geometry 
FROM j;

enter image description here

Further reading:

Jim Jones
  • 18,404
  • 3
  • 35
  • 44