2

I'd like to change the symbology of my vector layer and can't figure it out how to read the documentation. I would like to have an polygone with an orange outline, a dashed fill style and an orange stroke color. However that would be nice to have an answer that explains how to access most of the symbology change! (if possible ;-) )

Here is what I managed so far:


import os
import processing
from qgis.core import *
from qgis.gui import *
import qgis.utils

# initialize project
project = QgsProject.instance()
project.setCrs(QgsCoordinateReferenceSystem(21781))
project.setFileName("C:\\test_symbology.qgs")

my_shp = "Y:\\shp_collection\\A300.shp"
my_shp_name = "A300"

# load polygon
shplayer = iface.addVectorLayer(my_shp , my_shp_name , "ogr")
if not shplayer.isValid():
    print(".shp failed to load!")
shplayer.setCrs( QgsCoordinateReferenceSystem(21781,QgsCoordinateReferenceSystem.EpsgCrsId))

layer = iface.activeLayer()

# make my polygone orange
single_symbol_renderer = layer.renderer()
symbol = single_symbol_renderer.symbol()
symbol.setColor(QColor.fromRgb(255,128,0))

How can I set the other symbology properties?

enter image description here

1

The easiest for me would be to check the properties of the symbology layer which contains symbol properties affecting the whole layer:

dir(symbol)

>>>['DynamicRotation', 'Fill', 'Hybrid', 'Line', 'Marker', 'RenderHint', 'RenderHints', 'ScaleArea', 'ScaleDiameter', 'ScaleMethod', 'SymbolType', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_getLineString', '_getPoint', '_getPolygon', '_getPolygonRing', 'appendSymbolLayer', 'asImage', 'bigSymbolPreviewImage', 'changeSymbolLayer', 'clipFeaturesToExtent', 'clone', 'cloneLayers', 'color', 'createSimple', 'defaultSymbol', 'deleteSymbolLayer', 'drawPreviewIcon', 'dump', 'exportImage', 'forceRHR', 'hasDataDefinedProperties', 'insertSymbolLayer', 'layer', 'mapUnitScale', 'opacity', 'outputUnit', 'renderFeature', 'renderHints', 'renderPolygon', 'renderUsingLayer', 'renderVertexMarker', 'setAngle', 'setClipFeaturesToExtent', 'setColor', 'setForceRHR', 'setLayer', 'setMapUnitScale', 'setOpacity', 'setOutputUnit', 'setRenderHints', 'startRender', 'stopRender', 'symbolLayer', 'symbolLayerCount', 'symbolLayers', 'symbolRenderContext', 'takeSymbolLayer', 'toSld', 'type', 'usedAttributes']

To access individual symbol layers inside, you could use:

dir(symbol.symbolLayer(0))

>>>['Property', 'PropertyAngle', 'PropertyArrowHeadLength', 'PropertyArrowHeadThickness', 'PropertyArrowHeadType', 'PropertyArrowStartWidth', 'PropertyArrowType', 'PropertyArrowWidth', 'PropertyAverageAngleLength', 'PropertyBlurRadius', 'PropertyCapStyle', 'PropertyCharacter', 'PropertyCoordinateMode', 'PropertyCustomDash', 'PropertyDisplacementX', 'PropertyDisplacementY', 'PropertyDistanceX', 'PropertyDistanceY', 'PropertyFile', 'PropertyFillColor', 'PropertyFillStyle', 'PropertyGradientReference1IsCentroid', 'PropertyGradientReference1X', 'PropertyGradientReference1Y', 'PropertyGradientReference2IsCentroid', 'PropertyGradientReference2X', 'PropertyGradientReference2Y', 'PropertyGradientSpread', 'PropertyGradientType', 'PropertyHeight', 'PropertyHorizontalAnchor', 'PropertyInterval', 'PropertyJoinStyle', 'PropertyLayerEnabled', 'PropertyLineAngle', 'PropertyLineDistance', 'PropertyName', 'PropertyOffset', 'PropertyOffsetAlongLine', 'PropertyOffsetX', 'PropertyOffsetY', 'PropertyOpacity', 'PropertyPlacement', 'PropertyPreserveAspectRatio', 'PropertySecondaryColor', 'PropertyShapeburstIgnoreRings', 'PropertyShapeburstMaxDistance', 'PropertyShapeburstUseWholeShape', 'PropertySize', 'PropertyStrokeColor', 'PropertyStrokeStyle', 'PropertyStrokeWidth', 'PropertyVerticalAnchor', 'PropertyWidth', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_renderPolygon', 'angle', 'brushStyle', 'clone', 'color', 'copyDataDefinedProperties', 'copyPaintEffect', 'create', 'createFromSld', 'dataDefinedProperties', 'drawPreviewIcon', 'dxfAngle', 'dxfBrushColor', 'dxfBrushStyle', 'dxfColor', 'dxfCustomDashPattern', 'dxfOffset', 'dxfPenStyle', 'dxfWidth', 'enabled', 'estimateMaxBleed', 'fillColor', 'hasDataDefinedProperties', 'isCompatibleWithSymbol', 'isLocked', 'layerType', 'mapUnitScale', 'offset', 'offsetMapUnitScale', 'offsetUnit', 'ogrFeatureStyle', 'outputUnit', 'paintEffect', 'penJoinStyle', 'prepareExpressions', 'properties', 'propertyDefinitions', 'renderPolygon', 'renderingPass', 'restoreOldDataDefinedProperties', 'setAngle', 'setBrushStyle', 'setColor', 'setDataDefinedProperties', 'setDataDefinedProperty', 'setEnabled', 'setFillColor', 'setLocked', 'setMapUnitScale', 'setOffset', 'setOffsetMapUnitScale', 'setOffsetUnit', 'setOutputUnit', 'setPaintEffect', 'setPenJoinStyle', 'setRenderingPass', 'setStrokeColor', 'setStrokeStyle', 'setStrokeWidth', 'setStrokeWidthMapUnitScale', 'setStrokeWidthUnit', 'setSubSymbol', 'startRender', 'stopRender', 'strokeColor', 'strokeStyle', 'strokeWidth', 'strokeWidthMapUnitScale', 'strokeWidthUnit', 'subSymbol', 'toSld', 'type', 'usedAttributes', 'writeDxf']

Near the end of this list of properties, we can see there's settings for setBrushStyle and setStrokeColor. The setBrushStyle requires Qt.BrushStyle to set a brush style, we can check the properties inside Qt using:

dir(Qt)

So doing a little search, we can find brush patterns such as BDiagPattern or FDiagPattern


So now we can apply the relevant settings:

layer = iface.activeLayer()
single_symbol_renderer = layer.renderer()
symbol = single_symbol_renderer.symbol()
#Set fill colour
symbol.setColor(QColor.fromRgb(255,128,0))
#Set fill style
symbol.symbolLayer(0).setBrushStyle(Qt.BrushStyle(Qt.FDiagPattern))
#Set stroke colour
symbol.symbolLayer(0).setStrokeColor(QColor(255,128,0))
#Refresh
layer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(layer.id())
  • 1
    That worked perfectly! Thanks for the extended answer, that will be very usefull for setting other symbologies! Cheers – SaskiaG Aug 12 at 6:18
  • @SaskiaG - Most welcome! Glad it helped :) – Joseph Aug 12 at 13:06

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.