18
18
import tempfile
19
19
20
20
from osgeo import gdal , ogr # NOQA
21
- from qgis .core import (QgsFeature , QgsFeatureRequest , QgsSettings , QgsDataProvider ,
21
+ from qgis .PyQt .QtCore import QVariant
22
+ from qgis .core import (QgsFeature , QgsFeatureRequest , QgsField , QgsSettings , QgsDataProvider ,
22
23
QgsVectorDataProvider , QgsVectorLayer , QgsWkbTypes , QgsNetworkAccessManager )
23
24
from qgis .testing import start_app , unittest
24
25
@@ -318,10 +319,10 @@ def testSetupProxy(self):
318
319
self .assertEqual (gdal .GetConfigOption ("GDAL_HTTP_PROXY" ), "myproxyhostname.com" )
319
320
self .assertEqual (gdal .GetConfigOption ("GDAL_HTTP_PROXYUSERPWD" ), "username" )
320
321
321
- def testEditGeoJson (self ):
322
- """ Test bugfix of https://issues.qgis.org/issues/18596 """
322
+ def testEditGeoJsonRemoveField (self ):
323
+ """ Test bugfix of https://issues.qgis.org/issues/18596 (deleting an existing field) """
323
324
324
- datasource = os .path .join (self .basetestpath , 'testEditGeoJson .json' )
325
+ datasource = os .path .join (self .basetestpath , 'testEditGeoJsonRemoveField .json' )
325
326
with open (datasource , 'wt' ) as f :
326
327
f .write ("""{
327
328
"type": "FeatureCollection",
@@ -333,13 +334,41 @@ def testEditGeoJson(self):
333
334
self .assertTrue (vl .startEditing ())
334
335
self .assertTrue (vl .deleteAttribute (1 ))
335
336
self .assertTrue (vl .commitChanges ())
337
+ self .assertEqual (len (vl .dataProvider ().fields ()), 4 - 1 )
336
338
337
339
f = QgsFeature ()
338
340
self .assertTrue (vl .getFeatures (QgsFeatureRequest ()).nextFeature (f ))
339
341
self .assertEqual (f ['x' ], 1 )
340
342
self .assertEqual (f ['z' ], 3 )
341
343
self .assertEqual (f ['w' ], 4 )
342
344
345
+ def testEditGeoJsonAddField (self ):
346
+ """ Test bugfix of https://issues.qgis.org/issues/18596 (adding a new field)"""
347
+
348
+ datasource = os .path .join (self .basetestpath , 'testEditGeoJsonAddField.json' )
349
+ with open (datasource , 'wt' ) as f :
350
+ f .write ("""{
351
+ "type": "FeatureCollection",
352
+ "features": [
353
+ { "type": "Feature", "properties": { "x": 1 }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] } } ] }""" )
354
+
355
+ vl = QgsVectorLayer (datasource , 'test' , 'ogr' )
356
+ self .assertTrue (vl .isValid ())
357
+ self .assertTrue (vl .startEditing ())
358
+ self .assertTrue (vl .addAttribute (QgsField ('strfield' , QVariant .String )))
359
+ self .assertTrue (vl .commitChanges ())
360
+ self .assertEqual (len (vl .dataProvider ().fields ()), 1 + 1 )
361
+
362
+ f = QgsFeature ()
363
+ self .assertTrue (vl .getFeatures (QgsFeatureRequest ()).nextFeature (f ))
364
+ self .assertIsNone (f ['strfield' ])
365
+
366
+ # Completely reload file
367
+ vl = QgsVectorLayer (datasource , 'test' , 'ogr' )
368
+ # As we didn't set any value to the new field, it is not written at
369
+ # all in the GeoJSON file, so it has disappeared
370
+ self .assertEqual (len (vl .fields ()), 1 )
371
+
343
372
344
373
if __name__ == '__main__' :
345
374
unittest .main ()
1 commit comments
nyalldawson commentedon Jun 5, 2018
Thanks @rouault !