QGIS – Summing Values from Differently Intersecting or Overlapping Points in QGIS

attribute-tablefields-attributespyqgisqgis

I am currently digitizing an old archaeological excavation. I have created the 2m grid where finds were excavated, and have created centroids within each grid. All the corresponding finds has then been joined to the centroids and saved as a new point layer:

enter image description here

The point layer contains multiple points stacked upon each other, because they share the same "local coordinates" ie. 450/560.

What I want to do, is to sum the amount of "something" within between different columns. 2]
Yellow marks "Amount" and where two points share the same coordinates

So I should like to merge all rows with the same X and Y values and have their amount summed.

I should in the end, like to end up with a new polygon layer, where each m2 contains the SUM of all the finds from that specific m2, based on different query's. So it will be possible to graduate colors based on finds intensity

Best Answer

You may use this code as a new script from Processing Toolbox (from the main QGIS Window, go to Processing and then click on Toolbox. From the Processing toolbox (just activated on the right side of the main QGIS window), go to Create new script menu under the Tools group in the Scripts algorithms block of the toolbox. Then, copy & paste my code):

##Centroids=vector point
##Polygon=vector polygon

from qgis.core import *
from qgis.PyQt.QtCore import QVariant

p_layer = processing.getObject(Centroids)
crs = p_layer.crs().toWkt()

poly_layer = processing.getObject(Polygon)

# Create the output layer
outLayer = QgsVectorLayer('Polygon?crs='+ crs, 'Amounts' , 'memory')
prov = outLayer.dataProvider()
fields = poly_layer.pendingFields()
fields.append(QgsField('Amounts', QVariant.Int, '', 10, 0)) # Name for the new field in the output layer
prov.addAttributes(fields)
outLayer.updateFields()

all_points = {}
index = QgsSpatialIndex()
for ft in p_layer.getFeatures():
    if ft["Amount"]:
        index.insertFeature(ft)
        all_points[ft.id()] = ft["Amount"]

for feat in poly_layer.getFeatures():
    geom = feat.geometry()
    attrs = feat.attributes()
    idsList = index.intersects(geom.boundingBox())
    attrs.append(sum(all_points[x] for x in idsList))
    outFeat = QgsFeature()
    outFeat.setAttributes(attrs)
    outFeat.setGeometry(geom)
    prov.addFeatures([outFeat])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

The result will be a new polygon memory layer which stores the same original attributes, plus one additional field containing the required sum.

If you want to change the field for the sum, slightly edit the code where it's specified (i.e. the ft["Amount"] line).