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">&lt;<a href="mailto:rdmailings@duif.net">rdmailings@duif.net</a>&gt;</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>
&gt; Hi list<br>
&gt;<br>
&gt; I would like to know if there was a dedicated api method to create a new<br>
&gt; vector layer (and save it into a file) by copying a map canvas layer<br>
&gt; (which could be postgis, shapefile, etc.).<br>
&gt; I have read here<br>
&gt; <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>
&gt; how to write a new shapefile. But then I would need to  &quot;manually&quot; copy<br>
&gt; each feature from the chosen layer to the new one.<br>
&gt;<br>
&gt; I was thinking of a equivalent to the right-click &quot;Save as&quot; which brings<br>
&gt; an ui to choose the targer layer format, srid, etc., but I am trying to<br>
&gt; do it programatically.<br>
&gt;<br>
&gt; Is there a proper way to do it ?<br>
<br>
</div></div>Don&#39;t know if it is &#39;proper&#39;, 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>
&#39;/tmp/myjson.json&#39;, &#39;utf-8&#39;, l.crs(), &#39;GeoJson&#39;)<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>