Skip to content

Commit fda97b2

Browse files
committedJul 8, 2017
[OGR] Use OGR_F_SetFieldNull() with GDAL >= 2.2 to avoid GeoJSON fields to be unset (fixes #16812)
1 parent f6de1d4 commit fda97b2

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed
 

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,19 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
19761976
QVariant attrValue = feature.attribute( fldIdx );
19771977

19781978
if ( !attrValue.isValid() || attrValue.isNull() )
1979+
{
1980+
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
1981+
// whereas previously there was only unset fields. For a GeoJSON output,
1982+
// leaving a field unset will cause it to not appear at all in the output
1983+
// feature.
1984+
// When all features of a layer have a field unset, this would cause the
1985+
// field to not be present at all in the output, and thus on reading to
1986+
// have disappeared. #16812
1987+
#ifdef OGRNullMarker
1988+
OGR_F_SetFieldNull( poFeature, ogrField );
1989+
#endif
19791990
continue;
1991+
}
19801992

19811993
if ( mFieldValueConverter )
19821994
{

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,18 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
12721272
QVariant attrVal = attrs.at( qgisAttId );
12731273
if ( attrVal.isNull() || ( type != OFTString && attrVal.toString().isEmpty() ) )
12741274
{
1275+
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
1276+
// whereas previously there was only unset fields. For a GeoJSON output,
1277+
// leaving a field unset will cause it to not appear at all in the output
1278+
// feature.
1279+
// When all features of a layer have a field unset, this would cause the
1280+
// field to not be present at all in the output, and thus on reading to
1281+
// have disappeared. #16812
1282+
#ifdef OGRNullMarker
1283+
OGR_F_SetFieldNull( feature, ogrAttId );
1284+
#else
12751285
OGR_F_UnsetField( feature, ogrAttId );
1286+
#endif
12761287
}
12771288
else
12781289
{
@@ -1683,7 +1694,18 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
16831694

16841695
if ( it2->isNull() || ( type != OFTString && it2->toString().isEmpty() ) )
16851696
{
1697+
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
1698+
// whereas previously there was only unset fields. For a GeoJSON output,
1699+
// leaving a field unset will cause it to not appear at all in the output
1700+
// feature.
1701+
// When all features of a layer have a field unset, this would cause the
1702+
// field to not be present at all in the output, and thus on reading to
1703+
// have disappeared. #16812
1704+
#ifdef OGRNullMarker
1705+
OGR_F_SetFieldNull( of, f );
1706+
#else
16861707
OGR_F_UnsetField( of, f );
1708+
#endif
16871709
}
16881710
else
16891711
{

‎tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,17 @@ def testOverwriteLayer(self):
675675
self.assertIsNotNone(lyr)
676676
f = lyr.GetNextFeature()
677677
self.assertEqual(f['firstfield'], 3)
678-
self.assertFalse(f.IsFieldSet('secondfield'))
678+
if hasattr(f, "IsFieldSetAndNotNull"):
679+
# GDAL >= 2.2
680+
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
681+
else:
682+
self.assertFalse(f.IsFieldSet('secondfield'))
679683
f = lyr.GetNextFeature()
680684
self.assertEqual(f['firstfield'], 4)
681-
self.assertFalse(f.IsFieldSet('secondfield'))
685+
if hasattr(f, "IsFieldSetAndNotNull"):
686+
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
687+
else:
688+
self.assertFalse(f.IsFieldSet('secondfield'))
682689
f = lyr.GetNextFeature()
683690
self.assertEqual(f['firstfield'], 5)
684691
self.assertEqual(f['secondfield'], -1)

0 commit comments

Comments
 (0)
Please sign in to comment.