1

I would like to change the stroke width from mm to map units. The code below is mainly taken from ThomasG77's answer from a previous question. I only found a way to change the units of Labels unfortunately.

Use colums in attribute table to set colors and line thickness directly automatic

layer = iface.activeLayer()

# Get colors in rgb codes from Attribute table
layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().property(QgsSymbolLayer.PropertyStrokeColor).setExpressionString("\"Farben\" || ',255'")
layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().property(QgsSymbolLayer.PropertyStrokeColor).setActive(True)


# Get Stroke width from Attribute table
layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().property(QgsSymbolLayer.PropertyStrokeWidth).setExpressionString("\"Linien\"")
layer.renderer().symbol().symbolLayer(0).dataDefinedProperties().property(QgsSymbolLayer.PropertyStrokeWidth).setActive(True)


# Refresh rendering of the layer
layer.triggerRepaint()

1
  • I used your solution in a loop but for me it is not working. The units of the layer remain Milimeters. Even when I change manually the Map Units to something else and run the script it changes to Milimeters ![enter image description here](i.stack.imgur.com/ese6B.png) Here is how I used your suggestion: for a in layers_fimport: #print (a.name()) # get the layer's features as an iterator features = table.getFeatures() # Seach for a specific Layer in the table for feat in features: attrs = feat.attributes() if attrs[0] == a.name(): R = attrs(https:// Apr 5 at 14:41

1 Answer 1

2

I finally found it.

layer.renderer().symbol().symbolLayer(0).setWidthUnit(QgsUnitTypes.RenderMapUnits)

does the job

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.