On 09/01/2011 10:45 AM, kimaidou wrote:
> Hi list
> 
> I would like to know if there was a dedicated api method to create a new
> vector layer (and save it into a file) by copying a map canvas layer
> (which could be postgis, shapefile, etc.).
> I have read here
> http://www.qgis.org/pyqgis-cookbook/vector.html#writing-shapefiles
> how to write a new shapefile. But then I would need to  "manually" copy
> each feature from the chosen layer to the new one.
> 
> I was thinking of a equivalent to the right-click "Save as" which brings
> an ui to choose the targer layer format, srid, etc., but I am trying to
> do it programatically.
> 
> Is there a proper way to do it ?

Don't know if it is 'proper', but I can write a vector layer with the
method writeAsVectorFormat from QgsVectorFileWriter, see

http://doc.qgis.org/head/classQgsVectorFileWriter.html#3a4405a59d8f8ac147878cae5bd9bade

To test:

on an active vector layer (selected in legend), do the following in the
qgis python console:

i = qgis.utils.iface
l = i.activeLayer()
qgis.core.QgsVectorFileWriter.writeAsVectorFormat(l,
'/tmp/myjson.json', 'utf-8', l.crs(), 'GeoJson')

and you should find your json in /tmp :-)

(for more possibilities (like only saving selected features etc) see docs.

Regards

Richard Duivenvoorde
_______________________________________________
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to