Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a polygon around a linestring with PostGIS

I am new to PostGIS and need to ask for some help here. I have a polyline from google maps (representing an itinerary) and need to build a polygon (buffer) around it with a specific distance in meters or kilometers.

For input, I have the list of Latitude/Longitude points and the required buffer distance.

Can anyone help me build the query so that the returned result is the polygon in Latitude/Longitude coordinates, ready to be plotted on the map ?

like image 961
Simon Avatar asked Sep 03 '25 09:09

Simon


1 Answers

  1. Add the first vertex of your line string again at the end to be able to create a polygon.
  2. Convert line string to polygon
  3. Create a buffer around the polygon
SELECT
    ST_Buffer(ST_Polygon(ST_AddPoint(the_geom, ST_StartPoint(the_geom))),100)
FROM
    mytable
  1. If you have your buffer in meters/kilometers and your data in latitude/longitude you might want to first transform your polygon into an appropriate projection (I don't know where you are) and then back into latitude/longitude.
SELECT
    ST_Transform(ST_Buffer(ST_Transform(ST_Polygon(ST_AddPoint(the_geom,ST_StartPoint(the_geom)),4326),XXXX),100),4326)
FROM
    mytable

I haven't tried the code but it should work.

like image 154
Thomas Avatar answered Sep 04 '25 23:09

Thomas