Skip to content

Commit 754018a

Browse files
committed
[OGR provider] When editing a GeoJSON file, close and re-open the file at end of editing session (fixes #18596)
1 parent bd65fc6 commit 754018a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,6 +4160,13 @@ bool QgsOgrProvider::leaveUpdateMode()
41604160
}
41614161
if ( !mDynamicWriteAccess )
41624162
{
4163+
// The GeoJSON driver only properly flushes stuff in all situations by
4164+
// closing and re-opening. Starting with GDAL 2.3.1, it should be safe to
4165+
// use GDALDatasetFlush().
4166+
if ( mGDALDriverName == QLatin1String( "GeoJSON" ) )
4167+
{
4168+
reloadData();
4169+
}
41634170
return true;
41644171
}
41654172
if ( mUpdateModeStackDepth == 0 )

tests/src/python/test_provider_ogr.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ def testSetupProxy(self):
318318
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com")
319319
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username")
320320

321+
def testEditGeoJson(self):
322+
""" Test bugfix of https://issues.qgis.org/issues/18596 """
323+
324+
datasource = os.path.join(self.basetestpath, 'testEditGeoJson.json')
325+
with open(datasource, 'wt') as f:
326+
f.write("""{
327+
"type": "FeatureCollection",
328+
"features": [
329+
{ "type": "Feature", "properties": { "x": 1, "y": 2, "z": 3, "w": 4 }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] } } ] }""")
330+
331+
vl = QgsVectorLayer(datasource, 'test', 'ogr')
332+
self.assertTrue(vl.isValid())
333+
self.assertTrue(vl.startEditing())
334+
self.assertTrue(vl.deleteAttribute(1))
335+
self.assertTrue(vl.commitChanges())
336+
337+
f = QgsFeature()
338+
self.assertTrue(vl.getFeatures(QgsFeatureRequest()).nextFeature(f))
339+
self.assertEqual(f['x'], 1)
340+
self.assertEqual(f['z'], 3)
341+
self.assertEqual(f['w'], 4)
342+
321343

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

0 commit comments

Comments
 (0)