3

I am trying to execute the processing algorithm extractbylocation. Anyway, firstly I run that algorithm in QGIS3 and then just copy the code from the processing history and changes paths to variables. I have some features with invalid geometries (in QGIS in the settings it is set for processing to ignore that features),so that is maybe my problem. In order to avoid that I deleted all the features with invalid geometries and the problem is still there. So my code looks like this:

input_path = 'C:/Users/file.shp'
output_path = 'C:/Users/file_out.shp
crs = QgsCoordinateReferenceSystem("epsg:4326")
input = QgsVectorLayer(input_path, "links", "ogr")
input.setCrs(crs)

#deleting features with invalid geometries
for f in input.getFeatures():
    geom = f.geometry()
    if geom.isGeosValid():
        print('valid')
    else:
        input.deleteFeature(f.id())
        print('not valid')

sys.path.append('C:/Program Files/QGIS 3.4/apps/qgis/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

processing.run("native:extractbylocation",
               {'INPUT': input, 'PREDICATE': [1],
                'INTERSECT': 'C:/Users/points.shp', #I also tried to put here the QgsVectorLayer object instead of path
                'OUTPUT': outputh_path})

I got the error next to the output line in the processing algorithm.

 File "path_to_my_file_py", line 49,    'OUTPUT': outputh_path})
 File "path_to_my_file_py", line 84, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
 File "path_to_processing_file_py", line 194, in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: There were errors executing the algorithm.

And btw this code works at python console in qgis.

  • Double check that input.isValid(). You may be missing environment variable paths preventing qgis from loading layers correctly – ndawson Feb 25 at 18:30
  • It's not the issue. :/ Input is valid and the rest of the processing algorithms work in a standalone application. I run that part of the code in qgis script and everything was good. Btw I am using PyCharm. – Neven Feb 27 at 12:17
  • the variable output path = 'C: / Users / file_out.shp needs to close the quotes – Fran Raga Feb 28 at 11:23
  • It's not the issue. I accidentally deleted when I deleting the comments in question that I have in this code. – Neven Mar 10 at 22:29
1

I also found my scripts worked fine in the QGIS 3 Python Console, but when run in standalone PyQIS v3 they do not work. I found it does not accept strings as inputs and the only workaround is to keep trying the different field types the algorithm accepts as inputs (like you suggested using QgsVectorLayer), but even then, depending on the algorithm, it still sometimes doesn't work.

You can find all of the different types it accepts by running the command ~~~ processing.algorithmHelp("qgis:extractbylocation") ~~~ in the QGIS Python console.

I have the same problems with developing new standalone scripts in QGIS 3.x which I did not have with QGIs 2.x. I am unsure whether it is my coding or there is a problem with the PyQGIS v3. This is further compounded by a lack of easy to understand documentation on running PyQIS 3 in standalone mode.

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.