Skip to content

Commit f834a7e

Browse files
committedDec 18, 2018
[ogr] Fix conversion of boolean values
Fixes #20290 (cherry picked from commit 645ca2b)
1 parent 904c797 commit f834a7e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed
 

‎src/core/qgsogrutils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,11 @@ QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField
197197
break;
198198
}
199199
case QVariant::Int:
200-
case QVariant::Bool:
201200
value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) );
202201
break;
202+
case QVariant::Bool:
203+
value = QVariant( bool( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) ) );
204+
break;
203205
case QVariant::LongLong:
204206
value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) );
205207
break;

‎tests/src/python/test_provider_ogr.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
from osgeo import gdal, ogr # NOQA
2121
from qgis.PyQt.QtCore import QVariant
22-
from qgis.core import (QgsApplication,
22+
from qgis.core import (NULL,
23+
QgsApplication,
2324
QgsRectangle,
2425
QgsProviderRegistry,
2526
QgsFeature, QgsFeatureRequest, QgsField, QgsSettings, QgsDataProvider,
@@ -489,6 +490,15 @@ def testOSM(self):
489490
self.assertEqual(f.id(), 8)
490491
del it
491492

493+
def testBoolFieldEvaluation(self):
494+
datasource = os.path.join(unitTestDataPath(), 'bool_geojson.json')
495+
vl = QgsVectorLayer(datasource, 'test', 'ogr')
496+
self.assertTrue(vl.isValid())
497+
self.assertEqual(vl.fields().at(0).name(), 'bool')
498+
self.assertEqual(vl.fields().at(0).type(), QVariant.Bool)
499+
self.assertEqual([f[0] for f in vl.getFeatures()], [True, False, NULL])
500+
self.assertEqual([f[0].__class__.__name__ for f in vl.getFeatures()], ['bool', 'bool', 'QVariant'])
501+
492502

493503
if __name__ == '__main__':
494504
unittest.main()

‎tests/testdata/bool_geojson.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "FeatureCollection",
3+
"name": "qgis_boolean_bug",
4+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
5+
"features": [
6+
{ "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 ] ] ] } },
7+
{ "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 ] ] ] } },
8+
{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 12.601690134727434, 57.258467784057785 ], [ 12.602218634265689, 57.271240214704655 ], [ 12.620634688693473, 57.266681729179723 ], [ 12.601690134727434, 57.258467784057785 ] ] ] } }
9+
]
10+
}
11+

0 commit comments

Comments
 (0)
Please sign in to comment.