#DDP: Set Definition Query on a secondary index #Variables that need to be set by user: #mxdpath = r'C;\Users\Test.mxd' # string #outputfolder = 'PDFFolder' # string #namefield = 'THE FIELD CONTAINING THE NAME OF THE RESULTING PDF FILE' #string #queryfield = 'THE FIELD CONTAINING THE VALUE FOR THE DESIRED QUERY' #string #layers = ['LIST','OF','LAYERS'] # list ('Layer1_UpdateQuery', 'Layer2_UpdateQuery', 'etc') #Import Modules import arcpy, os # Set Variables mxdpath = r'D:\arcgis\Samples\DDP_Index2.mxd' # string outputfolder = 'D:\PDF' # string namefield = 'FIPS' # string queryfield = 'STATE_NAME' # string layers = ['CountySel_FIPS'] # list mxd = arcpy.mapping.MapDocument(mxdpath) # Start Loop through Data Driven Pages for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum # set the current page pageName = mxd.dataDrivenPages.pageRow.getValue(namefield) # get the pageName pageName += '.pdf' # add '.pdf' extension for file name pdf = os.path.join(outputfolder, pageName) # add pageName to output folder to get full output path # Start Loop through all layers of mxd file for lyr in arcpy.mapping.ListLayers(mxd): # for every layer in mxd's Layer List if lyr.name in layers: # if layer name in user input list above #value = mxd.DataDrivenPages.pageRow.getValue(queryfield) # get value of query field value = mxd.dataDrivenPages.pageRow.getValue(queryfield) print value if lyr.definitionQuery == True: # if a definitionQuery Exsts... query = lyr.definitionQuery + "AND" + queryfield + "=" + "\"" + value + "\"" # Add new to old else: query = queryfield + "=" + "\"" + value + "\"" # else create a new query lyr.definitionQuery = query # set the layers defintionQuery else: print 'Next' # else print next, this isn't necassary, but its a good place holder print pageName arcpy.mapping.ExportToPDF(mxd, pdf) # export the current Page to pdf del mxd, lyr, pageName, pageNum, query, value
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.