Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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).'. Any help is always appreciated.

share|improve this question
1  
Names need to be unique in the whole database. You can't create a feature class called PtsMerge in one feature dataset if it already exists in another. Try renaming the existing one first to Temp_XXXX before merging, XXXX can be a random number or counter so long as it doesn't already exist. –  Michael Miles-Stimson Aug 1 at 0:56
    
I should have added the merge feature class is pointing to a separate geodatabase. –  standard Aug 1 at 1:01
    
Yes but each iteration is writing to Output which is not set in the loop. –  Michael Miles-Stimson Aug 1 at 1:05
    
Sorry, I'm not really sure what you mean by using a counter, having create an iteration of the feature classes then merge those? –  standard Aug 1 at 1:13
    
You're stepping through the feature classes and merging each one individually to the same output. Are you trying to merge all of the database? at the moment you're just using merge to copy each feature class. Can you explain what you have and what you're trying to end up with? –  Michael Miles-Stimson Aug 1 at 1:17

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
thank you very much @Michael Miles-Stimson. This is something I had been working on periodically for a good bit and couldn't get it to work without using Append and having to set schema which could potentially be dynamic. Thanks again. –  standard Aug 1 at 1:56
    
You're welcome. There's a few gems in that code that you should be able to make use of later. –  Michael Miles-Stimson Aug 1 at 1:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.