I was wondering if there is an existing tool/way to aggregate an attribute based on the active symbology of a layer.

For instance, if I have a point layer with population and distance from the city center for each point and I create a specific symbology for the distance (let's say 0-1km, 1-5km 5++km) to get the total number of people within each class.

I would like to avoid creating dummy reclassified attributes if possible.

  • Are you talking about something like aggregate('pointlayer','sum',"population","distance"<1000)? Just adjust the distance for each symbology class.
    – MrXsquared
    Commented Jul 6, 2020 at 11:57
  • yeah, something like this, but i want it to read directly from the symbology so that i don't have to it manually.
    – vagvaf
    Commented Jul 6, 2020 at 12:09
  • What do you mean with reading data from the symbology? Your symbology is based on a field or on an expression?
    – Val P
    Commented Jul 6, 2020 at 12:56
  • on a specific field
    – vagvaf
    Commented Jul 6, 2020 at 13:37
  • So you want to aggregate based on a specific field, not on the symbology, did I understand you right? Would using the aggregated algorithm solve your problem? See the documentation here: docs.qgis.org/3.10/en/docs/user_manual/processing_algs/qgis/…
    – Babel
    Commented Jul 6, 2020 at 14:36

1 Answer 1


So this is more or less what I did. It was fine for me to just have the results printed in the terminal.

The symbology was drawn from a qml file and I don't know if this works with ad-hoc symbologies.

Finally, I am sure that there is a way to integrate the aggregate function, but for I just needed to sum.

from PyQt5.QtXml import QDomDocument
import xml.etree.ElementTree as ET

def sumBySymbologyClass(layer, attribute):
    s = d.toString()
    root = ET.fromstring(s)

    for attr in root.iter('renderer-v2'):
    for elem in root.iter('range'):
        lower = elem.attrib['lower']
        upper = elem.attrib['upper']
        if i==0:
            features = layer.getFeatures('"{}" >= {} AND "{}" <= {}'.format(classified_attribute, lower, classified_attribute, upper))
            features = layer.getFeatures('"{}" > {} AND "{}" <= {}'.format(classified_attribute, lower, classified_attribute, upper))
        for feature in features:
            sumAttribute = feature[attribute]
        print('{},{},{},{}\n'.format(classified_attribute, lower, upper, sum(class_pop)))

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.