arcpy.Describe(lyr).FIDSet failure?

1366
3
05-28-2014 02:45 PM
Emilbrundage
New Contributor III
I have writen a quick code just to improve my arcpy. The code uses a list of letters and then zooms around a California counties layer based on if their name contains each letter. However, it fails with 'i', 'v', and 'n'. 'W' and 'x' are the only letters it should bounce back. I am not sure why it does not detect a selection with 'i', 'v', and 'n'. The code is run in ArcMap through a toolbox. The code:
import arcpy
import time

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
    if lyr.name == "state_county_2010":
        countylyr = lyr
Alphabet = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']
subset = ['e', 'r', 'i', 'o', 'a', 's', 'f', 'h', 'l', 'x', 'n', 'm']
for letter in Alphabet:
    arcpy.SelectLayerByAttribute_management(countylyr, "NEW_SELECTION", '"NAME10" LIKE \'%' + letter + '%\' OR "NAME10" LIKE \'%' + letter.upper() + "%'")
    if arcpy.Describe(countylyr).FIDSet:
        df.zoomToSelectedFeatures ()
        arcpy.AddMessage(letter)
        time.sleep(6)
        
    else:
        if letter in subset: 
            arcpy.AddMessage("No counties contain an " + letter + "!")
        else:
            arcpy.AddMessage("No counties contain a " + letter + "!")


The output:

q
No counties contain a w!
e
r
t
y
u
No counties contain an i!
o
p
a
s
d
f
g
h
j
k
l
z
No counties contain an x!
c
No counties contain a v!
b
No counties contain an n!
m

Any ideas?

Thx
Tags (2)
0 Kudos
3 Replies
ChrisSnyder
Regular Contributor III
Not sure what's wrong, but suspect something in your SQL. Not sure what all the / are for, but you should be able to get away with this:

"NAME10 LIKE %'" + letter + "'% OR NAME10 LIKE %'" + letter.upper() + "'%")

Also, perhaps instead of:

arcpy.Describe(countylyr).FIDSet
use:
len([r[0] for r in arcpy.da.SearchCursor(countylyr. ["OID@"])])

I would think that the line:

if arcpy.Describe(countylyr).FIDSet:

would always evaluate as true (and execute the code indented underneath it), since you are not explicitly looking to see if the .fidset property actually retuned a string of selected OIDs or just a blank string (it returns a semicolon delimited list of selected OIDs as I recall, otherwise just a blank string... For example '1;2;3;4' vs. just '' if no selection... it does not evaluate to None however (at least in v10.1).
0 Kudos
ChrisSnyder
Regular Contributor III
Woops, I mean:

"NAME10 LIKE '%" + letter + "%' OR NAME10 LIKE '%" + letter.upper() + "%'"
0 Kudos
Emilbrundage
New Contributor III
It's actually an issue with select by attribute. You can try it yourself with this shapefile:

http://scec.usc.edu/internships/useit/content/california-counties-shapefiles

Then you can run my script with a few slight modifications (feature name, field name), and the results will be the same as what I found.

Or you can type in these two commands into the Python command line in arcmap and compare the results:

Succeeds:

arcpy.SelectLayerByAttribute_management("CaliforniaCounty", "NEW_SELECTION", '"Name" LIKE \'%a%\' OR "Name" LIKE \'%A%\'')

Fails:

arcpy.SelectLayerByAttribute_management("CaliforniaCounty", "NEW_SELECTION", '"Name" LIKE \'%i%\' OR "Name" LIKE \'%I%\'')
0 Kudos