Select to view content in your preferred language

zoom to selected features

8748
5
08-24-2011 10:57 AM
AmyMeehan
Deactivated User

I'm trying to write what *should be* a simple script to allow a user to input a nest number and zoom to that feature.  I have been playing around with the script for two days now and alternately ArcMap either crashes or it tells me that the script has run successfully but it does not actually zoom in anywhere.  Any suggestions?  Script is below:

I've tried both the df.zoomToSelectedFeatures as well as the lyr.getSelectedExtent.  ArcMap seems to crash more when using the zoomtoselectedfeatures. 

Amy Meehan

0 Kudos
5 Replies
MathewCoyle
Honored Contributor
Have you tried putting each line into the python window in ArcMap to see where an issue pops up? Where do you get errors? If this is supposed to be run on the mxd currently open you can change the mxd path to "CURRENT", also you shouldn't need to make the feature a feature layer if it is already in the mxd.
0 Kudos
MathewCoyle
Honored Contributor
Something simple like this should work.
import arcpy
arcpy.AddMessage("Starting")
val1 = arcpy.GetParameterAsText(0)
arcpy.AddMessage(val1)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Main Map")[0]
lyr = arcpy.mapping.ListLayers(mxd, <Your Feature Layer>, df)[0]

arcpy.AddMessage(lyr.name)
expression = "<Your Fieldname> = '"+val1+"'"
arcpy.AddMessage(expression)
arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression)
df.extent = lyr.getSelectedExtent()
arcpy.RefreshActiveView()
0 Kudos
AmyMeehan
Deactivated User
Thanks!  That mostly works better than what I had written.  I thought that since my "layer" that I'm selecting on is actually a feature class that I had to change it into a layer using MakeFeatureLayer.  But I couldn't even get it to select a feature correctly.  Your code does select the feature, but it still does not zoom to the selected feature.  Wondering if this is some type of bug in ArcMap 10 or if I keep doing something wrong. 

Thanks again!

Amy
0 Kudos
MathewCoyle
Honored Contributor
If the feature is selected it should zoom to it no problem. Are you on SP2? Are you sure only 1 feature is being selected? Try manually selecting a feature and running this from the python window. If this doesn't work, something else is going on.

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "<Your Data Frame>")[0]

lyr = arcpy.mapping.ListLayers(mxd, "<Your Layer>", df)[0]

df.extent = lyr.getSelectedExtent()
0 Kudos
EdwardConrad
Deactivated User

I came across this thread after encountering the same problem as Amy and I too initially tried Mathew's code to no avail. I'm posting my experience with the hope that it saves someone frustration.

    

# So initially code wasn't working

df.extent = lyr.getSelectedExtent()

The solution in my case was that I had previously set the inset map's data frame extent properties to "Fixed Extent" (it's a MXD template I use a lot where prior I didn't want it to move from the state boundaries extent). Since the purpose of part of the script was to zoom to the selected features, I changed this property back to the default, "Automatic".

Subsequently, the code worked like it's supposed to.

df.extent = lyr.getSelectedExtent()

0 Kudos