Loop through a list of fcs, select by attributes, and copy to new feature classes results in random shapefile?

923
4
Jump to solution
02-08-2021 07:13 PM
KathleenHoenke
Occasional Contributor

I'm trying to loop through a list of feature classes in a geodatabase, extract the polygons whose ATTRIBUTE field values end with x or d, and then copy features into a new feature classes into a new geodatabase. It works to select the correct attributes in one of the fcs, but then it exports it out as a shapefile titled Ditched_Excavated (the name of my output gdb), and keeps overwriting this for each input fc. I'm stumped... TIA!

#Define new clipped NWI
NWI_clip_fgdb = r'F:\Data\NWI\NWI_Wetlands_State_Clip.gdb'
arcpy.env.workspace = NWI_clip_fgdb
clipped_NWI_fcs = arcpy.ListFeatureClasses()
print (clipped_NWI_fcs)

#Define output gdb
channelized_gdb = r'F:\Data\NWI\Ditched_Excavated.gdb'        

# loop throughe each input fc, extract ditched and excavated polygons, write to new fc.
for clipped_NWI_fc in clipped_NWI_fcs:
     arcpy.MakeFeatureLayer_management(clipped_NWI_fc, "lyr")
     whereclause = "ATTRIBUTE LIKE '%r' Or ATTRIBUTE LIKE '%x'"
     selection_type = "NEW_SELECTION"
     arcpy.SelectLayerByAttribute_management("Lyr", selection_type, whereclause)
     in_nwi_path = os.path.join(NWI_clip_fgdb, clipped_NWI_fc)
     out_name = clipped_NWI_fc + "Channelized"
     ditched_features_path = os.path.join(out_name , channelized_gdb)
     arcpy.CopyFeatures_management("Lyr", ditched_features_path)
0 Kudos
2 Solutions

Accepted Solutions
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Hi Kathleen,

It looks like your 'lyr' was changed to 'Lyr' in your code and your

 

ditched_features_path = os.path.join(out_name , channelized_gdb)

 

is backwards, putting your gdb last.

I think you will need to get the name of the fc, using the describe:

 

fc_desc = arcpy.Describe(clipped_NWI_fc)
out_name = fc_desc.baseName + 'Channelized'
ditched_features_path = os.path.join(channelized_gdb, out_name)

 

 

View solution in original post

JamesBrander
Esri Contributor

Hi @KathleenHoenke ,

Try changing

# from this 
ditched_features_path = os.path.join(out_name, channelized_gdb)

# to this
ditched_features_path = os.path.join(channelized_gdb, out_name)

 

Hope that helps! 

View solution in original post

4 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Hi Kathleen,

It looks like your 'lyr' was changed to 'Lyr' in your code and your

 

ditched_features_path = os.path.join(out_name , channelized_gdb)

 

is backwards, putting your gdb last.

I think you will need to get the name of the fc, using the describe:

 

fc_desc = arcpy.Describe(clipped_NWI_fc)
out_name = fc_desc.baseName + 'Channelized'
ditched_features_path = os.path.join(channelized_gdb, out_name)

 

 

KathleenHoenke
Occasional Contributor

Thank you! I didn't end up needing the describe, but the other changes fixed it, thank you! 

0 Kudos
JamesBrander
Esri Contributor

Hi @KathleenHoenke ,

Try changing

# from this 
ditched_features_path = os.path.join(out_name, channelized_gdb)

# to this
ditched_features_path = os.path.join(channelized_gdb, out_name)

 

Hope that helps! 

KathleenHoenke
Occasional Contributor

This worked, thank you!

0 Kudos