Select to view content in your preferred language

Arcpy Select By Attribute

141
1
3 weeks ago
jbrower
New Contributor III

I have some code I wrote out that creates a grid index from a selection in a layer. However, I just recently upgraded from Pro 3.2 to Pro 3.3 and now the select by attribute doesn't seem to work in the code at all. Here is the original code with layer being a layer in the map and field and attribute as text values. 

            arcpy.management.SelectLayerByAttribute(
                in_layer_or_view=layer,
                selection_type="NEW_SELECTION",
                where_clause="{0} = '{1}'".format(field, attribute),
                invert_where_clause=None
            )
 
I haven't changed the code and looking at the documentation on it, the code should work, but it isn't. Any suggestions would be great!
 
I did also try this:
 
filter = "{0} = '{1}'".format(field, attribute)
            arcpy.management.SelectLayerByAttribute(layer,"NEW_SELECTION",filter)
 
And unfortunately that didn't work either.
0 Kudos
1 Reply
HaydenWelch
Occasional Contributor II

I've noticed some odd behavior from GridIndex features lately. Have you verified that the selection isn't occuring?

To test it, you can run a CopyFeatures on the selected layer and see if it's actually working. If that's working, try saving that copied layer to a 'memory/temp_selected' layer and indexing that.

If your total feature count isn't that high, you can also create a feature buffer that's dissolved into a single shape then pass that shape to an UpdateCursor spatial filter on the index of the whole grid feature set, then just re-number the grid features line by line (the insertion order matches the number order so re-numbering is trivial with an enumerate() or an index variable)

I'm currently on mobile, but if you need more help I can draft up some code for you.

0 Kudos