[GIS] Automatically adapt label text size to area in QGIS

labelingqgis

In Relating text buffer to text size in QGIS? I displayed various polygon sizes : the objective of my research was to try to adjust automatically the buffer size of the text to the font size because the buffer tended to be too large for little texts.

I'm now looking for a way to adapt the font size to the polygon area. This way, i won't have to put it into the attribute table and fix manually the font size i need.

Best Answer

EDIT June 2018: easier solution in QGIS 2.18+ !

It's possible to do it directly, without a virtual field as in my previous answer :

  • Open the Label properties
  • At the end of the line, click on the data defined icon
  • Select Edit
  • Write in the field ln($area) (or log(base, $area) as suggested by @Stephen Kay in comments, with base an integer you choose)
  • If necessary, do the same for the text buffer and write ln($area)/15 in the data defined properties of the size.

EDIT June 2018: I leave my previous solution below for info

An easy way to do this is by using the ln() function (for example).

  • create a virtual field named VRT_FONT
  • Use the formula ln($area)
  • Set the lable font size to the data defined field VRT_FONT
  • Draw new polygons, it will label automatically your polygon depending on it's area.

This basic formula answers my needs but you can of course tweak it to suits yours by adding/multiplying values (or else).

Tip : you can combine this with the following tip : do a virtual field VRT_BUFF with the formula "VRT_FONT"/15 and set the lable font buffer to this field to have an area adaptive buffer too. Tip 2 : you can modify the formula to fit your own scale needs and the size of buffer you would like.

What it does :

With_tip

The field properties :

With_tip_properties

The attribute table :

attributes

A new polygon :

With_tip 2

Related Question