HOW TO

Set a custom map extent using ArcPy

Last Published: December 31, 2024

Summary

Map extents are set to "Use extent of data in all layers" by default, however a custom extent is favorable to enhance the visibility of the map or to accentuate certain features. The extent can be changed manually through Map Properties. The map extent can also be set to use the extent of an existing layer in the map and this can be accomplished with a Python script. Setting the extent within the script allows better usage of the tool when using the map as an input. For example, the Create Vector Tile Package tool uses the map’s extent to determine the processed area. This article describes how to set the map’s extent to copy the layer’s extent using ArcPy. The Map Properties window below indicates a custom extent is used.

Custom extent map properties.jpg

Procedure

Set a new custom extent

  1. Import the necessary module and set the ArcGIS Pro project.
import arcpy

aprx = arcpy.mp.ArcGISProject(r"<project file location>\<project name>.aprx") slash is the wrong direction

m = aprx.listMaps()[0]
  1. Get the definition of the project.
m_cim = m.getDefinition('V3')
  1. Set the new custom extent using the Extent() function.
m_cim.customFullExtent = arcpy.Extent(<MinX>, <MinY>, <MaxX>, <MaxY>)
  1. Set the definition and save the project.
m.setDefinition(m_cim)

aprx.save()

The following is the full working script:

import arcpy

aprx = arcpy.mp.ArcGISProject(r"C:\Users\user\Documents\ArcGIS\Projects\MyProject3\MyProject3.aprx")

m = aprx.listMaps()[0]

m_cim = m.getDefinition('V3')

m_cim.customFullExtent = arcpy.Extent(11212821.822, 540265.3103, 11212953.0831, 540367.172200001) 

m.setDefinition(m_cim)

aprx.save()

Set the custom extent to a layer’s extent

  1. Import the necessary module and set the ArcGIS Pro project.
import arcpy

aprx = arcpy.mp.ArcGISProject(r"<project file location>\<project name>.aprx")

m = aprx.listMaps()[0]
  1. Set the reference layer and specify the layer’s extent as the new extent.
refLyr = m.listLayers('<Feature Name>')[0]

newExtent = arcpy.Describe(refLyr).extent
  1. Get the definition of the project.
m_cim = m.getDefinition('V3')
  1. Set the layer’s extent as the new map extent.
m_cim.customFullExtent = newExtent
  1. Set the definition and save the project.
m.setDefinition(m_cim)

aprx.save()

The following is the full working script:

import arcpy

aprx = arcpy.mp.ArcGISProject(r"C:\Users\user\Documents\ArcGIS\Projects\MyProject3\MyProject3.aprx")

m = aprx.listMaps()[0]

refLyr = m.listLayers('TestPoly')[0]

newExtent = arcpy.Describe(refLyr).extent

m_cim = m.getDefinition('V3')

m_cim.customFullExtent = newExtent

m.setDefinition(m_cim)

aprx.save()

Article ID: 000032946

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 3
  • ArcGIS Pro 3 2
  • ArcGIS Pro 3 4

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options