text property of the text element equal to the value from row in attribute field

624
6
12-10-2012 07:43 AM
AnneReis
New Contributor
In ArcMap 10.0, is it possible to access the attributes from a layer to populate the text property of text elements in the map document. Or do you have to be using data driven pages?

I'm trying to do just that. A portion of my code worked, but then I started to receive parsing errors including syntax and attribute errors. It might have to do with the whereclause under the SearchCursor. Perhaps I should set up a definition query based on the route number.

# import arcpy
import arcpy

#Get parameter as text
RtNum = arcpy.GetParameterAsText(0)

#set to current map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# set dataframe to current
df1 = arcpy.mapping.ListDataFrames(mxd,"SurveyMap")[0]
df2 = arcpy.mapping.ListDataFrames(mxd,"InsetMap")[0]

#set current layer
lyr = arcpy.mapping.ListLayers(mxd, "Routes2010_NHigh_Alldata", df1)


# get the reference to the text elements in the map document. I have 14 text elements.
RtNumElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "RTNUM")[0]
CoElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "COUNTY")[0]
LocElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "LOCATION")[0]
DateElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "DATE")[0]
TimeStartElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TimeStart")[0]
TimeEndElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TimeStop")[0]
TempStartElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TempStart")[0]
TempEndElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TempStop")[0]
RHStartElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "RelHumStart")[0]
RHEndElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "RelHumstop")[0]
WStartElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "WindStart")[0]
WEndElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "WindStop")[0]
SurvElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Surveyors")[0]
LengthElem = arcpy.mapping.ListLayoutElements (mxd, "TEXT_ELEMENT", "RouteLength")[0]



# create a cursor and or create definition query for SURVEYS All Text Elements
rows = arcpy.SearchCursor("Routes*", "RTNUM = ", "","","","")
row = rows.next()

RtNumElem.text = row.getValue("RTNUM")      
CoElem.text = row.getValue("County")
LocElem.text = row.getValue(???Location???)
DateElem.text = row.getValue(???Start_Date???)
TempStartElem.text = row.getValue(???Start_Temp???)
TempEndElem.text = row.getValue(???End_Temp???)
RHStartElem.text = row.getValue(???Start_Humi")
RHEndElem.text = row.getValue(???End_Humidi???)
WStartElem.text = row.getValue(???Start_Wind???)
WEndElem.text = row.getValue(???End_Wind???)
SurvElem.text = row.getValue(???Surveyor_s???)
LengthElem.text = row.getvalue("Length")

# refresh map
arcpy.RefreshActiveView()


Very new to python, so any help would be greatly appreciated.
Tags (2)
0 Kudos
6 Replies
curtvprice
MVP Esteemed Contributor
Here are few things to look for:

1. Make sure you are using only normal quotes. I see some back and forward quotes in your code, which would definitely break things.
2. SearchCursor can't use a wild card, it has to specifically name a layer or table view
3. The query expression in your search cursor is not complete, you need the whole expression, for example:
where = "RTNUM = '{}'".format(RtNum)
rows = arcpy.SearchCursor(lyr, "RTNUM = ", "","","","")
row = rows.next()


Hope this helps...
0 Kudos
JeffBarrette
Esri Regular Contributor
Here are a couple of other things I see:

First, your lyr variable is not a layer, it is currently a python list.  Change:
lyr = arcpy.mapping.ListLayers(mxd, "Routes2010_NHigh_Alldata", df1)

to
lyr = arcpy.mapping.ListLayers(mxd, "Routes2010_NHigh_Alldata", df1)[0]


To get better performance, don't call ListLayoutElements so many times.  Just call it once.

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
  if elm.name == "RTNUM": RtNumElem = elm
  if elm.name == "COUNTY": CoElem = elm
  if elm.name == "LOCATION": LocElem = elm

  ...



Jeff
0 Kudos
AnneReis
New Contributor
Thanks so much Jeff and Curtis for your corrections to my code. I'm still getting syntax errors during the 'set the text property of the text element equal to the value from the fields' portion of the code.

for row in rows:
RtNumElem.text = row.getValue("RTNUM")      
CoElem.text = row.getValue("County")
LocElem.text = row.getValue(�??Location�?�)
DateElem.text = row.getValue(�??Start_Date�?�)
TimeStartElem.text = row.getValue (�??Start_Time�?�)
TimeStopElem.text = row.getValue (�??End_Time�?�)
TempStartElem.text = row.getValue(�??Start_Temp�?�)
TempStopElem.text = row.getValue(�??End_Temp�?�)
RHStartElem.text = row.getValue(�??Start_Humi")
RHStopElem.text = row.getValue(�??End_Humidi�?�)
WStartElem.text = row.getValue(�??Start_Wind�?�)
WEndElem.text = row.getValue(�??End_Wind�?�)
SurvElem.text = row.getValue(�??Surveyor_s�?�)
LengthElem.text = row.getvalue("Length")


The first two lines (RTNUM and County) work without error (and the text elements are populated with the field data after RefreshActiveView), but then the syntax errors occur for Lines 3 through 12. Does this have anything to do with the field properties in the layer? Do I need a colon after each getValue line?

Thanks again!
0 Kudos
curtvprice
MVP Esteemed Contributor
I'm still getting syntax errors during the 'set the text property of the text element equal to the value from the fields' portion of the code.


- You need to indent all statements inside the loop (have you run through the Python tutorial at Python.org?)
- As I mentioned, avoid the curly quotes, your options are straight single and double quotes.

for row in rows:
    RtNumElem.text = row.getValue("RTNUM")      
    CoElem.text = row.getValue("County")
    LocElem.text = row.getValue(�??Location�?�) 
    (...)
0 Kudos
AnneReis
New Contributor
I wasn't even aware of curly quotes and not sure how I even typed them. Thanks again for pointing that out. Wasn't sure what you meant.
0 Kudos
curtvprice
MVP Esteemed Contributor
I wasn't even aware of curly quotes and not sure how I even typed them.


Beware the curse of the curly quotes (in Microsoft Word):

http://www.techrepublic.com/blog/msoffice/turn-off-words-smart-quotes/1547

If you're not using an IDE (like IDLE or PythonWin) to edit your Python code, I highly recommend it --  because of the requirement for fanatically accurate indentation!
0 Kudos