2

I'm trying to find how to get with the QGIS Python API the layer field name that contain the rotation angle used by a layer to rotate its labels?

I know that I can obtain the field name containing the text used to label features this way:

palyr = QgsPalLayerSettings()
palyr.readFromLayer(layer)
f = palyr.fieldName

but I can't figure out how to obtain the rotation field name when setted in the "Data defined" section... The QgsPalLayerSettings classe contain a lot of methods related to the manipulation of "DataDefined" properties and a basic example would be very appreciated...

Thanks a lot

Etienne

5

Access to the data defined properties was just fixed today (previously caused a crash). Following your example code, you can access it like so (most QgsPalLayerSettings properties are accessed directly):

layer = iface.activeLayer()
palyr = QgsPalLayerSettings()
palyr.readFromLayer(layer)

# get currently set data definitions as dict of 
#   {QgsPalLayerSettings.DataDefinedProperties: QgsDataDefined}
#   e.g. {18: <qgis.core.QgsDataDefined object at 0x1237daa70>, 10: ...}
ddp = palyr.dataDefinedProperties

# get a data definition from those currently set
ddft = None
if QgsPalLayerSettings.FontTransp in ddp:
    ddft = ddp[QgsPalLayerSettings.FontTransp]

# or try to get a data definition directly (returns None if not found)
ddfc = palyr.dataDefinedProperty(QgsPalLayerSettings.FontCase)
ddfc_field = ''
if ddfc is not None:
    ddfc_field = ddfc.field()


# you can update the data definition as well
ddfc = palyr.dataDefinedProperty(QgsPalLayerSettings.FontCase)
if ddfc:
    ddfc.setField('myfield')  # which would need to be a valid field

# you can also add a new data definition
if not QgsPalLayerSettings.Size in ddp:
    # add an active definition that defines to use an expression of 36 
    # (i.e. no attribute field is mapped)
    palyr.setDataDefinedProperty(QgsPalLayerSettings.Size, True, True, '36', '')

# whenever you update/add/remove a data definition,
# from your QgsPalLayerSettings object, if you want to
# save it to the project instance and see results on canvas
palyr.writeToLayer(layer)
iface.mapCanvas().refresh()

In order to know what DataDefinedProperties enums to use, review the QgsPalLayerSettings docs. Consult the docs on QgsDataDefined to see how to access/manipulate it.

  • Just found this - helping to work out some problems I'm having link. Thanks for sharing. – Katalpa Dec 30 '14 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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