I need to create a polygon that surrounds a given set of points using PostGIS.
Additionally, the polygon should extend slightly beyond the boundary points by a specified distance (ideally in meters), making the resulting shape slightly larger.
Could someone advise if this is possible and how it can be achieved with PostGIS?
You can ST_Collect()
your geometries, pass that to ST_ConvexHull()
and take an ST_Buffer()
around that:
demo at db<>fiddle
select st_buffer(st_convexhull(st_collect(geom)),9)
from your_table
group by cluster_num;
st_astext |
---|
POLYGON((200 191,191 200,191 300,200 309,300 309,309 300,309 200,300 191,200 191)) |
POLYGON((6.363961030678928 -6.363961030678928,-3.444150891285813 -8.314915792601578,-9 0,-9 100,0 109,100 109,108.31491579260158 103.4441508912858,106.36396103067892 93.63603896932108,6.363961030678928 -6.363961030678928)) |
Note that the buffer size 9
is measured in your geom
column unit dictated by the SRID if it's a geometry
, meters if it's a geography
. Popular SRID:4326
is in degrees, so you should either cast that to ::geography
or ST_Transform()
to a meter-based local coordinate reference system.
There are also SRIDs in feet and nautical miles, so the length of that 9
changes accordingly.
If you're also looking for a way to cluster them to decide which geometries should be together, there's ST_ClusterWithinWin()
.
Related: