Pass list of feature class to union

522
5
Jump to solution
08-12-2021 01:03 PM
2Quiker
Occasional Contributor II

I need to spatial join only certain feature class with specific names but I am not sure how to pass the list of feature class on arcpy.ListFeatureClasses. There is about 10 feature classes in the geodatabase but like I said just need certain ones.

 

 

import arcpy
from arcpy import env

env.workspace = r"C:\Temp\Default2.gdb"
env.overwriteOutput=True

#arcpy.CreateFileGDB_management(r"C:\Temp\Test", "test_6.gdb")
lst = ["Wetlands","Current_Panels"] # this is where I was trying to list the spcifice feature classes I need from the geodatabase.
fclist = arcpy.ListFeatureClasses(lst, "polygon")

for fc in fclist:
    print (fc)
    #fcdesc = arcpy.Describe(fc)
#print (fc)
#arcpy.Union_analysis(fc, "C:\Temp\Test\test_6.gdb")

 

 

0 Kudos
1 Solution

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

You can use a little list comprehension:

lst = [fc for fc in arcpy.ListFeatureClasses('', 'polygon') if fc in ["Wetlands", "Current_Panels", ....]]

View solution in original post

0 Kudos
5 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

You can use a little list comprehension:

lst = [fc for fc in arcpy.ListFeatureClasses('', 'polygon') if fc in ["Wetlands", "Current_Panels", ....]]
0 Kudos
RPGIS
by
Regular Contributor

You want to use the walk method on top of using the os.path.split method if you want to utilize a list of feature classes using python.

import arcpy

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

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        fcs.append(os.path.join(dirpath, filename))

for fc in fcs:
    #Get feature class name
    fcsname = os.path.basename(fc)
    name = os.path.splitext(fcsname)
    y = name[1].lstrip('.')
    #print y
    if name in A_list:#your list
        #do something

 

JoshuaBixby
MVP Esteemed Contributor

ListFeatureClasses—ArcGIS Pro | Documentation does not accept a list as input.  The first parameter is a string representing a wild card, which would work for you if there was a common naming element you could use with a wildcard.  To the best of my knowledge, the wildcard does not support regular expressions.

If you already have specific names of the feature classes, why use ListFeatureClasses at all?  Even if ListFeatureClasses supported passing a list, if you are not using wildcards then the list returned would simply be the list you passed in the first place, i.e., your fclist would be the same as lst.  (ListFeatureClasses doesn't actually return feature classes, it returns a list of strings of the names of feature classes)

 

2Quiker
Occasional Contributor II

Ha ha your right, I guess I was over thinking it.

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

I think this should be marked as the solution. I made my suggestion with the thinking of expanded wildcarding in case there wasn't a singular similarity in each fc name that you needed to filter for. And my post could have been more accurate as well so its not checking the full fc name against the fc name list which would need the complete fc names anyway to be true.  I think it would look like this for wildcarding against the fc name:

lst = [fc for fc in arcpy.ListFEatureClasses('', 'polygon') if any(['a' in fc, 'b' in fc, 'c' in fc])]

 

0 Kudos