19

I have had limited success creating smooth contour polygons from a raster layers.

I have created contour lines and boundary polylines and then used QGIS Union. Then I use QGIS Polygonize. Then use zonal statistics to the original raster to get the value on the polygons.

The 2nd method I tried was to convert raster to polygon. Then create a new attribute column rounded to the contour I desire. Then I dissolve lines between the same value. Then use v.generalize. This method is a little quicker, but it will leave some polygons jagged/unsmoothed.

enter image description here

Is there an easier method that actually works all the time. Seems like there should be a simple command that does the entire process.

4
  • 2
    Why are you using Polygonize? is your input raster continuous (like a floating point DEM)? QGIS has a contour tool, which is a shortcut to GDAL_Contour.. qgistutorials.com/en/docs/working_with_terrain.html Are you trying to get contours or hypsometric areas? Commented May 11, 2016 at 22:58
  • The input file is a tif and the required output file is a shape file with only polygons at specific increments. I have found numerous tools for creating polylines, but not polygons.
    – Rx_
    Commented May 12, 2016 at 12:24
  • Fair enough, you're trying to get hypsometric areas. I use (mostly) Esri which has an option to simplify the polygons created by raster to polygon but that wont help here. How would it look if you were to create the contours from the DEM, add a bounding box and then polygonize? You can then create label points, intersect/sample the values and label the polygons with the attributes... possibly you might get a few same value polygons which can be dissolved out but the bigger problem is jumping by more than an interval. I have code to attribute hypso from contours but it works on ArcInfo coverage Commented May 12, 2016 at 21:45
  • What you describe is what I think I'm doing in my first method. Its cumbersome. Need to make sure each file has an id attribute and then delete polygons that don't contain area above zero once polygonized. I failed to get the consistent correct label of the line to transfer to polygon. The second method is so close to being what I want, but v.generalize seems to not work 100% of the time. I wish the ESRI simplify polygons was an option in QGIS like you pointed out.
    – Rx_
    Commented May 13, 2016 at 14:50

4 Answers 4

16

I've experienced the same problems you area having in your second method. I exported a Raster to a Vector and try to and use v.generalise and I get mostly smooth polygons with the occasional 'stepped' boundary which appears to have been unaffected by the algorithm.

I found a process that worked for my task, not sure if its the best way but thought i'd share it in case it helped you.

What I started with was an ascii grid from BoM that looked like this: enter image description here

What I wanted something similar to what BoM produce like this: enter image description here

I was able to get to an outcome (that I was happy with) using the following steps.

  • Load grid (in my case ASCII Grid from BoM) into QGIS.
  • Recoded into discrete classes. (Processing Toolbox>Grass>Raster>r.recode)
  • Run a majority filter to 'clean up'.(Processing Toolbox>SAGA>Raster Filter>Majority Filter)
  • Created contours from the filtered grid at intervals of 1, and stored them in an attribute 'class' (because my classes were 1,2,3,4,etc). (Raster 'Menu'>Extraction>Contour)
  • The output contours are somewhat generalised, but I used v.generalised with the 'snakes' algorithm to smooth them out.(Processing Toolbox>Grass>Vector>v.generalize)
  • I then converted the lines to polygon.(Processing Toolbox>QGIS geoalgorithms>Vector Geometry Tools>Lines to Polygons)
  • Finally I had to convert the single part polygons into multiple parts, so that all the polygons drew nicely. (Processing Toolbox>QGIS geoalgorithms>Vector Geometry Tools>Singleparts to Mulitpart).

After styling my output is below: enter image description here

I would also be interested in hearing if someone knows a simpler way. Originally I was thinking similar to @Rx_ that I could just convert my raster to vector then generalise and I would be done. What I had to do was much much longer.

8

A more direct way is to use gdal_countour directly (as a command-line program) in "polygonal mode" (that is, with the -p option).

Example:

gdal_contour -p -amin min_val -amax max_val -i 1 input_raster.tif output_shape.shp

Documentation: https://gdal.org/programs/gdal_contour.html

This option is available only in version >= 2.4 (this can be checked with gdalinfo --version). If you're using Ubuntu 18.4, you might have to install it from UbuntuGIS

2
  • 1
    You can easily access gdal contour polygon from QGIS, so you don't have to use command line: menu processing > toolbox > contour polygon / or expand GDAL > Raster extraction > contour polygon.
    – Babel
    Commented Nov 25, 2020 at 10:02
  • 1
    You just need to add Additional Command Line Parameters that aren't defined in the UI: -p -amin ELEV1 -amax ELEV2 Commented Apr 26, 2021 at 3:08
3

I like the answers here but I cant believe that QGIS still doesnt have a quick and easy way to do this. MapInfo can generate contours as either polygons or lines with any interval that you can specify.

It seems like such a basic task and also important for obtaining extents. If anyone has an updated or faster way to do this please share!

1
  • In QGIS, use Raster->Extraction->Contours. Remove the ELEV attribute, and add the following Additional Command Line Parameters: -p -amin ELEV1 -amax ELEV2 Commented Apr 26, 2021 at 3:07
0

In QGIS 3.8 you can use Raster - Extraction - Contour

There is a good example here https://support.dronesmadeeasy.com/hc/en-us/articles/217759823-Using-QGIS-to-make-custom-Contour-Maps

1
  • That tool produces line contour layers, polygon contour layers are essentially what is being asked for here. Commented Nov 1, 2019 at 5:34

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.