Select to view content in your preferred language

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

27111
23
Jump to solution
05-02-2017 01:08 PM
JamesTaylor3
Regular 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
HenryWhitenack
Emerging Contributor

This bit of code works like a charm, thanks for posting Mark Hammond.

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Thanks for sharing.

This works for the layout view, but it won't change the map extent in the mapview. 

I can't believe it that this function has not been implemented in ArcGIS Pro 2.2.0.

simoxu
by MVP Regular Contributor
MVP Regular Contributor

I've created a ArcGIS Idea for this function to be considered by ESRI. Please vote it up.

https://community.esri.com/ideas/15227 

ChrisHolmes
Frequent Contributor

Is it still the case that zoom to extent on the map view is not supported in arcpy? So far, my searching makes me think that it is not. thanks

@JoshuaBixby 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Since 2018 when this thread was opened, ArcPy has added functionality.

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
ext = # ArcPy Extent Object
aprx.activeView.camera.setExtent(ext)

 

The activeView property returns either the active map or active layout, and the camera can be accessed to set the extent.

ChrisHolmes
Frequent Contributor

Yes, agreed. I was able to zoom to extents on the active view. But there's no way to do this on a map view that is not active or when working with a project that is not currently open in ArcGIS Pro.

 

thanks

0 Kudos
ChrisHolmes
Frequent Contributor

Having spent some time with the layout and mapframe I am now using zoom to extent and set scale on the map frame which works well. 

Having said that the end use will still need to manually zoom to extent in the map view to do their work.

ChrisHolmes_0-1607963259856.png

 

Ideally I would have liked to loop through all map views using arcpy and complete the zoom to extent before the aprx file opens so that all map views would be focused on the appropriate parcel when it opens, but this will have to do.

0 Kudos
KathrynCliftonGIS
Occasional Contributor

Chris,

I'd appreciate your insight on how to set a scale from an existing map view and apply it to a map frame in an existing layout.

Thank you!

Kathryn Clifton, GISP, PLS
0 Kudos
AustinRobey1
Occasional Contributor

I posted and up-voted on the ArcGIS Idea above, but posting here too in case it helps anyone...

You can sort-of hack this workflow in ArcPro by running the script in a Task Step and ending with a selection rather than a zoom extent. Then, pass the selection to a second Task Step which runs the Zoom To Selection command. It is not great, slower and clunkier than simply running the script directly, but I have gotten it to work.

PattyLake1
New Contributor

Wondering if the zoom to selected feature in ArcPy on the Map View versus the Map Frame has been resolved?  I am trying to do a select feature by attribute in Model Builder (ARCPRO) and zoom to the selected features.   I attempted to locate your submitted Idea but the link is broken.  Thanks!

0 Kudos