Hello,
I have look online for two days and cannot find the solution.
What I try to achieve is to get my mapframe extend in the layout and not the current map extend in the mapview.
The Dynamic text offer to have it but I do not know how to acces sit in arpcy
In the mapframe "extend" > "use a custome extend" > "current visible extend" it is displayed. Same here, I do not know how to access it.
The code below displays the map extend from the map view, not the actual extend in the layout.
import arcpy
def get_layout_bbox(layout_name):
aprx = arcpy.mp.ArcGISProject("CURRENT")
layouts = aprx.listLayouts(layout_name)if not layouts:
raise ValueError(f"No layout found with the name {layout_name}")layout = layouts[0]
layout_metadata = layout.metadata
# Get the layout's name or any other property as a title
layout_title = layout_metadata.title
# Reference to the last map frame in the layout
last_map_frame = layout.listElements("MAPFRAME_ELEMENT")[-1]# Get the extent of the current map frame
extent = last_map_frame.camera.getExtent()# Get the spatial reference of WGS 84
wgs84_sr = arcpy.SpatialReference(4326)# Project the extent to WGS 84
projected_extent = extent.projectAs(wgs84_sr)# Format the bounding box
bbox = (
f"{round(projected_extent.XMin, 6)}, "
f"{round(projected_extent.YMin, 6)}, "
f"{round(projected_extent.XMax, 6)}, "
f"{round(projected_extent.YMax, 6)}"
)return layout_title, bbox
if __name__ == "__main__":
# Get the layout name from the tool's parameters
layout_name = arcpy.GetParameterAsText(0)# Get the layout title and bounding box
arcpy.AddMessage(f"_"*25)
layout_title, bbox = get_layout_bbox(layout_name)
arcpy.AddMessage(f"Layout Title: {layout_title}")
arcpy.AddMessage(f"Bounding Box: {bbox}")
arcpy.AddMessage(f"_"*25)
Do you know how can I access the map extend in the layout ?
Thanks,
Alex
Code formatting ... the Community Version - Esri Community
to format your code to make it easier to comment on
Here is the snipped code:
import arcpy
def get_layout_bbox(layout_name):
aprx = arcpy.mp.ArcGISProject("CURRENT")
layouts = aprx.listLayouts(layout_name)
if not layouts:
raise ValueError(f"No layout found with the name {layout_name}")
# Select the layout
layout = layouts[0]
# Access layout metadata
layout_metadata = layout.metadata
# Get the layout's name or any other property as a title
layout_title = layout_metadata.title
# Reference to the last map frame in the layout
last_map_frame = layout.listElements("MAPFRAME_ELEMENT")[-1]
# Get the extent of the current map frame
extent = last_map_frame.camera.getExtent()
# Get the spatial reference of WGS 84
wgs84_sr = arcpy.SpatialReference(4326)
# Project the extent to WGS 84
projected_extent = extent.projectAs(wgs84_sr)
# Format the bounding box
bbox = (
f"{round(projected_extent.XMin, 6)}, "
f"{round(projected_extent.YMin, 6)}, "
f"{round(projected_extent.XMax, 6)}, "
f"{round(projected_extent.YMax, 6)}"
)
return layout_title, bbox
if __name__ == "__main__":
# Get the layout name from the tool's parameters
layout_name = arcpy.GetParameterAsText(0)
# Get the layout title and bounding box
arcpy.AddMessage(f"_"*25)
layout_title, bbox = get_layout_bbox(layout_name)
arcpy.AddMessage(f"Layout Title: {layout_title}")
arcpy.AddMessage(f"Bounding Box: {bbox}")
arcpy.AddMessage(f"_"*25)
Are you sure that the extent is wrong? I think it should be the correct extent? Maybe you could test it if you use mapFrame.camera.setExtent(arcpy.Describe(lyr).extent) to zoom in the map Frame to another extent, the one of a layer file in your map Frame?
There's also a solution here:
I don't know how the extent could be accessed other than through the camera, however, there is more details information on the cim at....