Fun with data-defined labels

Yesterday, I received an interesting QGIS question:

is there a way to make road label font size depending on road lenght (with osm layer)?
Indeed, it could be interresting to see all roads, even the smallest, on a city map rendering.

Thanks to the data-defined labeling capabilities of the new QGIS version, we can!

Just click the slightly weird symbol right of the label text size and select Edit …

Since OSM data is in WGS84 by default, street length will be measured in degrees and therefore the values will be small. To get to a reasonable font size, I selected $length * 1000.

The second part of the question can be addressed using a setting in the Rendering section which is – very descriptively – called “Show all labels for this layer (including colliding labels)”.

labelexperiment

While I doubt that this simple method alone will create a great road map, I think it’s still an interesting exercise with sometimes surprising results.

5 comments
  1. Nyall said:

    Hi Anita,

    Nice use of data defined labelling — I like it!

    One small suggestion – I noticed in your example that the smallest roads have tiny font sizes. You might have better luck using the “scale_linear” function, which allows you to smoothly vary the font size between a minimum and maximum value. I’ve had a play and something like: “scale_linear( $length, 0, 0.05, 5, 30)” seems to work quite well. This will vary the font size between 5 and 30, with roads of length 0.05 degrees having a font size of 30. Using this expression also plays nicely with curved placement of labels!

    • Oh, good point! Thanks!

  2. Hi!
    Have a question:
    Table values ​​from 0 to 100, numeric, how to make a label only for objects in which the corresponding value of> 10? Values ​​<10 is not labtling.

    Thanks!