3

I have contour lines that I am converting to polygons. Contours along the edge are clipped so are difficult to simply convert to polygons. So I'm trying to find an automated way to do this within QGIS.

Unfortunately, these contour lines don't have an elevation attribute to use and are simply indicative

Here is an example of what I mean: enter image description here

I have thought a way could be to extract the start/end vertices. Then somehow give them an ordered clockwise "ID", then iterate "Points to Path" leaving a gap between each line. I tried using @row_number but the "ID"'s aren't exactly clockwise.

3

1 Answer 1

3

Add a Virtual Layer

  • Layer | Add Layer | Add/Edit Virtual Layer...

and run

SELECT c.<column1>,
       c.<column2>, 
       ...,
       ST_MakePolygon(ST_AddPoint(geometry, ST_StartPoint(geometry)) AS geometry
FROM   <contour_layer_name>
WHERE NOT ST_Equals(ST_StartPoint(geometry), ST_EndPoint(geometry))
UNION ALL
SELECT c.<column1>,
       c.<column2>, 
       ...,
       ST_MakePolygon(geometry) AS geometry
FROM   <contour_layer_name>
WHERE ST_Equals(ST_StartPoint(geometry), ST_EndPoint(geometry))
;

This will close each 'open' contour by connecting its end to start point, and then generate polygons. Closing lines will not follow the 'square' at its corners, and might create invalid geometries there.

6
  • Added a filter for open, and a `UNION' with already closed contours. If the corner cases are an issue, it is possible to include the corner point, too. Add a comment then and I'll update the answer.
    – geozelot
    Jun 15, 2021 at 7:05
  • I don't have much experience with SQL and virtual layers. I tried copying this into the Query and replacing <contour_layer_name> with the name of my layer but I am getting a syntax error. I think there is a missing bracket on 4th line, I tried fixing that but am still getting a syntax error:
    – JUNGLE
    Jun 16, 2021 at 1:39
  • Here is the error: Query execution error on CREATE TEMP VIEW _tview AS SELECT c.<column1>, c.<column2>, ..., ST_MakePolygon(ST_AddPoint(geometry, ST_StartPoint(geometry)) AS geometry FROM Contours WHERE NOT ST_Equals(ST_StartPoint(geometry), ST_EndPoint(geometry))) UNION ALL SELECT c.<column1>, c.<column2>, ..., ST_MakePolygon(geometry) AS geometry FROM Contours WHERE ST_Equals(ST_StartPoint(geometry), ST_EndPoint(geometry)) ;: 1 - near "<": syntax error
    – JUNGLE
    Jun 16, 2021 at 1:40
  • 1
    @JGIS you need to replace the <column1> placeholders with the actual names of the fields you want to include. It has to be the same list in both sides of the UNION ALL.
    – geozelot
    Jun 16, 2021 at 7:10
  • e.g. c.<column1> may be c."id"
    – Taras
    Jun 17, 2021 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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