<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script tool yeilds different (wrong) result compared to Script run in python wind in Transportation Questions</title>
    <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407359#M1352</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: roy_hewitt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script doesn't use data driven pages.&amp;nbsp; There is one single map that is changed based on the watershed number (user input).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 16 Feb 2012 12:54:18 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2012-02-16T12:54:18Z</dc:date>
    <item>
      <title>Script tool yeilds different (wrong) result compared to Script run in python window</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407357#M1350</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: roy_hewitt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wrote a long analysis script that creates a watershed map with a summary of wetlands within it's boundary.&amp;nbsp; When I load the script in the python window (right click, load) the entire script runs correctly and exports the appropriate map.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code for python window (hard-coded user variables):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#userHucID = arcpy.GetParameterAsText(0)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # 12 digit HUC used to clip wetlands and zoom map to extent
#userName = arcpy.GetParameterAsText(1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # User Name is used to make map file are easy to find 
#boolPrint = arcpy.GetParameterAsText(2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Should the map print automatically; default is no

userHucID = "020403030102"
userName = "Hewitt"
boolPrint = True&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I create a script tool using the same script, I get different results.&amp;nbsp; Instead of zooming to the watershed it zooms to the full extent of the watershed shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code for script tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
userHucID = arcpy.GetParameterAsText(0)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # 12 digit HUC used to clip wetlands and zoom map to extent
userName = arcpy.GetParameterAsText(1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # User Name is used to make map file are easy to find 
boolPrint = arcpy.GetParameterAsText(2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Should the map print automatically; default is no
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've attached the two separate maps to give an indication of the difference in output.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:31:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407357#M1350</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T18:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: Script tool yeilds different (wrong) result compared to Script run in python wind</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407358#M1351</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ron,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you using Data driven pages to produce a map for each watershed boundary?&amp;nbsp; If so, you might be running into issue where the extent properties are not set properly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 12:50:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407358#M1351</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2012-02-16T12:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: Script tool yeilds different (wrong) result compared to Script run in python wind</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407359#M1352</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: roy_hewitt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script doesn't use data driven pages.&amp;nbsp; There is one single map that is changed based on the watershed number (user input).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 12:54:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407359#M1352</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2012-02-16T12:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: Script tool yeilds different (wrong) result compared to Script run in python wind</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407360#M1353</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the layer object extent to change your map's dataframe. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = layer.extent
df.extent = newExtent
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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().&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.MakeFeatureLayer_management("&amp;lt;data&amp;gt;","&amp;lt;layername&amp;gt;",sql)
layer = arcpy.mapping.Layer("&amp;lt;layername&amp;gt;")
df.extent = layer.getExtent()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:31:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407360#M1353</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2021-12-11T18:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Script tool yeilds different (wrong) result compared to Script run in python wind</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407361#M1354</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: roy_hewitt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is the method that I have already in my code (and it works in the python window):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Refresh view of mxd before exporting to PNG
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried your method as follows and got the same result:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm noticing that when I run the script tool I never see the feature layer "currentShed" created.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; The extent changes immediately after the selection tool succeeds.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:31:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407361#M1354</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T18:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Script tool yeilds different (wrong) result compared to Script run in python wind</title>
      <link>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407362#M1355</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've discovered the problem!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The default action for both the arcpy.MakeFeatureLayer_management() and arcpy.Select_analysis() tools when run in the python window &lt;/SPAN&gt;&lt;STRONG&gt;add the new layer to the map by default.&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the same script through ArcToolbox as a custom script tool, the layer isn't added to the map by default.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;This seems to be a problem that people have had in the past (&lt;A href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=1729&amp;amp;t=128409"&gt;http://forums.esri.com/Thread.asp?c=93&amp;amp;f=1729&amp;amp;t=128409&lt;/A&gt;)&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 17 Feb 2012 11:13:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/script-tool-yeilds-different-wrong-result-compared/m-p/407362#M1355</guid>
      <dc:creator>RoyHewitt</dc:creator>
      <dc:date>2012-02-17T11:13:56Z</dc:date>
    </item>
  </channel>
</rss>

