Skip to content

Commit

Permalink
[vector file writer] use field type to save attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Aug 6, 2018
1 parent dbbbdbd commit 71f9910
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Expand Up @@ -19,7 +19,7 @@
<ogr:directory>/home/nyall/dev/QGIS/python/plugins/processing/tests/testdata/custom/photos</ogr:directory>
<ogr:altitude>422.191</ogr:altitude>
<ogr:direction>59.9153</ogr:direction>
<ogr:longitude>149.2751666666667</ogr:longitude>
<ogr:longitude>149.27516666666668</ogr:longitude>
<ogr:latitude>-37.2305</ogr:latitude>
<ogr:timestamp>2012/03/06 14:28:40</ogr:timestamp>
</ogr:import_photos>
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2088,6 +2088,7 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
int ogrField = it.value();

QVariant attrValue = feature.attribute( fldIdx );
QgsField field = mFields.at( fldIdx );

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

if ( mFieldValueConverter )
{
field = mFieldValueConverter->fieldDefinition( field );
attrValue = mFieldValueConverter->convert( fldIdx, attrValue );
}

switch ( attrValue.type() )
switch ( field.type() )
{
case QVariant::Int:
case QVariant::UInt:
OGR_F_SetFieldInteger( poFeature.get(), ogrField, attrValue.toInt() );
break;
case QVariant::LongLong:
case QVariant::ULongLong:
OGR_F_SetFieldInteger64( poFeature.get(), ogrField, attrValue.toLongLong() );
break;
case QVariant::Bool:
Expand Down
21 changes: 21 additions & 0 deletions tests/src/python/test_qgsvectorfilewriter.py
Expand Up @@ -94,6 +94,27 @@ def testWrite(self):

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

def testWriteWithLongLongField(self):
ml = QgsVectorLayer('NoGeometry?crs=epsg:4326&field=fldlonglong:long',
'test2', 'memory')
provider = ml.dataProvider()
feat = QgsFeature()
feat.setAttributes([2262000000])
provider.addFeatures([feat])

filename = os.path.join(str(QDir.tempPath()), 'with_longlong_field')
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
rc, errmsg = QgsVectorFileWriter.writeAsVectorFormat(ml, filename, 'utf-8', crs, 'GPKG')

# open the resulting geopackage
vl = QgsVectorLayer(filename + '.gpkg', '', 'ogr')
self.assertTrue(vl.isValid())

# test values
idx = vl.fields().indexFromName('fldlonglong')
self.assertEqual(vl.getFeature(1).attributes()[idx], 2262000000)

def testWriteWithBoolField(self):

# init connection string
Expand Down

0 comments on commit 71f9910

Please sign in to comment.