Bug report #18370

Save an empty layer to GeoJSON drop all fields

Added by Etienne Trimaille over 6 years ago. Updated over 6 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Data Provider
Affected QGIS version:3.1(master) Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:26260

Description

I could replicate this issue on both QGIS 2.18 and 3.1, with Python script and using the UI.

If I create an empty memory layer and if I save to geojson, then all my fields are lost:

The python code shows the problem:

v = QgsVectorLayer('Point?crs=epsg:4326&field=id:integer&field=name:string(20)', 'test', 'memory')
print(v.isValid())
print(v.fields().count())

path = '/tmp/test_geojson.geojson'
QgsVectorFileWriter.writeAsVectorFormat(v, path, 'utf-8', v.crs(), 'GeoJSON')
export_layer = QgsVectorLayer(path, 'exported', 'ogr')
print(export_layer.isValid())
print(export_layer.fields().count())

It will output on both QGIS 2 and 3:

True
2
True
0

I hope it's not some layer/dataset creation option. I looked in the OGR documentation too. Nothing I could notice.

This is an issue because I expect to have the same attribute table.

Screen Shot 2018-03-07 at 10.45.18.png (181 KB) Etienne Trimaille, 2018-03-07 10:48 AM

History

#1 Updated by Etienne Trimaille over 6 years ago

Well, it seems it's not a bug. Because attributes in GeoJSON are defined at the feature level, not at the layer level. So obviously, if no features, then no attribute table.

#2 Updated by Nyall Dawson over 6 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

Also available in: Atom PDF