3

I'm designing a symbol for an external application that needs to be a vector field marker combined with a simple point marker, displaying a line at a certain angle around the center point. In PyQGIS, it's easy to instantiate the vector field class, as well as the field type (Polar), and angle units (Degrees). However, since it's polar, I need to set the length and angle attributes from the table of the layer I'm trying to display. I'm seeing in the API documentation for the QgsVectorFieldSymbolLayer that there are functions to set the X and Y attributes, which is necessary for a cartesian vector field marker, but irrelevant for a polar vector field marker. I need something like "setLengthAttribute()" and "setAngleAttribute()", much like what is possible in the QGIS GUI:

Available options for polar vector field marker symbol

My relevant snippet of code currently looks like this:

    symbol = QgsMarkerSymbolV2.createSimple({})
    symbol.deleteSymbolLayer(0)

    symbol_layer = QgsSimpleMarkerSymbolLayerV2()
    symbol_layer.setSize(.5)
    symbol_layer.setColor(QColor("yellow"))
    symbol_layer.setOutputUnit(QgsSymbolV2.MapUnit)
    symbol.appendSymbolLayer(symbol_layer)

    symbol_layer = QgsVectorFieldSymbolLayer()
    symbol_layer.setColor(QColor("yellow"))
    symbol_layer.VectorFieldType(QgsVectorFieldSymbolLayer.Polar)
    symbol_layer.AngleUnits(QgsVectorFieldSymbolLayer.Degrees)
    symbol_layer. # lengthAttribute?
    symbol_layer. # angleAttribute?
    symbol.appendSymbolLayer(symbol_layer)


    renderer = QgsSingleSymbolRendererV2(symbol)

    self.mylayer.setRendererV2(renderer)

Any idea how to set the angle and length attribute?

  • Does QgsMarkerSymbol.setAngle() or .setDataDefinedAngle() work for the angle? (qgis.org/api/…) – Richard Law Oct 1 '15 at 18:33
  • @alpha-beta-soup Maybe I'm not doing it correctly, but it doesn't seem to be working. setDataDefinedAngle is a function of QgsMarkerSymbolV2, not QgsVectorFieldSymbolLayer, so it doesn't seem to be recognizing it. My code looks like this, in varying orders: symbol_layer.setDataDefinedAngle(QgsMarkerSymbolV2("attribute field")) – Alex92 Oct 1 '15 at 20:07
1

You have two options:

  • using api directly:
symbol_layer.setXAttribute("length_attribute")
symbol_layer.setYAttribute("angle_attribute")
  • using data defined properties:
symbol_layer.setDataDefinedProperty("x_attribute", "length_attribute")
symbol_layer.setDataDefinedProperty("y_attribute", "angle_attribute")

Hope this helps.

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.