How to SelectByLocation, in memory, without adding to current map?

950
4
Jump to solution
07-07-2020 01:01 PM
VinceE
by
Occasional Contributor II

I am attempting to perform a SelectByLocation on a feature class and export that selection. I am running this code directly in the Python Window in ArcGIS Pro. Eventually this operation will be ported over to a larger script that will become a tool in a toolbox. If I use EnvManager as shown below to set "addOutputsToMap=True", the below works fine. When set to False, it does not.

import arcpy

arcpy.env.workspace = r"C:\FULLPATH\TEST.gdb"

sheets = "Sheets"
parcels = "Parcels"

with arcpy.EnvManager(addOutputsToMap=True):
    sheet_lyr = arcpy.management.SelectLayerByLocation(sheets, "INTERSECT", parcels)
arcpy.management.CopyFeatures(sheet_lyr, "Sheet_Intersect")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I have tried all sorts of combinations using MakeFeatureLayer(), using r"memory\XXX" as a workspace, using getOutput() from the Result, etc., and cannot figure out any combination that avoids adding the unnecessary intermediate layer "sheet_lyr" to the map. There seems to be several different methods that all work when adding the "sheet_lyr" to the map, but none when "addOutputsToMap=False".

Is there no way to perform a SelectByLocation and export the result entirely within memory workspace, without adding a layers to a map? Or is it just not possible to do directly in the Python Window?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

What you are seeing is an artifact, I am not sure if it is a "bug" or design choice, of how the interactive Python window interacts with the application within ArcGIS Pro.  The code you have written will work fine when run out of process, either as a script tool or stand-alone script, but it won't work in the interactive Python window because the layer that is created by Select Layer By Location (or would be created by Make Feature Layer) loses its connection to the referencing lookups that happen within ArcGIS Pro. 

Interestingly/oddly, if you run your code in a new ArcGIS Pro project before any Maps have been opened/created, it will run fine. 

Another workaround is to add the layer to the map and then remove it right after the code segment runs.

UPDATEVince Edwards‌, I raised this issue in the Pro beta forums, and they are considering it a defect and am looking in to addressing it in a future version.

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

returns a result in both python and notebook windows when set to False in my example


... sort of retired...
VinceE
by
Occasional Contributor II

Thanks taking a look Dan, but it performs this way for me as well--see below. The issue is on the export. I am getting errors on the last line where I'm attempting to use CopyFeatures. The goal is to extract the selection to a new feature class. Trying to perform the selection followed by the export is giving me issues. 

Specifically (with CopyFeatures), I am getting "SHEETS_Layer23 does not exist or is not supported." Since it clearly exists, my guess is that I am not referring to it in a format that CopyFeatures can understand. As it is, I suppose I'm passing in a Result object, but I'm not sure how to get a Feature Layer object from the Result to pass to CopyFeatures.


Please excuse the formatting, it's not clear to me how to get the code block up on a "Reply."
----------
import
arcpy

arcpy.env.workspace = r"C:\FULLPATH\TEST.gdb"

sheets = "SHEETS"

parcels = "PARCELS"

with arcpy.EnvManager(overwriteOutput=True, addOutputsToMap=False😞

         tmp = arcpy.management.SelectLayerByLocation(sheets, "INTERSECT", parcels)

#arcpy.management.CopyFeatures(tmp, "Output")

print(f"{*tmp,}")

('SHEETS_Layer23', 'SHEETS_Layer23', '37')

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What you are seeing is an artifact, I am not sure if it is a "bug" or design choice, of how the interactive Python window interacts with the application within ArcGIS Pro.  The code you have written will work fine when run out of process, either as a script tool or stand-alone script, but it won't work in the interactive Python window because the layer that is created by Select Layer By Location (or would be created by Make Feature Layer) loses its connection to the referencing lookups that happen within ArcGIS Pro. 

Interestingly/oddly, if you run your code in a new ArcGIS Pro project before any Maps have been opened/created, it will run fine. 

Another workaround is to add the layer to the map and then remove it right after the code segment runs.

UPDATEVince Edwards‌, I raised this issue in the Pro beta forums, and they are considering it a defect and am looking in to addressing it in a future version.

VinceE
by
Occasional Contributor II

Joshua,

Thanks a ton for this helpful information, glad to hear the issue is likely not just bad syntax on my end.

Since this snippet part of a larger tool that is destined for a toolbox anyway, hopefully it will be a nonissue in that context. Worst case, I'll go with the add/remove strategy.

Thanks again.

0 Kudos