Skip to content

Commit

Permalink
[ogr] Fix conversion of boolean values
Browse files Browse the repository at this point in the history
Fixes #20290

(cherry picked from commit 645ca2b)
  • Loading branch information
nyalldawson committed Dec 18, 2018
1 parent 904c797 commit f834a7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core/qgsogrutils.cpp
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion tests/src/python/test_provider_ogr.py
Expand Up @@ -19,7 +19,8 @@

from osgeo import gdal, ogr # NOQA
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsApplication,
from qgis.core import (NULL,
QgsApplication,
QgsRectangle,
QgsProviderRegistry,
QgsFeature, QgsFeatureRequest, QgsField, QgsSettings, QgsDataProvider,
Expand Down Expand Up @@ -489,6 +490,15 @@ def testOSM(self):
self.assertEqual(f.id(), 8)
del it

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()
11 changes: 11 additions & 0 deletions tests/testdata/bool_geojson.json
@@ -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 f834a7e

Please sign in to comment.