4

I have a script which works well and changes the symbol QuarterCircle's color and angle :

vLayer = QgsVectorLayer(uri, "myData" , "delimitedtext")

symbol = QgsSymbolV2.defaultSymbol(vLayer.geometryType())

simple_marker = QgsSimpleMarkerSymbolLayerV2() 
simple_marker.setShape(QgsSimpleMarkerSymbolLayerBase.QuarterCircle )
simple_marker.setSize(6.0)

ddpAngle=QgsDataDefined( True, True, "Direction")
ddpColor= QgsDataDefined( True, True, "CASE WHEN DATA % 3 = 0 then  color_rgb( 255,0,0) WHEN DATA % 3 = 1 then  color_rgb( 0,255,0) WHEN DATA % 3 = 2 then  color_rgb( 0,0,255) END" )
simple_marker .setDataDefinedProperty( "angle", ddpAngle)
simple_marker .setDataDefinedProperty( "color", ddpColor)

symbol.changeSymbolLayer(0,simple_marker)

vLayer.rendererV2().setSymbol(symbol)
vLayer.triggerRepaint()

I wanted to change the QgsSimpleMarkerSymbolLayerBase.QuarterCircle symbol to user defined .svg format symbol.

symbol = QgsSymbolV2.defaultSymbol(vLayer.geometryType())
svg_marker = QgsSvgMarkerSymbolLayerV2( 'D:\\sample.svg' )
svg_marker.setSize(12.0)
svg_marker.setOutlineWidth(0.25)
ddpAngle=QgsDataDefined( True, True, "Direction")
ddpColor= QgsDataDefined( True, True, "CASE WHEN DATA % 3 = 0 then  color_rgb( 255,0,0) WHEN DATA % 3 = 1 then  color_rgb( 0,255,0) WHEN DATA % 3 = 2 then  color_rgb( 0,0,255) END" )
svg_marker.setDataDefinedProperty( "angle", ddpAngle)
svg_marker.setDataDefinedProperty( "color", ddpColor)

symbol.changeSymbolLayer(0,svg_marker)

vLayer.rendererV2().setSymbol(symbol)
vLayer.triggerRepaint()

For this one it changes the angle but the color part doesn't work and all the colors are black. Can you please help me find the problem?

UPDATE: I realised that when I do it by manually from desktop app it works(entering data defined override by manually). But when I paste my code to desktop app's python console by changing vLayer to iface.activeLayer() it also doesn't change color and it shows no data defined override for color part of it, it just seems deactivated.

Can anyone give an example portion of code that imports an svg and modify its color according to some data belongs to a vector layer?

0

2 Answers 2

3

Does your svg support color changes if you manually set it for a layer within the qgis GUI? If not, you'll need to edit the svg to "parameterize" the colours. See How to create svg symbols that have modifiable fill color, stroke color and stroke width?

2
  • it supports color changes.i have checked by doing same thins manually with qgis app.in app both color and angle works fine but i couldnt achieve same thing in python
    – MS.
    Commented Dec 12, 2018 at 7:34
  • i realised that when i do it by manually from desktop app it works. But when i paste my code to desktop app's python console by changing vLayer to iface.activeLayer() it also doesnt change color and it shows no data defined override for color part of it , it just seems deactivated
    – MS.
    Commented Dec 12, 2018 at 11:05
1

After dealing for 2 days I have found the problem. The property name need to be "fill" instead of "color"

svg_marker.setDataDefinedProperty( "fill", ddpColor)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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