[QGIS Commit] r15942 - docs/trunk/english_us/developer_cookbook/source

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Sep 1 16:31:03 EDT 2011


Author: wonder
Date: 2011-09-01 13:31:03 -0700 (Thu, 01 Sep 2011)
New Revision: 15942

Modified:
   docs/trunk/english_us/developer_cookbook/source/vector.rst
Log:
Updated vector file writing section, added GeoJSON example


Modified: docs/trunk/english_us/developer_cookbook/source/vector.rst
===================================================================
--- docs/trunk/english_us/developer_cookbook/source/vector.rst	2011-08-29 06:48:54 UTC (rev 15941)
+++ docs/trunk/english_us/developer_cookbook/source/vector.rst	2011-09-01 20:31:03 UTC (rev 15942)
@@ -113,33 +113,49 @@
 
 
 
-Writing Shapefiles
-------------------
+Writing Vector Layers
+---------------------
 
-You can write shapefiles using :class:`QgsVectorFileWriter` class. Besides shapefiles, it supports any kind of vector file that OGR supports.
+You can write vector layer files using :class:`QgsVectorFileWriter` class. It supports any other kind of vector file that OGR supports (shapefiles, GeoJSON, KML and others).
 
-There are two possibilities how to export a shapefile:
+There are two possibilities how to export a vector layer:
 
 * from an instance of :class:`QgsVectorLayer`::
 
-    error = QgsVectorFileWriter.writeAsShapefile(layer, "my_shapes.shp", "CP1250")
+    error = QgsVectorFileWriter.writeAsVectorFormat(layer, "my_shapes.shp", "CP1250", None, "ESRI Shapefile")
 
     if error == QgsVectorFileWriter.NoError:
       print "success!"
 
+    error = QgsVectorFileWriter.writeAsVectorFormat(layer, "my_json.json", "utf-8", None, "GeoJSON")
+    if error == QgsVectorFileWriter.NoError:
+      print "success again!"
+
+  Third parameter specifies output text encoding. Only some drivers need this for correct operation - shapefiles are one of those - however in case you are
+  not using international characters you do not have to care much about the encoding. The fourth parameter that we left as None may specify destination CRS - if
+  a valid instance of :class:`QgsCoordinateReferenceSystem` is passed, the layer is transformed to that CRS.
+
+  For valid driver names please consult the `supported formats by OGR`_ - you should pass the value in "Code" column as the driver name.
+  Optionally you can set whether to export only selected features, pass further driver-specific options for creation or tell the writer not to create attributes
+  - look into the documentation for full syntax.
+
+.. _supported formats by OGR: http://www.gdal.org/ogr/ogr_formats.html
+
+
 * directly from features::
 
     # define fields for feature attributes
     fields = { 0 : QgsField("first", QVariant.Int),
                1 : QgsField("second", QVariant.String) }
 
-    # create an instance of vector file writer, it will create the shapefile. Arguments:
-    # 1. path to new shapefile (will fail if exists already)
+    # create an instance of vector file writer, it will create the vector file. Arguments:
+    # 1. path to new file (will fail if exists already)
     # 2. encoding of the attributes
     # 3. field map
     # 4. geometry type - from WKBTYPE enum
     # 5. layer's spatial reference (instance of QgsCoordinateReferenceSystem) - optional
-    writer = QgsVectorFileWriter("my_shapes.shp", "CP1250", fields, QGis.WKBPoint, None)
+    # 6. driver name for the output file
+    writer = QgsVectorFileWriter("my_shapes.shp", "CP1250", fields, QGis.WKBPoint, None, "ESRI Shapefile")
 
     if writer.hasError() != QgsVectorFileWriter.NoError:
       print "Error when creating shapefile: ", writer.hasError()
@@ -155,8 +171,6 @@
     del writer
 
 
-
-
 Memory Provider
 ---------------
 



More information about the QGIS-commit mailing list