Site menu:

Cyclocross For Roadies: How and Why to Get Started - Buy Now!

Site search

Categories

Merge A Directory of Shapefiles Using OGR

Here’s something that may seem obvious to power users of GDAL/OGR, but not to those that are just new to using these great free tools.  This is something that I use on an almost daily basis, and works when a lot of other options (QGIS, Kosmo, etc.) seem to fail.  Essentially, this sequence of commands will grab every shapefile in a directory, and merge them into a single file called “merged.shp” in a directory called “merged.”  Of course, you can monkey with the names if you wish, and customize to your needs, but this is a basic place to start:

mkdir merged
for %f in (*.shp) do (
if not exist merged\merged.shp (
ogr2ogr -f “esri shapefile” merged\merged.shp %f) else (
ogr2ogr -f “esri shapefile” -update -append merged\merged.shp %f -nln Merged )
)

I’ve found that the easiest way to install GDAL/OGR is via Frank’s wonderful FWTools, which you can download for free here.

Lets hear your useful GDAL/OGR tips in the comments below!

Comments

Comment from Tom Kralidis
Time May 7, 2010 at 8:34 pm

Simple and very useful. It’s posts like this that show how useful these tools are. Thanks!

Comment from Tyler Mitchell
Time May 7, 2010 at 10:09 pm

That’s the spirit! :) Nice to see your post Darren.

Comment from Darren
Time May 11, 2010 at 12:38 pm

Thanks Tyler :) I’m going to post a few more of these that I use on a regular basis–I figure if they’re useful for me, they will be to others too, even if they’re easy!

Pingback from DARREN COPE » Merge Subdirectories of Shapefiles Using OGR
Time May 18, 2010 at 9:17 am

[...] a follow-up to my post “Merge A Directory of Shapefiles Using OGR,” I came across a similar problem today.  I had a bunch of data in subfolders inside a [...]

Pingback from OGR – Merging Multiple SHP files « gis-programming.com
Time December 21, 2010 at 5:38 pm

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

Pingback from OGR & Python – Batch Processing « Arne's Adventures in GIS
Time December 23, 2010 at 12:04 pm

[...] char. But again, to simply aggregate many shapefile in one SHP, this would take to long. So for now Darren’s little Windows script to batch process the SHP’s with ogr2ogr has been the best way. I haven’t really used [...]

Pingback from OGR & Python – Batch Processing « gis-programming.com
Time December 23, 2010 at 12:34 pm

[...] char. But again, to simply aggregate many shapefile in one SHP, this would take to long. So for now Darren’s little Windows script to batch process the SHP’s with ogr2ogr has been the best way. I haven’t really used Python [...]

Pingback from Merging SHP-files with Geoscript « gis-programming.com
Time December 23, 2010 at 12:35 pm

[...] looks more straight forward than other Python examples I had looked at. But as Tim Schaub observes, Darren’s Windows script (which I adapted for my purposes) is much more light weight. Uncategorized    OGR [...]

Comment from Donald
Time July 26, 2012 at 3:41 pm

If you happen to be doing this on Linux, try the following Bash script instead:

mkdir merged;
for f in *.shp;
do
if [ -f "$f" ]
then
ogr2ogr -f “ESRI Shapefile” -update -append merged/merged.shp “$f” -nln Merged
else
ogr2ogr -f “ESRI Shapefile” merged/merged.shp “$f”
fi;
done;

Comment from Donald
Time July 30, 2012 at 10:46 pm

Correction to the script I posted:

mkdir merged;
for f in *.shp;
do
if [ -f merged/merged.shp ]
then
ogr2ogr -f “ESRI Shapefile” -update -append merged/merged.shp “$f” -nln Merged
else
ogr2ogr -f “ESRI Shapefile” merged/merged.shp “$f”
fi;
done;

And remember to change the “ to proper quotation marks.

Comment from darrencope
Time July 31, 2012 at 10:09 am

Great; thanks Donald!

Write a comment





Bookshelf 2.0 developed by revood.com

More in GIS (27 of 33 articles)


All I can say is that Paul Ramsey is a genius.  This is the best presentation I've seen in a ...