I am currently working on a python script where I am trying to copy selected features into a feature dataset.
Basically I have an mxd containing some layers, and for each layer I select features by their attributes, this query is based on the featuredataset name.
The script I have written get a list of the feature datasets of my geodatabase. With the name of the featuredataset, I can build a query filter (arcpy.SelectLayerByAttribute_management) for each layers of the mxd.
If my selection return something, I export the selected feature in the proper dataset (arcpy.FeatureClassToFeatureClass_conversion).
The script is processing the first dataset properly, but once it jumps to the next one it fails… I cannot figuring out why I am receiving the following message error(arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
Failed to execute (FeatureClassToFeatureClass).)
#-----Work-on-the-Geodatabase-----
##path of the geodatabase
gdb_hull="D:\\As_Con_Plans\\As_Con_Hull_part1.gdb"
##set the workspace
arcpy.env.workspace=gdb_hull
##get the list of the featuredataset
fdlist =arcpy.ListDatasets()
#-----Work-on-the-mxd-----
##get the mxd path and initialise it
path_mxd="D:\\As_Con_Plans\\As_constructed.mxd"
doc_mxd = arcpy.mapping.MapDocument(path_mxd)
##list of layers
fclist=arcpy.mapping.ListLayers(doc_mxd)
#-----Loop-on-each-featuredataset----
for fds in fdlist:
print "________","\n ", fds
##work on the featuredataset name, for suiting the query
get_fds= fds.replace("As_con_","")
fds_name=get_fds.replace("_","-")
##path of the featuredataset
fds_path=gdb_hull+"\\"+fds
##prepare the query
SQLexpression= "\"As_Constructed_Plan\"='"+fds_name+"'"
##path where we will store the selection
fds_path_test=str(fds_path)
#-----Loop-on-each-layers-of-the-mxd-----
for fc in fclist:
fc_to_copy=fc.dataSource
#fc_to_copy2=arcpy.mapping.Layer(fc_to_copy)
##Select the featureclasses
arcpy.SelectLayerByAttribute_management (fc,"NEW_SELECTION",SQLexpression)
##Count the selected features
count = int(arcpy.GetCount_management(fc).getOutput(0))
print fc,count
##if the query succeed, we will copy the selection into the proper featuredataset
if count>0:
##print some info
fc_path2=fds_path+"\\"+str(fc)
fc_path=str(fc_path2)
fc_name=str(fc)
fc_copy_from=str(fc_to_copy)
##check if the featureclass does not exist
if not arcpy.Exists(fc_path):
print "\n copying...",count," entities , on ",fc_path
print (fc_copy_from,fds_path_test,fc_name,SQLexpression)
arcpy.FeatureClassToFeatureClass_conversion(fc_to_copy,fds_path_test,fc_name,SQLexpression)
print "export ok"
Best Answer
Finally, I figured out that my problem was linked to the name of the feature class' output. It is the fc_name of the following line:
I have run some test and discovered that I can't add 2 similar output name... even if it is processing a different dataset... So to fix the problem, I have created an incremented index that I linked to the feature class name (new_fc_name=fc_name+str(i) with I incremented after a loop). It may not be the best way to process, but at least, it is working...