arcpy.SelectLayerByLocation_management not working

1587
9
Jump to solution
08-19-2019 10:48 AM
JessicaRamirez1
New Contributor II

Hi,

I am new to python and arcpy, I have a script I am trying to get to work. It fails at the select layer by location. If I take out the select by location the script runs fine and the field calculates works. I feel like I am missing something simple but I just don't know.

import arcpy
# Set the workspace environment
arcpy.env.workspace = r"G:\GIS\ESRI\Staging\Survey123\Testing\New File Geodatabase.gdb"
print arcpy.ListFeatureClasses()
print("this is a list of feature classes")
# Select by location
arcpy.SelectLayerByLocation_management("propertypnts", "INTERSECT", "retailpt2", "", "NEW_SELECTION")
print("I have selected the features")
# Field Calculate
arcpy.CalculateField_management('propertypnts', "CompleteY_N",
                                '!CompleteY_N!.replace("Not Completed","Completed")', "Python_9.3")
print("Calculation done")

This is the error I get.

Traceback (most recent call last):
File "G:/GIS/ESRI/Staging/Survey123/Testing/SelectCalculate.py", line 8, in <module>
arcpy.SelectLayerByLocation_management("propertypnts", "INTERSECT", "retailpt2", "", "NEW_SELECTION")
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 7891, in SelectLayerByLocation
raise e
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation)

#Python #arcpy

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Frequent Contributor

You need to use the make featurelayer tool to expose them as layer objects that can feed into the select by location tool. Have a look at the help file.

View solution in original post

9 Replies
DanPatterson_Retired
MVP Esteemed Contributor

try dropping the last 2 parameters ( ie "", "NEW_SELECTION"). 

0 Kudos
JessicaRamirez1
New Contributor II

Hi Dan,

Thank You for your reply. Unfortunately, I get the same error as before even if I drop the last 2 parameters. No idea what to do now. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The code snippet you provided isn't self contained, i.e., it relies on additional code we cannot see to be complete.  For example, where are you defining or making the "propertypnts" and "retailpt2" ?

0 Kudos
JessicaRamirez1
New Contributor II

That is a good question, they are in my geodatabase from my workspace. How do I define them?

0 Kudos
DuncanHornby
MVP Frequent Contributor

You need to use the make featurelayer tool to expose them as layer objects that can feed into the select by location tool. Have a look at the help file.

JessicaRamirez1
New Contributor II

Thank you! it worked!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Assuming "they" are feature classes, you need to use Make Feature Layer—Help | ArcGIS Desktop to define them as layers.  See the code sample on the documentation for the general workflow.

JessicaRamirez1
New Contributor II

Thank you, it worked! 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Please mark one of our answers correct.  Since Duncan beat me to the punchline, maybe mark his correct and mine helpful.