2

I exported a model as script using the properly option in QGIS 3.8. The Script works perfectly, but the output name is changed.

Heres a print of the model:

enter image description here

I want that the script´s output name, from the Processing Toolbox, be the one that I designed in the def initAlgorithm section, and not the given default name.

In the model, the output name comes as "output_name" and in the script, the output name comes as "Reprojected".

Here's the converted Script:

    from qgis.core import QgsProcessing
    from qgis.core import QgsProcessingAlgorithm
    from qgis.core import QgsProcessingMultiStepFeedback
    from qgis.core import QgsProcessingParameterVectorLayer
    from qgis.core import QgsProcessingParameterFeatureSink
    from qgis.core import QgsCoordinateReferenceSystem
    import processing


    class Rename(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('shapeinput', 'ShapeInput', types=[QgsProcessing.TypeVector], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Output_name', 'output_name', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}

        # Reproject Layer
        alg_params = {
            'INPUT': parameters['shapeinput'],
            'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
            'OUTPUT': parameters['Output_name']
        }
        outputs['ReprojectLayer'] = processing.run('native:reprojectlayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Output_name'] = outputs['ReprojectLayer']['OUTPUT']
        return results

    def name(self):
        return 'rename'

    def displayName(self):
        return 'rename'

    def group(self):
        return 'rename'

    def groupId(self):
        return ''

    def createInstance(self):
        return Rename()
1

If you need change the name of output, you can do it with this:

alg_params = {
        'INPUT': parameters['shapeinput'],
        'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
        'OUTPUT': 'memory:Name_layer'
    }

or if you want to save, only do this:

path = '/home/shade/Desktop/name_layer.shp' #or something like that

alg_params = {
        'INPUT': parameters['shapeinput'],
        'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
        'OUTPUT': path
    }

For example:

I run the next code:

layer = iface.activeLayer()

alg_params = {
    'INPUT': layer,
    'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
    'OUTPUT': 'memory:Name_layer'
}

result = processing.run('native:reprojectlayer', alg_params)

print ('This is the result: {}'.format(result['OUTPUT']))
print ('This is the name of the layer result: {}'.format(result['OUTPUT'].name()))

And obtained this:

enter image description here

  • The first option generated no result. But processed OK – Herbert Santos Sep 5 at 14:39
  • In this case results['Output_name'] saved the qgsvectorlayer, if you need the name, use results['Output_name'].name() – Jhon Galindo Sep 5 at 14:46
  • 1
    @JhonGalindo - I think the OP means that if you run the script from the Processing Toolbox, the output name is given the default name. Your code works from the Python Console, now test if it works by converting it into a script which you run from the toolbox. – Joseph Sep 6 at 9:54
  • 1
    Exactly! That is what I mean. Results from the toolbox, not from the console. – Herbert Santos Sep 6 at 11:58
  • 1
    thanks @Joseph , I will do it and I will try to show the answer here. – Jhon Galindo Sep 6 at 15:52

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.