Zoom To Selection

199
3
2 weeks ago
umar_iasf
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)
mf.camera.setExtent(ext)
mapView.panToExtent(ext)

0 Kudos
3 Replies
EdMorris
Esri Contributor

Hi
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
mv.zoomToAllLayers(True)

 

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
umar_iasf
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
EdMorris
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:

EdMorris_0-1713353380422.png

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...):

EdMorris_1-1713353555680.png

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:

EdMorris_2-1713353648007.png

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