Zoom To Selection

04-17-2024 12:10 AM
New Contributor

Can someone please help in figuring out 'Zoom to selection' based on Map, Using Layout Im able to achieve it through camera, but in map there are no methods for it, i tried this, 

lyr = aprxmap.listLayers()[0]
lyt = aprx.listLayouts("Layout")[0]
mf = lyt.listElements("mapframe_element")[0]

ext = mf.getLayerExtent(lyr,True,True)

0 Kudos
3 Replies
Esri Contributor

All you have to do is (assuming you are running your code in an ArcGIS NoteBook or Python Window in ArcGIS Pro):

1: Make sure you have a map open.

2: Make sure you have some selected features in the map:

3: Write the following code

proj = arcpy.mp.ArcGISProject("CURRENT")

mv = proj.activeView


You are working with a  MapView object which has a zoomToAllLayers() method. The boolean True argument says we want to use the selection of our layers.

I hope this helps

Many thanks ed

0 Kudos
New Contributor

Much appreciated @EdMorris  but zoomToAllLayers() refers to mapFrame object in documentation, when i use in mapView it gives no reference error,

AttributeError: 'NoneType' object has no attribute 'zoomToAllLayers'

0 Kudos
Esri Contributor

Hmmm interesting

So I have a map (it is a map view as it is open) called Validation and it has some selected point features in it:


I also have a layout called Crimes_Layout. This layout has a single Map Frame. That map frame is looking at the Validation map (notice the same selected features in the layout...):


When I click on my layout and I then run the code in my notebook, which I supplied previously, the map inside of the map frame (which is inside of my layout) zooms to the extent of the selected features in my map:


I think the order in which you click on the layout tab and then the notebook is important.

Also to get this work on your machine just try to keep it simple to begin with. Understand the workflow which is required to get the map in the layout to zoom to the extent of the selected features...

I hope this works for you.

Many thanks ed

0 Kudos