Lets say I have a temp layer made out of selected items from another layer and I am trying to look through 2 attributes in that layer with searchCursor:

 union_test = "C:/Workspace/Sandbox/MapChangeProject/intersects.gdb/footprint_cslf_union_10418"
 selects = arcpy.SelectLayerByAttribute_management(union_test, "NEW_SELECTION", "FID_master_merge_footprints_20181004 <> -1 and FID_cslf_20181004 <> -1 and DFIRM_ID like \''+ {} +'%\'".format(fip))
 selector = arcpy.MakeFeatureLayer_management(selects, 'selects_lyr')

I've noticed in testing that if I run the following in my terminal once, it works fine, and prints as expected. But if I try to run it again, nothing prints. Why would this be happening? Is it due to the selector layer just being temporary or something else. For runtime sake, I was trying to avoid using arcpy.CopyFeatures_management to make a physical feature layer that I would need to store in a .gdb somewhere.

cursor = arcpy.da.SearchCursor(selector,['Pre_Zone', 'NEW_ZONE']) 
    for row in cursor:
     pzone_value = row[0]
     nzone_value = row[1]
     test = placer(pzone_value, nzone_value)
  • See gis.stackexchange.com/a/297336/115 for one example of a simpler coding pattern. It won't apply directly to this question but may give you an idea for how code like yours may be refactored. – PolyGeo Oct 8 '18 at 12:22

You have asked this question many times. You are creating Result objects and then try to use these as layers. You need to start Reading the help sections for correct input/syntax. Input for SelectLayerByAttributes is a layer, not a feature class like you are trying to pass.

import arcpy

fc = r'C:\data.gdb\Parcels'

#You need a layer to use in SelectLayer..:
arcpy.MakeFeatureLayer_management(in_features=fc, out_layer='lyr')
sql="""{0} IN(1,2,3,4)""".format(arcpy.AddFieldDelimiters('lyr',arcpy.Describe('lyr').OIDFieldName))
selects = arcpy.SelectLayerByAttribute_management(in_layer_or_view='lyr', 
print type(selects)


class 'arcpy.arcobjects.arcobjects.Result'

You cant use this as a layer. The layer name is 'lyr', both before and after the selection.

|improve this answer|||||
  • While it's technically true that SelectLayerByAttributes takes a layer as input, according to the Usage information "If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied." I know that I've asked similar questions, but this question was mainly on explaining the impermanence of creating a layer out of a selection. Both my original method and your solution still produced then same result; a layer of temporarily selected records that did not remain through a second iteration of the cursor. – gwydion93 Oct 8 '18 at 17:25

Although I am still not completely sure of why the temporary layer of selected features does not remain when I do a second iteration, I am assuming that it is because temp layers and selected features within those layers are by nature not permanent (it's just a result that is cleared after process has run). Apologies if the question wasn't clear and feel free to correct my misunderstanding here. However, the suggestion presented by @PolyGeo of using a simpler code structure and assigning the cursor to a variable seemed to overcome this issue. Here is what I used that overcame the problem I was having:

 union_test = "C:/Workspace/Sandbox/MapChangeProject/intersects.gdb/footprint_cslf_union_10418"
 pzone_value = [row[0] for row in arcpy.da.SearchCursor(union_test, "Pre_Zone", "FID_master_merge_footprints_20181004 <> -1 and FID_cslf_20181004 <> -1 and DFIRM_ID = '{}C'".format(fip))]
 nzone_value = [row[0] for row in arcpy.da.SearchCursor(union_test, "NEW_ZONE", "FID_master_merge_footprints_20181004 <> -1 and FID_cslf_20181004 <> -1 and DFIRM_ID = '{}C'".format(fip))]
 zone_list = list(zip(pzone_value, nzone_value))
 for item in zone_list:
        pz_value = item[0]
        nz_value = item[1]
        z_place = placer(pz_value, nz_value)

This allowed me to preselect the desired feature using a query /where clause and completely avoid having to use SelectLayerByAttribute altogether. Hope this helps someone in a similar jam.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.