I have a set of layouts where their extents change based on the county being published. Basically, the script sets the definition query of the County layer and then uses
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
to move the layout extent. However, this zooms too far in on the county boundaries. The goal is to zoom to the extent of the county layer - n%.
I've attached current results vs goal
I've had the same problem. I am forced to set the map's layout scale after my feature is drawn from the x/y coordinates. Here's a snippet of what I do after running a script that collects data from a "utility marking request" that draws the feature from Lat/Long coordinates.
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
mf.camera.scale = 24000.0
After the script runs, I have to zoom to the feature using Pro (version 2.9).
I think Esri needs to make the getLayerExtent() to allow for a percentage, like when you "zoom to selected feature" or for a map book layout where you can set a percentage for the feature extent in Pro. Maybe you could submit it as a Python Idea. Just a thought.
Thanks for sharing your experience. I'll wait to see if I'm missing an option or setting somewhere, but I think we'll have to.
One alternative we're looking into is finding the extent of the layer, do math on the x/y max/min, and use the updated values to set our extent.
-Brett
I have to perform this operation also. Here's what I do that seems to work fine.
After you camera.setExtent like you did:
Notice you can both get and set the camera scale with mf.camera.scale.
HTH.
Tyler
This worked so well, thanks for the tip!
Hello to All,
I was looking for a solution to the same issue & found this thread. We can do this in an ArcMap project with way different syntax, but I'm in the process of migrating that project to Pro. So, I got a little stuck.
Agreed - Tyler's solution worked for me as well. Interesting that when typing mf.camera into Pro's built-in Python window, scale isn't exposed when typing a period after camera. It would be nice if Esri added that to the menu. At first, I didn't think it would work, but what-the-heck, why not give it a try. Voila! It worked.
Let's take this a little further. I like to round up the layout scale once I've zoomed out using the 1.1 factor. Adapting from my old ArcMap script, try adding this:
mf.camera.scale -= mf.camera.scale % -100.
I'm not much of a Python programmer, so maybe someone who's more knowledgeable can combine that line of code with Tyler's code to make it cleaner & more concise.
-Chris
Hi @CMorneau,
You can add this line after factoring to round up to nearest 100.
mf.camera.scale = math.ceil(mf.camera.scale / 100) * 100
Note: Add `import math` to your imports.
HTH,
Tyler