Nyall Dawson 4bcc9df5b4 [processing] Add explicit output type for multiple layers
This was a missing capability in the processing API - while algorithms
could declare multiple layer input parameters, there was no corresponding
multi-layer output. This meant that algorithms (such as Package Layers,
Vector Split) which create a set of layers which cannot be determined
in advance had no way to pass these generated layers on for further model
processing steps.

It's also useful for algorithms which operate on a specified folder,
processing all layers found there, and allowing these generated
outputs to be utilised in other model steps (e.g. packaging
all of them, merging them, etc)
2018-02-11 10:57:44 +10:00

102 lines
4.3 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
Output.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import sys
from qgis.core import (QgsExpressionContext,
QgsExpressionContextUtils,
QgsExpression,
QgsExpressionContextScope,
QgsProject,
QgsSettings,
QgsVectorFileWriter,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingOutputRasterLayer,
QgsProcessingOutputVectorLayer,
QgsProcessingOutputMapLayer,
QgsProcessingOutputHtml,
QgsProcessingOutputNumber,
QgsProcessingOutputString,
QgsProcessingOutputFolder,
QgsProcessingOutputMultipleLayers)
def getOutputFromString(s):
try:
if "|" in s and s.startswith("Output"):
tokens = s.split("|")
params = [t if str(t) != "None" else None for t in tokens[1:]]
clazz = getattr(sys.modules[__name__], tokens[0])
return clazz(*params)
else:
tokens = s.split("=")
if not tokens[1].lower()[:len('output')] == 'output':
return None
name = tokens[0]
description = tokens[0]
token = tokens[1].strip()[len('output') + 1:]
out = None
if token.lower().strip().startswith('outputraster'):
out = QgsProcessingOutputRasterLayer(name, description)
elif token.lower().strip() == 'outputvector':
out = QgsProcessingOutputVectorLayer(name, description)
elif token.lower().strip() == 'outputlayer':
out = QgsProcessingOutputMapLayer(name, description)
elif token.lower().strip() == 'outputmultilayers':
out = QgsProcessingOutputMultipleLayers(name, description)
# elif token.lower().strip() == 'vector point':
# out = OutputVector(datatype=[dataobjects.TYPE_VECTOR_POINT])
# elif token.lower().strip() == 'vector line':
# out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_LINE])
# elif token.lower().strip() == 'vector polygon':
# out = OutputVector(datatype=[OutputVector.TYPE_VECTOR_POLYGON])
# elif token.lower().strip().startswith('table'):
# out = OutputTable()
elif token.lower().strip().startswith('outputhtml'):
out = QgsProcessingOutputHtml(name, description)
# elif token.lower().strip().startswith('file'):
# out = OutputFile()
# ext = token.strip()[len('file') + 1:]
# if ext:
# out.ext = ext
elif token.lower().strip().startswith('outputfolder'):
out = QgsProcessingOutputFolder(name, description)
elif token.lower().strip().startswith('outputnumber'):
out = QgsProcessingOutputNumber(name, description)
elif token.lower().strip().startswith('outputstring'):
out = QgsProcessingOutputString(name, description)
# elif token.lower().strip().startswith('extent'):
# out = OutputExtent()
return out
except:
return None