2

I am having trouble with my code for selecting all points within a buffer layer, with the error given below. I am using this documentation as reference for the field formats: https://docs.qgis.org/2.6/en/docs/user_manual/processing_algs/qgis/vector_selection_tools/selectbylocation.html . I also tried the solution posted here How to use qgis:selectbylocation in PyQGIS? . Is there something that I'm missing?

processing.runalg("qgis:selectbylocation", 
                  "/Users/pgcseismolab/Desktop/qgis/moment_all.shp",
                  "/Users/pgcseismolab/Desktop/jaden_pgc_archive/Blast_Files/Blast_buffers/blast_induced_buff.shp", 
                  False, 
                  False, 
                  False, 
                  0)

Error: Wrong number of parameters
ALGORITHM: Select by location
    INPUT <ParameterVector>
    INTERSECT <ParameterVector>
    PREDICATE <ParameterGeometryPredicate>
    PRECISION <ParameterNumber>
    METHOD <ParameterSelection>
    OUTPUT <OutputVector>

METHOD(Modify current selection by) 0 - creating new selection 1 - adding to current selection 2 - removing from current selection

EDIT: I am using QGIS 2.14, and have updated my code as follows (with the error below). Following documentation posted here: http://docs.qgis.org/2.14/en/docs/user_manual/processing_algs/qgis/vector_selection_tools.html?highlight=selectbylocation

processing.runalg("qgis:selectbylocation", 
                  "/Users/pgcseismolab/Desktop/qgis/moment_all.shp", 
                  "/Users/pgcseismolab/Desktop/jaden_pgc_archive/Blast_Files/Blast_buffers/blast_induced_buff.shp", 
                  'within', 
                  0)

Error: Wrong number of parameters
ALGORITHM: Select by location
    INPUT <ParameterVector>
    INTERSECT <ParameterVector>
    PREDICATE <ParameterGeometryPredicate>
    PRECISION <ParameterNumber>
    METHOD <ParameterSelection>
    OUTPUT <OutputVector>
2

You can always use the Processing algorithm from QGIS GUI and then go to Processing -> History to see how to run the algorithm in QGIS Python Console.

processing.runalg("qgis:selectbylocation", 
    "/Users/pgcseismolab/Desktop/qgis/moment_all.shp", 
    "/Users/pgcseismolab/Desktop/jaden_pgc_archive/Blast_Files/Blast_buffers/blast_induced_buff.shp", 
    ['within'], 
    0, 
    0)

Note that:

  • PREDICATE parameter should be passed as a Python array of strings: ['within'] instead of 'within'.
  • You're missing the second to last parameter called PRECISION, which in the command above is 0. Of course, you can adjust it according to your scenario.
  • These 2 additions resolved my issue- thank you! The code in your answer worked perfectly. I didn't know about the processing -> history option; I'll certainly be using this more often :) – jrowley Jul 17 '17 at 20:56
  • You're welcome. Right, that's a very handy log. – Germán Carrillo Jul 17 '17 at 20:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.