Skip to content

Commit

Permalink
[OGR provider] Complementary fix for #18596, related to adding a new …
Browse files Browse the repository at this point in the history
…field to a GeoJSON file
  • Loading branch information
rouault committed Jun 5, 2018
1 parent 8ef0d1f commit e94b1ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -4264,7 +4264,14 @@ bool QgsOgrProvider::leaveUpdateMode()
// use GDALDatasetFlush().
if ( mGDALDriverName == QLatin1String( "GeoJSON" ) )
{
// Backup fields since if we created new fields, but didn't populate it
// with any feature yet, it will disappear.
QgsFields oldFields = mAttributeFields;
reloadData();
if ( mValid )
{
mAttributeFields = oldFields;
}
}
return true;
}
Expand Down
37 changes: 33 additions & 4 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -18,7 +18,8 @@
import tempfile

from osgeo import gdal, ogr # NOQA
from qgis.core import (QgsFeature, QgsFeatureRequest, QgsSettings, QgsDataProvider,
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFeature, QgsFeatureRequest, QgsField, QgsSettings, QgsDataProvider,
QgsVectorDataProvider, QgsVectorLayer, QgsWkbTypes, QgsNetworkAccessManager)
from qgis.testing import start_app, unittest

Expand Down Expand Up @@ -318,10 +319,10 @@ def testSetupProxy(self):
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com")
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username")

def testEditGeoJson(self):
""" Test bugfix of https://issues.qgis.org/issues/18596 """
def testEditGeoJsonRemoveField(self):
""" Test bugfix of https://issues.qgis.org/issues/18596 (deleting an existing field)"""

datasource = os.path.join(self.basetestpath, 'testEditGeoJson.json')
datasource = os.path.join(self.basetestpath, 'testEditGeoJsonRemoveField.json')
with open(datasource, 'wt') as f:
f.write("""{
"type": "FeatureCollection",
Expand All @@ -333,13 +334,41 @@ def testEditGeoJson(self):
self.assertTrue(vl.startEditing())
self.assertTrue(vl.deleteAttribute(1))
self.assertTrue(vl.commitChanges())
self.assertEqual(len(vl.dataProvider().fields()), 4 - 1)

f = QgsFeature()
self.assertTrue(vl.getFeatures(QgsFeatureRequest()).nextFeature(f))
self.assertEqual(f['x'], 1)
self.assertEqual(f['z'], 3)
self.assertEqual(f['w'], 4)

def testEditGeoJsonAddField(self):
""" Test bugfix of https://issues.qgis.org/issues/18596 (adding a new field)"""

datasource = os.path.join(self.basetestpath, 'testEditGeoJsonAddField.json')
with open(datasource, 'wt') as f:
f.write("""{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "x": 1 }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] } } ] }""")

vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertTrue(vl.startEditing())
self.assertTrue(vl.addAttribute(QgsField('strfield', QVariant.String)))
self.assertTrue(vl.commitChanges())
self.assertEqual(len(vl.dataProvider().fields()), 1 + 1)

f = QgsFeature()
self.assertTrue(vl.getFeatures(QgsFeatureRequest()).nextFeature(f))
self.assertIsNone(f['strfield'])

# Completely reload file
vl = QgsVectorLayer(datasource, 'test', 'ogr')
# As we didn't set any value to the new field, it is not written at
# all in the GeoJSON file, so it has disappeared
self.assertEqual(len(vl.fields()), 1)


if __name__ == '__main__':
unittest.main()

1 comment on commit e94b1ac

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rouault !

Please sign in to comment.