1

I am trying to use the "Extract by location" algorithm in a standalone script with QGIS 3.4.7 and Python 3.7. The algorithm, however is not listed in available algorithms as listed with: processingRegistry().algorithms(). Since there have been a lot of changes in QGIS 3, and the docs are not complete, I can't figure out how to use it. The algorithm exists in the app, since I can access it and use it from the QGIS app itself. Of the algorithms that have anything with 'location' or 'select', I can only use:

  • qgis:joinbylocationsummary
  • qgis:joinattributesbylocation
  • qgis:randomselectionwithinsubsets
  • qgis:selectbyattribute
  • qgis:selectbyexpression
  • qgis:extractspecifiedvertices

So I guess the processing tool for "Extract by location" is not using a dedicated algorithm object anymore, and it is using some other tool. Can anyone suggest, how to check what method/class/object is used to run "Extract by location" in QGIS 3?

In QGIS 2.x the algorithm was used with processing.runalg(...) but this is not used anymore, so all guides for running algorithms with python now do not apply.

My code starts with

qgs = QgsApplication([], False)
qgs.setPrefixPath(r"D:\QG\qgis-ltr", True)
qgs.initQgis()

sys.path.append(r'D:\QG\qgis-ltr\python\plugins')
from processing.core import Processing
Processing.Processing.initialize()

I use this code to list the algorithms:

qgs.processingRegistry().algorithms()

And this code to run one:

Processing.Processing.runAlgorithm('qgs:extractbylocation', <args>) 

Which throws:

    raise QgsProcessingException(msg)
_core.QgsProcessingException: Error: Algorithm qgs:extractbylocation not found

as if the algorithm was not available.

The QGIS app allows to use it with no problem. . enter image description here

Noura
  • 3,429
  • 3
  • 20
  • 41
  • See Using QGIS3 Processing algorithms from standalone PyQGIS scripts (outside of GUI) and add the native algorithms to your Processing. Then you can access that algorithm as natve:extractbylocation. – Germán Carrillo Apr 02 '20 at 13:20
  • @German Carrillo thank you, and I've seen that topic before, but I didn't understand the distinction between native and non-native algorithms. Please produce an naswer so I can mark one. – Sane_or_not Apr 02 '20 at 13:28
  • Native algorithms are written in C++. In your standalone Python script you need to add them manually after importing Processing. Otherwise, you won't get access to any native (C++) algorithm. The post I mentioned in the previous comment says even more, giving you an example. After adding the Native provider, you can run the extract by location algorithm like this: processing.run('native:extractbylocation', params). See what parameters are required using processing.algorithmHelp("native:extractbylocation"). – Germán Carrillo Apr 02 '20 at 13:45

0 Answers0