I've written an arcpy script that pulls coordinate data from a saved email/text file. I've also added the polygon created into a feature class, and used a definition query on the layer so that the new feature is the only one visible in the map.
What I'm trying to do is pan the layout's map frame to the new extent of the layer. The following script works (i.e. retains the current scale of the map frame, and pans to the layer), but it throws the included error. I'm running my script in ArcGIS Pro 2.9.
I've already read the MapFrame and Camera classes
https://pro.arcgis.com/en/pro-app/2.9/arcpy/mapping/mapframe-class.htm
https://pro.arcgis.com/en/pro-app/2.9/arcpy/mapping/camera-class.htm
I can't find ANY examples of how to use the MapFrame panToExtent method. Please help.
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.panToExtent(mf.getLayerExtent(lyr, False, True)))
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 389, in setExtent
return convertArcObjectToPythonObject(self._arc_object.setExtent(*gp_fixargs((extent,), True)))
TypeError: Camera.setExtent() missing required argument 'extent' (pos 1)
Solved! Go to Solution.
@Anonymous User Thanks for your suggestions. I FINALLY figured it out. I set the mf.camera.scale.
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
mf.camera.scale = 25200.0
I don't think the
mf.panToExtent(lyrExt)
returns anything so nesting it into the setExtent method is passing it None.
try separating them and passing in the extent.
lyrExt = mf.getLayerExtent(lyr, False, True)
mf.panToExtent(lyrExt)
mf.camera.setExtent(lyrExt)
Thanks for your help @Anonymous User . Unfortunately, when I tried your suggestion...
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
lyrExt = mf.getLayerExtent(lyr, False, True)
mf.panToExtent(lyrExt)
mf.camera.setExtent(lyrExt)
I found that it works the same as...
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
I'm trying to stay at a scale of 1:25,200, but the both of these scripts zoom to the feature's/layer's extent.
If I figure it out, I'll post my corrected script.
Try setting the symbolized_extent to False so it works off the geometry?
mf.getLayerExtent(lyr, False, False)
@Anonymous User Thanks for your suggestions. I FINALLY figured it out. I set the mf.camera.scale.
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("811Tickets")[0]
lyr = m.listLayers("AZ811 BlueStake")[0]
lyt = aprx.listLayouts()[0]
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
mf.camera.scale = 25200.0
Hello,
I tried following this example (from the solution) and I am having trouble. The script is finding the correct map, layer, layout, and mapframe but nothing seems to happen when I run the script. How can I debut this?
aprx = arcpy.mp.ArcGISProject("CURRENT")
#find the Layers Map
m = aprx.listMaps("Layers")[0]
#Find the AOI layer
lyr = m.listLayers("AOI")[0]
#Get the first (0) layout
lyt = aprx.listLayouts()[1]
#Get the mapframe element called MainMapFrame
mf = lyt.listElements("mapframe_element", "MainMapFrame")[0]
#Pan to the extent of the AOI layer in my Layers Map
mf.camera.setExtent(mf.getLayerExtent(lyr, False, True))
#Dont worry about setting an extent
#mf.camera.scale = 25200.0