Saturday, 17 December 2016

pyqgis - How to run Processing qgis:extractbylocation?


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 "", line 1, in
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
INTERSECT
PREDICATE

OUTPUT

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?



Answer



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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...