5

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?

  • can you share the ogr2ogr code you have so far? This answer suggests merging on a field is possible if GDAL is built with spatiallite support: gis.stackexchange.com/a/85046/9847 – toms Dec 9 '15 at 19:52
  • I don't actually have any code yet for the feature class merging. All i've been doing so far is merging multiple geodatabases. It works great, but then i end up with an unwieldy number of feature classes in the merged gdb. i was using: 'ogr2ogr -f 'FileGDB' -update -append merged.gdb gdbtomerge.gdb' – jamierob Dec 9 '15 at 20:00
  • Please edit the question in response to requests for clarification. It's not fair to those who would answer to need to search comments for critical details. – Vince Dec 9 '15 at 20:20
  • I thought about that but then realized that the code I provided in my comment is not relevant to the question because it is a simple merging of two geodatabases and not the feature classes within the geodatabase. – jamierob Dec 9 '15 at 20:32
  • @toms it looks like that example is dissolving features based on a field? or am I missing something? – jamierob Dec 9 '15 at 20:35
2

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.

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.