Select by location not working on in_memory layer

3530
2
Jump to solution
08-15-2013 06:57 AM
PatrickJurgens
New Contributor III
A coworker of my produced a great model and asked me to convert it to Python for him. The python script is failing at the same point each time I try to use it.
It is unable to run the following command:
>>> arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", outputGDB + '\\mtdbStreetBuffer', "", "NEW_SELECTION")  Traceback (most recent call last):   File "<pyshell#15>", line 1, in <module>     arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", outputGDB + '\\mtdbStreetBuffer', "", "NEW_SELECTION")   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6559, in SelectLayerByLocation     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000368: Invalid input data. Failed to execute (SelectLayerByLocation).


The "COMPLETELY_WITHIN" selection method requires that the selecting feature be of 'polygon' type, so I checked that:
>>> arcpy.Describe('in_memory\\mtdbStreetBuffer').shapeType u'Polygon'


I saved both of the layers as Feature Classes in a File GDB.
>>> arcpy.CopyFeatures_management('in_memory\\mergedStreetsPolygons', outputGDB + '\\mergedStreetsPolygon') <Result '..path\suppressed..gdb\\mergedStreetsPolygon'> >>> arcpy.CopyFeatures_management('in_memory\\mtdbStreetBuffer', outputGDB + '\\mtdbStreetBuffer') <Result '..path\suppressed..gdb\\mtdbStreetBuffer'>

*I blocked off the path to the GDB because it divulges more information about our servers than I should post on the forum.

I brought those features into ArcMap and used Arctoolbox to run the selection and it worked.
Using the python immediate window in ArcMap also worked.

So does anyone have any idea why the selection will not work 'in_memory'?
Other similar lines of code within the same script are working properly, just not this one.
Tags (2)
1 Solution

Accepted Solutions
PatrickJurgens
New Contributor III
I solved this problem by back tracking.
In case other people stumble on this post while searching for their own solution here is what happened:

The 'in_layer' that I was using ('in_memery\\mergedStreetsPolygons') was not actually a layer, it was an in_memory feature class.
It originated from this command which I thought would output as a layer:
arcpy.FeatureToPolygon_management('in_memory\\mergedStreets', 'in_memory\\mergedStreetsPolygons', "", "ATTRIBUTES", "")

I tried to modify the command to force it to output as a layer and that did not work, I guess it is only able to output to Feature Class:
>>> arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer.lyr', "", "NEW_SELECTION")  Traceback (most recent call last):   File "<pyshell#23>", line 1, in <module>     arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer.lyr', "", "NEW_SELECTION")   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6559, in SelectLayerByLocation     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000368: Invalid input data. ERROR 000732: Selecting Features: Dataset in_memory\mtdbStreetBuffer.lyr does not exist or is not supported Failed to execute (SelectLayerByLocation).

So I just added a line to convert it to a Layer and used the new layer name for the in_layer parameter. It worked:
>>> arcpy.MakeFeatureLayer_management('in_memory\\mergedStreetsPolygons', 'in_memory\\mergedStreetsPolygons.lyr') <Result 'in_memory\\mergedStreetsPolygons.lyr'> >>> arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons.lyr', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer', "", "NEW_SELECTION") <Result 'in_memory\\mergedStreetsPolygons.lyr'>

Of course the on disk copies that I made (for trouble shooting) worked okay in ArcMap because when I brought them into the map it made layers of them.

Just a quick reminder, if you do something like this to fix your code make sure that all future commands that referenced the old 'layer' [technically it wasn't a layer, hence the error message] get updated to use the new one...

View solution in original post

2 Replies
PatrickJurgens
New Contributor III
I solved this problem by back tracking.
In case other people stumble on this post while searching for their own solution here is what happened:

The 'in_layer' that I was using ('in_memery\\mergedStreetsPolygons') was not actually a layer, it was an in_memory feature class.
It originated from this command which I thought would output as a layer:
arcpy.FeatureToPolygon_management('in_memory\\mergedStreets', 'in_memory\\mergedStreetsPolygons', "", "ATTRIBUTES", "")

I tried to modify the command to force it to output as a layer and that did not work, I guess it is only able to output to Feature Class:
>>> arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer.lyr', "", "NEW_SELECTION")  Traceback (most recent call last):   File "<pyshell#23>", line 1, in <module>     arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer.lyr', "", "NEW_SELECTION")   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6559, in SelectLayerByLocation     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000368: Invalid input data. ERROR 000732: Selecting Features: Dataset in_memory\mtdbStreetBuffer.lyr does not exist or is not supported Failed to execute (SelectLayerByLocation).

So I just added a line to convert it to a Layer and used the new layer name for the in_layer parameter. It worked:
>>> arcpy.MakeFeatureLayer_management('in_memory\\mergedStreetsPolygons', 'in_memory\\mergedStreetsPolygons.lyr') <Result 'in_memory\\mergedStreetsPolygons.lyr'> >>> arcpy.SelectLayerByLocation_management('in_memory\\mergedStreetsPolygons.lyr', "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer', "", "NEW_SELECTION") <Result 'in_memory\\mergedStreetsPolygons.lyr'>

Of course the on disk copies that I made (for trouble shooting) worked okay in ArcMap because when I brought them into the map it made layers of them.

Just a quick reminder, if you do something like this to fix your code make sure that all future commands that referenced the old 'layer' [technically it wasn't a layer, hence the error message] get updated to use the new one...
DuncanHornby
MVP Notable Contributor
SelectLayerByLocation works with FeatureLayers, your code is referencing an in-memory FeatureClass.

You need to create a FeatureLayer for this before you call SelectLayerByLocation

arcpy.MakeFeatureLayer_management('in_memory\\mergedStreetsPolygons',"StreetPolygonFeatureLayer")

arcpy.SelectLayerByLocation_management("StreetPolygonFeatureLayer", "COMPLETELY_WITHIN", 'in_memory\\mtdbStreetBuffer.lyr', "", "NEW_SELECTION")
0 Kudos