mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-15 00:02:52 -04:00
[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:
parent
317b310525
commit
51b63e6b46
Binary file not shown.
@ -19,7 +19,7 @@
|
|||||||
<ogr:directory>/home/nyall/dev/QGIS/python/plugins/processing/tests/testdata/custom/photos</ogr:directory>
|
<ogr:directory>/home/nyall/dev/QGIS/python/plugins/processing/tests/testdata/custom/photos</ogr:directory>
|
||||||
<ogr:altitude>422.191</ogr:altitude>
|
<ogr:altitude>422.191</ogr:altitude>
|
||||||
<ogr:direction>59.9153</ogr:direction>
|
<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:latitude>-37.2305</ogr:latitude>
|
||||||
<ogr:timestamp>2012/03/06 14:28:40</ogr:timestamp>
|
<ogr:timestamp>2012/03/06 14:28:40</ogr:timestamp>
|
||||||
</ogr:import_photos>
|
</ogr:import_photos>
|
||||||
|
@ -2088,6 +2088,7 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
|
|||||||
int ogrField = it.value();
|
int ogrField = it.value();
|
||||||
|
|
||||||
QVariant attrValue = feature.attribute( fldIdx );
|
QVariant attrValue = feature.attribute( fldIdx );
|
||||||
|
QgsField field = mFields.at( fldIdx );
|
||||||
|
|
||||||
if ( !attrValue.isValid() || attrValue.isNull() )
|
if ( !attrValue.isValid() || attrValue.isNull() )
|
||||||
{
|
{
|
||||||
@ -2106,17 +2107,16 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
|
|||||||
|
|
||||||
if ( mFieldValueConverter )
|
if ( mFieldValueConverter )
|
||||||
{
|
{
|
||||||
|
field = mFieldValueConverter->fieldDefinition( field );
|
||||||
attrValue = mFieldValueConverter->convert( fldIdx, attrValue );
|
attrValue = mFieldValueConverter->convert( fldIdx, attrValue );
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ( attrValue.type() )
|
switch ( field.type() )
|
||||||
{
|
{
|
||||||
case QVariant::Int:
|
case QVariant::Int:
|
||||||
case QVariant::UInt:
|
|
||||||
OGR_F_SetFieldInteger( poFeature.get(), ogrField, attrValue.toInt() );
|
OGR_F_SetFieldInteger( poFeature.get(), ogrField, attrValue.toInt() );
|
||||||
break;
|
break;
|
||||||
case QVariant::LongLong:
|
case QVariant::LongLong:
|
||||||
case QVariant::ULongLong:
|
|
||||||
OGR_F_SetFieldInteger64( poFeature.get(), ogrField, attrValue.toLongLong() );
|
OGR_F_SetFieldInteger64( poFeature.get(), ogrField, attrValue.toLongLong() );
|
||||||
break;
|
break;
|
||||||
case QVariant::Bool:
|
case QVariant::Bool:
|
||||||
|
@ -94,6 +94,27 @@ class TestQgsVectorFileWriter(unittest.TestCase):
|
|||||||
|
|
||||||
writeShape(self.mMemoryLayer, 'writetest.shp')
|
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):
|
def testWriteWithBoolField(self):
|
||||||
|
|
||||||
# init connection string
|
# init connection string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user