[gdal-dev] Proposed ogrmerge.py utility script

Even Rouault even.rouault at spatialys.com
Fri Mar 3 14:23:11 PST 2017


Hi,

I've worked on a script to merge several OGR datasources into a single one.
Nothing that can't be done with ogr2ogr with some loops around, but I think this can be convenient.

There are basically 2 modes :
- Default one: each layer of each source datasource is written as a separate 
layer in the target datasource.
- 'Single' mode: all layers from all source datasources are written into a single
layer in the target datasource. This assumes that the schemas of those source
layers are more or less compatible.

I've not yet committed the script. You can grab it from
https://gist.github.com/rouault/3b49bffd67fb18e23cd4c5bcc10d3103

Short usage:

ogrmerge.py -o out_dsname src_dsname [src_dsname]*
            [-f format] [-single] [-nln layer_name_template]
            [-update | -overwrite_ds] [-append | -overwrite_lyr]
            [-src_geom_type geom_type_name[,geom_type_name]*]
            [-dsco NAME=VALUE]* [-lco NAME=VALUE]*
            [-s_srs srs_def] [-t_srs srs_def | -a_srs srs_def]
            [-progress] [-skip] [--help-general]

Options specific to -single:
            [-field_strategy FirstLayer|Union|Intersection]
            [-src_layer_field_name name]
            [-src_layer_field_content layer_name_template]

* layer_name_template can contain the following substituable variables:
     {AUTO_NAME}  : {DS_BASENAME}_{LAYER_NAME} if they are different
                    or {LAYER_NAME} if they are identical
     {DS_NAME}    : name of the source dataset
     {DS_BASENAME}: base name of the source dataset
     {DS_INDEX}   : index of the source dataset
     {LAYER_NAME} : name of the source layer
     {LAYER_IDX}  : index of the source layer


Examples:

* ogrmerge.py -f VRT -o merged.vrt  *.shp

     Creates a VRT with a layer for each input shapefiles

* ogrmerge.py -f GPKG -o merged.gpkg *.shp

     Same, but creates a GeoPackage file

* ogrmerge.py -single -o merged.shp france.shp germany.shp -src_layer_field_name country

	Concatenate the content of france.shp and germany.shp in merged.shp, and adds a 'country'
      field to each feature whose value is 'france' or 'germany' depending where it comes from


Internally this generates a VRT file, and if the output format is not VRT,
final translation is done with ogr2ogr / gdal.VectorTranslate. So for advanced uses, output
to VRT, potential manual editing of it and ogr2ogr can be done.

Opinions on script naming (I also tought to ogrcat.py to reflect the similarity with the Unix cat
utility used to concatenate files), option naming, etc.. are welcome

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170303/afa159a4/attachment.html>


More information about the gdal-dev mailing list