7

Is there a PostGIS function that creates polygons from multiple line segments?

I want to convert the pictured thirteen lines to four polygons. All lines belong to the table "boundary" (gid, geom). I've categorized them by the gid column for a better illustration.

Is ST_MakePolygon what I'm looking for? Can anyone help me with the SQL syntax that QGIS requires for loading the polygons?

line geometry Edit: changed "closed line geometries" to "multiple line segments"

asked May 2, 2015 at 13:10

2 Answers 2

15

ST_Polygonize will do the job:

CREATE VIEW boundarypolygons AS
SELECT 
 g.path[1] as gid, 
 g.geom::geometry(polygon, 31492) as geom 
FROM
 (SELECT 
 (ST_Dump(ST_Polygonize(geom))).* 
 FROM boundary
) as g;

enter image description here

answered May 3, 2015 at 11:05
3

Since you are using PostGIS and QGIS, you can try to convert them using both, to decide which one is best suited for your problem. To use PostGIS, the LINESTRING must be closed. You can check if they are closed with the query:

select gid, st_isclosed(geom) from boundary;

If the lines are closed, you can create another table to check the results, with:

create table newboundary as
select gid, ST_MakePolygon(geom) 
from boundary
where st_isclosed(geom);

Add the newboundary to QGIS afterwards.

To use QGIS, check Create polygon layer from polyline layer.

Evan Carroll
7,2592 gold badges34 silver badges67 bronze badges
answered May 3, 2015 at 0:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.