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

(relying on attribute value type isn't reliable under certain
scenarios, including memory layers created through a processing
python algorithm)
This commit is contained in:
Mathieu Pellerin 2018-08-06 11:07:56 +07:00 committed by GitHub
parent 317b310525
commit 51b63e6b46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 4 deletions

View File

@ -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>

View File

@ -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:

View File

@ -94,6 +94,27 @@ class TestQgsVectorFileWriter(unittest.TestCase):
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