arcGIS Pro Extents in the Viewing Pane

1429
5
01-12-2017 08:50 AM
JamesGooding1
New Contributor II

Hi, 

How do I get the extents that are visible in the viewing pane. In old arcpy it would've been:

  1. import arcpy  
  2. import arcpy.mapping  
  3.   
  4.   
  5. ## Sets the MXD file  
  6. IMXD = arcpy.mapping.MapDocument("CURRENT")  
  7. ## Sets the Dataframe  
  8. DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]  
  9.   
  10. print DF.extent  

Any help greatly appreciated.

James

#data frame extent#graphics extentknowni_coop

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
JamesGooding1
New Contributor II

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

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
MatthewDriscoll
MVP Regular Contributor

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.

Joshua Bixby

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
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
KimOllivier
Regular Contributor II

 

# 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.

 
0 Kudos