Zoom map to the extent of a layer in Arcpy.mp (ArcGIS Pro Project)

24070
23
Jump to solution
05-02-2017 01:08 PM
JamesTaylor3
Occasional Contributor

I'm writing code to create new ArcGIS Pro Projects using the Pro arcpy  (package arcpy.mp)

Doing something like this:

import arcpy.mp
aprx = arcpy.mp.ArcGISProject(r"C:\temp\Empty.aprx")
map = aprx.listMaps()[0]
map.map.addDataFromPath(r"C:\Users\xxxxxx\Documents\Test1\Orthomosaic.tif")
aprx.saveACopy(r"C:\temp\jNew.aprx")

But I can't figure out to zoom the map to the extent of Orthomosaic.tif (or any other extent for that matter).

From the docs at   Alphabetical list of arcpy.mp classes—ArcPy | ArcGIS Desktop 

I can see that a MapFrame has a zoomToAllLayers method, but a MapFrame is only available on a Layout element.

Anyone know how to do this?

Thanks.

0 Kudos
23 Replies
ChrisHolmes
Occasional Contributor III

As Joshua mentioned and as far as I can see you can only zoom on the active map view. Otherwise it must be done on a layout/map frame. 

0 Kudos
RogerDunnGIS
Occasional Contributor II

Even as of version 3.0, the documentation for MapView says, "The MapView returned from the activeView property is the only way to change the extent of the Camera associated with a map view."

My problem with this functionality is that I'm trying to zoom to selected features from a Notebook.  But when the user is IN the Notebook, the Notebook itself is the active view, so activeView returns None.  Even if I get a map object from listMaps, I cannot use defaultView or defaultCamera to change the extent.  As the documentation says, this only works if grabbing Camera from the object returned by the activeView property.

One way that does work is if I right-click Catalog > Toolboxes and select New Python Toolbox.  Then I create a new tool class and within its execute method call:

arcpy.mp.ArcGISProject("CURRENT").activeView.camera.setExtent()

I don't like this as much because the tools I'm writing for my end users work better as interactive scripts (using Python's input function) rather than as tools.

0 Kudos
Nicholas_ISeigal
New Contributor III

I haven't tried this but the Esri help on Map.openView() may suggest the route forward for you...

openView ()
 

This is useful if the map view is not already open or another view is active in the application. The method creates a map view zoomed to its default extent and activates it. To close other, existing views before opening a new view, use the ArcGISProject closeViews method.

There are two techniques for controlling the desired extent of your map view. First, prior to opening the view, you can set the defaultCamera for your map. Second, you can change the MapView camera extent after it is opened.

So, you can presumably do the following:

  1. Get a map object from listMaps.
  2. Set the defaultCamera for your map to the extent of the selected features
  3. Open the map with ovenView, which activates it and applies the extent of the defaultCamera.

Good luck!

0 Kudos
RogerDunnGIS
Occasional Contributor II

The trouble seems to be the fact that you can open the same map multiple times in ArcGIS Pro.  Although each new opening of the map contains the same selection, they don't all share the same extent.  I'm not sure why Pro was programmed like this; I would think each Map and Layout item were singleton objects, but not so.

0 Kudos