The ArcPy Camera object, utilized by both MapView and MapFrame objects, is currently broken for usage within a MapView. This flaw is acknowledged in the documentation for Camera.setExtent(): "Sets the Extent for a map frame in a layout." A camera object obtained from MapView.defaultCamera can have setExtent() called on it, but the Extent of the MapView will not change.
My suggestion is that Camera.setExtent() gets implemented for all instances of Camera, including Camera objects used by MapView objects. This would enable functionality for the Extent of an Active Map to be modified by ArcPy. An active map in a project should have this functionality just like a MapFrame in a Layout does.
I tried to design a script that pans the active map to specific features, but this is currently not possible with the broken implementation of the Camera object. Fixing this implementation will help my workflow in many ways because I will be able to easily pan the map to my selected features with a single function. Currently I have to Select By Attributes and then Zoom To Selection which is tedious when reviewing large numbers of features.
If implemented, a user may be able to do something like:
arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", f"OBJECTID = {oid}")
aprx = arcpy.mp.ArcGISProject("CURRENT")
map_view = aprx.activeMap
map_view.defaultCamera.setExtent(getLayerExtent(...))
@wtfineberg I believe there is a misunderstanding between changing a Map's default Extent property vs changing the extent of an active MapView.
Map.defaultCamera - Modifying the defaultCamera will not affect an existing view. This property is only applied when a new MapView is opened or a new MapFrame is inserted into a layout.
You can change the extent of an active MapView using the Camera object, not the defaultCamera property. I've modified the code above slightly to change the active MapView's extent.
aprx = arcpy.mp.ArcGISProject('current')
map_view = aprx.activeView
layer = map_view.map.listLayers('GreatLakes')[0]
arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", f"NAME = 'Lake Superior'")
map_view.camera.setExtent(map_view.getLayerExtent(layer, True))
Notice, I'm using activeView, NOT activeMap. ActiveView returns a MapView object whereas activeMap returns a Map object. It is from the active view that I get the associated map and then reference a layer.
If I were to then type:
m = aprx.listMaps('Map')[0]
m.defaultCamera = map_view.camera
The next time I go to open a new map view for the 'Map', it will be zoomed to Lake Superior.
Jeff
@JeffBarrette, thank you for the detailed explanation and this code example. This definitely helps me grasp the difference between a Map and a MapView object. Obtaining the MapView object using the activeView property of the ArcGISProject is exactly what I was looking for!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.