Hi,
How do I get the extents that are visible in the viewing pane. In old arcpy it would've been:
Any help greatly appreciated.
James
Something along the lines of:
map_name = # name of map to get extent object
aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.listMaps(map_name)[0]
ext = mp.defaultCamera.getExtent()
If you are interested in layouts instead of maps, there is a listLayouts() method to call.
Cheers Joshua,
I've found that saving the .aprx before the rest of the above code is essential as it seems to define where the "camera's" location.
James
I will have to take a closer look at that behavior. There either might be different code that doesn't require the APRX to be saved first, or possibly it is a bug.
Joshua,
This issue of having to save the project in order to get the current map view extents is almost three years old. Are there any plans to fix this?
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx.save
This does not work for this either, you have to physically save the map first.
Also if I move around in the Map View and then switch to the Layout View and I physically click to save the project the script to get the Map View extents does not work. If I physically click save in the Map View the script will work.
Here is the entire script.
arcpy.env.overwriteOutput = True
# Use the current arcgis pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# List the map
m = aprx.listMaps("Layers")[0]
aprx.save() # this is not saving the project????
layout = aprx.listLayouts("Template_8_5x11_Layout")[0]
frame = layout.listElements("MAPFRAME_ELEMENT")[0]
ext = m.defaultCamera.getExtent()
frame.camera.setExtent(ext)
increment = 300
currenscale = int(frame.camera.scale) + 300
#arcpy.AddMessage("current " + str(currenscale))
newscale = currenscale - (currenscale % increment)
#arcpy.AddMessage("NEW " + str(newscale))
frame.camera.scale = newscale
# zoom to selected features
import arcpy
p = arcpy.mp.ArcGISProject('CURRENT')
m = p.listMaps()[0]
mv = p.activeView
# part of trail selected
lay = m.listLayers('trail')[0]
extent = mv.getLayerExtent(lay, True, True)
mv.zoomToAllLayers(True,True)
Maybe this function was not available in 2017, but it is now in 2021 in version 2.8 or earlier.
The trick is to realise that the extents are available in the MapView of the active Map. While looking for this I tried out using Bookmarks which are also available, maybe a nice way to store the extent of selections.