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"
-
1Are you familiar with try..except blocks? Is it possible that the next feature class has the same name as the first? You can test with arcpy.exists(fds_path_test+"\\"+fc_name) see resources.arcgis.com/en/help/main/10.2/index.html#//…. A copy of the outputs may help (turn on quick edit in the command window, select the area and right click then paste into notepad) or screen grab.Michael Stimson– Michael Stimson2014年09月08日 05:20:04 +00:00Commented Sep 8, 2014 at 5:20
-
It looks like you are looping through the output feature datasets and storing the feature class more than once (once in each feature dataset) hence the error - names must be unique in the database, not just the dataset. If there is only one feature dataset in the output database gdb_hull = "D:\\As_Con_Plans\\As_Con_Hull_part1.gdb" then I'll need to look harder. Check also for spaces and non-alphanumeric characters in the layer names, that will also cause that error.Michael Stimson– Michael Stimson2014年09月08日 05:58:58 +00:00Commented Sep 8, 2014 at 5:58
2 Answers 2
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:
arcpy.FeatureClassToFeatureClass_conversion(fc_to_copy,fds_path_test,fc_name,SQLexpression).
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...
You can have 2 check:
Check if name is valid
vname = arcpy.ValidateTableName(fc_name,wksp)
Check if name exist
def validUniqueName(name, wk): """ Create a unique name not exist in GDB Note: you can fix limit to number of pas and add test if name is too long""" check = True while check: check = arcpy.Exists(os.path.join(wk, name)) if check: name = "{}_1".format(name) return name
Add 2 lines in function:
vname = arcpy.ValidateTableName(fc_name,wksp)
v_unique_name = validUniqueName(vname,wksp)
Test
>>> name = u"Voirie etroite"
>>> name
u'Voirie etroite'
>>> vname = arcpy.ValidateTableName(name,wk)
>>> vname
u'Voirie_etroite'
>>> vuname = validUniqueName(vname,wk)
>>> vuname
u'Voirie_etroite_1'