0

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 4, 2021 at 6:35
0

1 Answer 1

1

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')
 csv.write('RouteID,LINK-DIR,SEQ')
 ...
 csv.close()
answered Jul 5, 2021 at 2:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.