Skip to content

Commit 71f9910

Browse files
committed
[vector file writer] use field type to save attribute values
1 parent dbbbdbd commit 71f9910

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

python/plugins/processing/tests/testdata/expected/import_photos.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<ogr:directory>/home/nyall/dev/QGIS/python/plugins/processing/tests/testdata/custom/photos</ogr:directory>
2020
<ogr:altitude>422.191</ogr:altitude>
2121
<ogr:direction>59.9153</ogr:direction>
22-
<ogr:longitude>149.2751666666667</ogr:longitude>
22+
<ogr:longitude>149.27516666666668</ogr:longitude>
2323
<ogr:latitude>-37.2305</ogr:latitude>
2424
<ogr:timestamp>2012/03/06 14:28:40</ogr:timestamp>
2525
</ogr:import_photos>

src/core/qgsvectorfilewriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
20882088
int ogrField = it.value();
20892089

20902090
QVariant attrValue = feature.attribute( fldIdx );
2091+
QgsField field = mFields.at( fldIdx );
20912092

20922093
if ( !attrValue.isValid() || attrValue.isNull() )
20932094
{
@@ -2106,17 +2107,16 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
21062107

21072108
if ( mFieldValueConverter )
21082109
{
2110+
field = mFieldValueConverter->fieldDefinition( field );
21092111
attrValue = mFieldValueConverter->convert( fldIdx, attrValue );
21102112
}
21112113

2112-
switch ( attrValue.type() )
2114+
switch ( field.type() )
21132115
{
21142116
case QVariant::Int:
2115-
case QVariant::UInt:
21162117
OGR_F_SetFieldInteger( poFeature.get(), ogrField, attrValue.toInt() );
21172118
break;
21182119
case QVariant::LongLong:
2119-
case QVariant::ULongLong:
21202120
OGR_F_SetFieldInteger64( poFeature.get(), ogrField, attrValue.toLongLong() );
21212121
break;
21222122
case QVariant::Bool:

tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ def testWrite(self):
9494

9595
writeShape(self.mMemoryLayer, 'writetest.shp')
9696

97+
def testWriteWithLongLongField(self):
98+
ml = QgsVectorLayer('NoGeometry?crs=epsg:4326&field=fldlonglong:long',
99+
'test2', 'memory')
100+
provider = ml.dataProvider()
101+
feat = QgsFeature()
102+
feat.setAttributes([2262000000])
103+
provider.addFeatures([feat])
104+
105+
filename = os.path.join(str(QDir.tempPath()), 'with_longlong_field')
106+
crs = QgsCoordinateReferenceSystem()
107+
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
108+
rc, errmsg = QgsVectorFileWriter.writeAsVectorFormat(ml, filename, 'utf-8', crs, 'GPKG')
109+
110+
# open the resulting geopackage
111+
vl = QgsVectorLayer(filename + '.gpkg', '', 'ogr')
112+
self.assertTrue(vl.isValid())
113+
114+
# test values
115+
idx = vl.fields().indexFromName('fldlonglong')
116+
self.assertEqual(vl.getFeature(1).attributes()[idx], 2262000000)
117+
97118
def testWriteWithBoolField(self):
98119

99120
# init connection string

0 commit comments

Comments
 (0)