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):
layer.exportNamedStyle(d)
s = d.toString()
root = ET.fromstring(s)
for attr in root.iter('renderer-v2'):
classified_attribute=attr.attrib['attr']
i=0
for elem in root.iter('range'):
class_pop=[]
lower = elem.attrib['lower']
upper = elem.attrib['upper']
if i==0:
features = layer.getFeatures('"{}" >= {} AND "{}" <= {}'.format(classified_attribute, lower, classified_attribute, upper))
else:
features = layer.getFeatures('"{}" > {} AND "{}" <= {}'.format(classified_attribute, lower, classified_attribute, upper))
i+=1
for feature in features:
sumAttribute = feature[attribute]
class_pop.append(sumAttribute)
print('{},{},{},{}\n'.format(classified_attribute, lower, upper, sum(class_pop)))
aggregate('pointlayer','sum',"population","distance"<1000)
? Just adjust the distance for each symbology class.