Skip to content

Commit 7d67b02

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 40d833f commit 7d67b02

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
@@ -1949,7 +1949,19 @@ OGRFeatureH QgsVectorFileWriter::createFeature( const QgsFeature &feature )
19491949
QVariant attrValue = feature.attribute( fldIdx );
19501950

19511951
if ( !attrValue.isValid() || attrValue.isNull() )
1952+
{
1953+
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
1954+
// whereas previously there was only unset fields. For a GeoJSON output,
1955+
// leaving a field unset will cause it to not appear at all in the output
1956+
// feature.
1957+
// When all features of a layer have a field unset, this would cause the
1958+
// field to not be present at all in the output, and thus on reading to
1959+
// have disappeared. #16812
1960+
#ifdef OGRNullMarker
1961+
OGR_F_SetFieldNull( poFeature, ogrField );
1962+
#endif
19521963
continue;
1964+
}
19531965

19541966
if ( mFieldValueConverter )
19551967
{

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,18 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
12891289
QVariant attrVal = attrs.at( qgisAttId );
12901290
if ( attrVal.isNull() || ( type != OFTString && attrVal.toString().isEmpty() ) )
12911291
{
1292+
// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
1293+
// whereas previously there was only unset fields. For a GeoJSON output,
1294+
// leaving a field unset will cause it to not appear at all in the output
1295+
// feature.
1296+
// When all features of a layer have a field unset, this would cause the
1297+
// field to not be present at all in the output, and thus on reading to
1298+
// have disappeared. #16812
1299+
#ifdef OGRNullMarker
1300+
OGR_F_SetFieldNull( feature, ogrAttId );
1301+
#else
12921302
OGR_F_UnsetField( feature, ogrAttId );
1303+
#endif
12931304
}
12941305
else
12951306
{
@@ -1684,7 +1695,18 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
16841695

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

‎tests/src/python/test_qgsvectorfilewriter.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,17 @@ def testOverwriteLayer(self):
703703
self.assertIsNotNone(lyr)
704704
f = lyr.GetNextFeature()
705705
self.assertEqual(f['firstfield'], 3)
706-
self.assertFalse(f.IsFieldSet('secondfield'))
706+
if hasattr(f, "IsFieldSetAndNotNull"):
707+
# GDAL >= 2.2
708+
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
709+
else:
710+
self.assertFalse(f.IsFieldSet('secondfield'))
707711
f = lyr.GetNextFeature()
708712
self.assertEqual(f['firstfield'], 4)
709-
self.assertFalse(f.IsFieldSet('secondfield'))
713+
if hasattr(f, "IsFieldSetAndNotNull"):
714+
self.assertFalse(f.IsFieldSetAndNotNull('secondfield'))
715+
else:
716+
self.assertFalse(f.IsFieldSet('secondfield'))
710717
f = lyr.GetNextFeature()
711718
self.assertEqual(f['firstfield'], 5)
712719
self.assertEqual(f['secondfield'], -1)

0 commit comments

Comments
 (0)
Please sign in to comment.