qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.66k stars 3.02k forks source link

Using the SaveVectorOption.actionOnExistingFile=QgsVectorFileWriter.CreateOrOverwriteLayer changes behavior when the file doesn't exist #55398

Open louis2607 opened 1 year ago

louis2607 commented 1 year ago

What is the bug or the crash?

Setting actionOnExistingFile parameter of QgsVectorFileWriter.SaveVectorOptions to QgsVectorFileWriter.CreateOrOverwriteLayer also changes behavior when the file doesn't exist.

Saving to an empty location with that save options result in the function writeAsVectorFormatV3 returning a code 2 error which is 'Opening of data source in update mode failed (OGR error: )'.

However based on the name of the save option, this is not the behavior one would expect.

At least in my opinion the expected behavior would be that this save option parameter gets ignored if the file doesn't exists.

Steps to reproduce the issue

  1. Create a new memory scratch layer of any geometry type with the "New Temporary Scratch Layer" tool.
  2. Run the following script with the new layer selected in the Layers panel:
    
    # reference to memory layer
    layer = iface.activeLayer()

save location

geopackage = '/path/to/geopackage.gpkg' # PATH WITH NO EXISTING FILE

write the layer to disk

options = QgsVectorFileWriter.SaveVectorOptions() options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer options.layerName = layer.name() result = QgsVectorFileWriter.writeAsVectorFormatV3(layer, geopackage, QgsCoordinateTransformContext(), options) print(result)


3. see error:
> (2, 'Opening of data source in update mode failed (OGR error: )', '', '')

### Versions

Version de QGIS
3.32.1-Lima
Révision du code
4a078ecf63
Version de Qt
5.15.3
Version de Python
3.9.5
Version de GDAL/OGR
3.7.1
Version de Proj
9.2.1
Version de la base de données du registre EPSG
v10.088 (2023-05-13)
Version de GEOS
3.12.0-CAPI-1.18.0
Version de SQLite
3.41.1
Version de PDAL
2.5.5
Version du client PostgreSQL
15.2
Version de SpatiaLite
5.0.1
Version de QWT
6.1.6
Version de QScintilla2
2.13.1
Version de l'OS
Windows 10 Version 2009

Extensions Python actives
db_manager
0.1.20
grassprovider
2.12.99
MetaSearch
0.3.6
processing
2.12.99

### Supported QGIS version

- [x] I'm running a supported QGIS version according to [the roadmap](https://www.qgis.org/en/site/getinvolved/development/roadmap.html#release-schedule).

### New profile

- [X] I tried with a new [QGIS profile](https://docs.qgis.org/latest/en/docs/user_manual/introduction/qgis_configuration.html#working-with-user-profiles)

### Additional context

Also tested on QGIS LTR 3.22.11 and QGIS 3.40.0 with the same behavior
agiudiceandrea commented 1 year ago

See: https://github.com/qgis/QGIS/pull/3597.

louis2607 commented 1 year ago

See: #3597.

The flags could have been made a lot more understandable by changing them to something like:

SaveVectorOptions.behavior = QgsVectorFileWriter.CreateOrOverwriteFile and SaveVectorOptions.behavior = QgsVectorFileWriter.CreateOrOverwriteLayerInFile

But even then, I am not sure I personally agree with forcing trying to open the file in update mode with CreateOrOverwriteLayer even if there is no file. This seems illogical when you set a flag on what you expect to happen when there is an existing file and shouldn't have an impact when there is not one.