HOW TO
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.
import arcpy aprx = arcpy.mp.ArcGISProject(r"<project file location>\<project name>.aprx") slash is the wrong direction m = aprx.listMaps()[0]
m_cim = m.getDefinition('V3')
m_cim.customFullExtent = arcpy.Extent(<MinX>, <MinY>, <MaxX>, <MaxY>)
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()
import arcpy aprx = arcpy.mp.ArcGISProject(r"<project file location>\<project name>.aprx") m = aprx.listMaps()[0]
refLyr = m.listLayers('<Feature Name>')[0] newExtent = arcpy.Describe(refLyr).extent
m_cim = m.getDefinition('V3')
m_cim.customFullExtent = newExtent
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()
Get help from ArcGIS experts
Download the Esri Support App