OGR2OGR – Merge shapefiles

  • Shell
  • gdal 3.x
  • Updated: October 2021

GDAL is a great command line utility to do about anything you need to do with geospatial data. What if you had some shapefiles and you needed to meerge them together. Of course they need to have the same columns and be either points/lines/polygons.

I have a directory full of points and I want to merge them all together. I have two options from GDAL:

Option 1

Using the command line too ogr2ogr I will first make a new file from one of the existing files:

ogr2ogr -f ‘ESRI Shapefile’ merge.shp station_1.shp

Then merge the following files by using:

ogr2ogr -f ‘ESRI Shapefile’ -update -append merge.shp station_3.shp -nln merge

ogr2ogr -f ‘ESRI Shapefile’ -update -append merge.shp station_4.shp -nln merge

Option 2

There is a utility that appeared in gdal 2.x called ogrmerge.py

To run the utility you do the following:

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

An Entire directories worth of shapefiles are merged into a Geopackage. It puts each shapefile into a separate table in the Geopackage.

What if you want everything in the same table?

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

What if you wanted everything in the same shapefile?

ogrmerge.py -single -f ‘ESRI Shapefile’ -o merged.shp *.shp

Want QGIS Training? Need help with a project? Give us a shout at info at northrivergeographic.com