QGIS – Save as CSV with Coordinates in Both QGIS 2.2 and PyQGIS

csvpyqgisqgis

These questions have been asked before, but since they are not answered I will try to pick up the ends here.

1) First, as stated here:

QGIS 2.2 saving layer as CSV with GEOMETRY=AS_WKT not working

in Qgis 2.2 it is not possible to save a shapefile as .csv including the coordinates using the function _GEOMETRY=AS_WKT_. The issue

http://hub.qgis.org/issues/9597

is set to closed. Does it mean that it has been fixed? And if so, how to make it work again?

2) Second, if you want to save a shape file as .csv with coordinates through PyQgis it can be done with:

https://gis.stackexchange.com/questions/61857/how-to-write-vector-layer-to-csv-file-with-geometry-by-python

as stated here:

How to write vector layer to csv file with geometry by PYTHON?

But again, this does not save the coordinates, and I can't find any solution to this either. So right now, I still have to use an old version on QGIS.

IS there any solutions to these, available at the moment?

Best Answer

In PYQGIS a shape file can be save as a .csv with coordinates by:

QgsVectorFileWriter.writeAsVectorFormat(vlayer, "xy.csv", "utf-8", None, "CSV", layerOptions ='GEOMETRY=AS_WKT')

This is not working in QGIS 2.2 but will work again in QGIS 2.4.