Background
I have 19 layers which ALWAYS have the same file name, but are located in varying folders. These layers are all inputs to several big models.
Now, instead of defining each algorithm input manually, I want my model to pick them up automatically by only providing a folder location.
Basically I create one algorithm for each layer which has the file name hard-coded, it takes a an input folder location from the model and outputs a layer (code for an example algo at the end). But even now, I have to tell each algorithm manually to use the model's input folder parameter, schematically (in reality there's 16 more algos needing the same folder input)
Question
Is there a way that I can tell the "Get OUT_Distribution.." algorithms to use the first folder picker parameter it finds if the environment is a model? I expect it to be available over QgsProcessingAlgorithm.initAlgorithm
's configuration
argument, but there's little guidance on how to use that.
Or maybe this is overly complex for what I want to achieve?! @Joseph had a nice solution via simple Python scripts, but I've NEVER seen that notation before, nor can I find any documentation on how one could add inputs using that method.
Example code
Example code for a custom algo returning a layer from hard-coded file name and input folder location (just to show the gist of it, not necessarily working):
class ExampleAlgo(QgsProcessingAlgorithm):
IN_FOLDER = 'IN_FOLDER'
OUT_LAYER = 'OUT_DistributionCableEntries'
def initAlgorithm(self, config=None):
self.addParameter(
QgsProcessingParameterFile(
self.IN_FOLDER,
'Input folder',
behavior=QgsProcessingParameterFile.Folder,
fileFilter='Alle Dateien (*.*)',
defaultValue=None
)
)
self.addParameter(
QgsProcessingParameterVectorDestination(
self.OUT_LAYER,
'OUT_DistributionCableEntries',
type=QgsProcessing.TypeVectorAnyGeometry,
createByDefault=True
)
)
def processAlgorithm(self, parameters, context, model_feedback: QgsProcessingFeedback):
input_folder = self.parameterAsString(
parameters,
self.IN_FOLDER,
context
)
out_layer = self.parameterAsVectorLayer(
parameters,
self.OUT_LAYER,
context
)
output_path = path.join(input_folder, self.OUT_LAYER) + '.shp'
layer_out = QgsVectorLayer(output_path, self.OUT_LAYER, 'ogr')
return {self.OUT_LAYER: layer_out}
def name(self):
return f'get_{self.OUT_LAYER}'
def displayName(self):
return f'Get {self.OUT_LAYER}'
def createInstance(self):
return D4SAlgoGetFileDistributionCableEntries()