Select to view content in your preferred language

Merge Script

1012
3
Jump to solution
02-13-2013 07:02 PM
ChristopherClark1
Deactivated User
Does this python script look correct for a merge?

# Import arcpy module import arcpy, sys, traceback arcpy.env.overwriteOutput = True from os import path as p   # Set Workspace arcpy.env.workspace = r"G:\ChrisGIS\PS_Steelhead\Archive"  # Set Variables. "out" will be our output variable. Inside the "Try" loop we search all workspaces # for the Feature Class we are interested in. This list of feature classes is set to listFCS  out = r"G:\ChrisGIS\PS_Steelhead\Scratch"  for ws in arcpy.Listworkspaces("*", "FileGDB"):  arcpy.env.workspace = ws  print '\n\Searching in %s\n\n' %ws  listFCS = arcpy.ListFeatureClasses("*_NWIFC_STHD")  # Now Merge all Feature Classes and export to the Output  arcpy.Merge_management(listFCS, os.path.join(out, "Merged_HUC8_STHD") print "Done"
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
It's not clear what you want to do.
I suppose you want to merge feature classes from many geodatabases. If so, you have to collect all feature classes names with full path in one list and use this list in Merge.
Here's how code can look like (remember to create geodatabase in your scratch folder first):
import arcpy, sys, os, traceback arcpy.env.overwriteOutput = 1  # Set Workspace arcpy.env.workspace = r"G:\ChrisGIS\PS_Steelhead\Archive"  # Set Variables. "out" will be our output variable. Inside the "Try" loop we search all workspaces # for the Feature Class we are interested in. This list of feature classes is set to listFCS  out = r"G:\ChrisGIS\PS_Steelhead\Scratch\Scratch.gdb"   # list for all feature classes from different GDBs to merge fcToMerge = []  for ws in arcpy.ListWorkspaces("*", "FileGDB"):     arcpy.env.workspace = ws     print '\n\Searching in %s\n\n' %ws     listFCS = arcpy.ListFeatureClasses("*_NWIFC_STHD")      # create a list with full pathes to feature classes     listFCsFullPath =[]     for fc in listFCS:         listFCsFullPath.append(os.path.join(ws, fc))      fcToMerge += listFCsFullPath  # Now Merge all Feature Classes and export to the Output arcpy.Merge_management(fcToMerge, os.path.join(out, "Merged_HUC8_STHD")) print "Done"

View solution in original post

0 Kudos
3 Replies
MarcinGasior
Frequent Contributor
It's not clear what you want to do.
I suppose you want to merge feature classes from many geodatabases. If so, you have to collect all feature classes names with full path in one list and use this list in Merge.
Here's how code can look like (remember to create geodatabase in your scratch folder first):
import arcpy, sys, os, traceback arcpy.env.overwriteOutput = 1  # Set Workspace arcpy.env.workspace = r"G:\ChrisGIS\PS_Steelhead\Archive"  # Set Variables. "out" will be our output variable. Inside the "Try" loop we search all workspaces # for the Feature Class we are interested in. This list of feature classes is set to listFCS  out = r"G:\ChrisGIS\PS_Steelhead\Scratch\Scratch.gdb"   # list for all feature classes from different GDBs to merge fcToMerge = []  for ws in arcpy.ListWorkspaces("*", "FileGDB"):     arcpy.env.workspace = ws     print '\n\Searching in %s\n\n' %ws     listFCS = arcpy.ListFeatureClasses("*_NWIFC_STHD")      # create a list with full pathes to feature classes     listFCsFullPath =[]     for fc in listFCS:         listFCsFullPath.append(os.path.join(ws, fc))      fcToMerge += listFCsFullPath  # Now Merge all Feature Classes and export to the Output arcpy.Merge_management(fcToMerge, os.path.join(out, "Merged_HUC8_STHD")) print "Done"
0 Kudos
MelissaBennett
New Contributor
I modified the included script, but I'm receiving an error
Failed to execute. Parameters not valid.
Error 000735: Input Datasets: Value is required
Failed to execute(merge)

I'm trying to combine all the fc in each gdb, in the topo folder, into a single gdb.  Each gdb has a feature dataset "lts" that contains all the fc (in case it matters). I'm not sure why fcToMerge isn't a valid input. Any help would be appreciated- thanks.
0 Kudos
IanMurray
Honored Contributor
I believe it would be because you are not going into each feature dataset and then listing feature classes.  The example given assumes your feature classes are directly in the gdb, not in a feature dataset in a gdb.

you would need to use arcpy.ListDatasets in there to fix


import arcpy, sys, os, traceback
arcpy.env.overwriteOutput = 1

# Set Workspace
arcpy.env.workspace = r"G:\ChrisGIS\PS_Steelhead\Archive"

# Set Variables. "out" will be our output variable. Inside the "Try" loop we search all workspaces
# for the Feature Class we are interested in. This list of feature classes is set to listFCS

out = r"G:\ChrisGIS\PS_Steelhead\Scratch\Scratch.gdb"


# list for all feature classes from different GDBs to merge
fcToMerge = []

for ws in arcpy.ListWorkspaces("*", "FileGDB"):
    arcpy.env.workspace = ws
    print '\n\Searching in %s\n\n' %ws
    
    for fd in  arcpy.ListDatasets("*", "Feature")
        arcpy.env.workspace = fd
        print '\n\Searching in %s\n\n' %fd
    
    

        listFCS = arcpy.ListFeatureClasses("*_NWIFC_STHD")

    

        # create a list with full pathes to feature classes
        listFCsFullPath =[]
        for fc in listFCS:
            listFCsFullPath.append(os.path.join(ws, fc))

        fcToMerge += listFCsFullPath

# Now Merge all Feature Classes and export to the Output
arcpy.Merge_management(fcToMerge, os.path.join(out, "Merged_HUC8_STHD"))



Edit: I copied the old code from m.gasior and edited that one to work, so you will need to rework it from what I did for yours, but that should fix it.
0 Kudos