[GIS] Zooming to layer with page definition query set in ArcPy

arcgis-10.2arcgis-desktoparcpydata-driven-pages

I have data driven pages enabled on a mxd, with a page definition query set on one of the layers to only show features that match the current index feature. This layer looks like this:

Full layer

After enabling the page definition query, right clicking on the layer and clicking Zoom to Layer yields this:

Using the UI to zoom to layer

I would like to perform this function in ArcPy. Setting the data frame's extent to the extent of the layer using the getExtent() method zooms to the extent of the entire layer:

Arcpy method

This method does not honour the extent of the page definition query. I cannot use the zoomToSelectedFeatures() method of the data frame because nothing is actually selected; similarly I cannot use the layer's getSelectedExtent() method. The only workaround I see is updating a normal definition query on the layer from ArcPy and then getting the extent.

Is there a way to use the UI's Zoom to Layer method from ArcPy?

Best Answer

As suggested in your Question, the way that I do this is by "updating a normal definition query on the layer from arcpy" and then using getSelectedExtent().

Some code using a Queensland (Australia) Lot on Plan example:

import arcpy

inputMap = r"C:\temp\test.mxd"
inputLotNo = "1"
inputPlanNo = "RP1"

mxd = arcpy.mapping.MapDocument(inputMap)
df = arcpy.mapping.ListDataFrames(mxd,"Main Map")[0]
subjectLotLayer = arcpy.mapping.ListLayers(mxd,"Subject Lot",df)[0]

where_clause = "LOT = '" + inputLotNo + "' AND PLAN = '" + inputPlanNo + "'"
subjectLotLayer.definitionQuery = where_clause
df.extent = subjectLotLayer.getSelectedExtent(False)

If you must do this using page definition queries then I suspect that you will need to work through the support system and/or ArcGIS Ideas to try and achieve that.

Related Question