Select to view content in your preferred language

Script tool yeilds different (wrong) result compared to Script run in python window

1771
5
02-16-2012 04:07 AM
by Anonymous User
Not applicable
Original User: roy_hewitt

I wrote a long analysis script that creates a watershed map with a summary of wetlands within it's boundary.  When I load the script in the python window (right click, load) the entire script runs correctly and exports the appropriate map.

Code for python window (hard-coded user variables):
#userHucID = arcpy.GetParameterAsText(0)     # 12 digit HUC used to clip wetlands and zoom map to extent
#userName = arcpy.GetParameterAsText(1)     # User Name is used to make map file are easy to find 
#boolPrint = arcpy.GetParameterAsText(2)      # Should the map print automatically; default is no

userHucID = "020403030102"
userName = "Hewitt"
boolPrint = True


When I create a script tool using the same script, I get different results.  Instead of zooming to the watershed it zooms to the full extent of the watershed shapefile.

Code for script tool:
userHucID = arcpy.GetParameterAsText(0)     # 12 digit HUC used to clip wetlands and zoom map to extent
userName = arcpy.GetParameterAsText(1)     # User Name is used to make map file are easy to find 
boolPrint = arcpy.GetParameterAsText(2)      # Should the map print automatically; default is no


I've attached the two separate maps to give an indication of the difference in output.
0 Kudos
5 Replies
AndrewChapkowski
Esri Regular Contributor
Ron,

Are you using Data driven pages to produce a map for each watershed boundary?  If so, you might be running into issue where the extent properties are not set properly.

To get and set the extent, there are many ways to achieve this, could you possible post your code for your whole process or at least where you think the issue is?
0 Kudos
by Anonymous User
Not applicable
Original User: roy_hewitt

The script doesn't use data driven pages.  There is one single map that is changed based on the watershed number (user input).
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Use the layer object extent to change your map's dataframe.

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = layer.extent
df.extent = newExtent


One way to obtain the extent is to use a searchcursor(), if you select the feature, you can use getSelectedExtent() or even the layer's getExtent().
Example:
arcpy.MakeFeatureLayer_management("<data>","<layername>",sql)
layer = arcpy.mapping.Layer("<layername>")
df.extent = layer.getExtent()


Hope this helps.
0 Kudos
by Anonymous User
Not applicable
Original User: roy_hewitt

This is the method that I have already in my code (and it works in the python window):
arcpy.MakeFeatureLayer_management(sheds, "currentShed", clipClause) # Create feature layer of HUC (from user input)
arcpy.SelectLayerByAttribute_management("currentShed", "NEW_SELECTION", clipClause) # Select current HUC
df.zoomToSelectedFeatures() # Zoom to extent of selected HUC
arcpy.RefreshActiveView     # Refresh view of mxd before exporting to PNG


I tried your method as follows and got the same result:
arcpy.MakeFeatureLayer_management(sheds, "currentShed", clipClause) # Create feature layer of HUC (from user input)
arcpy.SelectLayerByAttribute_management("currentShed", "NEW_SELECTION", clipClause) # Select current HUC
layer = arcpy.mapping.Layer("currentShed")
df.extent = layer.getExtent()
arcpy.RefreshActiveView()


I'm noticing that when I run the script tool I never see the feature layer "currentShed" created. 

When I run the code in the python window I see each successful step with the creation of the feature layer, and the selection made.  The extent changes immediately after the selection tool succeeds.
0 Kudos
RoyHewitt
Deactivated User
I've discovered the problem!

The default action for both the arcpy.MakeFeatureLayer_management() and arcpy.Select_analysis() tools when run in the python window add the new layer to the map by default.

When I run the same script through ArcToolbox as a custom script tool, the layer isn't added to the map by default.  Calls to the feature class in the TOC fail, therefore I can't make a selection on the fclass to change the data frame extent.

This seems to be a problem that people have had in the past (http://forums.esri.com/Thread.asp?c=93&f=1729&t=128409)

I added the feature layer as a user input parameter, but the feature layer doesn't appear in my TOC until the script has ended (despite using the arcpy.RefreshTOC() method).
0 Kudos