Skip to content

Commit

Permalink
Fix __geo_interface__ should use None for NULL values
Browse files Browse the repository at this point in the history
Fixes #58084
  • Loading branch information
nyalldawson committed Jul 15, 2024
1 parent ba2ff0c commit 74ea9e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/core/additions/qgsfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* *
***************************************************************************
"""
from PyQt5.QtCore import QVariant


def mapping_feature(feature):
geom = feature.geometry()
fields = [field.name() for field in feature.fields()]
properties = dict(list(zip(fields, feature.attributes())))
properties = {k: None if (v is None or isinstance(v, QVariant) and v.isNull()) else v for k, v in feature.attributeMap().items()}
return {'type': 'Feature',
'properties': properties,
'geometry': geom.__geo_interface__}
23 changes: 23 additions & 0 deletions tests/src/python/test_qgsfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,29 @@ def testUnsetFeature(self):
self.assertTrue(f.isUnsetValue(3))
self.assertTrue(f.isUnsetValue(4))

def test_geo_interface(self):
fields = QgsFields()
field1 = QgsField('my_field', QVariant.String)
fields.append(field1)
field2 = QgsField('my_field2', QVariant.String)
fields.append(field2)
field3 = QgsField('my_field3', QVariant.Int)
fields.append(field3)

feat = QgsFeature(fields)
feat.setAttributes(['abc', 'def', 123])
feat.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(123, 456)))
self.assertEqual(feat.__geo_interface__, {'geometry': {'coordinates': [123.0, 456.0], 'type': 'Point'},
'properties': {'my_field': 'abc', 'my_field2': 'def', 'my_field3': 123},
'type': 'Feature'})

feat.setAttributes([NULL, None, NULL])
self.assertEqual(feat.__geo_interface__, {
'geometry': {'coordinates': [123.0, 456.0], 'type': 'Point'},
'properties': {'my_field': None, 'my_field2': None,
'my_field3': None},
'type': 'Feature'})


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

0 comments on commit 74ea9e6

Please sign in to comment.