speleonerd

Looping through a definition query in data driven pages

Discussion created by speleonerd on Mar 20, 2012
Latest reply on Jun 3, 2015 by rr1040
I'm hoping someone can help me with this one; I've read every post I could find and I'm still stuck. I've got a series of data driven pages that I can get to export with no problem, but I also need to update a mask layer while I'm exporting each page. The ddp name is based on state names and the mask layer is also based on state name. I've got a simple definition query that I use to turn the mask layer on and off for each state. For example, the current ddp is Arizona so the definition query on the mask layer would be "STATE_NAME" <> 'Arizona'. This turns the mask layer off for Arizona while leaving the surrounding states grayed out.

Here's the script that I have so far:

import arcpy, sys, os
arcpy.OverWriteOutput = 1
mxd = arcpy.mapping.MapDocument("CURRENT")

print "Enter save as pdf location:"
pdfDir = arcpy.GetParameterAsText(0)
outputFolder = pdfDir + r"\PDFs"

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
maskLayer = arcpy.mapping.ListLayers(mxd, "state_bnd110", df)[0]
maskField = "STATE_NAME"

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageName = mxd.dataDrivenPages.pageRow.STATE_NAME
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == maskLayer:
            lyr.definitionQuery = '"STATE_NAME" <> pageName'
        arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf")
        lyr.definitionQuery = ""
        arcpy.RefreshActiveView()

del mxd
arcpy.GetMessages()

If I take out the layer loop the script will export all the ddp with no trouble (and with no mask symbology). As the script is now, it will export the first state (Arizona) with all the states grayed out (for this page, Arizona should not be) then it will export Arizona again and again.

Any help would be greatly appreciated!
Karla

Outcomes