Simple select and zoom code works in Python window but not as a script

1368
6
01-08-2013 08:21 AM
BrigidGrund
New Contributor
Hi all,

I'm working on a script tool that automatically selects and zooms based on user inputted Township, Range, and Section. The code runs and works perfectly in the Python window. It runs as a script tool with no errors, but does not zoom to the designated location... I suspect it's a problem with the MakeFeatureLayer command but I'm not sure. It's messy because i tried to use arcpy.MakeFeatureLayer(WySectns, "WySectnsLyr") but it was not working properly; this line was taken from a modelbuilder export. Here's the code:

import arcpy
from arcpy import env
env.workspace = 'C:\Users\...'
arcpy.env.overwriteOutput = True

# Local variables:
WySectns = "C:\\Users\\..."
WySectns_Lyr = "WySectns_Lyr"
mxd = arcpy.mapping.MapDocument("CURRENT") 

#User Defined Parameters
Township = arcpy.GetParameterAsText(0)
Range = arcpy.GetParameterAsText(1)
Section = arcpy.GetParameterAsText(2)

#Make Feature Layer
arcpy.MakeFeatureLayer_management(WySectns, WySectns_Lyr, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;AREA AREA VISIBLE NONE;PERIMETER PERIMETER VISIBLE NONE;STATE_S_ STATE_S_ VISIBLE NONE;STATE_S_ID STATE_S_ID VISIBLE NONE;SECTION_ SECTION_ VISIBLE NONE;TR TR VISIBLE NONE;MERIDIAN MERIDIAN VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
#Select Layer By Attribute
quote = "\""
whereClause = "[TOWNSHIP] = " + quote + Township + quote +" AND [RANGE] = " + quote + Range + quote +" AND [SECTION_] = " + quote + Section + quote
arcpy.SelectLayerByAttribute_management(WySectns_Lyr, "NEW_SELECTION", whereClause)

#zoom to selected features
df = arcpy.mapping.ListDataFrames(mxd) [0]
df.zoomToSelectedFeatures()
#df.extent = WySectns_Lyr.getSelectedExtent()
arcpy.RefreshActiveView()

#Clean layer files
arcpy.Delete_management(WySectns_Lyr)



Thanks for any help!
Brigid
Tags (2)
0 Kudos
6 Replies
by Anonymous User
Not applicable
If you want this to work as a stand alone script, you cannot reference the mxd as "CURRENT", I think this only works within the ArcMap PythonWin.  You must instead point to the full path to the mxd.  You may also want to consider using raw strings for your path names rather than the 'C:\\path\\name\\Map.mxd' (use r'C:\path\name\Map.mxd' instead).  This is much safer.

Also, another tip is to use string substitutions in your where clause ('%s') so that it is not so messy.  It may help to add a print statement to confirm there are selected features and how many as well.
0 Kudos
BrigidGrund
New Contributor
Thanks, Caleb.

I changed the path name to your format and the scripting tool still runs with no errors but does not zoom to the selected section. I just don't understand why this would work in the Python Window and  not as a script. I have never written a script before so it's probably something very basic that I'm just missing...

I tried adding messages to indicate the number of features selected. This should be "1" each time, and this is what the get count function was returning on my temporary layer. It's almost as if the layer is disappearing in the middle of the script when I run it as a script... yet it doesn't do that when I run it in the Python window.

Brigid
0 Kudos
BrigidGrund
New Contributor
Also I am running Arc 10.0 with Service Pack 5. I just tried it on a computer with Arc 10.1 and had the same problem.
0 Kudos
by Anonymous User
Not applicable
can you post your updated code?
0 Kudos
LucasDanzinger
Esri Frequent Contributor
So is the layer already in the map? Perhaps when you are running it through the Python window, you aren't needing to create another layer object because it is already in the map, so you just call the layer. Once it is in a script, you are creating a layer (not in the map) are making a selection and wanting to zoom, but it isn't really in the map yet. Not sure if this is it, but just a though. If your layer is in the map, could you instead use ListLayers and use a wildcard to find the applicable layer and then perform the selection on that layer object?
0 Kudos
BrigidGrund
New Contributor
Thank you all for the responses!

Yesterday, when I added a message to tell me the number of items selected in my layer, it was telling me there was one. Today, there is not.

At this section of code (when I run this as an arcscript):
arcpy.MakeFeatureLayer_management(WySectns, WySectns_Lyr, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;AREA AREA VISIBLE NONE;PERIMETER PERIMETER VISIBLE NONE;STATE_S_ STATE_S_ VISIBLE NONE;STATE_S_ID STATE_S_ID VISIBLE NONE;SECTION_ SECTION_ VISIBLE NONE;TR TR VISIBLE NONE;MERIDIAN MERIDIAN VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
arcpy.AddMessage("Layers " + str(arcpy.mapping.ListLayers(mxd, "WySectns_Lyr") [0]))


my script returns an error saying that <type 'exceptions.IndexError'>: list index out of range
Failed to execute (SelectAndZoom). So, in the script, the layer is simply not being created? What am I doing wrong here?

When I replace the code with this, it again works in the python window but not in the script, returning the same error:
arcpy.MakeFeatureLayer_management(WySectns, WySectns_Lyr)
arcpy.AddMessage("Layers " + str(arcpy.mapping.ListLayers(mxd, "WySectns_Lyr") [0]))


Why is the layer not created when running the script? How do I get it into the map?
0 Kudos