mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-28 00:05:04 -04:00
[OGR] Use OGR_F_SetFieldNull() with GDAL >= 2.2 to avoid GeoJSON fields to be unset (fixes #16812)
This commit is contained in:
parent
40d833ff5f
commit
7d67b02ae4
@ -1949,7 +1949,19 @@ OGRFeatureH QgsVectorFileWriter::createFeature( const QgsFeature &feature )
|
|||||||
QVariant attrValue = feature.attribute( fldIdx );
|
QVariant attrValue = feature.attribute( fldIdx );
|
||||||
|
|
||||||
if ( !attrValue.isValid() || attrValue.isNull() )
|
if ( !attrValue.isValid() || attrValue.isNull() )
|
||||||
|
{
|
||||||
|
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
|
||||||
|
// whereas previously there was only unset fields. For a GeoJSON output,
|
||||||
|
// leaving a field unset will cause it to not appear at all in the output
|
||||||
|
// feature.
|
||||||
|
// When all features of a layer have a field unset, this would cause the
|
||||||
|
// field to not be present at all in the output, and thus on reading to
|
||||||
|
// have disappeared. #16812
|
||||||
|
#ifdef OGRNullMarker
|
||||||
|
OGR_F_SetFieldNull( poFeature, ogrField );
|
||||||
|
#endif
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ( mFieldValueConverter )
|
if ( mFieldValueConverter )
|
||||||
{
|
{
|
||||||
|
@ -1289,7 +1289,18 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
|
|||||||
QVariant attrVal = attrs.at( qgisAttId );
|
QVariant attrVal = attrs.at( qgisAttId );
|
||||||
if ( attrVal.isNull() || ( type != OFTString && attrVal.toString().isEmpty() ) )
|
if ( attrVal.isNull() || ( type != OFTString && attrVal.toString().isEmpty() ) )
|
||||||
{
|
{
|
||||||
|
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
|
||||||
|
// whereas previously there was only unset fields. For a GeoJSON output,
|
||||||
|
// leaving a field unset will cause it to not appear at all in the output
|
||||||
|
// feature.
|
||||||
|
// When all features of a layer have a field unset, this would cause the
|
||||||
|
// field to not be present at all in the output, and thus on reading to
|
||||||
|
// have disappeared. #16812
|
||||||
|
#ifdef OGRNullMarker
|
||||||
|
OGR_F_SetFieldNull( feature, ogrAttId );
|
||||||
|
#else
|
||||||
OGR_F_UnsetField( feature, ogrAttId );
|
OGR_F_UnsetField( feature, ogrAttId );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1684,7 +1695,18 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
|
|||||||
|
|
||||||
if ( it2->isNull() || ( type != OFTString && it2->toString().isEmpty() ) )
|
if ( it2->isNull() || ( type != OFTString && it2->toString().isEmpty() ) )
|
||||||
{
|
{
|
||||||
|
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
|
||||||
|
// whereas previously there was only unset fields. For a GeoJSON output,
|
||||||
|
// leaving a field unset will cause it to not appear at all in the output
|
||||||
|
// feature.
|
||||||
|
// When all features of a layer have a field unset, this would cause the
|
||||||
|
// field to not be present at all in the output, and thus on reading to
|
||||||
|
// have disappeared. #16812
|
||||||
|
#ifdef OGRNullMarker
|
||||||
|
OGR_F_SetFieldNull( of, f );
|
||||||
|
#else
|
||||||
OGR_F_UnsetField( of, f );
|
OGR_F_UnsetField( of, f );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -703,10 +703,17 @@ class TestQgsVectorFileWriter(unittest.TestCase):
|
|||||||
self.assertIsNotNone(lyr)
|
self.assertIsNotNone(lyr)
|
||||||
f = lyr.GetNextFeature()
|
f = lyr.GetNextFeature()
|
||||||
self.assertEqual(f['firstfield'], 3)
|
self.assertEqual(f['firstfield'], 3)
|
||||||
self.assertFalse(f.IsFieldSet('secondfield'))
|
if hasattr(f, "IsFieldSetAndNotNull"):
|
||||||
|
# GDAL >= 2.2
|
||||||
|
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
|
||||||
|
else:
|
||||||
|
self.assertFalse(f.IsFieldSet('secondfield'))
|
||||||
f = lyr.GetNextFeature()
|
f = lyr.GetNextFeature()
|
||||||
self.assertEqual(f['firstfield'], 4)
|
self.assertEqual(f['firstfield'], 4)
|
||||||
self.assertFalse(f.IsFieldSet('secondfield'))
|
if hasattr(f, "IsFieldSetAndNotNull"):
|
||||||
|
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
|
||||||
|
else:
|
||||||
|
self.assertFalse(f.IsFieldSet('secondfield'))
|
||||||
f = lyr.GetNextFeature()
|
f = lyr.GetNextFeature()
|
||||||
self.assertEqual(f['firstfield'], 5)
|
self.assertEqual(f['firstfield'], 5)
|
||||||
self.assertEqual(f['secondfield'], -1)
|
self.assertEqual(f['secondfield'], -1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user