Skip to content

Commit

Permalink
[ogr] Fix conversion of boolean values
Browse files Browse the repository at this point in the history
Fixes #20290
  • Loading branch information
nyalldawson committed Dec 17, 2018
1 parent 4131e0b commit 645ca2b
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
11 changes: 11 additions & 0 deletions tests/testdata/bool_geojson.json
Original file line number Diff line number Diff line change
@@ -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.