4

I want to be able to categorize my layer by a field called 'Location'. The sole purpose of this is to quickly be able to toggle sections of my layer on and off as I am creating multiple map figures. Only some sections of my layer are relevant for specific figures.

Is there a way to create categories that are styled the exact same way (i.e., same colour and same symbol)?

My marker is not a single colour, so making a colour ramp only with one colour (i.e., green) won't work (as it will change the white parts of my symbol to the green colour).

enter image description here

I also know you can use rule-based labelling, but I'm purely asking so I can have a quick solution to categorizing my layer (rather than manually changing each symbol).

2
  • You could work with filters instead of categories. Or you could style one symbol to your liking, then right-click and copy it, then paste it to all other categories.
    – Erik
    Commented Apr 24, 2023 at 9:37
  • You can duplicate you layer multiple times and use a different definition query on each; then turn the layers on or off as needed. You could also go into settings and change the color of selected features to show the way you want, and then manually select those in the area you are working to stand out.
    – John
    Commented Apr 24, 2023 at 10:33

2 Answers 2

3

Set the symbol and use this code to change the styling to categorized and apply the old symbol to all new categories:

#The layer name and field used to categorize
layer = QgsProject.instance().mapLayersByName("pointlayer123")[0] #Change to match your layer name
category_field = "objekttypnr" #And category field

#Find all unique values in category field
uniquevals = list({f[category_field] for f in layer.getFeatures()})

#Create a new renderer object
new_renderer = QgsCategorizedSymbolRenderer(attrName=category_field)

#Fetch the current layer symbol
oldsymbol = layer.renderer().symbol()

for val in uniquevals: #For each unique value
    qrc = QgsRendererCategory() #Create a render category object
    qrc.setLabel(str(val)) #Set the label (whats shown in layer tree)
    qrc.setValue(val) #Set the category value 
    qrc.setSymbol(oldsymbol.clone()) #Clone the old symbol
    new_renderer.addCategory(qrc) #Add the render category object to the new renderer
layer.setRenderer(new_renderer)

enter image description here

QgsCategorizedSymbolRenderer QgsRendererCategory

2

If you want to stick with graduated symbols, you can right click the 1st symbol, copy the style, then right click every other symbol (one by one) and paste the style.

On the other hand, you can switch to rule based symbols, which will automatically create a rule for each of the class you had in the graduated symbology. Select all rules, right click, paste symbol to set all of them at once.

enter image description here

1
  • Seems like the easiest way without any code necassary
    – ibdmn
    Commented Apr 24, 2023 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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