Search code examples
postgresqlgeometrygisgeospatialpostgis

Increase the BBOX size but keep the ratio (Lon/Lat)


I have a Bbox that is defined by the following values:

xmin: 11.555333537980914 
ymin: 47.76067947037518 
xmax: 11.995692579075694 
ymax: 48.281587762758136

I would like to increase the size of this Bbox but keep the ratio. One approach I tried is to calculate the middle point of the Bbox and calculate a new Bbox with the value of radius increased by 50%. The problem: the ratio gets lost.

How could I increase the size of Bbox to 50% but keep the ratio.


Solution

  • Perhaps ST_Expand is what you're looking for. You could first calculate the area of the input bbox using ST_Area and then use the output as a unit to expand the bbox.

    SELECT            -- here you can play with different sizes
      ST_Expand(geom, ST_Area(geom)/2) 
    FROM yourtable;
    

    Example:

    WITH j (geom) AS (
      SELECT ST_MakeEnvelope(11.555333537980914,
                             47.76067947037518,
                             11.995692579075694,
                             48.281587762758136,4326)
    )
    SELECT 
      ST_Expand(geom,ST_Area(geom)/2) 
    FROM j;
    

    The image below represents the result set. The inner bbox is the one you provided and the outer one was created with ST_Expand.

    enter image description here

    Demo: db<>fiddle