Zoom to layer on the map view arcpy.mp

10605
14
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. 
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

14 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

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. 

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
AndyAnderson
Occasional Contributor II

What do you mean by "scripts run inside the application"? Doesn't this mean a script in a Python window or in a Python notebook opened within an ArcGIS Pro project? aprx.activeView always returns None.

Or maybe the question should be, how to make a map active? When I open a new map project,  aprx.activeView always returns None even in this case with only one possible View.

Put more explicitly, the first code sample at the end of https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/mapview-class.htm always fails.

-- Andy

0 Kudos
JeffBarrette
Esri Regular Contributor

First, we fixed an issue in 3.0 concerning Notebooks.  Notebooks are a View so if you tried getting the activeView, None would be returned because a Notebook is NOT a mapview or layout view.  At 3.0 Notebooks can appropriately reference the (previously) ActiveView.

Second, also at 3.0 we introduced new functions to OpenViews (e.g., Map/Layout.OpenView()) and Project.CloseViews().  This allows you to active a view for a specific Map or Layout and modify the extent the way you want it.  Also, we have the ability to closeviews to remove any unwanted clutter. 

 

NOTE - again, any API functions concerning Views in Pro MUST be executed within the application (PyWIn, NoteBooks, ScriptTools).  These functions are ignored outside the application using stand alone scripts.

 

Jeff - arcpy.mp and Layout teams.

AndyAnderson
Occasional Contributor II

That's great to hear that this will be fixed in 3. Do you know when that will be released?

The new functions look useful. However, I'd like to recommend that you also implement a new method arcpy.listViews() that would return all of the view types you mention, which would allow me to search different views for their names and types. This would be more generally useful than knowing just the active view.

Thanks,

-- Andy

0 Kudos
JeffBarrette
Esri Regular Contributor

3.0 is planned to be available by the User Conference in July.  

Concerning ListViews() this really isn't possible because views are NOT uniquely named.  You can open a map view for the same map many times and every view can have a different extent.  How would you know which one you wanted.  I personally would like to see view with unique names so we could manage views  better.

Jeff - arcpy.mp and Layout teams.

0 Kudos