[postgis-users] Expanding a polygon

Kevin Neufeld kneufeld at refractions.net
Mon Jan 11 12:29:51 PST 2010


It sounds like you'd like to apply an affine transformation (ST_Affine) to your geometry.

Two simple wrappers may help you:
   ST_Scale will expand/scale a polygon by a specified value in x and y.
   ST_Translate will translate the geometry if you wanted to scale at the midpoint of the geometry.

SELECT
   ST_AsText(
     ST_Translate(
       ST_Scale(geom, 3, 2),
       -3 * (ST_Xmin(geom)+ST_XMax(geom))/2 + ((ST_Xmin(geom)+ST_XMax(geom))/2),
       -2 * (ST_Ymin(geom)+ST_YMax(geom))/2 + ((ST_Ymin(geom)+ST_YMax(geom))/2))
   )

FROM
   (SELECT 'POLYGON (( 2 5, 2 6, 3 6, 3 5, 2 5 ))'::geometry AS geom) a;
                 st_astext
------------------------------------------
  POLYGON((1 4.5,1 6.5,4 6.5,4 4.5,1 4.5))
(1 row)


-- Or use ST_Affine directly:
SELECT
   ST_AsText(
     ST_Affine(
       geom, 3, 0, 0, 2,
       -3 * (ST_Xmin(geom)+ST_XMax(geom))/2 + ((ST_Xmin(geom)+ST_XMax(geom))/2),
       -2 * (ST_Ymin(geom)+ST_YMax(geom))/2 + ((ST_Ymin(geom)+ST_YMax(geom))/2))
   )

FROM
   (SELECT 'POLYGON (( 2 5, 2 6, 3 6, 3 5, 2 5 ))'::geometry AS geom) a;
                 st_astext
------------------------------------------
  POLYGON((1 4.5,1 6.5,4 6.5,4 4.5,1 4.5))
(1 row)


Ideally, it looks like there should an optional parameter to ST_Scale that would allow a user to scale about the 
midpoint or lower left directly.  I'll add this as a wish list item.

Cheers,
Kevin


Yurka wrote:
> I'm wondering if such thing exists.
> I have a polygon that i need to expand by x for each coordinate
> equivalent would be a function ST_Expand(), but ST_Expand expands the
> bounding box not the polygon itself.
> 
> maybe someone has a workaround?
> 
> thanks in advance. 



More information about the postgis-users mailing list