Select to view content in your preferred language

Zoom to Selected Layer with Python

1032
6
Jump to solution
06-14-2024 12:28 PM
ZandBakhtiari1
Occasional Contributor

We have an old python script that zooms to the extent of the layers selected in the dataframe.  We are trying to update it for Pro and was hoping someone could point us in the right direction.

 

import arcpy
MXD=arcpy.mapping.MapDocument('current')
dataframe = arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
dataframe.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

0 Kudos
2 Solutions

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Although the name is zoomToAllLayers, the documentation shows there is a selection_only parameter.  The ArcMap script using dataframe.zoomToSelectedFeatures is not layer specific so all selected features across all layers determine the extent, which is the same as zoomToAllLayers.

View solution in original post

0 Kudos
ZandBakhtiari1
Occasional Contributor

@JoshuaBixby ahhhh I see that now.  Thanks to your help I was able to update the script!

 

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
dataframe = aprx.activeView
dataframe.zoomToAllLayers(True)

View solution in original post

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Have you tried anything yet?  Which part of translating arcpy.mapping code to arcpy.mp are you stuck on?  I would start with Migrating from arcpy.mapping to ArcGIS Pro - ArcGIS Pro | Documentation, it has good code examples to at least get you to opening a specific map.

0 Kudos
ZandBakhtiari1
Occasional Contributor

@JoshuaBixby  The part I am stuck on is "zoomToSelectedFeatures()"  I cannot find the arcpy,mp equivalent.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor
0 Kudos
ZandBakhtiari1
Occasional Contributor

@JoshuaBixbyI don't want to zoom to all layers.  I want to zoom to the selected features within a layer.  Example: Parcel is selected then you right click "Zoom to Selection"

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Although the name is zoomToAllLayers, the documentation shows there is a selection_only parameter.  The ArcMap script using dataframe.zoomToSelectedFeatures is not layer specific so all selected features across all layers determine the extent, which is the same as zoomToAllLayers.

0 Kudos
ZandBakhtiari1
Occasional Contributor

@JoshuaBixby ahhhh I see that now.  Thanks to your help I was able to update the script!

 

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
dataframe = aprx.activeView
dataframe.zoomToAllLayers(True)

0 Kudos