1

Attempt I - ogrmerge

I have a few GeoJSONs in a directory called jordsmonnfiles. I want to get one GeoJSON file with the features from the files matching the pattern jordsmonnfiles/jordsmonn_*JK1.geojson. My first attempt:

python3 ogrmerge.py -f GeoJSON -o JK1.geojson -nln JK1 jordsmonnfiles/jordsmonn_*JK1.geojson

I get the error:

ERROR 1: Layer JK1 already exists, and -append not specified. Consider using -append, or -overwrite. ERROR 1: Terminating translation prematurely after failed translation of layer JK1 (use -skipfailures to skip errors)

I read up on the -append flag in the documentation, which describes it as:

Open an existing dataset in update mode, and if output layers already exist, append the content of input layers to them.

Trying it:

python3 ogrmerge.py -f GeoJSON -o JK1.geojson -nln JK1 jordsmonnfiles/jordsmonn_*JK1.geojson -append

Output:

ERROR: -f incompatible with -update

Take away the -f flag then:

python3 ogrmerge.py -o JK1.geojson -nln JK1 jordsmonnfiles/jordsmonn_*JK1.geojson -append

ERROR: Destination dataset does not exist

How could I unite all the defined geometries in files matching the pattern jordsmonnfiles/jordsmonn_*JK1.geojson into a single file?

The schematic view of the desired output would be:

{
"type": "FeatureCollection",
"name": "WHATEVER_NAME",
"features": [
    { "type": "Feature",
      "properties": "WHATEVER, I DON'T NEED TO PRESERVE PROPERTIES OF SOURCE FILES",
      "geometry":
        { "type": "Polygon",
        "coordinates": [[[10.0,11.2], [10.5, 11.9],[10.8, 12.0],[10.0,11.2]]] }
    },
    { "type": "Feature",
      "properties": "WHATEVER, I DON'T NEED TO PRESERVE PROPERTIES OF SOURCE FILES",
      "geometry":
        { "type": "Polygon",
        "coordinates": [[[12.0,14.2], [12.5, 14.9],[12.8, 14.0],[12.0,14.2]]] }
    },
]
}

I only care about geometries, not properties of each. I don't want to unite them in the sense of producing a MultiPolygon, but I want to list all of the geometries separately in the features part of the GeoJSON.


Attempt II - mapbox/geojson-merge

I have also tried to use geojson-merge.

npm install -g @mapbox/geojson-merge # installs successfully
geojson-merge jordsmonnfiles/jordsmonn_*JK1.geojson > JK1.geojson 

Output:

/opt/homebrew/lib/node_modules/@mapbox/geojson-merge/geojson-merge:17 process.stdout.write(JSON.stringify(merge(argv._.map(function(n) { ^

RangeError: Invalid string length at JSON.stringify () at Object. (/opt/homebrew/lib/node_modules/@mapbox/geojson-merge/geojson-merge:17:29) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Module._load (node:internal/modules/cjs/loader:827:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47

Node.js v18.0.0

1

1 Answer 1

2

You did not find one option when reading https://gdal.org/programs/ogrmerge.html

-single If specified, all input vector layers will be merged into a single one.

Then do:

 ogrmerge -single -o merged.json *.json

How to merge just geometries but not attributes is worth another question. I do not believe it is possible with ogrmerge.

1

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.