Ask A Cartographer

Batch Merge Shape Files

December 23 2010 | 2 comments
Categories: Map Data

I have a large quantity of UK shape files from the Ordnance Survey OpenOS Data catalogue (VectorMap District). The data is divided into 10km tiles and each tile has up to 14 different shape files (point, line and polygon). Each tile is in its own folder and all files are named the same way, ie. "Road_Line.shp" or "Height.shp" in each folder.
Is there a way that I can merge all "Height" or "Road" files in a batch, rather than having to manually select each individual file through the Merge tool. Is there a way of merging files with the same name, that sit within different sub-folders? I have looked at the batch option for Merge, but it does not appear to do what I need.

Mapping Center Answer:

Yes, you can merge in a batch mode using Python Scripting or Iterators in Model Builder. In python, you would need to loop through each subfolder and then merge the files. I have created a sample script for your case, which is attached here. 

You can modify the attached script in a text editor and then load it in a python window and run it. This script will merge all your roads/height shapefiles in one process.

Hope this helps!

different filenames posted by Dale Kunce on Jan 11 2011 3:31PM
Thanks for posting the response. If the files weren't named exactly the same how would you alter your python script? For instance my data is named "[county][year].shp" I would like to merge years into one shapefile for each county.
modify the script posted by Rajinder Nagi on Jan 13 2011 10:22AM
In this case, you can modify the script to check if a county name exists in a shapefile name and if it does exist, create a list of that and then do the merge. You would need to change the script logic starting at line 19 with something like this (the formatting is lost in the code below so you will need to add it back in):

# check if 'Riverside' name exists in shapefile name
if filename.find("Riverside") == 0:
print os.path.join(root, filename)
riverside = str(os.path.join(root, filename))
riversideList.append(riverside)
elif filename.find("San Bernadino") == 0:
print os.path.join(root, filename)
SanBernadino= str(os.path.join(root, filename))
SanBernadinoList.append(SanBernadino
elif
--- code here---

There are a number of ways to acheive what you want, and I am suggesting just one. You can read about arcpy here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_ArcPy/000v00000001000000/

and regarding python here: http://docs.python.org/

If you would like to post a comment, please login.

Contact Us | Legal | Privacy |