[Qgis-developer] Python - saving a layer into a geojson layer

kimaidou kimaidou at gmail.com
Thu Sep 1 07:27:09 EDT 2011


WONDERFULL !
Thanks a lot Richard, this is exactlu what I was looking for. Qgis rocks !

Kimaidou

2011/9/1 Richard Duivenvoorde <rdmailings at duif.net>

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20110901/3ef11813/attachment.html


More information about the Qgis-developer mailing list