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).
-
1Names 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 Stimson– Michael Stimson2014年08月01日 00:56:49 +00:00Commented Aug 1, 2014 at 0:56
-
I should have added the merge feature class is pointing to a separate geodatabase.standard– standard2014年08月01日 01:01:18 +00:00Commented Aug 1, 2014 at 1:01
-
Yes but each iteration is writing to Output which is not set in the loop.Michael Stimson– Michael Stimson2014年08月01日 01:05:05 +00:00Commented Aug 1, 2014 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– standard2014年08月01日 01:13:42 +00:00Commented Aug 1, 2014 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 Stimson– Michael Stimson2014年08月01日 01:17:15 +00:00Commented Aug 1, 2014 at 1:17
1 Answer 1
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.
-
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– standard2014年08月01日 01:56:32 +00:00Commented Aug 1, 2014 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 Stimson– Michael Stimson2014年08月01日 01:59:12 +00:00Commented Aug 1, 2014 at 1:59