Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks a lot it's exactly what I was looking for... –  Etienne Desgagné Sep 4 '13 at 19:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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