Monday, 15 June 2015

arcgis 10.2 - Merging all feature classes with same name nested in multiple geodatabases using ArcPy?


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.



Answer




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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...