Skip to content

Commit

Permalink
[OGR] Use OGR_F_SetFieldNull() with GDAL >= 2.2 to avoid GeoJSON fiel…
Browse files Browse the repository at this point in the history
…ds to be unset (fixes #16812)
  • Loading branch information
rouault committed Jul 8, 2017
1 parent f8d50ce commit fb88071
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -1771,7 +1771,19 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
int ogrField = mAttrIdxToOgrIdx[ fldIdx ];

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;
}

switch ( attrValue.type() )
{
Expand Down
22 changes: 22 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1035,7 +1035,18 @@ bool QgsOgrProvider::addFeature( QgsFeature& f )
QVariant attrVal = attrs.at( targetAttributeId );
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, targetAttributeId );
#else
OGR_F_UnsetField( feature, targetAttributeId );
#endif
}
else
{
Expand Down Expand Up @@ -1323,7 +1334,18 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_

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 );
#endif
}
else
{
Expand Down

0 comments on commit fb88071

Please sign in to comment.