I'm using QGIS 3.10 with Python 3.7. I would like to sum/group the length of 5 attributes by an ID and write the result in an other layer (to an attribute with the same ID).

Example: I have two layers: one layer with houses and one with lines. Every house has 5 lines. Both layers have a field with an ID, so I can assign the lines to the houses. I would like to sum/group the length of the 5 lines by the field "id" and write the result in a field of the house-layer. I have a very large data set, so I need a solution with the shortest possible processing time.

my code (doesn't work / too long processing time (the computer crashes)):

for a in house_layer.getFeatures():
    houseID_a = a['id']                     
    dist_5 = 0
    for b in line_layer.getFeatures():
        lineID_b = b['id']
        if lineID_b == houseID_a:
            dist = b['length']
            dist_5 += dist
        a['sum_dist_5'] = dist_5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.