3

In QGIS 3.4.7 I have a script that I use in the toolbox and one of the steps is to store the name of the input tiff in a variable. I accomplish this perfectly with this part of the code:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterRasterLayer
        
class tiffname(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):    
        self.addParameter(QgsProcessingParameterRasterLayer('tiff', 'TIFF', defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        feedback = QgsProcessingMultiStepFeedback(25, model_feedback)
     
        NAMETIFF = parameters['tiff'].name()
        
        feedback.pushInfo(NAMETIFF)
        
        return {}

    def name(self):
        return 'tiffname'

    def displayName(self):
        return 'tiffname'

    def tr(self, string):
        return QCoreApplication.translate('Processing2', string)

    def createInstance(self):
        return tiffname()

However in QGIS 3.14 I get this error:

NAMETIFF = parameters['tiff'].name()
AttributeError: 'str' object has no attribute 'name'

How to call name() in this version?

1 Answer 1

1

I am testing your script and this work for me: NAMETIFF = str(parameters['tiff'])

And when run the code finish without errors.

1
  • 1
    Great. But with a file called "raster" the answer is raster_7cab7d12_876f_4143_8898_21612deacb72. Any other idea? Oct 3, 2020 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.