Zoom to layer on the map view arcpy.mp

978
5
Jump to solution
04-12-2021 01:43 PM
MarvisKisakye1
Occasional Contributor

I am attempting to zoom to layer on the map view NOT the layout view. I am aware that I can zoom to a layer using the code below

cam = c_project.activeView.camera
ext = arcpy.Describe(layer).extent
cam.setExtent(ext)
c_project.save()
 
My issue is that c_project.activeView returns None because the project is not yet open. 
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor

Marvis, this is a known limitation with ActiveMap and ActiveView and is documented under ArcGISProject.

"The activeMap property will return the map associated with either an active map view or the active map frame on an active layout view. The activeView property will either return a MapView object if a map view is active or it will return a Layout object if a layout view is active. Both of these properties are intended for scripts to be executed in the application, such as scripts run in the Python pane or those associated with a script tool. If no views are active, both properties will return None. These properties will always return a None value when a script is run outside of the application, because views are only relevant when the application is open. The MapView returned from the activeView property is the only way to change the extent associated with a map view. There are multiple functions available on the MapView class that allow you to change the extent, such as, the camera property, panToExtent, ZoomToAllLayers, and ZoomToBookmarks."

Jeff - arcpy.mp and Layout teams

View solution in original post

5 Replies
JeffBarrette
Esri Regular Contributor

Marvis, this is a known limitation with ActiveMap and ActiveView and is documented under ArcGISProject.

"The activeMap property will return the map associated with either an active map view or the active map frame on an active layout view. The activeView property will either return a MapView object if a map view is active or it will return a Layout object if a layout view is active. Both of these properties are intended for scripts to be executed in the application, such as scripts run in the Python pane or those associated with a script tool. If no views are active, both properties will return None. These properties will always return a None value when a script is run outside of the application, because views are only relevant when the application is open. The MapView returned from the activeView property is the only way to change the extent associated with a map view. There are multiple functions available on the MapView class that allow you to change the extent, such as, the camera property, panToExtent, ZoomToAllLayers, and ZoomToBookmarks."

Jeff - arcpy.mp and Layout teams

View solution in original post

MarvisKisakye1
Occasional Contributor

Well that's a bummer. All my work is performed in an external script that on completion, opens up ArcGIS Pro. It would be nice to have the project open up zoomed to the extent of the data in the map view. 

0 Kudos
JeffBarrette
Esri Regular Contributor

It would definitely be helpful if MapViews could be managed outside of the application. But MapViews only exist in the application AND you can have many map views of the same map so without a management system or some unique name to find the specific map view of interest, its not possible. And if the app is not opened, there definitely isn't the notion of an active map view. 

One possible work around is to create a map frame that is the same size of a layout.  You can do everything you need to do against a map frame out of process (or outside the application).

Jeff - arcpy.mp and Layout teams

Kaz
by
New Contributor II

I have a similar scenario; I have a python script that automatically creates maps from a template in ArcGIS Pro. There are 3 maps and 2 layouts in the ArcGIS Pro project. I can easily get the layouts to zoom to the new extent of the layers, but (as stated above) there is no functionality to zoom to the layers on the Map View, only the Map Frame within the layout. 

Kaz_0-1622579396039.png

 

My output looks a bit strange because the 2 layouts are centered on the data but the 3 maps are set to the template's extent (which doesn't show any data). The user could always open the project, right click on a layer from the content pane, and choose "zoom to layer" but that feels a bit redundant. 

Could you explain the work around you mentioned above? Or maybe there is a better work around for my use case? 

0 Kudos
JeffBarrette
Esri Regular Contributor

You can use arcpy.mp to change the extent of an active map view BUT only using scripts run inside the application and only against the active view.  Check out the MapView class:

 

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/mapview-class.htm

We do hope in the near term that we will be able to set the active view (vs only working with the current active view).  This will have its limitations as well.  The idea is you would call something like OpenMapView and it would simply open a new view, with the default extent, and it would become active.  Again, the limitation exists because we can't distinguish multiple map views pointing to the same map.

0 Kudos