Select to view content in your preferred language

Text Element Questions

4045
34
Jump to solution
08-14-2013 11:36 AM
GeoffOlson
Regular Contributor
So I've got my text element function working in layout view, but it leaves me with two questions.  Can multiple rows be concatenated into the row.getValue or can it only find single fields?  Also, is it possible to return all feature records?

Here is the script I'm using to get the first result to fill the text box:

import arcpy  mxd = arcpy.mapping.MapDocument("Current") mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0]  concatElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat")[0]  rows = arcpy.SearchCursor(mapLyr.dataSource) row = rows.next()  typeElem.text = row.getValue("CONCAT")  mxd.save() del mxd, row, rows,


Any suggestions a much appreciated.
Tags (2)
0 Kudos
34 Replies
GeoffOlson
Regular Contributor
Okay, so I had an idea of how to try this script and I have it working with a definition query.  The only problem is that I can't get python to make the definition query for me.  I've tried to get the page number from the ddp but it sends the map book back to page 1 with with a query of FIELDNAME = ""

mxd = arcpy.mapping.MapDocument("current")
pageNum = arcpy.GetParameterAsText(0)

ddp = mxd.dataDrivenPages
arcpy.AddMessage(pageNum)

pageID = mxd.dataDrivenPages.getPageIDFromName(pageNum)
mxd.dataDrivenPages.currentPageID = pageID


Would pageRow be a better way of getting the page number from the index layer?
0 Kudos
GeoffOlson
Regular Contributor
I have a full working script!  Mzcoyle and Wayne, I couldn't have done this without you guys, so thank you so much for your patience and help!  I have to also thank South Kingstown, RI for their ESRI example, I wouldn't have been able to figure this out without their template.  Only one parameter was created and that was for the page number selection.

The tool takes the features in the data driven page map extent and lists the "CONCAT" field into two text elements on the screen.  The "CONCAT" field is a concatenated field of the feature's number on the map and the feature description.  Each feature's string is listed on a new line and the lines are divided so it appears as though there are two columns of text.  If there are an uneven number of lines, then the first column will have one more line than the second.

Here's the python code:
import arcpy, os  #set map doc and the layer to be used mxd = arcpy.mapping.MapDocument("Current") mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0]  #Get page number from data driven page - specified in the tool parameter dialogue box pageNum = arcpy.GetParameterAsText(0) ddp = mxd.dataDrivenPages arcpy.AddMessage(pageNum) pageID = mxd.dataDrivenPages.getPageIDFromName(pageNum) mxd.dataDrivenPages.currentPageID = pageID  #Set layer definition query, this contols the rowcount variable pageFieldValue = pageNum mapLyr.definitionQuery = '"pageNum" = %s' % pageNum  #Listing the text elements on the page concatElem1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat1")[0] concatElem2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat2")[0]  #Finds the number of features in the map and sets up the lists to evenly distribute into the two text elements/columns rowcount = int(arcpy.GetCount_management("Detail_2013").getOutput(0)) percolumn = round(rowcount / 2.0) count1 = 1 count2 = rowcount  #Specifies the features being used for the SearchCursor rows = arcpy.SearchCursor(mapLyr, "", "", "CONCAT") fieldrow  = arcpy.SearchCursor(mapLyr, "", "", "pageNum") currentpage = ""  text_var1 = str() text_var2 = str()  #The first for and if block limits the searched rows to the definition query #The seconded/indented for and if block adds the text fields to the text elements for row in fieldrow:     if currentpage != row.pageNum:         currentpage = row.pageNum         for row in rows:             if count1 <= percolumn:                 text_var1 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)                 concatElem1.text = text_var1                 count1 += 1             elif count2 > percolumn:                 text_var2 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)                 concatElem2.text = text_var2                 count2 - 1             else:                 pass     else:         pass  #Removed the definition query so all page numbers appear when the script is run next and refresh he layout view mapLyr.definitionQuery = "" arcpy.RefreshActiveView() del mxd, row, rows, rowcount, percolumn, count1, count2


And this part is what is in the validation field of the script tool:
class ToolValidator:   """Class for validating a tool's parameter values and controlling   the behavior of the tool's dialog."""    def __init__(self):     """Setup the Geoprocessor and the list of tool parameters."""     import arcpy     self.params = arcpy.GetParameterInfo()    def initializeParameters(self):     """Refine the properties of a tool's parameters.  This method is     called when the tool is opened."""     import arcpy, os, sys      #Reference Plat Index Layer to get unique Plat Numbers     mxd = arcpy.mapping.MapDocument("CURRENT")     mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0]      #Iterate rows in Plat Index Layer to generate unique name list     rows = arcpy.SearchCursor(mapLyr)     row = rows.next()      #Create and populate list     uniqueList = []     while row:       #If the value is not already in the list, append it       if row.getValue("pageNum") not in uniqueList:         uniqueList.append(row.getValue("pageNum"))       row = rows.next()            #Sort the list alphanumerically         uniqueList.sort()     self.params[0].filter.list = uniqueList     self.params[0].value = uniqueList[0]     return    def updateParameters(self):     """Modify the values and properties of parameters before internal     validation is performed.  This method is called whenever a parmater     has been changed."""     return    def updateMessages(self):     """Modify the messages created by internal validation for each tool     parameter.  This method is called after internal validation."""     return
0 Kudos
RhettZufelt
MVP Notable Contributor
Is pageNum actually the name of the field you used as the "Name Field" when you set up your DDP?

getPageIDFromName (page_name)
Parameter Explanation Data Type
page_name A value in the index layer that corresponds to the Name field that was used to set up Data Driven Pages
String

Many of the Data Driven Pages properties and methods use an internal index value rather than the literal names of the pages used to create the index layer. The index values are automatically generated based on the Name and Sort fields. It may not be obvious which index value represents a specific page. The getPageIDFromName method provides a mechanism for this translation.

pageID = mxd.dataDrivenPages.getPageIDFromName("HarborView")
mxd.dataDrivenPages.currentPageID = pageID


R_

[ATTACH=CONFIG]26820[/ATTACH]
0 Kudos
GeoffOlson
Regular Contributor
@rzufelt

I received the subscription email for your post, but I'm not seeing it in the thread.  Yes, I did use "pageNum" as my field name in both my index layer and my feature layer.
0 Kudos
RhettZufelt
MVP Notable Contributor
Seems we were both posting at the same time.  When mine posted, I saw that you had already figured it out, so I deleted my "irrelevant" post.

R_
0 Kudos