1

I have a list of lines and I want to create a polygon from them using PostgreSQL. I can't find a function that gets a list or an array of lines and returns a polygon.

For example I have the following lines:

SELECT 'LINESTRING(12.50226 41.900927,12.076105 41.949897)' AS WKT UNION ALL
SELECT 'LINESTRING(12.50226 41.900927,12.0974 41.9967)' AS WKT UNION ALL
SELECT 'LINESTRING(12.50226 41.90093,12.0644 41.953876)' AS WKT UNION ALL
SELECT 'LINESTRING(12.50226 41.90093,12.0644 41.953876)' AS WKT UNION ALL
SELECT 'LINESTRING(12.50226 41.900927,12.076105 41.949897)' AS WKT UNION ALL
SELECT 'LINESTRING(12.50226 41.900927,12.0974 41.9967)' AS WKT UNION ALL
SELECT 'LINESTRING(12.507033 41.896497,12.076105 41.949897)' AS WKT UNION ALL
SELECT 'LINESTRING(12.507033 41.896497,12.076105 41.949897)' AS WKT 

My main goal is to create a corridor from group of lines. I think the best way to do it is to create a polygon.

Is there a better way, if not, how can I create it using PostGIS?

The following picture is the lines:

lines

I would like to create the following polygon polygon

Or to create a main route of the lines: main line

1
  • 2
    Your lines seem to make a long and narrow zig-zag shape. Could you add an image about the original linestings and what you would like to achieve? That would make your question much more clear.
    – user30184
    Commented Nov 8, 2016 at 8:42

1 Answer 1

3

You are looking for ST_ConvexHull()

  1. change your LINESTRING to a Geometry with ST_GeomFromText()
  2. group the LINESTRING Geometries with ST_Collect()
  3. finally process the collection into a polygon at 100% (1.0) with ST_ConcaveHull()

Here is the code using your provided data:

WITH LineCollection as (SELECT ST_GeomFromText('LINESTRING(12.50226 41.900927,12.076105 41.949897)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.50226 41.900927,12.0974 41.9967)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.50226 41.90093,12.0644 41.953876)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.50226 41.90093,12.0644 41.953876)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.50226 41.900927,12.076105 41.949897)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.50226 41.900927,12.0974 41.9967)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.507033 41.896497,12.076105 41.949897)') AS Geom UNION ALL
SELECT ST_GeomFromText('LINESTRING(12.507033 41.896497,12.076105 41.949897)') AS Geom)
SELECT ST_ConcaveHull(ST_Collect(Geom),1.0) FROM LineCollection;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.