Data Driven Page Text Element

410
3
03-20-2013 01:32 PM
andreagriffith
New Contributor
I'm using a combination of data driven pages and a python script to populate text elements on a layout. I have a project set up with an index layer, and a mask layer.  I also have a third layer (permits), which I have set up as a page definition query, to match the index layer selected (they both have a map_number field). This works as I scroll through the pages, the permits layer updates to show only those permits within the active page.  I would like for those queried permits to then populate a table made up of two text elements.

Now, when I run though the script, the text elements are populated but not with correct information.  As I change pages, and the queried permits change, I cannot get the related text elements to change/update.  All the rows will show in the text element(s), and not the queried ones only. What am I missing here?  I'm not sure if it is a definition query problem or a cursor problem (or neither of these)? 

Code:

import arcpy

# get the reference to the map document
mxd = arcpy.mapping.MapDocument("Current")

#Reference appropriate data frames
operationsDF = arcpy.mapping.ListDataFrames(mxd, "Operations")[0]
locatorDF = arcpy.mapping.ListDataFrames (mxd, "Locator Map")[0]

#Reference appropriate data layers
mapLyr = arcpy.mapping.ListLayers(mxd, "Permits_final", operationsDF)[0]

# get the reference to the text elements in the map document.
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.name == "FacNameelem": FacNameelem = elm
    if elm.name == "Stateelem": Stateelem = elm

#Clear all text element values
FacNameelem.text = ' '
Stateelem.text = ' '

# create a cursor and grab the rows in the feature class for the layer specified above
rows = arcpy.SearchCursor(mapLyr.dataSource)
row = rows.next()
text1Value = ""
text2Value = ""
for row in rows:
    text1Value +=row.getValue("FACILITY") +"\n"   
    FacNameelem.text = text1Value
    text2Value +=row.getValue("State") +"\n"
    Stateelem.text = text2Value
            
#Refresh map
arcpy.RefreshActiveView()
 
# save the map document and delete the reference to the row, cursor and map object
mxd.save()
del mxd, row, rows
Tags (2)
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
A couple of things.  First, I'm not seeing in the code below where you are driving the DDP pages.  Your script needs to advance each DDP page, perform the custom logic, then continue onto the next page.  DDP from the UI can't call custom code.  You must write a script that drives both DDP along with your custom logic.

Second,  your cursor needs to include a query that isolates that particular map_number.  For example,

rows = arcpy.SearchCursor(mapLyr.dataSource, "/"map_number/" = "101NW")


Jeff
0 Kudos
andreagriffith
New Contributor
Thanks for the help Jeff.  I've added an activation of the data driven pages in the code:

#Reference the Data Driven Page object
ddp = mxd.dataDrivenPages
pageNum = mxd.dataDrivenPages.currentPageID


I've also added a query to the cursor to isolate the map number:

mapLyr.definitionQuery = '[MAP_NUMBER_FM] = %s' % pageNum
queryExp = '[MAP_NUMBER_FM] = %s' % pageNum
rows = arcpy.SearchCursor(mapLyr.dataSource, queryExp, "")
row = rows.next()


That does seem to work with setting the definition query for my table layer.  However, when I scroll to a new ddp and run the script, the query doesn't refresh.  For example, I ran the script with the ddp pageNum = 1 and the query was set in the related "Permits_final" layer as MAP_NUMBER_FM = 1.  When, I advanced to another page and ran the script again, the query/map_number was still set to 1, although the ddp pageNum was 2. 

Thanks for your help.
0 Kudos
andreagriffith
New Contributor
Actually, I have gotten the query to update properly!

I still have one problem - the search cursor is missing a row (the first row). Or should I say the text element is updating with the selected rows, but leaving one out (the first row).  For example, I have 10 selected features in the queried layer but only 9 showing related in the text element. I must have something written incorrectly.

Thanks for the help!
0 Kudos