<?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: Select Layer By Location different between stand-alone script and immediate mode? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341996#M26817</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I appreciate the detailed explanation, it definitely clears things up. I had called arcpy.MakeFeatureLayer_management right after calling arcpy.mapping.AddLayer, thinking that I should convert into a Feature Layer what I had just added to the map. I removed the AddLayer statements, added &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;arcpy.env.addOutputsToMap = True&lt;/SPAN&gt;&lt;SPAN&gt;, and my code works fine now.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 13 Sep 2013 12:21:39 GMT</pubDate>
    <dc:creator>KaitlynnDavis</dc:creator>
    <dc:date>2013-09-13T12:21:39Z</dc:date>
    <item>
      <title>Select Layer By Location different between stand-alone script and immediate mode?</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341992#M26813</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm fairly new to arcpy, and I don't understand why this one line of code executes in immediate mode, but not in a stand alone script. &lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run my .py script in ArcMap (shown below), my dialog box shows that the script completed successfully, but I don't see any evidence of a selection. Performing my code line by line in ArcMap's built-in python window, however, selects and highlights the applicable records. Must I add something more to my standalone script to perform this selection task?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy arcpy.env.workspace = 'PATH_TO_RESOURCE' arcpy.env.overwriteOutput = True inFeatureClass = arcpy.GetParameterAsText(0) if inFeatureClass == "Elderly block groups":&amp;nbsp; selectedLayer = 'PATH_TO_RESOURCE' elif inFeatureClass == "Minorities":&amp;nbsp; selectedLayer = 'PATH_TO_RESOURCE' mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd,"*")[0] newlayer = arcpy.mapping.Layer(selectedLayer) projects = 'PATH_TO_RESOURCE' projectsLayer = arcpy.mapping.Layer(projects) arcpy.mapping.AddLayer(df, newlayer) arcpy.mapping.AddLayer(df, projectsLayer, "TOP") arcpy.MakeFeatureLayer_management(projects, "ProjectsLayer") arcpy.MakeFeatureLayer_management(newlayer, "RegionOverlay") arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay") arcpy.MakeFeatureLayer_management("ProjectsLayer", "OverlayProjects")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Sep 2013 17:29:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341992#M26813</guid>
      <dc:creator>KaitlynnDavis</dc:creator>
      <dc:date>2013-09-10T17:29:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Location different between stand-alone script and immediate mode?</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341993#M26814</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Kaitlynn,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try adding &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//018v0000006s000000"&gt;arcpy.RefreshActiveView()&lt;/A&gt;&lt;SPAN&gt; at the end of your code.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Sep 2013 17:41:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341993#M26814</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-09-10T17:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Location different between stand-alone script and immediate mode?</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341994#M26815</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks, I added that line, and then tried the RefreshTOC method, and neither worked. It seems that the geoprocessing tasks in my code are not executing. I saved my script as a .py in Notepad ++, not sure if that has any impact on how it's interpreted in ArcMap&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Sep 2013 18:33:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341994#M26815</guid>
      <dc:creator>KaitlynnDavis</dc:creator>
      <dc:date>2013-09-10T18:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Location different between stand-alone script and immediate mode?</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341995#M26816</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Kaitlynn,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you want to skip my attempts to explain things, just jump to the end of this post to see how I would import two layers into an mxd, and then use them to do a select by location. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I think is happening in your script:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;There might be some redundancy in your code, because arcpy.MakeFeatureLayer_management() will generally add a copy of the temporary layer it creates to the TOC (if arcpy.env.addOutputsToMap = True), but it seems that you've also added similar layers (possibly with the same names which might make it confusing) using arcpy.mapping.AddLayer().&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Keeping that in mind, Your line of code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;refers to the temporary FeatureLayers you created using arcpy.MakeFeatureLayer_management(), so you won't see the selections in the layers you added with arcpy.mapping.AddLayer().&amp;nbsp; If the output of geoprocessing tools aren???t being added to the TOC (i.e. if arcpy.env.addOutputsToMap = False), then the selection you did was occurring in a layer that wasn???t even in ArcMap, just in temporary memory independent of your mxd.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I'm using an mxd, I tend to refer to layers using something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;LayerObject = arcpy.mapping.ListLayers(mxd, "nameOfLayerInTOC")[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Conversely, I don't tend to use arcpy.MakeFeatureLayer_management() unless I'm doing geoprocessing completely independently of an mxd.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So here's the way I'd do it (didn't need a refresh):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd)[0]&amp;nbsp; rootPath = r"V:\Projects\Shared\RouteLogSystem\ArcGIS_10_Prototype\Prototype_V10" LyrFile1 = arcpy.mapping.Layer(os.path.join(rootPath, r"LayerFiles\rtlogpts.lyr")) #points LyrFile2 = arcpy.mapping.Layer(os.path.join(rootPath, r"LayerFiles\envelope.lyr")) #polygon arcpy.mapping.AddLayer(df, LyrFile1, "TOP")&amp;nbsp;&amp;nbsp; Lyr1 = arcpy.mapping.ListLayers(mxd, "rtlogpts")[0] arcpy.mapping.InsertLayer(df, Lyr1, LyrFile2, "AFTER") Lyr2 = arcpy.mapping.ListLayers(mxd, "envelope")[0]&amp;nbsp; arcpy.SelectLayerByLocation_management(Lyr1, "INTERSECT", Lyr2)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Sep 2013 21:16:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341995#M26816</guid>
      <dc:creator>KerryAlley</dc:creator>
      <dc:date>2013-09-12T21:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Location different between stand-alone script and immediate mode?</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341996#M26817</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I appreciate the detailed explanation, it definitely clears things up. I had called arcpy.MakeFeatureLayer_management right after calling arcpy.mapping.AddLayer, thinking that I should convert into a Feature Layer what I had just added to the map. I removed the AddLayer statements, added &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;arcpy.env.addOutputsToMap = True&lt;/SPAN&gt;&lt;SPAN&gt;, and my code works fine now.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Sep 2013 12:21:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-different-between-stand/m-p/341996#M26817</guid>
      <dc:creator>KaitlynnDavis</dc:creator>
      <dc:date>2013-09-13T12:21:39Z</dc:date>
    </item>
  </channel>
</rss>

