How to merge hundreds of shapefiles into one gdb

2107
4
07-09-2019 06:50 PM
DarcyKenyon1
New Contributor

Hi there

I am trying to merge a couple hundred shapefiles into one gdb, however, I only want to select shapefiles that contain "bpoly" in the name (as I will repeat for shapefiles that only contain other names with common expressions). When trying to do this manually, the merge tool has not been capable of running with up to 200 shapefiles. 

I have not used python script before but believe this will be the solution, if anyone has an idea of how to go about it that would be great. 

Thanks

Andrew 

0 Kudos
4 Replies
LanceCole
MVP Regular Contributor

Andrew, 

Please take a look at Ian Murray post in Python Script to Merge Files.  This would need to be modified to check for "bpoly" in the filename.  Another python sample source is Batch merge ArcGIS.

DarcyKenyon1
New Contributor

Thanks Lance

As I have never used python before I am unsure of what inputs I need to add to change the script to suit my needs. I am having trouble finding tutorials for python in arc or a directory to understand the terms used in the script. Do you have any other useful links that may help me? 

Thanks

Andrew 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
#Importing Modules  
import arcpy  
import os  
#Setting root workspace where files are located  
workspace = r"FilePath"  
#Making an empty list to put filepaths to feature classes in  
feature_classes = []  
#Walking workspace recursively checking type, and appending filepath to list  
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype="FeatureClass", type="Polygon"):  
    for filename in filenames:  
        desc = arcpy.Describe(os.path.join(dirpath, filename)  
        if desc.shapeType == "Polygon":  
            feature_classes.append(os.path.join(dirpath, filename))  
            
#merging my list of feature classes to a new dataset  
arcpy.Merge_management(feature_classes, "C:\Path\MergedPolygons") ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Line 5... the path to the folder containing the shapefiles eg  r"C:\GIS\Data"

Line 9.... change 'type' to either Polygon, Polyline, Point as appropriate.... you can't merge different geometry types.

Line 16... change the "C:\Path\Merged\Polygons" to

      r"C:\PathToYour\Geodatabase.gdb\MergedPolygons"

check Merge—Data Management toolbox | ArcGIS Desktop for exact syntax

Of course the whole thing about field mapping has been left out, so if you fields aren't identical... doing it manually may be faster

0 Kudos
LanceCole
MVP Regular Contributor

Andrew Becker

1) Use Google and search for "youtube getting started with python" this will give you a basic understanding of the language and how to use the command line interface.  Once you have experimented with the basics try searching for ArcGIS python to get more examples specific to GIS. E360 is another great source for python videos specific to ESRI

2) You will need some type of text editor.  You can use Notepad but this will not have python syntax highlighting.  As you have ArcGIS installed you will also have IDLE on your system that does support syntax highlighting. 

3) Jump in with both feet, take the code sample from Python Script to Merge Files, put a couple of shapefiles and a GDB in a test directory (do not try to run this on your entire set of data from the start), start to make the changes as Dan Patterson noted above, see what happens and work through the issues that arise.  It is like a lottery, you are guaranteed not to win if you do not but a ticket.

4) If you need help with a specific issue,  post a new question and guaranteed someone on GeoNet will lend a hand.

0 Kudos