3

I am trying to load a geojson file using the following code:

capa = QgsVectorLayer("C:/...file.geojson","capa","ogr")
if not layer.isValid():
  print("Layer failed to load!")

QgsProject.instance().addMapLayer(capa)

The geojson file contents a FeatureCollection, some points feature and a multiline. But just the poinst are rendered. Cannot load the multiline.

How can I do that stuff?

1
  • 1
    I think the easiest way is to split manually the geojson, if you can Commented Apr 30, 2018 at 10:56

1 Answer 1

4

The QGIS GUI gives a dialog when adding a "multi type GeoJSON", maybe you could dig through the QGIS source to see what QGIS does in the code behind the dialog?

An easy answer would be "Split your input GeoJSON, so that each file contains only one geometry type".

Using ogr2ogr, this will give you 2 shapefiles, one for point and one for line features:

ogr2ogr -f "ESRI Shapefile" -nlt MULTILINESTRING -geomfield GEOM -skipfailures line.shp test.json
ogr2ogr -f "ESRI Shapefile" -nlt POINT -geomfield GEOM -skipfailures point.shp test.json

This is the test.json I tested with:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "this is a line"
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -10.469970703124998,
            51.41291212935532
          ],
          [
            -8.67919921875,
            50.10648772767332
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "this is a point"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -9.964599609375,
          50.43651601698633
        ]
      }
    }
  ]
}
2
  • 2
    The geojson is made by me. Them I think It will a better solution to construct two diferent geojson files instead if using shp files.
    – kamome
    Commented Apr 30, 2018 at 9:56
  • Yeah, thats way better!
    – til_b
    Commented May 1, 2018 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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