1

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] (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()
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Katalpa
  • 871
  • 10
  • 20

1 Answers1

2

Works for me:

valLabel=QgsPalLayerSettings()
valLabel.readFromLayer(iface.activeLayer())
valLabel.fontSizeInMapUnits=True // change to map units
valLabel.textFont.setPointSize(100000) // set font size
valLabel.writeToLayer(iface.activeLayer())
iface.mapCanvas().refresh()

For the data-defined way check QgsPalLayerSettings::DataDefinedProperties FontSizeUnit

and enum QgsPalLayerSettings::SizeUnit

Units used for option sizes, before being converted to rendered sizes.

Enumerator

Points
MM
MapUnits
Percent

underdark
  • 84,148
  • 21
  • 231
  • 413
  • Thank you underdark! Added some edits per your advice. Although, it seems syntax errors might be present - as desired effect is not implaced. – Katalpa Dec 31 '14 at 12:28
  • Wow - way simpler than I was making things to be. This seems right 'valLabel.fontSizeInMapUnits=True' although having secondary problems setting actual measure. Thanks for speedy wisdom from that side of the pond! – Katalpa Dec 31 '14 at 12:54
  • COMMENT REMOVED: figured it out, the data-defined 'size' is still used following the advice you gave. Thanks'again – Katalpa Dec 31 '14 at 13:19
  • "valLabel.textFont.setPointSize(100000) // set font size " Even simpler than the working implemented data-defined method I had. Nice Crown! – Katalpa Dec 31 '14 at 13:53
  • For some reason, my variable 'textSize' doesn't scale properly when using the setPointSize() method. Ergo - if I change the 'scale' distance [textSize = (abs(scale[0]-scale[1]))/4], the text becomes oversized. Yet if I stick with the data-defined means, scale is maintained. Not to sure at the moment why this is the case - but figured it might be useful for others if they have a variable input setting their font size. – Katalpa Dec 31 '14 at 13:59
  • Sorry, I don't follow. If you want, open a new thread and explain what you want to achieve with this scale construct. – underdark Dec 31 '14 at 14:18
  • I feel bad to flood the boards - so I will try to be succinct here. I have a equidistant point array, where 'scale' is =to the distance between points. The user inputs a value for this distance when the array is created. Which is retrieved and resized via 'textSize = (abs(scale[0]-scale[1]))/4' to assure each point's label text does not overlap neighboring labels. The data-defined method maintains this scale appropriately whilst the user makes changes to the inter-point distance, but I could not get the setPointSize() method to fulfill this same need. – Katalpa Dec 31 '14 at 14:52