4

I am developing a QGIS plugin in Python and I need some help to efficiently save my output layer to a CSV. I understand that by right-clicking on the layer and going to "Export -> Save features as" opens the window that enables me to save my output as a CSV. This window is shown below.

Expected save layer window

But I expect people who are new to QGIS (or GIS even) to use this tool and they might not be able to find this saving option straight away, hence thinking of adding that feature to the plugin itself. What I would ideally want is to bring up this inbuilt saving window (shown above) through my python code. So, when the user presses a button on the GUI, this window would come up straightaway. Does anyone have an idea on how to do this?

Maybe there is a processing algorithm of the form processing.runalg(appropriate arguments) that would enable me to do this?

1 Answer 1

4

The easiest way to save a vector layer to CSV using processing.run is to use native:savefeatures tool.

layer = iface.activeLayer()
output = "C:/path/to/output.csv"

processing.run("native:savefeatures", 
               {'INPUT': layer,
                'OUTPUT': output})

If you want to specify other parameters, you can use 'LAYER_OPTIONS'.

# separate individual options with semicolon
layer_options = 'SEPARATOR=SEMICOLON;GEOMETRY=AS_WKT'

processing.run("native:savefeatures", 
               {'INPUT': layer,
                'OUTPUT': output,
                'LAYER_OPTIONS': layer_options})
            

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.