Select to view content in your preferred language

Zoom Out Slightly When Using getLayerExtent() To Set Extent of Map Frame

619
4
08-23-2023 12:09 PM
BrettLessner
Occasional Contributor

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

4 Replies
Shauna-RaeBrown
Occasional Contributor II

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.

0 Kudos
BrettLessner
Occasional Contributor

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

0 Kudos
TylerT
by
Occasional Contributor III

I have to perform this operation also.  Here's what I do that seems to work fine.

After you camera.setExtent like you did:

TylerT_0-1695049096331.png

Notice you can both get and set the camera scale with mf.camera.scale. 

HTH.

Tyler



edward33
New Contributor III

This worked so well, thanks for the tip!

0 Kudos