0

I am new to QGIS (I am using QGIS 3.26). The bosses at my work want to get an automated process for converting simple data into maps for visualization purposes. So I started to learn Python used to write scripts in QGIS, however I got stuck at a certain point. The idea is to automatically import 2 vector layers (which I did) and have one of them automatically change its Project Style to 'Outline', the colour of that style to black and the opacity to something like 50%. Values currently less important. The problem is that I can't figure out how to access these variables from within Python. I know how to get to the Attributes, but the Properties remain a mystery to me.

I am attaching a visualisation of what I mean. enter image description here

enter image description here enter image description here

1
  • Welcome to GIS Stack Exchange. As a new user it is important to understand that questions asking for help with coding need to include a code attempt. In this case you should add the code you already have to load your layers.
    – Ben W
    Nov 9, 2022 at 11:08

1 Answer 1

1

You can use the script below. See in-code comments for explanation of each step.

layer_name = 'Województwa'# The name of the layer you want to style
# Access the layer object
layer = QgsProject.instance().mapLayersByName(layer_name)[0]
# Create a new outline symbol layer with desired color and alpha value (opacity)
symbol_layer = QgsSimpleLineSymbolLayer(QColor(0,0,0,125))
# Replace existing symbol layer with new symbol layer
layer.renderer().symbol().changeSymbolLayer(0, symbol_layer)
# Update symbology in canvas and layer tree view
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())

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