Skip to content

Commit

Permalink
[OGR provider] When editing a GeoJSON file, close and re-open the fil…
Browse files Browse the repository at this point in the history
…e at end of editing session (fixes #18596)
  • Loading branch information
rouault committed Jun 2, 2018
1 parent bd65fc6 commit 754018a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -4160,6 +4160,13 @@ bool QgsOgrProvider::leaveUpdateMode()
}
if ( !mDynamicWriteAccess )
{
// The GeoJSON driver only properly flushes stuff in all situations by
// closing and re-opening. Starting with GDAL 2.3.1, it should be safe to
// use GDALDatasetFlush().
if ( mGDALDriverName == QLatin1String( "GeoJSON" ) )
{
reloadData();
}
return true;
}
if ( mUpdateModeStackDepth == 0 )
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -318,6 +318,28 @@ 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 """

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

vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertTrue(vl.startEditing())
self.assertTrue(vl.deleteAttribute(1))
self.assertTrue(vl.commitChanges())

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


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

2 comments on commit 754018a

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

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

@rouault I'm seeing a regression in a plugin's test suite since this commit.

Steps to reproduce:

  • geojson layer
  • startEditing on the layer
  • add an attribute to the layer
  • commit changes
  • field is added to the provider without issue by ogr
  • QgsOgrProvider::leaveUpdateMode is called by QgsVectorLayer::commitChanges()
  • reloadData() is called by this commit
  • new field is no longer present

@rouault
Copy link
Contributor Author

@rouault rouault commented on 754018a Jun 5, 2018

Choose a reason for hiding this comment

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

Should be fixed with e94b1ac

Please sign in to comment.