Advertisements

Creating dynamic icon series

Today’s post was motivated by a question on GIS.StackExchange, which is looking for an automated way to symbolize the amenities available at a location using a series of icons, like this:

Screenshot 2016-03-19 23.02.30

Assuming the information is available in  a format similar to this example attribute table

Screenshot 2016-03-19 23.02.00

we can create a symbol, which adapts to the values in the icon columns using data-defined overrides:

Screenshot 2016-03-19 23.04.17

The five potential symbol locations are aligned next to each other using offsets. We use the following expression to determine the correct SVG symbol:

CASE
WHEN "icon4" = 'dinner'
 THEN 'C:/OSGeo4W64/apps/qgis-dev/svg/entertainment/amenity=restaurant.svg'
WHEN "icon4" = 'sleep'
 THEN 'C:/OSGeo4W64/apps/qgis-dev/svg/accommodation/accommodation_hotel2.svg'
WHEN "icon4" = 'ship'
 THEN 'C:/OSGeo4W64/apps/qgis-dev/svg/transport/amenity=ferry_terminal.svg'
WHEN "icon4" = 'house'
 THEN 'C:/OSGeo4W64/apps/qgis-dev/svg/accommodation/accommodation_house.svg'
 ELSE  ''
END

To hide icons if the icon value is NULL, the marker size is set to 0 using, for example:

CASE
WHEN "icon4" is not NULL
 THEN 4
 ELSE 0
END

Finally, to ensure that the labels don’t cover the icons, we can use the cartographic label placement with the position priority set to ‘TR,TL,BL’, which restricts labels to the top right, top left, and bottom left position.

Screenshot 2016-03-19 23.04.43

With these settings in place, we can zoom out and the labeling algorithm picks the most suitable position from the list of allowed positions:

Screenshot 2016-03-19 23.02.11

For more cartography tips and tricks check my new book QGIS Map Design or join my QGIS training courses.

Advertisements
6 comments
  1. Eltivo Makanaki said:

    Good day!

    Tried your tutorial, but I failed to set the icons right.

    Instead of seeing icons as processed by the CASE command, I see only one blue bubble with a question mark next to the name of my units.

    Where am I going wrong?

    Thanks!.

    • If you see question marks, the path to the SVG is incorrect. Check the path, particularly the direction of the slashes. They need to be forward-slashes (even on Win).

  2. It’s very nice!
    How do you size the marker to zero ONLY if is null?
    Thanks a lot!

    • Hi Francesc, I’ve added the expression to the post.

  3. kikocanas said:

    Nice!
    How do you size to zero only when the icon is null? If I put zero by default never display anything.

    • Hi kikocanas, I’ve added the expression to the post.

%d bloggers like this: