5

Is there a way to get the height and width of labels in the field calculator? Perhaps with the help of the function editor? Units are in meters.

enter image description here enter image description here

See also: Creating leader lines for moved labels

1

1 Answer 1

5

Perhaps a look at http://pyqt.sourceforge.net/Docs/PyQt4/qfontmetrics.html might help. You can obtain pixel dimensions of a string for a given font as follows:

from PyQt4 import QtGui
f = QtGui.QFont("times", 24)
fm = QtGui.QFontMetrics(f)

>>> fm.width("My test text")
149
>>> fm.height()
36

Since this function doesn't seem to take line breaks into account, I suggest not to use auto-linebreaks in your labels and a) sum up the line height's and b) estimate the maximum of the line width's in order to obtain your labels pixel dimensions. If you have drawn a background to your labels you may have to add twice its x/y size to the width/height.

If you put it all together in a custom function, you may directly use it in the field calculator.

Here is a nice example on how creating custom functions work: http://www.qgistutorials.com/de/docs/custom_python_functions.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.