Symbologies
Simple symbology on points
myLayer = QgsProject.instance().mapLayersByName("My points")[0] symbol_peak = QgsMarkerSymbol.createSimple({'name': 'Triangle', 'color': 'green', 'outline_color': 'black', 'size': '4'}) myLayer.renderer().setSymbol(symbol_peak) myLayer.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myLayer.id()) iface.mapCanvas().refresh()
Other point markers:
circle square cross rectangle diamond pentagon triangle equilateral_triangle star regular_star arrow filled_arrowhead x
Use PNG on points
You can build your own picture as PNG and use it in symbology with QgsRasterMarkerSymbolLayer
:
myPath = r'C:\\Users\\georg\\Downloads\\' project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh() # Path of layers peaks = QgsVectorLayer(myPath + 'peaks_selection/peaks_selection.shp', 'Peaks', 'ogr') project.addMapLayer(peaks) myLayer = QgsProject.instance().mapLayersByName("Peaks")[0] # Use a SVG in symbology myPicture = myPath + 'flower.png' symbolLayer = QgsRasterMarkerSymbolLayer(myPicture) symbolLayer.setSize(10) symbol_peak = QgsMarkerSymbol.createSimple({}) symbol_peak.changeSymbolLayer(0, symbolLayer) myLayer.renderer().setSymbol(symbol_peak) myLayer.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myLayer.id()) iface.mapCanvas().refresh()
Use SVG on points
Use QgsSvgMarkerSymbolLayer
, but attention, SVG is a very specific format, maybe prefer PNG.
myPath = r'C:\\Users\\georg\\Downloads\\' project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh() # Path of layers peaks = QgsVectorLayer(myPath + 'peaks_selection/peaks_selection.shp', 'Peaks', 'ogr') project.addMapLayer(peaks) myLayer = QgsProject.instance().mapLayersByName("Peaks")[0] # Use a SVG in symbology mySvg = 'C:/Program Files/QGIS 3.28.13/apps/qgis-ltr/resources/themes/Night Mapping/icons/eye.svg' svg_symbol_layer = QgsSvgMarkerSymbolLayer(mySvg) svg_symbol_layer.setSize(10) symbol_peak = QgsMarkerSymbol.createSimple({}) symbol_peak.changeSymbolLayer(0, svg_symbol_layer) myLayer.renderer().setSymbol(symbol_peak) myLayer.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myLayer.id()) iface.mapCanvas().refresh()
Simple symbology on lines
With HTML color.
myroads = QgsProject.instance().mapLayersByName("Roads")[0] myrivers = QgsProject.instance().mapLayersByName("Rivers")[0] # Roads symbol = QgsLineSymbol.createSimple({'line_style': 'dash', 'line_width': '0.5', 'color': 'black'}) myroads.renderer().setSymbol(symbol) myroads.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myroads.id()) # Rivers symbol = QgsLineSymbol.createSimple({'line_style': 'solid', 'line_width': '0.75', 'color': '#0088CC'}) myrivers.renderer().setSymbol(symbol) myrivers.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myrivers.id()) iface.mapCanvas().refresh()
Simple symbology on polygons
With RGB color (50,165,77,75
), and some transparency (here 75, from 0 to 255), et on rafraîchit le panneau des couches afin que notre légende y soit aussi prise en compte:
myground = QgsProject.instance().mapLayersByName("Ground")[0] symbol = QgsFillSymbol.createSimple({'line_style': 'solid', 'line_width': '0.2', 'color': '50,165,77,75'}) myground.renderer().setSymbol(symbol) myground.triggerRepaint() iface.layerTreeView().refreshLayerSymbology(myground.id()) iface.mapCanvas().refresh()
Check current symbology properties
my_layer = QgsProject.instance().mapLayersByName("My points")[0] print(my_layer.renderer().symbol().symbolLayers()[0].properties())
Categorized Symbology
For a categorized symbology, we will need to define several symbologies, then assign them based on attribute values.
Here we put all our peaks in blue triangle (▲, then we will look for a single peak based on its name (My best peak, in the NAME field). This will change to a dark blue triangle and larger size (▲).
symbol_peak = QgsMarkerSymbol.createSimple({'name': 'Triangle', 'color': '#0088CC', 'outline_color': 'black', 'size': '4'}) symbol_peak_selected = QgsMarkerSymbol.createSimple({'name': 'Triangle', 'color': 'blue', 'outline_color': 'black', 'size': '7'}) color_peak = QgsRendererCategory(None, symbol_peak, 'Peaks', True) color_peak_selected = QgsRendererCategory('My best peak', symbol_peak_selected, 'My best peak', True) renderer = QgsCategorizedSymbolRenderer("NAME", [color_peak,color_peak_selected]) mylayer.setRenderer(renderer) mylayer.triggerRepaint() iface.mapCanvas().refresh()
Transparency on a basemap (OSM)
osm = QgsRasterLayer(urlWithParams, "OpenStreetMap", "wms") project.addMapLayer(osm) osm.setOpacity(0.75) iface.mapCanvas().refresh()