Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[vector file writer] Fix another FID corner scenario (fixes #34613)
  • Loading branch information
nirvn authored and nyalldawson committed May 17, 2021
1 parent 75d9dd6 commit 2df97e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -238,6 +238,23 @@ void QgsVectorFileWriter::init( QString vectorFileName,
mOgrDriverName = driverName;
}

#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,3,1)
QString fidFieldName;
if ( mOgrDriverName == QLatin1String( "GPKG" ) )
{
for ( const QString &layerOption : layerOptions )
{
if ( layerOption.startsWith( QStringLiteral( "FID=" ) ) )
{
fidFieldName = layerOption.mid( 4 );
break;
}
}
if ( fidFieldName.isEmpty() )
fidFieldName = QStringLiteral( "fid" );
}
#endif

// find driver in OGR
OGRSFDriverH poDriver;
QgsApplication::registerOgrDrivers();
Expand Down Expand Up @@ -653,6 +670,14 @@ void QgsVectorFileWriter::init( QString vectorFileName,
break;

case QVariant::Double:
#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,3,1)
if ( mOgrDriverName == QLatin1String( "GPKG" ) && attrField.precision() == 0 && attrField.name().compare( fidFieldName, Qt::CaseInsensitive ) == 0 )
{
// Convert field to match required FID type
ogrType = OFTInteger64;
break;
}
#endif
ogrType = OFTReal;
break;

Expand Down
23 changes: 22 additions & 1 deletion tests/src/python/test_provider_ogr.py
Expand Up @@ -18,10 +18,11 @@
from datetime import datetime

from osgeo import gdal, ogr # NOQA
from qgis.PyQt.QtCore import QVariant, QByteArray
from qgis.PyQt.QtCore import QVariant, QByteArray, QTemporaryDir
from qgis.core import (
NULL,
QgsApplication,
QgsCoordinateTransformContext,
QgsProject,
QgsField,
QgsFields,
Expand All @@ -33,6 +34,7 @@
QgsDataProvider,
QgsVectorDataProvider,
QgsVectorLayer,
QgsVectorFileWriter,
QgsWkbTypes,
QgsNetworkAccessManager
)
Expand Down Expand Up @@ -824,6 +826,25 @@ def testTransactionGroupExpressionFields(self):
self.assertTrue(vl.addFeature(feature))
self.assertFalse(vl.dataProvider().hasErrors())

def testFidDoubleSaveAsGeopackage(self):
"""Test issue GH #25795"""

ml = QgsVectorLayer('Point?crs=epsg:4326&field=fid:double(20,0)', 'test', 'memory')
self.assertTrue(ml.isValid())
self.assertEqual(ml.fields()[0].type(), QVariant.Double)

d = QTemporaryDir()
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = 'GPKG'
options.layerName = 'fid_double_test'
err, _ = QgsVectorFileWriter.writeAsVectorFormatV2(ml, os.path.join(d.path(), 'fid_double_test.gpkg'),
QgsCoordinateTransformContext(), options)
self.assertEqual(err, QgsVectorFileWriter.NoError)
self.assertTrue(os.path.isfile(os.path.join(d.path(), 'fid_double_test.gpkg')))

vl = QgsVectorLayer(os.path.join(d.path(), 'fid_double_test.gpkg'))
self.assertEqual(vl.fields()[0].type(), QVariant.LongLong)


if __name__ == '__main__':
unittest.main()

0 comments on commit 2df97e5

Please sign in to comment.