Select Layer By Location different between stand-alone script and immediate mode?

2229
4
Jump to solution
09-10-2013 10:29 AM
KaitlynnDavis
Occasional Contributor
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.
arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay")


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?

import arcpy arcpy.env.workspace = 'PATH_TO_RESOURCE' arcpy.env.overwriteOutput = True inFeatureClass = arcpy.GetParameterAsText(0) if inFeatureClass == "Elderly block groups":  selectedLayer = 'PATH_TO_RESOURCE' elif inFeatureClass == "Minorities":  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")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KerryAlley
Occasional Contributor
Hi Kaitlynn,

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.


What I think is happening in your script:
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(). 


Keeping that in mind, Your line of code:
arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay")
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().  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. 


If I'm using an mxd, I tend to refer to layers using something like:
LayerObject = arcpy.mapping.ListLayers(mxd, "nameOfLayerInTOC")[0]
Conversely, I don't tend to use arcpy.MakeFeatureLayer_management() unless I'm doing geoprocessing completely independently of an mxd.


So here's the way I'd do it (didn't need a refresh):
import arcpy, os mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd)[0]  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")   Lyr1 = arcpy.mapping.ListLayers(mxd, "rtlogpts")[0] arcpy.mapping.InsertLayer(df, Lyr1, LyrFile2, "AFTER") Lyr2 = arcpy.mapping.ListLayers(mxd, "envelope")[0]  arcpy.SelectLayerByLocation_management(Lyr1, "INTERSECT", Lyr2)

View solution in original post

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Kaitlynn,

Try adding arcpy.RefreshActiveView() at the end of your code.
0 Kudos
KaitlynnDavis
Occasional Contributor
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
0 Kudos
KerryAlley
Occasional Contributor
Hi Kaitlynn,

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.


What I think is happening in your script:
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(). 


Keeping that in mind, Your line of code:
arcpy.SelectLayerByLocation_management("ProjectsLayer", "INTERSECT", "RegionOverlay")
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().  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. 


If I'm using an mxd, I tend to refer to layers using something like:
LayerObject = arcpy.mapping.ListLayers(mxd, "nameOfLayerInTOC")[0]
Conversely, I don't tend to use arcpy.MakeFeatureLayer_management() unless I'm doing geoprocessing completely independently of an mxd.


So here's the way I'd do it (didn't need a refresh):
import arcpy, os mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd)[0]  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")   Lyr1 = arcpy.mapping.ListLayers(mxd, "rtlogpts")[0] arcpy.mapping.InsertLayer(df, Lyr1, LyrFile2, "AFTER") Lyr2 = arcpy.mapping.ListLayers(mxd, "envelope")[0]  arcpy.SelectLayerByLocation_management(Lyr1, "INTERSECT", Lyr2)
0 Kudos
KaitlynnDavis
Occasional Contributor
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 arcpy.env.addOutputsToMap = True, and my code works fine now.
0 Kudos