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.59k stars 3.01k forks source link

QgsVectorFileWriter.writeAsVectorFormatV3 ignores COORDINATE_PRECISION when appending to GeoJSON #56335

Open rtrwalker opened 9 months ago

rtrwalker commented 9 months ago

What is the bug or the crash?

When appending to an existing GeoJson QgsVectorFileWriter.writeAsVectorFormatV3 ignores the layer option COORDINATE_PRECISION. The coordinates have the correct number of decimal places when I create the geojson but when I use QgsVectorFileWriter.AppendToLayerNoNewFields to append to the file there are 15 decimal places.

Steps to reproduce the issue

Minimal code to reproduce is below. I write the active layer to a geojson file, then I append the same layer to the same file. 2nd half of file has more (the default 15) decimal places in the coordinates.

fileName=r'C:\temp\out.geojson'
for i in [0,1]:
    layer = iface.activeLayer()
    transformContext = QgsProject.instance().transformContext()  # required for V3
    options = QgsVectorFileWriter.SaveVectorOptions()            
    options.driverName = 'GeoJSON'
    options.encoding = 'utf-8'
    options.layerOptions = ['COORDINATE_PRECISION=8']#layerOptions            
    if os.path.isfile(fileName):                
        options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields                

    writer = QgsVectorFileWriter.writeAsVectorFormatV3(
        layer=layer,
        fileName =fileName,
        transformContext=transformContext,
        options=options
        )

Screenshot of resulting geojson: image

Versions

QGIS version | 3.34.3-Prizren | QGIS code revision | 47373234ac -- | -- | -- | -- Qt version | 5.15.3 Python version | 3.9.18 GDAL/OGR version | 3.8.3 PROJ version | 9.3.1 EPSG Registry database version | v10.098 (2023-11-24) GEOS version | 3.12.1-CAPI-1.18.1 SQLite version | 3.41.1 PDAL version | 2.6.0 PostgreSQL client version | 15.2 SpatiaLite version | 5.1.0 QWT version | 6.1.6 QScintilla2 version | 2.13.4 OS version | Windows 10 Version 2009   |   |   |   Active Python plugins AnotherDXF2Shape | 1.2.2 DEMto3D | 3.4 GeometryShapes | 0.7 nominatim | 1.2.3 openlayers_plugin | 2.0.0 OSMDownloader | 1.0 pointsamplingtool | 0.5.2 Projestions | 1.0.1 Qgis2threejs | 2.5 QuickOSM | 1.6.1 quick_map_services | 0.19.29 shapetools | 3.3.18 tile_plus | 0.1 db_manager | 0.1.20 grassprovider | 2.12.99 MetaSearch | 0.3.6 processing | 2.12.99 QGIS version 3.34.3-Prizren QGIS code revision [47373234ac](https://github.com/qgis/QGIS/commit/47373234ac) Qt version 5.15.3 Python version 3.9.18 GDAL/OGR version 3.8.3 PROJ version 9.3.1 EPSG Registry database version v10.098 (2023-11-24) GEOS version 3.12.1-CAPI-1.18.1 SQLite version 3.41.1 PDAL version 2.6.0 PostgreSQL client version 15.2 SpatiaLite version 5.1.0 QWT version 6.1.6 QScintilla2 version 2.13.4 OS version Windows 10 Version 2009 Active Python plugins AnotherDXF2Shape 1.2.2 DEMto3D 3.4 GeometryShapes 0.7 nominatim 1.2.3 openlayers_plugin 2.0.0 OSMDownloader 1.0 pointsamplingtool 0.5.2 Projestions 1.0.1 Qgis2threejs 2.5 QuickOSM 1.6.1 quick_map_services 0.19.29 shapetools 3.3.18 tile_plus 0.1 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 _No response_
rtrwalker commented 8 months ago

I checked and I get the same issue if I go through the QGIS user interface. Right click a layer, Export --> Save Features As ... Geojson file with COORDINATE_PRECISION=7. Repeat the export on the same layer with same file and same COORDINATE_PRECISION=7. I end up with the appended rows in the Geojson having 15 decimal places.

agiudiceandrea commented 8 months ago

I guess the issue may be in the GDAL/OGR library may discard the layer creation options when the features are appended to an existing layer. Probably @rouault knows.

rouault commented 8 months ago

may discard the layer creation options when the features are appended to an existing layer.

yes, layer creation options only apply at layer creation time, not when appending features to an existing layers.

agiudiceandrea commented 8 months ago

It seems to me it would be reasonable in this case to also apply the COORDINATE_PRECISION option when appending features to an existing layers.

rtrwalker commented 8 months ago

If I had the same geometry type I could merge layers before exporting to geojson and the coordinate precision would be ok. But I have different geometry types (e.g. point layer and line layer) which as far as I know have to be appended.