Thursday 18 June 2015

pyqgis - How can I get the path for an input layer in a QGIS 3 script?


Writing scripts in previous versions of QGIS, I would get the layer path to pass to algorithms using processing.getObject(layer). In QGIS 3, the scripting system has been rewritten, and I now define a 'source' for input layers like this:


SPECIES_POINTS = 'SPECIES_POINTS'      # INPUT = 'INPUT'

[...]

speciesPointSource = self.parameterAsSource(

parameters,
self.SPECIES_POINTS,
context
)

I then try to pass it to an algorithm:


pointsReprojected = processing.run("native:reprojectlayer", {
'INPUT': speciesPointSource,
'TARGET_CRS': CRS,
'OUTPUT' : speciesExtent_id

}, context=context, feedback=feedback)['OUTPUT']

but when I try to run this, I get an error:


Unable to execute algorithm
Could not load source layer for INPUT: invalid value
Traceback (most recent call last):
File "", line 191, in processAlgorithm
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\tools\general.py", line 96, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\Processing.py", line 142, in runAlgorithm

raise QgsProcessingException(msg)
_core.QgsProcessingException: Unable to execute algorithm
Could not load source layer for INPUT: invalid value

I can see that speciesPointSource has type QgsFeatureSource, so I'm not surprised this doesn't work, but I can't see any attributes that would give me the file path for the layer. I have also tried self.SPECIES_POINTS but then I get the error Could not load source layer for INPUT: SPECIES_POINTS not found. What should I be passing as 'INPUT'?




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...