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.
Solved! Go to Solution.
This bit of code works like a charm, thanks for posting Mark Hammond.
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.
I've created a ArcGIS Idea for this function to be considered by ESRI. Please vote it up.
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
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.
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
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.
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.
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!
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.
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!