Skip to content
Permalink
Browse files
[vector file writer] Fix another FID corner scenario (fixes #34613)
  • Loading branch information
nirvn authored and nyalldawson committed Jun 21, 2021
1 parent 6332a1e commit e464d420d6c8ac64b607fdc37386bbe50e195401
Showing with 47 additions and 1 deletion.
  1. +25 −0 src/core/qgsvectorfilewriter.cpp
  2. +22 −1 tests/src/python/test_provider_ogr.py
@@ -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();
@@ -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;

@@ -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,
@@ -33,6 +34,7 @@
QgsDataProvider,
QgsVectorDataProvider,
QgsVectorLayer,
QgsVectorFileWriter,
QgsWkbTypes,
QgsNetworkAccessManager
)
@@ -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 e464d42

Please sign in to comment.