I have a workflow where a raster should be polygonized and afterwards simplified. The simplified vector should be saved as a shapefile. I am using the template script from QGIS to create a processing tool.
I am getting the following error:
Could not load source layer for INPUT: memory: not found
There might be other errors as well. I don't understand what to write in OUTPUT: in any of the processing.run
tools.
Furthermore, I am not sure when it is necessary to instance variables (e.g. in_raster = self.parameterAsRasterLayer(...)
and I am not sure about the return at the end of the script.
class ExampleProcessingAlgorithm(QgsProcessingAlgorithm):
INPUT_RASTER = 'INPUT_RASTER'
OUTPUT_POLYGON = 'OUTPUT_POLYGON'
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterRasterLayer(
self.INPUT_RASTER,
self.tr('Input raster')
)
)
self.addParameter(
QgsProcessingParameterVectorDestination(
self.OUTPUT_POLYGON,
self.tr('Output layer')
)
)
def processAlgorithm(self, parameters, context, feedback):
in_raster = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)
#in_fs_limit = self.parameterAsDouble(parameters, self.THRESHOLD, context)
#out_polygon = self.parameterAsOutputLayer(parameters, self.OUTPUT_POLYGON, context)
polygonized_output = processing.run(
"gdal:polygonize",
{
'INPUT':in_raster,
'BAND':1,
'FIELD':'DN',
'EIGHT_CONNECTEDNESS':False,
'EXTRA':'',
'OUTPUT':'memory:'
}
)['OUTPUT']
simplified = processing.run(
"native:simplifygeometries",
{
'INPUT':polygonized_output,
'METHOD':0,
'TOLERANCE':350,
'OUTPUT':parameters['OUTPUT_POLYGON']
},
context=context,
feedback=feedback
)['OUTPUT']
return {self.OUTPUT_POLYGON: simplified}