Zooming to selected features.

1433
7
12-07-2012 09:20 AM
Emilbrundage
New Contributor III
Hi all,

I have been told that in ArcGIS 10 we can now script display behaviors. I'm very new to scripting and trying to write a simple script that will zoom to a selected feature. The zoom would apply to all selected features in all layers in a data frame (but only the first data frame found if there are multiple). Any help on what the new code is to do this?

Thanks!
Tags (2)
0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Here is an example:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.zoomToSelectedFeatures()
0 Kudos
Emilbrundage
New Contributor III
That worked like a charm, thanks!
0 Kudos
Emilbrundage
New Contributor III
Hmmm, now I'm trying to zoom to selections in a layer selected by the user. I see this code:

df.extent = lyr.getSelectedExtent()

but can't quite get it to work. Here's my code:

import arcpy

# put map info into variable mxd
mxd = arcpy.mapping.MapDocument("CURRENT")
arcpy.AddMessage ("0")

# get layer from user
lyr = arcpy.GetParameterAsText (1)
arcpy.AddMessage ("1")

# get list of data frames
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.AddMessage ("2")


#zoom to the selected layer
df.extent = lyr.getSelectedExtent()
arcpy.AddMessage ("3")


The printing numbers is for my own debugging. It gets to 2, so clearly there's something wrong with "df.extent = lyr.getSelectedExtent()"

My error message:
<type 'exceptions.AttributeError'>: 'unicode' object has no attribute 'getSelectedExtent'

Thanks!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You will first need to create a Layer object so you can call the 'getSelectedExtent()' method.  Ex:

import arcpy
from arcpy import mapping

mxd = mapping.MapDocument("CURRENT")
df = mapping.ListDataFrames(mxd)[0]
lyr = mapping.Layer(arcpy.GetParameterAsText(0))
df.extent = lyr.getSelectedExtent()

del mxd
0 Kudos
Emilbrundage
New Contributor III
Thanks tons for the help! I have a few questions that are just for my own curiosity and understanding.

1) What is the difference between what I did

lyr = arcpy.GetParameterAsText (1)

(when the 1 variable is set to layer type), and getting the layer as described in the code provided?

lyr = mapping.Layer(arcpy.GetParameterAsText(0))

2)  for df = mapping.ListDataFrames(mxd)[0], what does the [0] indicate?

3) why del mxd?

Any answers would be appreciated. I am sure they are very noobish questions.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
1) What is the difference between what I did

Even though you set the data type to Layer, this will not create the Layer object.  In order to call any methods for a layer, you will first need to create the object.  Also, when you use arcpy.GetParameterAsText, the first parameter is always 0 instead of 1.

2)  for df = mapping.ListDataFrames(mxd)[0], what does the [0] indicate?

The arcpy.mapping.ListDataFrames function returns a list.  The [0] indicates to use the first value in the list.  Even if you only have one data frame in your MXD, you will still need to indicate the index value for the list object.

3) why del mxd?

This will release any locks on the MXD.
0 Kudos
Emilbrundage
New Contributor III
Thanks a lot. The answers are very helpful.
0 Kudos