Even if it's the same text, it's a good practice to put your question in both the title and body of your posts here.
What you're looking for is Camera.setExtent(), which you can read up on here: https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/camera-class.htm
Hi Josh
Thanks for pointer around etiquette.
I have an empty map frame in a layout that I want to set the map view in and then zoom to the extent of one of the layers. I can't seem to get off the ground with this one or find much help on the web. Any assistance would be appreciated. Seems like it a fairly simple standard thing to want to do?
Matt
I'd start with the Introduction to arcpy.mp article. There are some linked tutorials that can walk you through some of the simpler procedures.
Basically, you'd use something like this:
import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')
# get your layout
lyt = aprx.listLayouts('Your Layout Name')[0]
# get the map frame
mf = lyt.listElements('MAPFRAME_ELEMENT', 'your map frame name')[0] # note that the name of the frame is NOT the name of the map
# get specific layer you want
lyr = mf.map.listLayers('Your Layer Name')[0]
# set frame extent to match layer
mf.camera.setExtent(mf.getLayerExtent(lyr))
Hi Josh
That is what I ran and I receive the following error:
RuntimeError Traceback (most recent call last)
In [74]:
Line 7: mf.camera.setExtent(mf.getLayerExtent(lyr))
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))
RuntimeError:
Try running mf.getLayerExtent(lyr) on its own line? Maybe explicitly state the optional parameters, too.
mf.getLayerExtent(lyr, False, False)
RuntimeError Traceback (most recent call last)
In [88]:
Line 7: mf.getLayerExtent(lyr, False, False)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))
RuntimeError:
Double check the value of the variables, make sure the lyr, lyt, and mf are the objects they're supposed to be.
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
lyt = aprx.listLayouts("Layout1")[0]
mf = lyt.listElements("MAPFRAME_ELEMENT", "Map Frame 1")[0]
lyr = m.listLayers("Osterley")[0]
mf.getLayerExtent(lyr,False, False)
RuntimeError Traceback (most recent call last)
In [105]:
Line 7: mf.getLayerExtent(lyr,False, False)
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in getLayerExtent:
Line 2150: return convertArcObjectToPythonObject(self._arc_object.getLayerExtent(*gp_fixargs((layer, selection_only, symbolized_extent), True)))
RuntimeError:
---------------
I mean try running a cell that's just "m" and see what is returned, make sure they're actually layouts, layers, etc.