0

Output details from toolThe attached file is my script for merging different FCs as they get dissolve and join by the input FC. I don’t know why the output for merge does not include all FCs.

import arcpy, os
from arcpy import env
arcpy.env.overwriteOutput = True
env.workspace = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(1)
gdbname=arcpy.ListWorkspaces()
for gdb_Name in gdbname:
  env.workspace = gdb_Name
  for fc in arcpy.ListFeatureClasses():
  hh=fc
  gg=arcpy.FeatureClassToFeatureClass_conversion(fc, 'in_memory', '{}'.format(fc) )
  kk=arcpy.Dissolve_management(fc,gg,"Route_Type","","","")
  arcpy.JoinField_management (kk,"Route_Type",hh,"Route_Type",'#')

fClasses = []   #initialize array to hold feature classes
fClasses.append(gg)
arcpy.Merge_management(fClasses, "Merged")
arcpy.FeatureClassToFeatureClass_conversion(fClasses, output, '#' )
  • 1
    Move fClasses.append inside loop – FelixIP Apr 4 '16 at 20:14
  • I think it would help potential answerers, if you could take the 2-minute Tour to become more familiar with framing good questions for GIS SE. – PolyGeo Apr 4 '16 at 21:31
4

Here are the changes you need to make for what I think you are trying to do. The script, with comments, is below, in case I'm not being clear (and for the sake of other readers).

  1. Create your empty fClasses list at the top of your workspace loop, before you start looping through your feature classes. You are only getting one FC in the merged FC because you are re-creating the empty list with each iteration of the loop.
  2. Your indenting was a little out of wack for the for loop.
  3. Also, the conversion you are using is for single feature classes. You want to use arcpy.FeatureClassToGeodatabase_conversion to make copies of your list of feature classes at the end of your script.

    import arcpy, os
    from arcpy import env
    arcpy.env.overwriteOutput = True
    env.workspace = arcpy.GetParameterAsText(0) # Workspace parameter
    output = arcpy.GetParameterAsText(1) # Workspace parameter
    gdbname=arcpy.ListWorkspaces()
    for gdb_Name in gdbname: # Iterate through workspaces
        env.workspace = gdb_Name
        fClasses = [] # Initialize list to hold feature classes
        for fc in arcpy.ListFeatureClasses(): # Iterate through FC's in each workspace
            hh=fc
            # Make copy of original FC in output workspace - interesting way to create an output path!
            gg=arcpy.FeatureClassToFeatureClass_conversion(fc, 'in_memory', '{}'.format(fc) )
            kk=arcpy.Dissolve_management(fc,gg,"Route_Type","","","")
            arcpy.JoinField_management (kk,"Route_Type",hh,"Route_Type",'#')
            # Append modified FC to list
            fClasses.append(kk)
        # Create merged FC in this workspace
        arcpy.Merge_management(fClasses, "Merged")
        # Make copies of inidividual updated FC's in output workspace
        arcpy.FeatureClassToGeodatabase_conversion(fClasses, output)
    
  • Thanks for response, but I got this error when I ran the script: Traceback (most recent call last): File "C:\Users\Script for FC to Single GDB - Dissolve and meregd By Route Type 3333.py", line 25, in <module> arcpy.FeatureClassToFeatureClass_conversion(fClasses, output, '#' ) File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 1789, in FeatureClassToFeatureClass raise e RuntimeError: Object: Error in executing tool Failed to execute (GrabFeatureClasses). – Koush Apr 4 '16 at 21:37
  • Yes, I made a mistake and fixed my response. Please check it out (last line of code). – RHB Apr 4 '16 at 21:39
  • No, I got posted error when I used edited script from the previous post. It was about last line – Koush Apr 4 '16 at 21:41
  • The error message indicates that you used FeatureClassToFeatureClass instead of FeatureClassToGeodatabase--that was my fix. The first is if you are converting only one feature class; the last is if you are converting one or more feature classes (which you are). You can't feed FeatureClassToFeatureClass a list of multiple feature classes. – RHB Apr 4 '16 at 21:44
  • 1
    If you only want one output file, you need to 1) Initialize the fClasses list outside of ALL of the loops, and 2) move the last two operations--the Merge and the FeatureClassToGeodatabase--outside ALL of the loops. Also, since you are switching workspaces, you need to make sure your list in fClasses includes entire paths. This would actually be easier with da.Walk. You might want to check that out. Sorry I was not available for chat yesterday. – RHB Apr 6 '16 at 15:47

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.