Select to view content in your preferred language

Create new Feature Classes from attribute selection loop

3580
3
06-22-2012 11:49 AM
deleted-user-IR249IovB3CN
Deactivated User
I am trying to loop through year values in an attribute field of a point feature class and create new point feature classes of all features for each year. For example an FC for 2004, an FC for 2005, and FC for 2006, etc.. The attribute field "yr" is the numeric integer field containing the year values. I want to select all points with "yr" = 2004 and create a new feature class 2004_ELB and so on for the remaining years. My code is below. My SelectLayerByAttribute_management query seems to be incorrect. Beyond that I'm not clear if my loop logic is correct either. Any help is greatly appreciated.

# import system modules
import arcpy

# set workspace environment
env = arcpy.env.workspace = "C:/Files/GIS/Projects/Hypoxia/Shrimp_Hypoxia/AnalysisData.gdb"

#Set OverWrite if files already exist
arcpy.env.overwriteOutput = True

# Make a layer from the feature class
FC = arcpy.MakeFeatureLayer_management("Full_Dataset_Albers", "FC")

#Create a list of years to iterate through to enable selection of each year
yrlist = [2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011]
print yrlist

#Initiate Search Cursor to use in loop of attribute values
rows = arcpy.SearchCursor(FC)

for row in rows:
    fieldName = "yr"
    value = row.getValue(fieldName)
    for year in yrlist:
        arcpy.SelectLayerByAttribute_management (FC, "NEW_SELECTION", "yr" = yrlist)
        arcpy.MakeFeatureLayer_management(YearFC, str(year) + "_ELB")
Tags (2)
0 Kudos
3 Replies
NobbirAhmed
Esri Alum
Here is your code simplified - at the end I'm calling Copy Features tool. In your case you need to Point Density tool.

import arcpy

# set workspace environment
arcpy.env.workspace = r"C:\data\my.gdb"

#Set OverWrite if files already exist
arcpy.env.overwriteOutput = True

#Create a list of years to iterate through to enable selection of each year
yrlist = [2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011]

YearFC = "myfc"

for year in yrlist:

    Year_layer = str(year) + "_ELB"

    arcpy.MakeFeatureLayer_management(YearFC, Year_layer)

    arcpy.SelectLayerByAttribute_management (Year_layer, "NEW_SELECTION", "\"Yr\" = %d" % (year))

    arcpy.CopyFeatures_management(Year_layer, "fc_" + str(year))
0 Kudos
deleted-user-IR249IovB3CN
Deactivated User
Thank you ! Your code worked! Below is my final code with the kernel denisty estimation replacing point density. I still do not understand the for loop though. You create a variable with the name specified by the year list and then an empty layer in the workspace with that name. You are then selecting by attributes in that new layer, but how are the attributes associated with that layer? I thought this was a new empty layer from the arcpy.MakeFeatureLayer_management function? I don't see how the attribute table from the YearFC variable is associated with the new layer. Can you explain?

Thank you.
0 Kudos
NobbirAhmed
Esri Alum
The Make Feature Layer creates a layer, say named "2004_ELB", from the source feature class YearFC. This layer actually is same as the YearFC but lives in memory - it has (in fact, refers to) same number of records and fields. It's just an image of the feature class. So, the Make Feature Layer is not creating any "empty layer" 🙂

Then you make selection from that layer and so on.

Note: if this solves your issue please mark the thread as "answered" 🙂
0 Kudos