I have a point layer in QGIS with an SVG marker. I set the rotation via a data-defined override to the value of one field in the layer.

How do I retrieve that field in Python? Not the numeric value for a given feature, but the field (ie the expression) used?


You can do that by accessing the QgsDataDefined object for the angle (of rotation) from a QgsMarkerSymbolV2 object. Then you can use expressionOrField() or field() methods.

For instance:

symbol = layer.rendererV2().symbols()[0] # Getting the first symbol
print symbol.dataDefinedAngle().expressionOrField()

See the docs for more details.

  • 1
    This works perfectly, though I also need to use QgsDataDefined.isActive() and QgsDataDefined.useExpression() to catch all possibilities. Thanks! – Tom Chadwin Sep 21 '16 at 10:54

The rotationField of a SingleSymbolRenderer seems to get it (QGIS 2.14):

>>> layer.rendererV2().rotationField()

That's the field I'm using. If I change it slightly in the Properties dialog:

>>> layer.rendererV2().rotationField()
u'"angle" / 12'

Interesting. My style definition here is a Single Symbol which shows as a "Marker" with a "SVG Marker" in the style tree. I've set the "Angle" parameter of the SVG Marker component, but the "Rotation" parameter of the parent "Marker" component has also been set as data defined. But if I have two SVG layers in my Marker, I can set one "Angle" by field value and one to have constant Angle and the parent "Marker" doesn't get the Rotation set to data defined. At which point my python above fails to get the rotation of the layer.

So I suspect the other solution posted here might be more correct... When I have more than one substyle of SVG symbol I can get the Angle expression with:

>>> r.symbols()[0].symbolLayers()[0].dataDefinedProperty('angle').dump()
u'angle * 23'
  • Thanks, but Germán's answer is what I need. – Tom Chadwin Sep 21 '16 at 10:53

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.