populating a Python List and Queue.Empty error

1504
3
05-05-2013 10:12 PM
DanielBrenner
Occasional Contributor
I am working on a project where I simply want  to create maps of the FHWA Functional Classifications for all the roads in a county.  The field name in the roads layer is FUNC.  I created a Python List with this name (func), and want to populate it with the functional class values.   I keep getting Queue.Empty messages every time I run it. 

I am a relative newbie at Python, and while I can intuitively get the flow involved--create list, go through rows in table, add values to the list--I don't know what code I'd use to fix this. 

I have a gut feeling I might have to use GetValue to populate the List with the Functional Classification values in order for the script to really work, but I don't know how I'd write it.

Script results and Error message below, Script attached.

Thank you,

Dan B

*************************************************

SONOMA COUNTY FUNCTIONAL CLASSES and SEGMENTS

Map Extent of Data Frame is:
XMin: 6114361.750469, YMin: 1685797.745094,
XMax: 6477131.750469, YMax: 2180587.745094
Layer Name: SONOMA COUNTY STREETS
Longname: SONOMA COUNTY STREETS
StreetLayer name is: SONOMA COUNTY STREETS
Processing text elements...
Export Functional Class = 0
ARCPY ERRORS:


PYTHON ERRORS:
Traceback Info:
  File "C:\Users\Owner\Desktop\GEOG 375\FINAL PROJECT\Brenner_FINAL_TEMPLATE_2_NJ_Coding.py", line 138, in <module>
    ExportToPDF(mxd, mappath + 'Map ' + str(func_value) + '.pdf')

Error Info:
     <class 'Queue.Empty'>:
Tags (2)
0 Kudos
3 Replies
markdenil
Occasional Contributor III
To start, you should include your code in the post using the <square bracket>CODE<square bracket> tags
accessable in the # formating button. to wit:
            func_list = []  # Creates an empty Python List  

            srows = arcpy.SearchCursor(StreetLayer)   # Create a search cursor on the function class feature class


            for srow in srows:   # loop over the rows of the functional class layer

                    if srow.FUNC not in func_list:
                        func_list.append(srow.FUNC)  # adds unique FUNC values to the list


            for func_value in func_list:

                query = '"FUNC" = ' + str(func_value)

            StreetLayer.definitionQuery = query


The list building looks okay
but you then run through the list, building stings and assigning them to 'query'
but you don't do anything with any except the last.
The StreetLayer.definitionQuery = query assignment is outside the loop
so only the last func_value is used.

In any event, the query clause syntax looks funky.
try
"\"FUNC\" = '%s'" % (func_value)

You need double quotes around the field, and single quotes around the func_value string
and some sort of quotes around the whole string (to assign it to the variable).
Thus, you either wrap it all in double quotes, and escape the double quotes on the field
or wrap it all in single quotes and escape the single quotes on the value string,
or futz around with tripple quotes, which is a headache.

There may be other issues, but start with these.
try printing some of the values you make and use inside the script, to make sure you are passing what you you expect.
0 Kudos
DanielBrenner
Occasional Contributor
Hello,

Thank you big time for reaching out to me.  I do appreciate it.

I did consult with my Professor on the issue and I had to clarify for him that the values in the FUNC field are in Number format and not Text.  (They are Short type with a field width of 3.  Would the fact they are Short and not Double or? have something to do with my problems?)

Given that they are in number format, he instructed me to use the query statement that you will see on line 72. 

However, even after doing this, and even bringing in a streets layer with a smaller amount of segments, I am STILL getting the same error message.  I am a newbie at Python, but at the same time something is not making it into the "Queue" with  the 'for..." loops.  Is it because the funct_values aren't getting stored somewhere?  Are they even being selected? 

Thanks again,

Dan Brenner
0 Kudos
markdenil
Occasional Contributor III
Python execution is goverened by indentation.
your for srow in srows: loop is all twisted up.

My impression is that you want to export a pdf for each setting of the StreetLayer.definitionQuery.
Thus, you should
1. make your tElements list
2. open the cursor, make your funct_list list, and close the cursor
3. start the for funct_value in funct_list:
3a. set the query string and apply it to StreetLayer.definitionQuery
3b. set the text elements  ( run through the tElements list you made at the start)
3c. check for an existing pdf,
3d. make the new PDF
3e. start the step three loop over for the next funct_value

also...

In my experiance, you have to have the correct double and single quotes INSIDE the string you pass as the query.
The format
'"FUNC" = ' + str(funct_value)
does not do that.

If you have
funct_value = 99999
query = '"FUNC" = ' + str(funct_value)
print query

it prints:     "FUNC" = 99999
which will not work.

while the format I suggested:
funct_value = 99999
query = "\"FUNC\" = '%s'" % (func_value)
print query

will print:     "FUNC" = '99999'
which is what you need

The python % formating (specifically, the %s) casts the variable as a string,
just as str(var) does,
so you should not have type problems.

alternatly, you could modify your method as so:
query = '"FUNC" = \'' + str(funct_value) + "'"

One trick is that, instead of
print srow.FUNC
try
print query
At this point it is the query string you want to inspect, not the field value
(and you should have been printing funct_value anyway, that is what you are trying to use)

finally

Your error messege clearly shows that it gets to the ExportToPDF before bailing,
but I suspect it bails because there is nothing for it to export.
You could try cutting out all the fancy StreetLayer.definitionQuery
stuff, and making sure it will export anything at all...
0 Kudos