I'm trying to run a Python script to merge all polygon features in a single file geodatabase but my ListFeatureClasses function doesn't appear to be returning any FC names!
See attached with screen shots and code
import arcpy
import os
# This will be the list of polygon features to merge
listFC = []
# Set the workspace to state CoreLogic FGDB
arcpy.env.workspace = r"L:\SRS_Heirs_29Mar19\20190310_R8_HeirsProperty_AL_CoreLogicParcelsMods_SRS4952.gdb"
# Set output FGDB
outputFGDB = r"D:\Documents\ArcGIS\Projects\SRS4952_Heirs\SRS4952_Heirs.gdb"
# Set output FGDB and new merged FC
mergedFC = os.path.join(outputFGDB, "CL_AL_ParcelsArea")
# Get list of polygon feature classes in FGDB
listFC = arcpy.ListFeatureClasses(feature_type='Polygon')
# Now merge
print ("Starting merge now...")
arcpy.Merge_management(inputs=listFC, output=mergedFC)
print("Finished polygon merge...")
ListFeatureClasses is not returning any polygon feature classes (there are about 100 in the FGDB)
All polygon features have the same data schema
Solved! Go to Solution.
As Dan is desrcribing, ListFeatureClasses will only give you a list from the current workspace. If the structure is only one deep you can replace this:
# Get list of polygon feature classes in FGDB
listFC = arcpy.ListFeatureClasses(feature_type='Polygon')
with this
from arcpy import env
# Get list of polygon feature classes in FGDB
wk = env.workspace
fds = arcpy.ListDatasets()
listFC = []
for fd in fds:
env.workspace = os.path.join(wk, fd)
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')
listFC += fcs
env.workspace = wk
have you got them into feature datasets, everything is a level down from the gdb
As Dan is desrcribing, ListFeatureClasses will only give you a list from the current workspace. If the structure is only one deep you can replace this:
# Get list of polygon feature classes in FGDB
listFC = arcpy.ListFeatureClasses(feature_type='Polygon')
with this
from arcpy import env
# Get list of polygon feature classes in FGDB
wk = env.workspace
fds = arcpy.ListDatasets()
listFC = []
for fd in fds:
env.workspace = os.path.join(wk, fd)
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')
listFC += fcs
env.workspace = wk
Curtis,
Thanks... I usually don't use datasets so wasn't familiar with that -
this was an inherited FGDB
Another approach, which I would argue is more Pythonic, would be to use Walk—Help | ArcGIS Desktop :
import arcpy
import os
# Set workspaces and output(s)
inputFGDB = r"L:\SRS_Heirs_29Mar19\20190310_R8_HeirsProperty_AL_CoreLogicParcelsMods_SRS4952.gdb"
outputFGDB = r"D:\Documents\ArcGIS\Projects\SRS4952_Heirs\SRS4952_Heirs.gdb"
mergedFC = os.path.join(outputFGDB, "CL_AL_ParcelsArea")
# Get list of polygon feature classes in FGDB
walk = arcpy.da.Walk(inputFGDB, datatype="FeatureClass")
listFC = [
os.path.join(root, feature_class)
for root, data_sets, feature_classes in walk
for feature_class in feature_classes
]
# Now merge
print ("Starting merge now...")
arcpy.Merge_management(inputs=listFC, output=mergedFC)
print("Finished polygon merge...")
Thank you
Yes I've seen that function and actually tested it; much cleaner
Thanks for the replies, I've implemented Curtis' and filing the walk() function for later use. Final code as follows for others interested:
# 02Apr19
# Heirs Property Processing Script - SRS4952
# Dudley R. Hartel
# 747 Maxine Dr
# Baton Rouge, LA 70808
# 706-988-3916 cell
# arborist.dudley.hartel@gmail.com
# Python 2.7 (ArcMap) and 3 (ArcGIS Pro) should both work
# GeoNet Assistance: Curtis Price, Joshua Bixby, Dan Patterson [01Apr19]
# Finds all polygons in a FGDB (e.g. CoreLocic state level FGDB) and merges them into a new feature class.
# Takes county level features and merges into a single feature for the state
import arcpy
import os
from arcpy import env
# For each state to be processed...
# 1) workspace - change the 2 character state code
# 2) outputFGDB - verify the output location
# 3) mergedFC - change the 2 character state code
# Set the workspace to state CoreLogic FGDB
env.workspace = r"L:\SRS_Heirs_29Mar19\20190310_R8_HeirsProperty_GA_CoreLogicParcelsMods_SRS4952.gdb"
# Set output FGDB
outputFGDB = r"L:\SRS_Heirs_29Mar19\SRS4952_StateHeirsJoins.gdb"
# Set output FGDB and new merged FC
mergedFC = os.path.join(outputFGDB, "CL_GA_AreaParcels")
# This will be the list of polygon features to merge
listFC = []
# This will be the list of datasets in the FGDB
listDS = []
# Get list of polygon feature classes in FGDB
currentWS = env.workspace
print ("\n \n \n" + "Getting polygon features now...")
# Each county is in a dataset with a point and polygon feature
# Get each dataset
listDS = arcpy.ListDatasets()
# Now look into each datset and find the polygon features
for fds in listDS:
env.workspace = os.path.join(currentWS, fds)
fcs = arcpy.ListFeatureClasses(feature_type='Polygon')
listFC += fcs
env.workspace = currentWS
# Now merge
print("\n{}".format("Starting merge now..."))
arcpy.Merge_management(inputs=listFC, output=mergedFC)
print("\n{}".format("Finished polygon merge..."))