Select to view content in your preferred language

Is there a way to create a polygon from a map frame extent in ArcGIS Pro?

5839
7
Jump to solution
01-07-2021 04:11 PM
SterlingL
New Contributor II
 
Tags (3)
2 Solutions

Accepted Solutions
MarkBryant
New Contributor III

You can do it via code.

 

import arcpy
#Reference the Project document from within PRO
aprx = arcpy.mp.ArcGISProject("CURRENT")
#Reference the layout
lyt = aprx.listLayouts("your_layout_name_here")[0]
#Get the Map Frame
mf = lyt.listElements("mapframe_element", "your_mapframe_name")[0]
extent = mf.camera.getExtent()
polygon = extent.polygon

 

You then have a polygon object that you can work with.

Note that this code just grabs the extent, if the map frame is rotated, the extent doesn't match what you see on screen.

Mark.

View solution in original post

Tags (1)
DanPatterson
MVP Esteemed Contributor
7 Replies
MarkBryant
New Contributor III

You can do it via code.

 

import arcpy
#Reference the Project document from within PRO
aprx = arcpy.mp.ArcGISProject("CURRENT")
#Reference the layout
lyt = aprx.listLayouts("your_layout_name_here")[0]
#Get the Map Frame
mf = lyt.listElements("mapframe_element", "your_mapframe_name")[0]
extent = mf.camera.getExtent()
polygon = extent.polygon

 

You then have a polygon object that you can work with.

Note that this code just grabs the extent, if the map frame is rotated, the extent doesn't match what you see on screen.

Mark.
Tags (1)
SterlingL
New Contributor II

Thank you sir! This worked marvelously.

Katerina1
New Contributor

Hi Mark,

Can you please indicate how to save the extent as a feature class? I tried InsertCursor but it didn't accept the polygon object, it requires list of floats instead.
Many thanks,

Katerina.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Using tools in ArcToolbox

Create Fishnet (Data Management)—ArcGIS Pro | Documentation

aoi_create_fishnet.png

 

aoi_create_fishnet1.png

 


... sort of retired...
SterlingL
New Contributor II

Dan this works great as well! Thank you.

VeraNeroni1
New Contributor II

This worked great! Thank you!

0 Kudos
StuartGrey1
New Contributor III

I realise there is a solution here but wanted to add a script for use in an ArcGIS Pro which creates a new polygon row in a feature class based on the current extent. The script also adds a scale from current extent to a field called SCALE (it will need to exist in feature class for it to work).

This is particularly useful for users who are trying to build map books using data driven pages.

Users will just need to create a script within a toolbox and add a parameter that is a feature layer type so that users can input a feature class for it to work. Here is script:

# Import system module
import arcpy

# Input parameter for feature class name
feature_class_name = arcpy.GetParameterAsText(0)

# Extracting the geodatabase path from the feature class name
geodatabase = "\\".join(feature_class_name.split("\\")[:-1])

# Ensure overwriting of output data
arcpy.env.overwriteOutput = True

# Select the current project map
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Select the current map layout
map_layout = aprx.listLayouts()[0]

# Extract the extent of the selected data frame
map_frame = map_layout.listElements("MAPFRAME_ELEMENT")[0]
extent = map_frame.camera.getExtent()
polygon = extent.polygon

# Append a new row to the existing feature class
arcpy.management.Append([polygon], feature_class_name, "NO_TEST")
arcpy.AddMessage("Polygon appended to the existing feature class.")

# Get the current layout scale
current_scale = map_frame.camera.scale

# Check if the SCALE field exists
field_exists = False
for field in arcpy.ListFields(feature_class_name):
if field.name == "SCALE":
field_exists = True
break

if not field_exists:
arcpy.AddError("SCALE field does not exist in the feature class.")
else:
# Update SCALE field for the newly added row
with arcpy.da.UpdateCursor(feature_class_name, "SCALE") as cursor:
for row in cursor:
row[0] = current_scale
cursor.updateRow(row)

arcpy.AddMessage("SCALE field updated for the newly added record.")

Hopefully this saves someone some time 🙂

0 Kudos