Created
January 28, 2015 14:13
-
-
Save kaotika/24efa632276ef06b0192 to your computer and use it in GitHub Desktop.
write a csv file inside qgis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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