0

I am using arcpy and I want to extract a specific feature class called "building" nested in a number of geodatabase within my workspace.

Here is the code:

import arcpy
import os

workspace = "C:/Wiley/P1/gis"
search = "Building"
outdir = "C:/Wiley/P1/gis/HK80.gdb"
fc = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")

for dirpath, dirnames, filenames in walk:
          for filename in filenames:
             if search == filename:
                     fc.append(os.path.join(dirpath, filename))
if fc:
     output = os.path.join(outdir, os.path.basename(search) + "_merge")
     arcpy.Merge_management(fc, output)

This code was successful.

But there are many other featureclass in the geodatabase apart from "Building" So I expanded the code a bit and try to loop the merge command through all these feature class:

    import arcpy
import os

#--------------------------
#example list workspace
arcpy.env.workspace = "C:/Wiley/P1/gis/HKU_Job69.gdb"

#Generate exmaple list dataset
datasets = arcpy.ListDatasets(feature_type='All')
datasets = [''] + datasets if datasets is not None else []
fc_list = []

#--------------------------

#where all the gdb is nested
workspace = "C:/Wiley/P1/gis"

#define output
outdir = "C:/Wiley/P1/gis/HK80.gdb"

#List of Feature classes
fc = []

#define walk for looping
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Any")

for ds in datasets:
     for fc_example in arcpy.ListFeatureClasses(feature_dataset=ds):
        fc_list.append(fc_example)


for f in fc_list: #Loop through the list   
   for dirpath, dirnames, filenames in walk:        
        for filename in filenames:
           if f == filename:
                     fc.append(os.path.join(dirpath, filename))
   if fc:
        output = os.path.join(outdir, os.path.basename(search) + "_merge")
        arcpy.Merge_management(fc, output)

This is where I hit a wall. Is there anything wrong with this code? I am new to Python so for a trained eye you guys may spot out the error in the logic immediately. The f variable didn't seem to loop at all.

0

You'll want to use a python dictionary for this task, with the key being your feature class name and the value being a python list of merge feature classes.

import arcpy
import os

workspace = "C:/Wiley/P1/gis"
outdir = "C:/Wiley/P1/gis/HK80.gdb"

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
di = {}

for dirpath, dirnames, filenames in walk:
          for filename in filenames:
             fcFullPath = os.path.join(dirpath, filename)
             #try adding feature class to dictionary with already-existing key
             try: di [filename] += [fcFullPath]
             #key not in dictionary yet. Create key
             except: di [filename] = [fcFullPath]

#iterate dictionary
for filename in di:
    #get merge feature classes
    mergeFcs = di [filename]
    output = os.path.join(outdir, os.path.basename(fcFullPath) + "_merge")
    arcpy.Merge_management(mergeFcs, output)

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.