Skip to content

Commit

Permalink
Merge pull request #43118 from nirvn/fid_again_n_again
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 committed May 10, 2021
2 parents 7604b6e + 81d29e3 commit 8360b39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -237,6 +237,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 @@ -650,6 +667,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
19 changes: 19 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -1297,6 +1297,25 @@ def testShapefilesWithNoAttributes(self):
vl = QgsVectorLayer(os.path.join(d.path(), 'writetest.shp'))
self.assertEqual(vl.featureCount(), 1)

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)

def testNonGeopackageSaveMetadata(self):
"""
Save layer metadata for a file-based format which doesn't have native metadata support.
Expand Down

0 comments on commit 8360b39

Please sign in to comment.