Skip to content
Permalink
Browse files

[ogr] Fix conversion of boolean values

Fixes #20290
  • Loading branch information
nyalldawson committed Dec 14, 2018
1 parent 4131e0b commit 645ca2b3510b4226b509a05c212170748bea51e3
Showing with 25 additions and 2 deletions.
  1. +3 −1 src/core/qgsogrutils.cpp
  2. +11 −1 tests/src/python/test_provider_ogr.py
  3. +11 −0 tests/testdata/bool_geojson.json
@@ -197,9 +197,11 @@ QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField
break;
}
case QVariant::Int:
case QVariant::Bool:
value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) );
break;
case QVariant::Bool:
value = QVariant( bool( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) ) );
break;
case QVariant::LongLong:
value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) );
break;
@@ -20,7 +20,8 @@

from osgeo import gdal, ogr # NOQA
from qgis.PyQt.QtCore import QVariant, QByteArray
from qgis.core import (QgsApplication,
from qgis.core import (NULL,
QgsApplication,
QgsRectangle,
QgsProviderRegistry,
QgsFeature, QgsFeatureRequest, QgsField, QgsSettings, QgsDataProvider,
@@ -544,6 +545,15 @@ def testBlobCreation(self):
f2 = [f for f in dp.getFeatures()][1]
self.assertEqual(f2.attributes(), [2, 'str2', 200, QByteArray(bin_1)])

def testBoolFieldEvaluation(self):
datasource = os.path.join(unitTestDataPath(), 'bool_geojson.json')
vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(vl.fields().at(0).name(), 'bool')
self.assertEqual(vl.fields().at(0).type(), QVariant.Bool)
self.assertEqual([f[0] for f in vl.getFeatures()], [True, False, NULL])
self.assertEqual([f[0].__class__.__name__ for f in vl.getFeatures()], ['bool', 'bool', 'QVariant'])


if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,11 @@
{
"type": "FeatureCollection",
"name": "qgis_boolean_bug",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "bool":true }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 12.561367491202688, 57.265545355012357 ], [ 12.578188838268714, 57.271515650824504 ], [ 12.580543376998371, 57.25634426931056 ], [ 12.561367491202688, 57.265545355012357 ] ] ] } },
{ "type": "Feature", "properties": { "bool":false }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 12.601690134727434, 57.258467784057785 ], [ 12.602218634265689, 57.271240214704655 ], [ 12.620634688693473, 57.266681729179723 ], [ 12.601690134727434, 57.258467784057785 ] ] ] } },
{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 12.601690134727434, 57.258467784057785 ], [ 12.602218634265689, 57.271240214704655 ], [ 12.620634688693473, 57.266681729179723 ], [ 12.601690134727434, 57.258467784057785 ] ] ] } }
]
}

0 comments on commit 645ca2b

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