Skip to content
Permalink
Browse files

[vector file writer] use field type to save attribute values

  • Loading branch information
nirvn committed Aug 6, 2018
1 parent dbbbdbd commit 71f9910794348f30248c731a6e57df8dd3f30227
@@ -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>
@@ -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() )
{
@@ -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:
@@ -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

0 comments on commit 71f9910

Please sign in to comment.
You can’t perform that action at this time.