2

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

Your Answer

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

Browse other questions tagged or ask your own question.