Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use Python try to write vector layer file to csv file with this code

QgsVectorFileWriter.writeAsVectorFormat(mylayer, r'c:\temp\xyz.csv', "utf-8", None, "CSV")

It can export to xyz.csv but only attribute show in csv not geometry column. How can I export both attribute and spatial data into csv file.


share|improve this question
Here is the same question and the solution [link][1]. [1]:… – mete7 May 25 '13 at 15:00
I'm sorry for make question not clear. my work use python with pyqgis write vector layer to csv file. I want csv file have geometry but it not. still need answer – Anubiz May 26 '13 at 7:55
Depends on what type of vector layer you want to export. For point layer you could just add the lat, long values as attribute table columns. For all others you need WKT – Curlew May 26 '13 at 8:39
Thank you Curlew, I work with point and polygon layer. for point layer you mean add lat, lon column in attribute table? how about polygon? – Anubiz May 27 '13 at 8:39
Is this really a duplicate? This question here is aimed particularly at python, which is not covered by the linked answer. – Matthias Kuhn Jun 10 '13 at 8:18

A similar answer was provided from this post:

Save as .csv with coordinates in both QGIS 2.2 and PyqGIS

Just add layerOptions ='GEOMETRY=AS_XYZ at the end:

layer = QgsVectorLayer("path/to/shapefile", "name", "ogr")
QgsVectorFileWriter.writeAsVectorFormat(layer, r'c:\temp\xyz.csv', "utf-8", None, "CSV", layerOptions='GEOMETRY=AS_XYZ')
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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