QgsVectorFileWriter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

QgsVectorFileWriter

Möri Cedric
Hi list

in a python plugin im using QgsVectorFileWriter.writeAsShapefile to write a shapefile to the disk.

The first time I use a certain file name/path (e.g /tmp/foo.shp), it works properly. If I try to rerun the script, it fails saying:
ERROR 1: /tmp/foo.shp is not a directory.

It looks like it is not possible to replace an existing shapefile that way. Am I correct? Do I have to manage the files myself (i.e. delete them before writing the new file)? Or is there another way to replace an existing shapefile?

Thans a lot for any help

regards
Cédric


mit freundlichen Grüssen

Cédric Möri
GIS-Informatiker

--
Kanton Solothurn
Bau- und Justizdepartement
Amt für Geoinformation
Rötistrasse 4
4501 Solothurn

Telefon: +41 (0)32 627 24 75
Telefax: +41 (0)32 627 22 14
mailto:[hidden email]
http://www.agi.so.ch
_______________________________________________
Qgis-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QgsVectorFileWriter

Carson Farmer
Cedric,
> Hi list
>
> in a python plugin im using QgsVectorFileWriter.writeAsShapefile to write a shapefile to the disk.
>
> The first time I use a certain file name/path (e.g /tmp/foo.shp), it works properly. If I try to rerun the script, it fails saying:
> ERROR 1: /tmp/foo.shp is not a directory.
>
> It looks like it is not possible to replace an existing shapefile that way. Am I correct? Do I have to manage the files myself (i.e. delete them before writing the new file)? Or is there another way to replace an existing shapefile?
>  

Yes you are correct, you cannot overwrite shapfiles like that... but you
can do the following first:

QgsVectorFileWriter.deleteShapefile( "path/to/the/shapefile/shp" )


Cheers,

Carson

--
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation (NCG),
Email: [hidden email]
Web:   http://www.carsonfarmer.com/
       http://www.ftools.ca/


_______________________________________________
Qgis-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QgsVectorFileWriter

Lionel Roubeyrie
In this way, it would be better to save in a temporary shapefile first,
and if the process finishes successfully, remove the destination
shapefile and copy the temporary shp to the new location.

Le mercredi 21 janvier 2009 à 14:27 +0000, Carson Farmer a écrit :

> Cedric,
> > Hi list
> >
> > in a python plugin im using QgsVectorFileWriter.writeAsShapefile to write a shapefile to the disk.
> >
> > The first time I use a certain file name/path (e.g /tmp/foo.shp), it works properly. If I try to rerun the script, it fails saying:
> > ERROR 1: /tmp/foo.shp is not a directory.
> >
> > It looks like it is not possible to replace an existing shapefile that way. Am I correct? Do I have to manage the files myself (i.e. delete them before writing the new file)? Or is there another way to replace an existing shapefile?
> >  
>
> Yes you are correct, you cannot overwrite shapfiles like that... but you
> can do the following first:
>
> QgsVectorFileWriter.deleteShapefile( "path/to/the/shapefile/shp" )
>
>
> Cheers,
>
> Carson
>
--
Lionel Roubeyrie
chargé d'études
LIMAIR - La Surveillance de l'Air en Limousin
http://www.limair.asso.fr


_______________________________________________
Qgis-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QgsVectorFileWriter

Carson Farmer

> In this way, it would be better to save in a temporary shapefile first,
> and if the process finishes successfully, remove the destination
> shapefile and copy the temporary shp to the new location.
>  
hmm, I don't  know about this...
Firstly, it's only useful to do this if you're concerned about the
initial file, but since you appear to be writing to a tmp directory,
this probably isn't all that critical...

Secondly, this creates the possibility that if something goes wrong, you
have TWO useless files lying about on your system (one you were trying
to replace, and the other that didn't finish writing properly).

The better implementation would probably be to use the memory provider
to create a temporary QgsVectorLayer in memory, and if the process
finishes successfully, remove the old version and write the memory layer
to file. Of course, this only works if you're working with relatively
'small' layers, if you have huge vector layers (> 500,000 features) then
you're probably better off just writing directly to file...

mem_layer = QgsVectorLayer('Point', 'layer_name', 'memory')

you'll have to double check the above, this is just from 'memory' ;-)

Carson

--
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation (NCG),
Email: [hidden email]
Web:   http://www.carsonfarmer.com/
       http://www.ftools.ca/


_______________________________________________
Qgis-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QgsVectorFileWriter

Lionel Roubeyrie
Hi Carson,

Le mercredi 21 janvier 2009 à 16:48 +0000, Carson Farmer a écrit :
> > In this way, it would be better to save in a temporary shapefile first,
> > and if the process finishes successfully, remove the destination
> > shapefile and copy the temporary shp to the new location.
> >  
> hmm, I don't  know about this...
> Firstly, it's only useful to do this if you're concerned about the
> initial file, but since you appear to be writing to a tmp directory,
> this probably isn't all that critical...
Sometimes I don't want to get 2 versions of a shp and select the source
shp to be overwritten. If something goes wrong and the source is deleted
first, all is lost...
>
> Secondly, this creates the possibility that if something goes wrong, you
> have TWO useless files lying about on your system (one you were trying
> to replace, and the other that didn't finish writing properly).
A simple test where if a problem occurs, we delete the tmp file...

>
> The better implementation would probably be to use the memory provider
> to create a temporary QgsVectorLayer in memory, and if the process
> finishes successfully, remove the old version and write the memory layer
> to file. Of course, this only works if you're working with relatively
> 'small' layers, if you have huge vector layers (> 500,000 features) then
> you're probably better off just writing directly to file...
>
> mem_layer = QgsVectorLayer('Point', 'layer_name', 'memory')
>
> you'll have to double check the above, this is just from 'memory' ;-)
It may be a solution :-)

>
> Carson
>
--
Lionel Roubeyrie
chargé d'études
LIMAIR - La Surveillance de l'Air en Limousin
http://www.limair.asso.fr


_______________________________________________
Qgis-user mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/qgis-user