I'd like to merge shapefiles of different geometry types into a single geojson feature collection, but OGR says it doesn't support update of GeoJSON files:

$ ogr2ogr -f GeoJSON -t_srs crs:84 -lco GEOMETRY_AS_COLLECTION merge.geojson points.shp
$ ogr2ogr -f GeoJSON -t_srs crs:84 -update -append merge.geojson -lco GEOMETRY_AS_COLLECTION lines.shp -nln merged
ERROR 4: GeoJSON Driver doesn't support update.
Unable to open existing output datasource `merge.geojson'.

This seems like a common use case to me. Am I missing something obvious in the ogr2ogr manual? I can put together something in python (and finally use fiona) but I'm rather surprised I don't see anything existing when searching here or github... which makes me thing I'm messing something up.



Fortunately, OGR objects model does allow to have geometries of different types in single layer - a heterogeneous layer.

Source: http://www.gdal.org/ogr/drv_geojson.html

For instance, if we have three shapefiles of different geometry types (points.shp, lines.shp and polygons.shp) we can define the union layer using the OGR VRT driver specifying its geometry type as wkbGeometryCollection. So we can write the following VRT file, named i.e. merge.vrt:

    <OGRVRTUnionLayer name="union">
        <OGRVRTLayer name="points">
        <OGRVRTLayer name="lines">
        <OGRVRTLayer name="polygons">

Then, we can easily convert merge.vrt into merge.geojson, preserving all the attributes of the source layers, simply executing:

ogr2ogr -f GeoJSON merge.geojson merge.vrt

Finally, you can check if the conversion is done successfully:

ogrinfo merge.geojson OGRGeoJSON
  • 1
    Thanks, that answers the question. I didn't know about the VRT driver. I stopped using topojson and have been using merge-geojsons gist.github.com/migurski/3759608 as recommended by another user, whos comment seems to be gone.
    – snorris
    Dec 3 '13 at 0:14

A modification of fiona's dumpgj to handle multiple inputs seemed to be the way to go.


I started - and was learning things - but then remembered that topojson accepts multiple inputs and it seems to work.


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.