Saturday 15 October 2016

qgis - Creating polygons from line segments using PostgreSQL and PostGIS


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"



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...