3

My QGIS projects always have around 20 layers that need to be Categorized by a certain value every day. Right now, I need to manually hit the "delete" and "classify" buttons. Fine for a while, but tiresome after 6 months, I want to automate this process but don't know where to start. Could you give me some insight? Symbology-LayerConfigured

3
  • 1
    Can you provide example data and describe how the data changes? Are the changes random, or within a certain range? Are the values numeric, or strings?
    – Erik
    May 3 at 12:15
  • It has a fixed range and values. The data is a string. Example data is like: ID | << Something in between>> | Validation data. The Validation data changes daily after fixing/improving the previous one.
    – Data2Logic
    May 4 at 2:27
  • 1
    That's not really example data, since we still don't know, how the validation looks like. If you had integers ranging from 1 to 10 you simply could classify once for those 10 classes and be done with it.
    – Erik
    May 4 at 7:12

1 Answer 1

1

If the field name of the layers is the same you can create a list of the 20 layers you have to update and use this script of this answer by @Ben W like this :

    field_name = "name_of_your_field"
    layers_name = ["layer_name1", "layer_name2", "layer_name3"]
    for layer_name in layers_name:
        layer = QgsProject.instance().mapLayersByName(layer_name)[0]
        field_index = layer.fields().lookupField(field_name)
        unique_values = list(layer.uniqueValues(field_index))
        categories = []
        for value in sorted(unique_values):
            symbol = QgsSymbol.defaultSymbol(layer.geometryType())
            category = QgsRendererCategory(value, symbol, str(value))
            categories.append(category)
        renderer = QgsCategorizedSymbolRenderer(field_name, categories) 
    
            
        layer.setRenderer(renderer)
        layer.triggerRepaint()

If the field name is different you can create a dictionary like this :

    layers_fields_name = {"layer_name1":"field_name1", "layer_name2":"field_name2", "layer_name3":"field_name3"}
    for layer_name in layers_fields_name:
        layer = QgsProject.instance().mapLayersByName(layer_name)[0]
        field_name = layers_fields_name[layer_name]
        field_index = layer.fields().lookupField(field_name)
        unique_values = list(layer.uniqueValues(field_index))
        categories = []
        for value in sorted(unique_values):
            symbol = QgsSymbol.defaultSymbol(layer.geometryType())
            category = QgsRendererCategory(value, symbol, str(value))
            categories.append(category)
        renderer = QgsCategorizedSymbolRenderer(field_name, categories) 
    
            
        layer.setRenderer(renderer)
        layer.triggerRepaint()

Every time you launch the script, the style is deleted, recreated and applied to your layers.

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.