For any vector output, we can include a QgsProcessingParameterFeatureSink in the initAlgorithm definition. Is there a way to do this with generic filetypes? I have a script that will create a lookup table that needs to be exported as a CSV to feed into PowerBI.

This is what I have tried:

def initAlgorithm(self, config=None):
    self.addParameter(QgsProcessingParameterMapLayer('Network', 'Road Network', defaultValue=None, types=[QgsProcessing.TypeVectorLine]))
    self.addParameter(QgsProcessingParameterMapLayer('RoutesLayer', 'Routes Layer', defaultValue=None, types=[QgsProcessing.TypeVectorLine]))
    self.addParameter(QgsProcessingParameterCrs('OutputCRS', 'Output CRS', defaultValue='EPSG:7855'))
    self.addParameter(QgsProcessingParameterFeatureSink('OUTPUT', 'Output Layer', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, supportsAppend=True, defaultValue=None))
    self.addParameter(QgsProcessingParameterFeatureSink('csvOutput', 'CSV Output', QgsProcessing.TypeFile))

The last line creates an output field when you run the script but it will attempt to save it in a GIS layer format.

Is there a way to set the output as a specific filetype that is not a GIS layer?


I found the culprit: QgsProcessingParameterFileDestination

This can be implemented in the initAlgorithm definition as so:

def initAlgorithm(self, config=None):
    self.addParameter(QgsProcessingParameterFileDestination('csvOutput', 'CSV Output', 'Comma Separated Values (*.csv)'))

When a temporary output is used (i.e. the user chooses not to specify an output file), the lines added using the file.write function are not stored and disappear at the completion of the script.

From this point, I used the parameter as if it was a standard text output in Python:

def processAlgorithm(self, parameters, context, model_feedback):
    csv = open(parameters['csvOutput'], 'w')

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.