Select to view content in your preferred language

Zoom to Selected Layer with Python

386
6
Jump to solution
a month ago
ZandBakhtiari1
New Contributor III

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
New Contributor III

@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
New Contributor III

@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
New Contributor III

@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
New Contributor III

@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