3

I try to use the processing 'qgis:extractbylocation' in QGIS 2.8.2 from the QGIS algorithm provider.

import processing
# import the layers by a given uri
inputlayer = QgsVectorLayer(uriTr.uri(), "inputlayer", "postgres")
intersectlayer = QgsVectorLayer(uriOSM.uri(), "intersectlayer", "postgres")

# initialize a predicate instance
pred_intersects = processing.core.parameters.ParameterGeometryPredicate(enabledPredicates = 'intersects')
# run the processing
processing.runalg('qgis:extractbylocation', inputlayer, intersectlayer, pred_intersects, None)

However I get the following error:

Traceback (most recent call last):
    File "<input>", line 1, in <module>
    File "~/.qgis2/python/plugins/processing/tools/general.py", line 71, in runalg
        alg = Processing.runAlgorithm(algOrName, None, *args)
    File "~/.qgis2/python/plugins/processing/core/Processing.py", line 317, in runAlgorithm
        if not param.setValue(args[i]):
    File "~/.qgis2/python/plugins/processing/core/parameters.py", line 829, in setValue
        elif len(value) == 0:
AttributeError: ParameterGeometryPredicate instance has no attribute '__len__'

The algorithm needs the following inputs

processing.alghelp("qgis:extractbylocation")
     ALGORITHM: Extract by location
     INPUT <ParameterVector>
     INTERSECT <ParameterVector>
     PREDICATE <ParameterGeometryPredicate>
     OUTPUT <OutputVector>

ParameterGeometryPredicate is described here: https://fossies.org/dox/qgis-2.8.2/classprocessing_1_1core_1_1parameters_1_1ParameterGeometryPredicate.html

How do I need to initialize a correct instance of the ParameterGeometryPredicate class?

underdark
  • 84,148
  • 21
  • 231
  • 413
telemachos
  • 71
  • 1
  • 7

3 Answers3

4

The predicate is u'intersects'. For more than one predicate use an array like "[u'within',u'intersects']"

The full command is

processing.runalg('qgis:extractbylocation', inputlayer, intersectlayer, u'intersects', None)

The possible arguments for the predicates are:

[u'disjoint',u'intersects',u'contains',u'equals',u'touches',u'overlaps',u'within',u'crosses']

The similar algorithm selectbylocation is used like

processing.runalg('qgis:selectbylocation', inputlayer, intersectlayer, u'intersects', 0)
telemachos
  • 71
  • 1
  • 7
3

Run the "Extract by location" command of the Toolbox and examine the last line of the ../.qgis2/processing/processing.log file (that records all the commands executed)

ALGORITHM|Sun Jun 14 2015 17:34:39|
processing.runalg("qgis:extractbylocation","/Users/Shared/result.shp","/Users/Shared/input.shp","['intersects']",None)

The predicate is simply "['intersects']"

gene
  • 54,868
  • 3
  • 110
  • 187
  • This does not work. In the script /.qgis2/python/plugins/processing/algs/qgis/SelectByLocation.py there is the loop for predicate in predicates: This loop just iterates over the single chars of "['intersects']". You won't get an error, but no selected features either. – telemachos Jun 15 '15 at 13:04
0

this works for me:

['intersects']