0

In the Geopackage below (in.gpkg) I would like to extract the first layer (i.e. pinios_eudem2_basins) and write it to a new Geopackage (out.gpkg). However I would like to do this not by name, but with the help of a wildcard or an index. Is this possible?

This works, but I don't need it.

ogr2ogr -f "GPKG" -overwrite out.gpkg in.gpkg pinios_eudem2_basins

I would like to have something like this..

ogr2ogr -f "GPKG" -overwrite out.gpkg in.gpkg *_eudem2_basins
ogr2ogr -f "GPKG" -overwrite out.gpkg in.gpkg $1

ogrinfo output:

INFO: Open of `in.gpkg'
      using driver `GPKG' successful.
1: pinios_eudem2_basins (Multi Polygon)
2: pinios_eudem2_basins_h1 (Multi Polygon)
3: pinios_eudem2_basins_h2 (Multi Polygon)
4: pinios_eudem2_basins_h3 (Multi Polygon)
5: pinios_eudem2_basins_h4 (Multi Polygon)
6: pinios_eudem2_basins_h5 (Multi Polygon)
7: pinios_eudem2_basins_h6 (Multi Polygon)
8: pinios_eudem2_basins_h7 (Multi Polygon)
0

1 Answer 1

1

ogr2ogr does not support wildcards but it supports layer lists both on the command line and with an external file. Using layer indexes as they appear in the ogrinfo layer list is not supported.

If layer name is not given ogr2ogr converts all the layers from the datastore. If one or more layers are given on the command line then just those layers get converted. However, it is possible to give a layer list pinios_eudem2_basins pinios_eudem2_basins_h1....

It is also possible to save the layer list into a text file

my_list.txt
===========
pinios_eudem2_basins
pinios_eudem2_basins_h1
pinios_eudem2_basins_h2
pinios_eudem2_basins_h3
pinios_eudem2_basins_h4
pinios_eudem2_basins_h5
pinios_eudem2_basins_h6
pinios_eudem2_basins_h7

Then you can use the list as "optfile" https://gdal.org/programs/vector_common_options.html#cmdoption-optfile.

Usage would be

ogr2ogr -f "GPKG" -overwrite out.gpkg in.gpkg --optfile my_list.txt

If you want to have something automatic need to have a script that filters the layer names and either builds one ogr2ogr command per layer or writes out the optfile.

Depending on what you plan to do it may be good to use also -update in the ogr2ogr command or otherwise the whole geopackage will be overwritten.

4
  • Thanks the --optfile option is convenient! Funny though that the file has to end on .txt.
    – andschar
    Nov 18, 2021 at 17:30
  • Works for me also with ogr2ogr -f gpkg delete.gpkg test.gpkg --optfile list.foo. Do you mean that you made a test with some other extension than .txt and that failed? What extension did you use?
    – user30184
    Nov 18, 2021 at 18:33
  • Ok. Just tried a file without extension, namely lookup. That didn't work. Normally not a problem on a Linux machine.
    – andschar
    Nov 18, 2021 at 18:34
  • Also ogr2ogr -f gpkg delete.gpkg test.gpkg --optfile lookup works for me on Windows.
    – user30184
    Nov 18, 2021 at 18:37

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.