6

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.
FAILURE:
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.

thanks!

0

2 Answers 2

5

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:

<OGRVRTDataSource>
    <OGRVRTUnionLayer name="union">
        <OGRVRTLayer name="points">
            <SrcDataSource>points.shp</SrcDataSource>
        </OGRVRTLayer>
        <OGRVRTLayer name="lines">
            <SrcDataSource>lines.shp</SrcDataSource>
        </OGRVRTLayer>
        <OGRVRTLayer name="polygons">
            <SrcDataSource>polygons.shp</SrcDataSource>
        </OGRVRTLayer>
        <GeometryType>wkbGeometryCollection</GeometryType>
    </OGRVRTUnionLayer>
</OGRVRTDataSource>

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
  • 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
    Commented Dec 3, 2013 at 0:14
0

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

http://toblerity.org/fiona/manual.html#dumpgj

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

https://github.com/mbostock/topojson/wiki/Command-Line-Reference

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.