I'm writing some scripts in QGIS 3 to automate processing using a number of the inbuilt algorithms. I'm using memory layers to store the intermediate results of the algorithms. I'm looking for a simple way to persist the layer into the layer list in the project once the algorithms have been executed.
At the moment, the only way I can work out how to persist the final processed layer is to use one of the algorithms (such as native:merge) as a post-processing step. The issue I have is that it adds it the the layer list with the name "merged". I would like to be able to define the name that it gets in the layers list.
I've looked at alternatives such as addMapLayer()
but it doesn't add the layer to the project. In the example below I want the layer to be added with the name "layername".
The script is being added as an algorithm under the processing toolbox.
# -*- coding: utf-8 -*-
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
QgsFeatureSink,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsVectorLayer,
QgsProject)
from qgis import processing
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
INPUT1 = 'INPUT1'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def createInstance(self):
return ExampleProcessingAlgorithm()
def name(self):
return 'testpersistence'
def displayName(self):
return self.tr('Testpersistence')
def group(self):
return self.tr('Example scripts')
def groupId(self):
return 'examplescripts'
def shortHelpString(self):
return self.tr("Testing the script")
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.INPUT1,
self.tr('Input layer 1'),
[QgsProcessing.TypeVectorAnyGeometry]
)
)
def processAlgorithm(self, parameters, context, feedback):
source1 = self.parameterAsSource(parameters,self.INPUT1,context)
fixgeometries1_result = processing.run(
'native:fixgeometries',
{
'INPUT': parameters['INPUT1'],
'OUTPUT': 'memory:'
},
is_child_algorithm=True,
context=context,
feedback=feedback)
uri = fixgeometries1_result['OUTPUT'].dataProvider().dataSourceUri()
layer1 = QgsVectorLayer(uri,"layername","memory")
QgsProject.instance().addMapLayer(layer1)
return {}
The code fails with an error:
AttributeError: 'str' object has no attribute 'dataProvider'
QgsProcessingAlgorithm
. In either case it should be added as a script to the Processing toolbox, to be available as an algorithm. Is that what you are doing? Or are you trying to run a script from the Python console?