1

I am working on a Python script that I am running through a toolbox and ArcGIS Pro interface and am running into a problem with getting the camera extent correct. From what I can tell the setExtent function should be looking at the selected record and take the extent and apply it to the camera. This works when I manually select the record from the attribute table. But when I leave the attribute table unselected and let the code run as it should I get the feature selected, but the setExtent function considers the whole feature class. Where am I going wrong? I assume its an easy fix, but after scouring countless stack exchanges and ESRI help forums I cannot find a solution.

mapFrame=Layout.listElements("MAPFRAME_ELEMENT","MapFrame")[0]
Parcel=dataFrame.listLayers()[0]
Parcel.definitionQuery = "LandParcelArea.landParcelAreaIDPK = '" + LandParcelAreaIDPK + "'"
arcpy.SelectLayerByAttribute_management(Parcel,"NEW_SELECTION","LandParcelArea.landParcelAreaIDPK = '" + LandParcelAreaIDPK + "'")
new_extent = mapFrame.camera.setExtent(mapFrame.getLayerExtent(Parcel, True, True))
mapFrame.camera.scale *= 1.5

https://i.stack.imgur.com/hi9wQ.jpg

1
  • I'd recommend first confirming that your extent object is honoring the layer definition query. Curious if you were able to resolve this? I'm a having a similar issues after upgrading from 2.8 to 2.9. I'm able to to get extent fine, but it doesn't seem to be honoring the definition query on the layer. It's returning the extent of the entire dataset.
    – Aloha Ray
    Sep 7, 2022 at 23:14

2 Answers 2

0

A potential failure concern is the layout not being refreshed. In my code I am saving the aprx to new project, and opening it, ensuring that I am getting the latest state of the project. I am unsure if your definition query with the "." in the field will work. Since it is returning the extent of the layer, I am suspecting that the code is not getting the extent of the feature you are interested in. In my code I am setting the definition query on the map layer, then calling the layer with the definition query to set the camera extent. I have not used the camera.scale. Hope my example can help you get the extent.

Here is some code that I have ran to set the layout out to a county:

# Get the aprx project
# Get your map from the .aprx
# Get the layer you want to apply definition query to
# sql can fail in the enterprise geodatabase I work from, so I add field delimiters.
# Set the query on the layer.
# Get the layout
# Get the map frame element in the layout
# set the camera extent of the map frame element to the layer with the def. query
# save as on the .aprx

import arcpy
countyName = 'Douglas'
aprx = arcpy.mp.ArcGISProject(r"{path to aprx}\ZoomToLayer.aprx")
map = aprx.listMaps()[0]
lyr = map.listLayers('Counties')[0]
whereClause = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(lyr, 'Cnty_Name'), countyName)
lyr.definitionQuery = whereClause
lyt = aprx.listLayouts('Layout')[0]
mf = lyt.listElements('MAPFRAME_ELEMENT', 'Map Frame*')[0]
mf.camera.setExtent(mf.getLayerExtent(lyr))
aprx.saveACopy(r"{path to aprx}\ZoomToCounty_" + countyName + ".aprx")
del aprx
0

I use a workaround, which after upgrade to 3.1 gets even more complicated. I create a dummy shapefile and used to use arcpy.analysis.select to create the new file. arcpy.analysis.select seems not to work correctly any more. You can use arcpy.SelectLayerByAttribute_management and save the selection to a new file and then zoom to the extent. I turn the new layer invisible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.