Skip to main content
7 of 9
by accepting an answer the system automatically marks the thread as solved
underdark
  • 84.5k
  • 22
  • 233
  • 415

Connecting vector label size with map-units?

I have a working application of labels to a point array - but the size of the text is not relative to the map scale. This decreases the readability of labels when panning and zooming.

To resolve~

How would one implement a text-size based on MapUnits?

    field = str(csvGridFieldNames[index])
    csvGrid = ftools_utils.getMapLayerByName(unicode('mfLayer1_Grid'))#str(activeLayer)))
    textSize = 7
    valLabel = QgsPalLayerSettings()
    valLabel.readFromLayer(csvGrid)
    valLabel.enabled = True
    valLabel.fieldName = field
    valLabel.placement= QgsPalLayerSettings.OverPoint
    valLabel.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'%f' %(textSize),'')
    valLabel.writeToLayer(csvGrid)
    iface.legendInterface().refreshLayerSymbology(csvGrid)
    iface.mapCanvas().refresh()

This question appears related, but having some trouble understanding the doc's...

"...lables 'Data Defined' section in pyqgis"

...and FWIW:

A screen shot of the Qgis tool, with the two (needed) PyQgis settable parameters highlighted in green: lable settings

Updates following underdark's advice:

Input syntax errors (on my part) are not raising error flags - but are also not implementing MapUnit based font/label size...

    valLabel.SizeUnit = QgsPalLayerSettings.MapUnits
    valLabel.setDataDefinedProperty(QgsPalLayerSettings.FontSizeUnit,True,True,':%f' %(textSize),'')

LAST UPDATE [SOLVED] This forums pool of wisdom is awesome. Final thanks to underdark for setting things off right in short. Cheers, (final working code below):

    textSize = (abs(scale[0]-scale[1]))/4
    valLabel = QgsPalLayerSettings()
    valLabel.readFromLayer(csvGrid)
    valLabel.enabled = True
    valLabel.fontSizeInMapUnits = True
    valLabel.fieldName = field
    valLabel.placement = QgsPalLayerSettings.OverPoint
    valLabel.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'%f' %(textSize),'')
    valLabel.writeToLayer(csvGrid)
    iface.legendInterface().refreshLayerSymbology(csvGrid)
    iface.mapCanvas().refresh()
Katalpa
  • 871
  • 10
  • 20