[GIS] Merge feature classes in feature datasets using ArcPy

arcgis-10.1arcpymerge

I'm trying to merge a group of like feature classes which are contained within separate feature datasets. I can merge feature classes within a file geodatabase without having issues by creating a list of the feature classes and using Merge_management. The trouble comes when I need to do the same for feature classes contained in feature datasets.

My code so far, looks like:

Output = filepath for new fc
for datasetList1 in arcpy.ListDatasets("","Feature")+ [""]
    PointsFC in arcpy.ListFeatureClasses("*_PointName*","Point",datasetList1):
    for points in PointsFC:
    arcpy.Merge_management(points,Output)
        print "Merged Points"

The error message I get states:

PtsMerge already exists.
Failed to execute (Merge).

Best Answer

Here is how I would do it:

import arcpy, os, sys

# set your parameters input and output database
InDB = sys.argv[1]
OutDB = sys.argv[2]

if not os.path.exists(OutDB):
    DBpath = os.path.dirname(OutDB)
    DBname = os.path.basename(OutDB)
    name,ext = os.path.splitext(DBname)
    if ext.upper() == ".MDB":
        arcpy.AddMessage("Creating output personal database")
        arcpy.CreatePersonalGDB_management(DBpath,DBname)
    elif ext.upper() == ".GDB":
        arcpy.AddMessage("Creating output file database")
        arcpy.CreateFileGDB_management(DBpath,DBname)
    else:
        arcpy.AddError("Unknown output database format")

# set your workspace for ListDatasets
arcpy.env.workspace = InDB

# create empty lists
LineList = list()
PointList = list()
PolyList = list()

# Standalone feature classes
for FeatClass in arcpy.ListFeatureClasses():
    desc = arcpy.Describe(InDB + "\\" + FeatClass)
    if desc.shapeType == "Point":
        PointList.append(InDB + "\\" + FeatClass)
    elif desc.shapeType == "Polyline":
        LineList.append(InDB + "\\" + FeatClass)
    elif desc.shapeType == "Polygon":
        PolyList.append(InDB + "\\" + FeatClass)

# iterate through feature datasets
for FC in arcpy.ListDatasets():
    arcpy.env.workspace = InDB + "\\" + FC
    for FeatClass in arcpy.ListFeatureClasses():
        desc = arcpy.Describe(InDB + "\\" + FC + "\\" + FeatClass)
        if desc.shapeType == "Point":
            PointList.append(InDB + "\\" + FC + "\\" + FeatClass)
        elif desc.shapeType == "Polyline":
            LineList.append(InDB + "\\" + FC + "\\" + FeatClass)
        elif desc.shapeType == "Polygon":
            PolyList.append(InDB + "\\" + FC + "\\" + FeatClass)

arcpy.AddMessage("Performing merge")
if len(PointList) > 0:
    arcpy.Merge_management(PointList,OutDB + "\\Merged_Points")
if len(LineList) > 0:
    arcpy.Merge_management(LineList,OutDB + "\\Merged_Lines")
if len(PolyList) > 0:
    arcpy.Merge_management(PolyList,OutDB + "\\Merged_Polygons")

Going through the database first on the root level and then for each feature class build up a list of features separated using the describe properties then merge them all at the end.

Related Question