1

Is ogr2ogr capable of merging two GeoJSON files to a new one? It is possible to merge shapefiles to one GeoJSON using the VRT driver. But when trying to do the same using GeoJSON files ogr2ogr fails.

VRT is:

<OGRVRTDataSource> <OGRVRTUnionLayer name="union"> <OGRVRTLayer name="Bundeslaender"> <SrcDataSource>C:\Users\root\AppData\Local\OGR_Contextmenue\Bundeslaender.geojson</SrcDataSource> </OGRVRTLayer> <OGRVRTLayer name="standorte"> <SrcDataSource>C:\Users\root\AppData\Local\OGR_Contextmenue\standorte.geojson</SrcDataSource> </OGRVRTLayer> <GeometryType>wkbGeometryCollection</GeometryType> </OGRVRTUnionLayer> </OGRVRTDataSource>

And the ogr command:

ogr2ogr.exe -f "GeoJSON" "C:\Users\root\Desktop\Plugin_new\Shapefiles_Karte\Bundeslaender\Bundeslaender.geojson" "C:\Users\root\AppData\Local\OGR_Contextmenue\vrt.vrt" -overwrite

The error ogr2ogr gives me is:

ERROR 1: Failed to find layer 'Bundeslaender' on datasource 'C:\Users\root\AppData\Local\OGR_Contextmenue\Bundeslaender.geojson'. ERROR 1: Failed to find layer 'standorte' on datasource 'C:\Users\root\AppData\Local\OGR_Contextmenue\standorte.geojson'.

To clear out: the files are existing at the path given in the vrt. I double checked it. Also the path where the merged GeoJSON file should be created exists and is accessible. Doing the same with Shapefiles is no problem! So what am I missing here?

2
  • 1
    Have you noticed that for the GDAL geojson driver the layer name is always OGRGeoJSON?
    – user30184
    Commented Jul 14, 2016 at 9:49
  • I don't know what you want to say with that?
    – Leo.1906
    Commented Jul 14, 2016 at 12:40

2 Answers 2

5

You can "merge" GeoJSON with ogr2ogr by appending the second to the first or writing both into one new file if you want to keep the original files. In your example: use bundeslaender.geojson and standorte.geojson as parameters. This works for me:

ogr2ogr -f GeoJSON bundeslaenderUndStandorte.geojson bundeslander.geojson
ogr2ogr -f GeoJSON -append bundeslaenderUndStandorte.geojson standorte.geojson

Maybe it even works with VTR if you use -append instead of -overwrite

5
  • I had geopackage output, but geojson input, so I am not certain if it is exactly the same, but without specifying the layer name (-nln) it appends an entire additional layer to the file, instead of appending the rows of the second input layer to the rows of the first input layer.
    – Kevin
    Commented Jul 11, 2022 at 19:58
  • What about appending everything to geojson as output and then in the end converting everything to gepackage?
    – Alex
    Commented Jul 14, 2022 at 20:30
  • hmm, either might work, but at the end it is nice to have clean reference to the final layer name for my uses, so I would need to use -nln eventually. Working with a mix of geopackage/sqlite (cold storage), geojson/json (interchange), shapefile (old), geodatabase (from analysts), and postgis/postgresql (production). It would be amazing if everything could be in one format.
    – Kevin
    Commented Jul 15, 2022 at 15:05
  • 1
    For a.json and b.json with copying a to new.json, then trying to append b, yields: ERROR 1: Layer 'b' does not already exist in the output dataset, and cannot be created by the output driver. I can append a to new over and over. When the FeatureCollection does not have a name, it uses the filename. If I manually add a matching name inside b.json, then it can be appended. Warnings are given if Feature id values overlap, but it will append them anyway.
    – Kevin
    Commented Jul 15, 2022 at 15:12
  • Related: gis.stackexchange.com/q/447359/191643.
    – zabop
    Commented Dec 12, 2022 at 14:51
2

You can also merge GeoJSON files using a JSON processing command line tool such as jq.

jq '{"type": "FeatureCollection", "features": [.[] | .features[]]}' --slurp input*.geojson > output.geojson

--slurp reads all input files into one big array. You get a virtual JSON document with an array as top level element where each input file becomes one element. You can see the effect with jq . --slurp input*.geojson.

.[] | iterates over the top level array coming from --slurp.

.features[] iterates over all features in all files. And with "features": […] they are output at this place in the output file.

ogr2ogr uses the properties of the first input file to determine which properties exist in the output file.

If you happen to have input files with variable properties, you might find that the output is missing some of them, if the first file doesn’t happen to contain all of them.

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.