Arcpy Copy Feature Classes with Overwrite to specific feature datasets

Arcpy Copy Feature Classes with Overwrite to specific feature datasets

Objective - I have a target GDB "schema" with set feature dataset names and feature classes (no features) within them. I want to copy submittal geodatabase feature classes into that target. I first reproject the target to match the submittal coordinate system.

What I want to do with python is copy all feature classes from the submittal over to the target, where if a submittal feature class name matches one in the target GDB, I want to replace it in the target GDB in the feature dataset location where the empty feature class was in the target. If submittal feature class has no name match in target GDB, I want to copy it over to the target GDB outside any feature dataset.

I am a python novice. I used as a start and now got code (below) to work to copy all feature classes from the source GDB to the target GDB, deleting out feature classes within feature datasets with matching names between the source and target GDBs, but I can't get it to put the source feature classes in the feature datasets where the matched name feature classes are in the target, it just puts them in no feature dataset in the GDB.

UPDATE: Code does work when I make a copy of my submittal database. This revealed that code didn't work as intended because the reproject I do on my target database feature datasets initially isn't truly bringing in all the spatial reference information. In order to get this to work the way I want, I believe I have to add code to create new feature datasets in the target GDB with same names as the target feature datasets with spatial reference information from the submittal feature datasets, then copy over feature classes where names submittal names match target names, then delete the original target feature datasets (or overwrite when creating).

Code snippet below updated per comment instruction:

import arcpy, os, string arcpy.env.overwriteOutput = True def FeatureClassToFeatureClass_conversion(start_db,end_db): #Set workspaces arcpy.env.workspace = start_db wk2 = end_db datasetList = arcpy.ListDatasets() #for feature classes within datasets for dataset in datasetList: fclist = arcpy.ListFeatureClasses("*","",dataset) for fc in fclist: print "Reading: {0}".format(fc) name = arcpy.Describe(fc) if arcpy.Exists(wk2 + os.sep + new_data)==True: arcpy.Delete_management(wk2 + os.sep + new_data) arcpy.Copy_management(fc, wk2 + os.sep + new_data) print "Completed copy on {0}".format(new_data) else: arcpy.Copy_management(fc, wk2 + os.sep + new_data) print "Completed copy on {0}".format(new_data) #Clear memory del fc if __name__== "__main__": start_db = arcpy.GetParameterAsText(0) end_db = arcpy.GetParameterAsText(1) FeatureClassToFeatureClass_conversion(start_db,end_db)

if there is a name match in your target database, it migt be easier & perform faster to

  • purge the target feature class (delete the objects, not the class itself)

  • define its projection to match that of the submittal feature class. Two big assumptions here: the target featue class is NOT in a feature dataset AND the attribute table structure is identical. So this depends on your workflow.

  • append the records from the submittal feature class.

Otherwise, for your projection issues, you can try setting the environmental variables


and, if necessary:


Last, it seems to me that your statement

wk2 + os.sep + new_data

references the target database and the feature class name, this is why you are creating standalone feature classes. You should explicitly include the target dataset name in there, something along the lines of:

wk2 + os.sep + dataset + os.sep + new_data

Watch the video: ArcGIS - Apply Symbology From Layer - Copy symbology from a layer file