I have a shapefile with points, and a shapefile with polygons. I want to sum the attribute value of all points that are within a certain polygon, and I'd like to do this for each polygon in my dataset. QGIS Join Attributes by Location tool is helping me to achieve this, and is working perfectly when using the tool via the Processing Toolbox. However, I would like to include the tool in an automated script. I tried many different arguments, but my code is (still) not working, and results in a dataset where the new polygon attribute has NULL values.
I'm using the following code, which should be exactly the same as the tool is running when I'm accessing it from the Processing Toolbox (I even tried copying and running the exact same code from the History & Log window):
sum = processing.runalg("qgis:joinattributesbylocation","polygonlayer.shp",
"pointlayer.shp","['contains']",1,"sum",1,None)
I'm using QGIS 2.8.2 Wien.
1 Answer 1
For QGIS 2.8.2 with Processing plugin 2.9.0:
processing.runalg("qgis:joinattributesbylocation", layer1, layer2, u'contains', 1, 'sum', 1, None)
For QGIS 2.12.2 with Processing plugin 2.12.2:
processing.runalg("qgis:joinattributesbylocation", layer1, layer2, u'contains', 0, 1, 'sum', 1, None)
(note the extra parameter for "precision")
qgis:joinbylocation
which is what I have?qgis:joinbylocation
tool works. I just downgraded it again as I personally consider it very stable even though it has less tools available than the latest versions. You can download the plugin from here and unzip the folder to your C:\Users\You\.qgis2\python\plugins directory. Hope this helps a little anyway!