Selection within Nested Loop Not Working as Expected

357
2
Jump to solution
03-15-2022 11:42 AM
ssintelmann
New Contributor III

Thought this was fixed yesterday, mistaken.  I have two sets of polygons.  One featureclass has different fields for YEAR (integer), and GEAR type (string).  I am trying to loop through each of these two fields and do an Identity with the other polygon.  Output I am looking for will ONLY contain the polygons for each year and gear combination.  The code seems to run without error, but I get the exact same number of records for each identity output, so the selection within the loop clearly is not working right.  I am using a function to get the unique values for the year and gear fields.  Wondering what I am messing up?

import arcpy
#Set the data environment 
arcpy.env.overwriteOutput = True

arcpy.env.workspace = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb'
trawlBuffs = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\buffers\trawl_buffers'
fishnet = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\fishnets\vms_net1k'
spidentityOut = r'C:\Data\working\AK_Fishing_VMS\2021_Delivery\ArcPro_proj\ArcPro_proj.gdb\sp_identity\fishnet_'

# function to list unique values of a field within a fc
def unique_values(table, field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})
    del cursor
# Get the list of values for the YEAR' field within the 'trawl_buffs' fc and place into a set
yearVals = unique_values(trawlBuffs, "YEAR")
unique_year = set(yearVals)
# Get the list of values for the 'AGENCY_GEAR' field within the 'trawl_buffs' fc and place into a set
gearVals = unique_values(trawlBuffs, "AGENCY_GEAR")
unique_gear = set(gearVals)
#loop through the years/gears with a selectionClause, and do the identity
try:
    for year in unique_year:
        for gear in unique_gear:
            new_layer = str(year) + str(gear) 
            arcpy.MakeFeatureLayer_management(trawlBuffs, new_layer)
            selectionClause = '{0} = {1}'.format('YEAR', year) + str(" AND ") + '{0} = '"'{1}'"''.format('AGENCY_GEAR',gear)
            arcpy.SelectLayerByAttribute_management(new_layer, "NEW_SELECTION", selectionClause)
            out_feat = spidentityOut + str(year) + str(gear)  
            arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
            print("Writing Data File: " + out_feat)
            # Drop unnecessary Fields
            dropFields = ['FID_vms_net1k']
            arcpy.DeleteField_management(out_feat, dropFields)
except Exception as e:
    #dump errors
    print("Error: " + e.args[0]) 
finally:
    #Clean up the temp layer
    arcpy.Delete_management(new_layer)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Maybe I'm missing something but you've applied a selection to new_layer then not done anything with it after.

#arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
#do you mean?
arcpy.Identity_analysis(new_layer, fishnet, out_feat)

View solution in original post

0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor

Maybe I'm missing something but you've applied a selection to new_layer then not done anything with it after.

#arcpy.Identity_analysis(trawlBuffs, fishnet, out_feat)
#do you mean?
arcpy.Identity_analysis(new_layer, fishnet, out_feat)
0 Kudos
ssintelmann
New Contributor III

No, you are absolutely correct!  sigh. but relief, i need a walk now. Thanks yet again.