I have a gdb folder which contains ~3000 multipolygon layers (with very few features on each layer). I would like to take around 100 of those layers and merge them together while adding a field to the features to indicate what layer they originate from.
I think that ogrmerge.py
can help me with the merge and with feature labeling. But I haven't found a way to either select specific layers to merge (ogrmerge
seems to take an entire dataset as its input) or to extract the relevant layers to create a sub-dataset to pass to ogrmerge
.
Any idea how this could be achieved (preferably with command line open source tools)?
-
Try the advice from the documentation "So, for advanced uses, output to VRT, potential manual editing of it and ogr2ogr can be done." VRT is XML file and with 3000 layers there is quite a lot of text to delete. It might be faster to take section of one layer as a template and copy-paste-edit it for the next layers.user30184– user301842020年02月20日 06:44:09 +00:00Commented Feb 20, 2020 at 6:44
2 Answers 2
You can use QGIS, it is open source and downloadable from: https://qgis.org/en/site/forusers/download.html
Upload your Layers then from the Vector Menu, choose "Data Management Tools" ---> Merge Vector Layers
Select the layers you would like to merge (make sure they are of the same geometry)
Select the destination format and file name . Check the "Open output after running the algorithm" (it will load the new layer after running the process).
Click on Run
The new layer will be added in you view.
If you click on the "Open Attribute Table Button", you will notice that a new field has been created by the tool containing the original "Layer Name"
You could do the same with Layers from personal geodatabase without going into the trouble to convert into shapefile.
You can run the same process in batch or select the files from you directory as well.
I hope this helps.
-
Thank you for the detailed answer. I agree that this is generally the way to go for this problem. In my case though, QGIS (3.10) would not list the content of the gdb folder (although ogrinfo did list its content). And since manually selecting ~100 layers in a list of ~3000 layers was bound to lead omissions, I went for a command line operation (I added it below).Frederic– Frederic2020年02月21日 15:11:44 +00:00Commented Feb 21, 2020 at 15:11
I was unable to directly extract layers from gdb. However, but using ogr2ogr
, I was able to convert gdb to shp. Once the data was in shapefile format (with files named after the layer they contain), it was easy to copy the relevant layers to a separate folder and use this folder as the input for ogrmerge.py
.
The process looks like this:
ogr2ogr extracted.shp original_data.gdb
mkdir to_merge.shp/
cp extracted.shp/target_layers* to_merge.shp
python3 ogrmerge.py -single -src_layer_field_name "source" -src_layer_field_content {LAYER_NAME} -o merged.shp to_merge.shp
That created a lot of intermediary files, but it worked.