OGR – Merging Multiple SHP files

So I have 25 shapefiles with data for 25 counties, and I want to merge them all into one shapefile.

Using OGR, you can merge 2 files like this:

ogr2ogr merge.shp file1.shp
ogr2ogr -update -append merged.shp file2.shp -nln merge

You create a new shapefile merged and copy the contents of file1 into the new shapefile. Then you append the contents of file2 to the newly created shapefile and name it merged.

What do you do if you don’t want to do this 25 times ?

On page 22 of this document (Using GDAL / OGR for Data Processing and Analysis) is a Python script for doing this. But I couldn’t get the Python bindings for GDAL to work. The errors were always with importing _gdal which I understand is the gdal dll and references to _gdal are created by SWIG. I don’t care about the script. I can write my own. But I would like to get GDAL(OGR) working from Python.

So after giving up on this concept temporarily, I resorted to Windows scripting, as modified from this page:

as above, use: ogr2ogr merge.shp file1.shp to create a shapefile merge containing the data of file1


for %f in (*.shp) do (
ogr2ogr -update -append merge.shp %f  -f “esri shapefile” -nln merge )
That worked.

1 Comment

Filed under Uncategorized

One Response to OGR – Merging Multiple SHP files

  1. Hi, I this script in python is for the same purpose, but with plenty options:

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>