Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers 2

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
share|improve this answer
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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