1

i want to check through all my gdb if a FeatureClassSet contains more than one FeatureClass. If so merge them and save the result in an other gdb otherwise just copy them to the gdb. It sounds really simple to me - but it is not working. So far I've got this:

import arcpy, os
from arcpy import env

workspace = arcpy.GetParameter(0)
EK = arcpy.GetParameter(1)

outWorkspace = r"C:\...\20_Ges.gdb"
Zieldataset = os.path.join(outWorkspace, EK)

fc = arcpy.ListFeatureClasses(feature_dataset=EK)
for item in fc: 
    item.count(<1):
        arcpy.FeatureClassToFeatureClass_conversion (workspace, Zieldataset)
    else:
        arcpy.Merge_management(fc, Zieldataset) 

I connected the script to a model that iterates the variable EK and sets the workspace. The error in the script is in the line that starts with "item.count". enter image description here

2

I adjusted your code on line 12 (originally starting with item.count) to use Python syntax for if statements - added the keyword if and removed the test <1 from parenthesis.

import arcpy, os
from arcpy import env

ws = arcpy.GetParameterAsText(0)
EK = arcpy.GetParameterAsText(1)

outWorkspace = r"C:\...\20_Ges.gdb"
Zieldataset = os.path.join(outWorkspace, EK)

arcpy.env.workspace = ws
fc = arcpy.ListFeatureClasses(feature_dataset=EK)
for item in fc: 
    if item.count == 1:   # This will never be true, see below
        arcpy.FeatureClassToFeatureClass_conversion (ws, Zieldataset)
    else:
        arcpy.Merge_management(fc, Zieldataset)

However, I don't think will do exactly what you are looking for, as the ListFeatureClasses output is a list of individual feature classes, and you are testing a count method with no parameters for each individual feature class. item.count is a built-in method to a unicode object. Since item is a string, the count method counts the number of occurrences of a substring. For example, if a lot of your feature classes had the name myfcnameis4512345 and you perform item.count('fcname'), the result would be 1. If you perform item.count('45') the result would be 2 because it found 2 occurrences of the substring 45.

Instead, just check the length of the list of feature classes, such as below:

import arcpy, os
from arcpy import env

ws = arcpy.GetParameterAsText(0)
EK = arcpy.GetParameterAsText(1)

outWorkspace = r"C:\...\20_Ges.gdb"
Zieldataset = os.path.join(outWorkspace, EK)
env.workspace = ws
fc = arcpy.ListFeatureClasses(feature_dataset=EK)
if len(fc) > 1: 
    arcpy.Merge_management(fc, Zieldataset)
elif len(fc) == 1:        
    arcpy.FeatureClassToFeatureClass_conversion(os.path.join(ws, fc[0]), Zieldataset, fc[0])
else:
    print "Empty feature dataset {}, nothing to convert".format(Zieldataset)

EDIT: I explicitly included a call to either arcpy.env.workspace or env.workspace depending on how the imports were called. This is because ListFeatureClasses requires a workspace to be set. To avoid confusion I renamed your variable workspace to ws.

EDIT: I think the GetParameter, which returns an object, might cause some unexpected results. I've changed to using GetParameterAsText, which returns a string.

  • oh. You're right. Thanks. But it is still not working. maybe because the featureclass to featureclass does not know what the feature in the selected feature set is named...just a guess. Is there an additional field step needed? – Lisa Nov 28 '18 at 18:15
  • Ah, yeah - good catch. You've already imported os, so I think you can just use arcpy.FeatureClassToFeatureClass_conversion(os.path.join(workspace, fc[0]), Zieldataset, fc[0]) This assumes you want the fc named the same in the new gdb. (Third parameter can be changed to give a different fc name.) – smiller Nov 28 '18 at 18:28
  • okay thanks again for your help. I tried it out. But the IndexError: list index out of range - appeared and what does the [0] mean? – Lisa Nov 28 '18 at 18:36
  • and i'm not sure if I can combine a list with a path? I tried os.path.join(workspace, fc) before as separate parameter - but it wouldn't work. – Lisa Nov 28 '18 at 18:38
  • OK, interesting. It looks like we may need to check for having an empty feature_dataset. The brackets are an index, and Python indexes start with zero. The presumption was that you would have a list called fc with lengh 1, or a list of fc with length more than 1. The List Index out of range message tells me that ListFeatureClasses came up empty. – smiller Nov 28 '18 at 18:38
0

thanks to the help from @smiller I found all mistakes:

  • use len() insead of count
  • then the workspace and index were missing
  • finally it had a problem with the "else" part if not >1 so i replaced it with a elsif >2 and it worked.

Here the working script:

import arcpy, os
from arcpy import env

ws = r"C:\......21_IN.gdb"
EK = arcpy.GetParameter(0)
outWorkspace = r"C:\....21_Ges.gdb"
temp=EK+"_temp"
Zieldataset = os.path.join(outWorkspace, EK)
Zieldataset_temp = os.path.join(outWorkspace, temp)

env.workspace = ws 
fc = arcpy.ListFeatureClasses(feature_dataset=EK)

if len(fc) > 1:
    arcpy.Merge_management(fc, Zieldataset_temp)
    arcpy.Dissolve_management (Zieldataset_temp, Zieldataset, dissolve_field="", statistics_fields="", multi_part="MULTI_PART", unsplit_lines="DISSOLVE_LINES")
    arcpy.Delete_management (Zieldataset_temp)
elif len(fc) < 2:
    arcpy.FeatureClassToFeatureClass_conversion(fc[0], outWorkspace, EK)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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