Dynamically set the DPP "Page Definition" query based on a changing variable

557
2
Jump to solution
09-19-2012 12:57 PM
MarcoBoeringa
MVP Regular Contributor
Hi all,

I have the need to dynamically set a layer's "Page Definition" query of multiple layers based on a ModelBuilder variable retrieved from a field using an iterator. Each value in the field thus must lead to it's own query and has to be set as the Page Definition of an exported layer.

However, looking around in the ArcToolbox, and the arcPy layer properties, it seems the "Page Definition" isn't actually being exposed anywhere except through the user operated dialogs of ArcGIS itself??

I may well be overlooking something, but are there really no options to programmatically set the DPP "Page Definition" through python scripting that can be integrated with a ModelBuilder model?

Marco
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
Page Definition Queries are only available in the UI if you are using DDP but that is not to say you can't accomplish the same thing outside of the UI.

With arcpy.mapping you would simply set a .definitionQuery on each appropriate layer.

For example, let us say I add two State layers to a dataframe/map.  The bottom layer is the index layer and the top layer is a masking layer.  I'm using the masking layer to mask away anything outside the current index feature.  I set the top layer to be solid white.  In the UI, I would set the Page Definition Query to Not Match the index field state name.

In arcpy, I would use code similar to:

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") maskLyr = arcpy.mapping.ListLayers(mxd, "Masking Layer")[0]  #Layer to apply def query. for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):     mxd.dataDrivenPages.currentPageID = pageNum     stateName = mxd.dataDrivenPages.pageRow.STATE_NAME  #Get field used in dynamic query     maskLyr.definitionQuery = "\"STATE_NAME\" <> '" + stateName + "'"  #apply dynamic query     arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\\" + stateName + ".pdf") del mxd


Hope this helps,
Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
Page Definition Queries are only available in the UI if you are using DDP but that is not to say you can't accomplish the same thing outside of the UI.

With arcpy.mapping you would simply set a .definitionQuery on each appropriate layer.

For example, let us say I add two State layers to a dataframe/map.  The bottom layer is the index layer and the top layer is a masking layer.  I'm using the masking layer to mask away anything outside the current index feature.  I set the top layer to be solid white.  In the UI, I would set the Page Definition Query to Not Match the index field state name.

In arcpy, I would use code similar to:

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") maskLyr = arcpy.mapping.ListLayers(mxd, "Masking Layer")[0]  #Layer to apply def query. for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):     mxd.dataDrivenPages.currentPageID = pageNum     stateName = mxd.dataDrivenPages.pageRow.STATE_NAME  #Get field used in dynamic query     maskLyr.definitionQuery = "\"STATE_NAME\" <> '" + stateName + "'"  #apply dynamic query     arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\\" + stateName + ".pdf") del mxd


Hope this helps,
Jeff
0 Kudos
MarcoBoeringa
MVP Regular Contributor
Thanks Jeff,

In my case, I finally decided to set the visibility of the layers on/off depending on data driven page index, as this did the job I wanted it to do for this particular case. It took some time to figure out how to implement this based on Python example scripts I found in the Help.

I still think it would be nice if more of the DDP stuff was exposed through the ArcToolbox and thus directly usuable in ModelBuilder. Quite a lot of people are still unfamiliar with Python, and learning a new programming language can be a long story, especially for those without any previous programming experience or simply difficulties to adjust to programming type tasks. I am not one of them, but I have seen other people struggling with any kind of programming style "logic". Connecting things up in a graphic style designer like ModelBuilder, is just more accessible for some.

Marco
0 Kudos