Skip to content

Commit

Permalink
Merge pull request #58175 from qgis/backport-58119-to-queued_ltr_back…
Browse files Browse the repository at this point in the history
…ports

[Backport queued_ltr_backports] Fix __geo_interface__ should use None for NULL values
  • Loading branch information
alexbruy authored Jul 19, 2024
2 parents 68e1181 + b361a52 commit 6ffde7a
Show file tree
Hide file tree
Showing 2 changed files with 27 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__}
25 changes: 25 additions & 0 deletions tests/src/python/test_qgsfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import os

import qgis # NOQA
from qgis.PyQt.QtCore import QVariant

from qgis.core import (
NULL,
QgsFeature,
Expand Down Expand Up @@ -305,6 +307,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 6ffde7a

Please sign in to comment.