WONDERFULL !<br>Thanks a lot Richard, this is exactlu what I was looking for. Qgis rocks !<br><br>Kimaidou<br><br><div class="gmail_quote">2011/9/1 Richard Duivenvoorde <span dir="ltr"><<a href="mailto:rdmailings@duif.net">rdmailings@duif.net</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">On 09/01/2011 10:45 AM, kimaidou wrote:<br>
> Hi list<br>
><br>
> I would like to know if there was a dedicated api method to create a new<br>
> vector layer (and save it into a file) by copying a map canvas layer<br>
> (which could be postgis, shapefile, etc.).<br>
> I have read here<br>
> <a href="http://www.qgis.org/pyqgis-cookbook/vector.html#writing-shapefiles" target="_blank">http://www.qgis.org/pyqgis-cookbook/vector.html#writing-shapefiles</a><br>
> how to write a new shapefile. But then I would need to "manually" copy<br>
> each feature from the chosen layer to the new one.<br>
><br>
> I was thinking of a equivalent to the right-click "Save as" which brings<br>
> an ui to choose the targer layer format, srid, etc., but I am trying to<br>
> do it programatically.<br>
><br>
> Is there a proper way to do it ?<br>
<br>
</div></div>Don't know if it is 'proper', but I can write a vector layer with the<br>
method writeAsVectorFormat from QgsVectorFileWriter, see<br>
<br>
<a href="http://doc.qgis.org/head/classQgsVectorFileWriter.html#3a4405a59d8f8ac147878cae5bd9bade" target="_blank">http://doc.qgis.org/head/classQgsVectorFileWriter.html#3a4405a59d8f8ac147878cae5bd9bade</a><br>
<br>
To test:<br>
<br>
on an active vector layer (selected in legend), do the following in the<br>
qgis python console:<br>
<br>
i = qgis.utils.iface<br>
l = i.activeLayer()<br>
qgis.core.QgsVectorFileWriter.writeAsVectorFormat(l,<br>
'/tmp/myjson.json', 'utf-8', l.crs(), 'GeoJson')<br>
<br>
and you should find your json in /tmp :-)<br>
<br>
(for more possibilities (like only saving selected features etc) see docs.<br>
<br>
Regards<br>
<font color="#888888"><br>
Richard Duivenvoorde<br>
</font></blockquote></div><br>