13

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.

  • 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? – Michael Stimson May 11 '16 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_ May 12 '16 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 – Michael Stimson May 12 '16 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_ May 13 '16 at 14:50
11

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.

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!

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

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.