5

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 9, 2015 at 13:03
6
  • Welcome to GIS:SE Linda! Are you sure the algorithm is not qgis:joinbylocation which is what I have? Commented Jun 9, 2015 at 13:09
  • Thanks! Using qgis:joinbylocation does not work for me ("Error: Algorithm not found"). Are you using QGIS 2.8.2? Commented Jun 9, 2015 at 13:18
  • Ignore my previous comment, I didn't update the processing plugin. I always forget this! Commented Jun 9, 2015 at 13:21
  • Similar problem here too as it also doesn't work through the modeler. Commented Jun 9, 2015 at 14:12
  • Most welcome, as mentioned in a comment by @mike from the post I linked, downgrading the processing plugin to 2.2 and using the 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! Commented Jun 9, 2015 at 14:33

1 Answer 1

2
  • 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")

answered Jan 25, 2016 at 14:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.