6

I have 2 shapefiles in QGIS (one point layer and one Line layer as shown in Figure). I am trying to save these two layers in one GeoJSON. I have seen some of the previous answers such as answer [1] , answer [2] but they are merging the files into one with UNION functions.

  • My goal is not to merge the features but to save the two layers in one GeoJSON file. For instance, when I import the GeoJSON file to QGIS it should show the point and line layer seperately as show in Figure.
  • I used Export Features As / GeoJSON but it only save 1 layer.

enter image description here

2
  • What if you use the Geometry type > GeometryCollection option on your export? – nmtoken Jan 15 at 12:28
  • I mean I want to group two shapefiles into one GeoJSON as shown here. i.stack.imgur.com/4Ztwj.png . In other words, I want to export two Layers into 1 GeoJSON and when I drag the GeoJSON file again to QGIS, It should look ike this. i.stack.imgur.com/qpxxj.png – user12729159 Jan 16 at 12:27
1

If the point and line layers have the same attributes it is possible to save them one by one into the same GeoJSON file. Save the first layer as a new file. Then save the second layer with the same name and select option "Append to layer".

enter image description here

All the features, both points and lines, are now in the same GeoJSON file. For GeoJSON that's not any problem. QGIS that cannot handle different geometry types on the same layer is scanning the GeoJSON file before opening it and shows what sort of geometries the file contain. They appear like separate layers and user can open one or more layers. It is also possible to select just line layer, edit it, and save the changes without affecting the points in the GeoJSON file.

enter image description here

It is also easy to create the merged GeoJSON file with ogr2ogr. Initial command:

ogr2ogr -f geojson merged.json lines.json -nln merged

Next commands:

ogr2ogr -f geojson -update -append merged.json points.json -nln merged
3

GeoJSON can have only one layer.

https://tools.ietf.org/html/rfc7946#section-2

  1. GeoJSON Text

A GeoJSON text is a JSON text and consists of a single GeoJSON object.

  1. GeoJSON Object

A GeoJSON object represents a Geometry, Feature, or collection of Features.

For GDAL and QGIS this means https://gdal.org/drivers/vector/geojson.html

A GeoJSON datasource is translated to single OGRLayer object with pre-defined name OGRGeoJSON:

ogrinfo -ro http://featureserver/data/.geojson OGRGeoJSON

It is also valid to assume that OGRDataSource::GetLayerCount() for GeoJSON datasource always returns 1.

5
  • I think OP is looking for a GeometryCollection, e.g. stackoverflow.com/questions/34044893/… ? – Timothy Dalton Jan 15 at 9:31
  • I mean I want to group two shapefiles into one GeoJSON as shown here. i.stack.imgur.com/4Ztwj.png . In other words, I want to export two Layers into 1 GeoJSON and when I drag the GeoJSON file again to QGIS, It should look like this. i.stack.imgur.com/qpxxj.png – user12729159 Jan 16 at 12:28
  • 1
    If the point shapefile and line shapefile have different attribute schemas then you can't do what you want with GeoJSON. Use a format that supports multiple layers (GML, GeoPackage). If attributes are the same for both shapefiles it might be possible to use a workaround by merging the layers into one for writing and split by geometry type when reading. – user30184 Jan 16 at 15:43
  • If attributes are the same for both shapefiles, what could be the potential solution? – user12729159 Jan 17 at 11:52
  • I tested the solution as posted it as a separate answer. – user30184 Jan 17 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.