Skip to content

Instantly share code, notes, and snippets.

@kaotika
Created January 28, 2015 14:13
Show Gist options
  • Save kaotika/24efa632276ef06b0192 to your computer and use it in GitHub Desktop.
Save kaotika/24efa632276ef06b0192 to your computer and use it in GitHub Desktop.
write a csv file inside qgis
from PyQt4.QtCore import QVariant
from qgis.core import QGis
from qgis.core import QgsVectorFileWriter, QgsVectorLayer, QgsFields, QgsField, QgsCoordinateReferenceSystem, QgsApplication
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsField
import tempfile
import os
QgsApplication.setPrefixPath("/usr", True)
QgsApplication.initQgis()
fields = QgsFields()
fields.append(QgsField("test", QVariant.Int))
crs = QgsCoordinateReferenceSystem()
crs.createFromString("EPSG:4326")
encoding = "UTF-8"
#path = "/vsizip/{0}".format(os.path.join(tempfile.gettempdir(), "test.zip"))
path = os.path.join(tempfile.gettempdir(), "test.csv")
writer = QgsVectorFileWriter(
path,
encoding,
fields,
QGis.WKBPoint,
crs,
'CSV',
[],
["CREATE_CSVT=YES", "WRITE_BOM=YES", "SEPARATOR=SEMICOLON", "GEOMETRY=AS_WKT"]
)
feature = QgsFeature(fields, 1)
feature.setGeometry(
QgsGeometry.fromPoint(QgsPoint(13, 52))
)
feature.setAttribute('test', 10)
writer.addFeature(feature)
print path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment