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