1

enter image description here

enter image description here

I need to change a line style from Simple Line to Marker Line, and after that, use it as an arrow and change it's rotation and colors using PyQGIS.

  • 2
    What did you try so far, did you already started with some code and encountered an error? Please specify your problema nd have a look to docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook – eurojam Jun 27 at 9:46
  • Actually, I have no idea about how to do this using PyQgis. I made 'by hand', but I need to automate the process. It's arrows to show flow directions, based on a Digital Elevation Model. I already have the line slopes, and I also need to show which side they are going. – Luiz Paulo Zampronio Jun 28 at 13:02
2

Here a standalone recipe. We've seen you are using "Rule-based" symbology. Instead of doing the case for this particular case, we've made the sample for "Single Symbol" and at the end made the transformation to "Rule-based" symbology

iface.addVectorLayer('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_rivers_lake_centerlines.geojson', 'Rivers lake centerlines', 'ogr')
layer = iface.activeLayer()

# We apply the recipe to QgsSingleSymbolRenderer ("Single Symbol")
simpleMarkerSymbolLayer = QgsSimpleMarkerSymbolLayer()
simpleMarkerSymbolLayer.setShape(QgsSimpleMarkerSymbolLayer.Arrow)
property = QgsProperty()
property.setExpressionString("45 * 2")
property.setActive(True)
simpleMarkerSymbolLayer.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, property)

propertyColor = QgsProperty()
propertyColor.setExpressionString("if(attribute( 'scalerank' ) > 3, 'red', 'green')")
propertyColor.setActive(True)
simpleMarkerSymbolLayer.setDataDefinedProperty(QgsSymbolLayer.PropertyFillColor, propertyColor)

markerSymbol = QgsMarkerSymbol([simpleMarkerSymbolLayer])
# markerSymbol.setAngle(90.0) # Commented as we already set dynamically angle from an expression

markerLineSymbolLayer = QgsMarkerLineSymbolLayer()
markerLineSymbolLayer.setColor(QColor('#00ff00'))
markerLineSymbolLayer.setSubSymbol(markerSymbol)
lineSymbol = QgsLineSymbol([markerLineSymbolLayer])
layer.renderer().setSymbol(lineSymbol)

# Convert to rule based renderer ("Single Symbol" to "Rule-based")
# If not required, just comment below two lines and keep the last line
newRenderer = QgsRuleBasedRenderer.convertFromRenderer(layer.renderer())
layer.setRenderer(newRenderer)

layer.triggerRepaint()
| improve this answer | |
  • Nice. And how can I pass an expression in setAngle() as a parameter? I mean, it's gonna be flow direction arrows, and the angle needs to be variable, according with the difference between each line altitude. – Luiz Paulo Zampronio Jun 28 at 12:53
  • Updated answer to account with expression for angle – ThomasG77 Jun 28 at 14:11
  • Perfect. Another question that came to me is that I have an expression for the colors as well, color = 'case\r\nwhen \"MDT\" > \"Z1\" then color_rgb(255,0,0)\r\nwhen \"MDT\" < \"Z1\" then color_rgb(0,0,255)\r\nend' How can I use this expression to set the arrows colors? – Luiz Paulo Zampronio Jun 28 at 22:26
  • Updated my answer again. If you need to change other properties for the same symbol using expressions , look at qgis.org/api/… – ThomasG77 Jun 30 at 21:01

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