Friday, 24 January 2020

Arcpy mapping text element reset


I am writing a script to create thousands of maps and I am trying to create a table that updates for every map. Basically an updating table of the information specific to each map. I have been trying to link a an excel table to the layout view but arc does not seem to have that functionality. So what I am doing instead is creating a manual table in the layout view. I have 27 values for each map that I have been storing in a dictionary (using searchcursors to extract the info and store into dictionary).


I cannot copy and paste my table into here but it essentially looks this:


Receptors               AEGbuffer    CONbuffer    ERGbuffer
buffer 1 10 19
schools 2 11 20
childcares 3 12 21
hospitals 4 13 22
nursinghomes 5 14 23
critical 6 15 24

sw 7 16 25
streams 8 17 26
respop 9 18 27



for k,v in sorted(table.items()):
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.text == "1":
if k == 'AEGbuffer': elm.text= v
if elm.text == "2":

if k == 'AEGschools': elm.text = v
if elm.text == "3":
if k == 'AEGchildcares': elm.text = v
if elm.text == "4":
if k == 'AEGhospitals': elm.text = v
if elm.text == "5":
if k == 'AEGnursinghomes': elm.text = v
if elm.text == "6":
if k == 'AEGcritical': elm.text = v
if elm.text == "7":

if k == 'AEGsw': elm.text = v
if elm.text == "8":
if k == 'AEGstreams': elm.text = v
if elm.text == "9":
if k == 'AEGrespop': elm.text = v

this is just for the first row, the rest of the code is the same process for the pasting of the dictionary values.


where I am stuck: I do not know how to properly reset the newly changed values to their original 1-27 values so the next map can be easily produced. is there a tool or trick to this?


what I have tried: after I export the map I reopen the text elements and reverse the process like..


for k,v in sorted(table.items()):

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if k == 'AEGbuffer':
if elm.text == v: elm.text = "1"
and so on......

but this approach hasn't worked great. a few of the values are wrong when there are the same values a few times on the table.



Answer



Instead of setting the text contents of each element set their name to something like "Text1" or even better you could make the element names match up with the key names in your table like 'AEGbuffer'. If it were me I would also add a suffix to the fields so i can make sure I am operating only on the table elements. You might come back later and add text elements that you don't want to include in your table loop.


enter image description here


Then your giant loop could look like this



for table_elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*_table"):
if text_elm.name == k:
text_elm.text = v

To reset you just


for table_elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*_table"):
table_elm.text = ""

EDIT: Based on your comments I think your real problem is your data format.


{'hospitals':{'AEGBuffer': 'None','ConBuffer': '65,200', 'ERGBuffer': 'None'}, 'schools':{'AEGBuffer': '600','ConBuffer': '7,300', 'ERGBuffer': '550'},'respop':{}}



lets you do:


#your k is now going to be the row name, i.e. hospitals and v is going to be another dictionary
for k,v in sorted(table.items()):
#this relies on elements being named hospitalERGBuffer, schoolCONBuffer etc.
for bufferType, bufferValue in v.items():
fieldName = str(k + bufferType) # i.e. 'hospitalsERGBuffer'
table_elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", fieldName + "*")[0] #there's only going to be one if you've done this right.
table_elm.text = bufferValue

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...