[GIS] Merging multiple feature classes in file geodatabase with ogr2ogr

file-geodatabasemergeogr2ogrsql

Is there a way to merge multiple feature classes into one feature class in a geodatabase with ogr2ogr? If this isn't possible with ogr2ogr, is there another method? I'm using the ESRI File Geodatabase driver for GDAL.

If the solution could merge all feature classes in a geodatabase matching a particular string, that would be even better!

update

org2org command I'm using to merge geodatabases, which works well but creates an unwieldy number of feature classes:

ogr2ogr -f 'FileGDB' -update -append merged.gdb gdbtomerge.gdb

Here is the structure of the geodatabase feature classes i'd like to merge:

geodatabase #1 feature classes (multipolygon) to merge:

Sec01_Frm01
Sec01_Frm02
Sec01_Frm03
etc…

geodatabase #2 feature classes (multipolygon) to merge:

Sec02_Frm01
Sec02_Frm02
Sec02_Frm03
etc…

There are no attributes for any of the features except for SHAPE_Length and SHAPE_Area for all feature classes. Ideally I'd like to give each feature a text attribute with the feature class name, but that is out of the scope of this question I think. Just merging all of the feature classes that either match the string 'Frm' or even the same geometry type would work.

update

I think I've gotten a little closer using SQL expressions with ogrinfo:

-drop a table (feature class):

ogrinfo -dialect FileGDB -sql "drop table tabletodrop" geodbname.gdb

-I tried this to merge two tables but I get: '(Cannot acquire a lock.)' with this command:

ogrinfo -dialect FileGDB -sql "INSERT INTO table01 SELECT * FROM table02" geodbname.gdb

Any thoughts on the lock issue or a better way to do this?

Best Answer

To select a single feature class from a gdb, we query for all entities inside it:

-sql "select * from FEATURE_CLASS_NAME"

To generate a list of feature classes following your sample use nested FOR loops:

@echo off
for %%S in (01 02 03) do (
    for %%F in (01 02 03) do (
        echo ogr2ogr out.gdb in.gdb -sql "select * from Sec%%S_Frm%%F"
        )
     )    

Emits:

ogr2ogr out.gdb in.gdb -sql "select * from Sec01_Frm01"
ogr2ogr out.gdb in.gdb -sql "select * from Sec01_Frm02"
ogr2ogr out.gdb in.gdb -sql "select * from Sec01_Frm03"
ogr2ogr out.gdb in.gdb -sql "select * from Sec02_Frm01"
...

From there add -nln OUT_LAYER_NAME to say we want it all going to one place, and -nlt multipolygon for geometry type. (NB: nlt is often automatically derived from input, but for some operations like this one geometry is omitted and you get just a table):

ogr2ogr out.gdb in.gdb -nln Form01 -nlt multipolygon -sql ...

And finally we will be updating existing data so:

ogr2ogr out.gdb in.gdb -update -append -nln ...

Useful options:

-config FGDB_BULK_LOAD YES
-skipfailures

How do I include the source filename in a field when merging hundreds of shapefiles (Windows)?

for %f in (*.shp) do (
    ogr2ogr -sql "select *, '%~nf' as s_file from %~nf" -update -append merged\output.shp %f -nln output
    )

Sources


By the way, if you still have the gdb with the "unwieldy number of feature classes" hanging around from previous attempts you might be able to just ogr2ogr -f FileGDB clean.gdb unwieldy.gdb -nln Clean_Form01 -nlt multipolygon in one go.


Code samples are in Windows batch file syntax. Thank you for this question. It prompted me to extend a related project that I've been needing to come back to.

I don't think ogrinfo can write, so that would be the origin of the lock error in the Dec 11 update.

Related Question