Shapeburst fill styles in QGIS 2.4

With QGIS 2.4 getting closer (only a few weeks away now) I’d like to take some time to explore an exciting new feature which will be available in the upcoming release… shapeburst fills!

As a bit of background, QGIS 2.2 introduced a gradient fill style for polygons, which included linear, radial and conical gradients. While this was a nice feature, it was missing the much-requested ability to create so-called “buffered” gradient fills. If you’re not familiar with buffered gradients, a great example is the subtle shading of water bodies in the latest incarnation of Google maps. ArcGIS users will also be familiar with the type of effects possible using buffered gradients.

Gradient fills on water bodies in Google maps

Gradient fills on water bodies in Google maps

Implementing buffered gradients in QGIS originally started as a bit of a challenge to myself. I wanted to see if it was possible to create these fill effects without a major impact on the rendering speed of a layer. Turns out you can… well, you can get pretty close anyway. (QGIS 2.4’s new multi-threaded responsive rendering helps a lot here too).

So, without further delay, let’s dive into how shapeburst fills work in QGIS 2.4! (I’ve named this fill effect ‘shapeburst fills’, since that’s what GIMP calls it and it sounds much cooler than ‘buffered gradients’!)

Basic shapeburst fills

For those of you who aren’t familiar with this fill effect, a shapeburst fill is created by shading each pixel in the interior of a polygon by its distance to the closest edge. Here’s how a lake feature polygon looks in QGIS 2.4 with a shapeburst from a dark blue to a lighter blue colour:

A simple shapeburst fill from a dark blue to a lighter blue

A simple shapeburst fill from a dark blue to a lighter blue

You can see in the image above that both polygons are shaded with the dark blue colour at their outer boundaries through to the lighter blue at their centres. The screenshot below shows the symbol settings used to create this particular fill:

A simple shapeburst fill from a dark blue to a lighter blue

Creating a simple shapeburst fill from a dark blue to a lighter blue

Here we’ve used the ‘Two color‘ option, and chosen our shades of blue manually. You can also use the ‘Color ramp’ option, which allows shading using a complex gradient containing multi stops and alpha channels. In the image below I’ve created a red to yellow to transparent colour ramp for the shapeburst:

Colour ramp shapeburst with alpha channels

Colour ramp shapeburst with alpha channels

Controlling shading distance

In the above examples the shapeburst fill has been drawn using the whole interior of the polygon. If desired, you can change this behaviour and instead only shade to a set distance from the polygon edge. Let’s take the blue shapeburst from the first example above and set it to shade to a distance of 5 mm from the edge:

Shapeburst fills can shade to a set distance only

Shapeburst fills can also shade to a set distance from the polygon’s exterior

This distance can either be set in millimetres, so that it stays constant regardless of the map’s scale, or in map units, so that it scales along with the map. Here’s what our lake looks like shaded to a 5 millimetre distance:

Shading to 5mm from the lake's edge

Shading to 5mm from the lake’s edge

Let’s zoom in on a portion of this shape and see the result. Note how the shaded distance remains the same even though we’ve increased the scale:

Zooming in maintains a constant shaded distance

Zooming in maintains a constant shaded distance

Smoothing shapeburst fills

A pure buffered gradient fill can sometimes show an odd optical effect which gives it an undesirable ‘spiny’ look for certain polygons. This is most strongly visible when using two highly contrasting colours for the fill. Note the white lines which appear to branch toward the polygon’s exterior in the image below:

Spiny artefacts on a pure buffered gradient fill

Spiny artefacts on a pure buffered gradient fill

To overcome this effect, QGIS 2.4 offers the option to blur the results of a shapeburst fill:

Blur option for shapeburst fills

Blur option for shapeburst fills

Cranking up the blur helps smooth out these spines and results in a nicer fill:

Adding a blur to the shapeburst fill

Adding a blur to the shapeburst fill

Ignoring interior rings

Another option you can control for shapeburst fills is whether interior polygon rings should be ignored. This option is useful for shading water bodies to give the illusion of depth. In this case you may not want islands in the polygon to affect their surrounding water ‘depth’. So, checking the ‘Ignore rings in polygons while shading‘ option results in this fill:

Ignoring interior rings while shading

Ignoring interior rings while shading

Compare this image with the first image posted above, and note how the shading differs around the small island on the polygon’s left.

Some extra bonuses…

There’s two final killer features with shapeburst fills I’d like to highlight. First, every parameter for the fill can be controlled via data defined expressions. This means every feature in your layer could have a different start and end colour, distance to shade, or blur strength, and these could be controlled directly from the attributes of the features themselves! Here’s a quick and dirty example using a random colour expression to create a basic ‘tint band‘ effect:

Using a data defined expression for random colours

Using a data defined expression for random colours

Last but not least, shapeburst fills also work nicely with QGIS 2.4’s new “inverted polygon” renderer. The inverted polygon renderer flips a normal fill’s behaviour so that it shades the area outside a polygon. If we combine this with a shapeburst fill from transparent to opaque white, we can achieve this kind of masking effect:

Creating a smooth exterior mask using the "inverted polygons" renderer

Creating a smooth exterior mask using the “inverted polygons” renderer

This technique plays nicely with atlas prints, so you can now smoothly fade out the areas outside of your coverage layer’s features for every page in your atlas print!

All this and more, coming your way in a few short weeks when QGIS 2.4 is officially released…

Tagged , , , , , ,

17 thoughts on “Shapeburst fill styles in QGIS 2.4

  1. Régis Haubourg says:

    Sweet!
    And a plugin is coming to easily generate those mask feature, with shapeburst style by default.
    Thks a lot Nyall!

  2. Andreas Neumann says:

    Hi Nyall,

    How did you do the inverted polygon renderer example. I can’t find where I can set this behavior.

    Thanks for any hints.

    Andreas

    • Nyall Dawson says:

      Look in the combo box where you choose between “categorised” and “graduated” renderers. There’s a new option for “inverted polygons”. To use it with an atlas, first set the renderer to “inverted polygons”, then set the sub-renderer as “rule based renderer”. Then, make a rule so that the current atlas feature is shown in a white-to-transparent shapeburst fill, and a rule for all the other features. Turn off “symbol” for the other features. Tweak as desired 🙂 Hope that helps!

  3. Darren Cope says:

    Nyall; this is awesome! However, I tried to generate an atlas with a faded ‘transition’ mask where $id=$atlasfeatureid like your last example, but it appears that the atlas renderer renders the ‘cloudburst’ only once, and then applies that rendering to all features in the atlas. For example, if the first feature is a rectangle, the cloudburst will be rectangular and not follow the boundaries of later features in the atlas. Essentially, the ‘cloudburst’ doesn’t seem to be rendered for every feature.

    • Nyall Dawson says:

      Are you using the inverted polygon renderer with a rule based sub renderer? I just tested and it works fine for me. You need one rule for matching the atlas feature, and an “else” rule for non matching features. The else rule must have symbol unticked.

  4. […] forward to all the great maps we’ll see with the new gradient & inverted fill #QGIS nyalldawson.net/2014/06/shapeb… […]

  5. Darren Cope says:

    Thanks Nyall! Got it now–just started over and it worked, so obviously I had a setting wrong somewhere!

  6. […] create coastal vignettes, QGIS 2.4 offers a fill style that allows polygon shapefiles to be filled with a gradient. […]

  7. Liam Allison says:

    Hello,

    I have got the shapeburst fill to work, but I cant get it from transparent to opaque white?
    How do you set it to transparent to opaque white – I can only see the options for 2 colours when using shapeburst fill. I can add transparency to the whole layer but ofcourse that doesnt work as the white goes transparent.

    Thanks for any help.

    • Nyall Dawson says:

      When you’re setting one of the two colours for the shapeburst you’ll need to adjust the “alpha” value. Click the color button and look in to bottom right – there’s an option there. 255 means fully opaque, 0 is fully transparent. Hope that helps!

  8. […] Nueva herramienta para rellenar de color los polígonos mediante gradientes. Nos permitirá realizar gradientes de color desde las líneas de contorno, dándole un aspecto muy mejorado a nuestras capas. Te dará la posibilidad de pintar los polígonos con opciones muy interesantes (puedes ver un artículo con todas ellas en nyalldawson). […]

  9. Dan says:

    I tried to use the shapeburst fill. It works great in QGIS however I try to export it to an SVG file and open this file in Adobe Illustrator and my entire map is filled in black color where I applied the shapeburst fill. Does anybody have any idea of whether I am doing anything wrong. I’ve tried using two colors with the second color set to a separate color as well no color, as well as tried using the color ramp option. It always results in a black fill.

  10. […] with QGIS 2.4 we have seen the introduction of both shapeburst fills and inverse fills. This allows for some great […]

  11. Tom says:

    I cannot find Shapeburst in QGIS 2.6 or 2.7. I also do not find it under Plugins. Has it been removed?
    Thanks
    Tom

  12. […] Coup de chance, Nyall Dawson ajoute en même temps le mode de rendu de bordure de polygone dégradés: […]

  13. […] Coup de chance, ou plutôt inspiration réciproque, Nyall Dawson ajoute en même temps le mode de rendu de bordure de polygone dégradés: […]

  14. […] finishing, I was really pleased to discover that since version 2.4 QGIS even supports gradient or shapeburst fills. It allows you to create subtle shading of water bodies for […]

Leave a Reply to Adding interactivity to static maps – MAPPABLE Cancel reply

Your email address will not be published. Required fields are marked *