Dear community,
I am having problems selecting points from a layer which are not within another polygon layer. I have tried using arcpy.SelectLayerByLocation_management(point, 'WITHIN', poly, '', 'NEW_SELECTION', 'INVERT'), but it has not yielded the desired results.
To test this functionality, I have created the MWE below.
import arcpy
def main():
arcpy.env.workspace = arcpy.env.scratchGDB
arcpy.env.overwriteOutput = True
poly = 'test.gdb/test_poly'
point = 'test.gdb/test_point'
arcpy.SelectLayerByLocation_management(point, 'WITHIN', poly, '', 'NEW_SELECTION', 'INVERT')
point_select = 'point_select'
arcpy.CopyFeatures_management(point, point_select)
arcpy.SelectLayerByAttribute_management(point, selection_type='CLEAR_SELECTION')
if __name__ == "__main__":
main()
I have also created a new tool to test this with, for which I am getting the output (with some small print statements added):
Which suggests that no selection is being done. Using the Select By Location tool within ArcGIS Pro (the same equivalent?) the selection works:
Things I have tried:
Am I misunderstanding how this method works, or is there some other explanation for the selection not working?
Solved! Go to Solution.
You are running your python on feature class inputs, not feature layers (which is what you're running the tool in Pro on). A feature layer is a temporary construct within a Pro session which gives you a view, symbology etc of your feature class.
Use the Make Feature Layer tool first in your script, then use the two layers you create as your inputs for the select by location and I suspect you will have more success.
Or (as I can see you've attached it to a toolbox) make the inputs for the tool "feature layers" allowing you to drag and drop from your table of contents
You are running your python on feature class inputs, not feature layers (which is what you're running the tool in Pro on). A feature layer is a temporary construct within a Pro session which gives you a view, symbology etc of your feature class.
Use the Make Feature Layer tool first in your script, then use the two layers you create as your inputs for the select by location and I suspect you will have more success.
Or (as I can see you've attached it to a toolbox) make the inputs for the tool "feature layers" allowing you to drag and drop from your table of contents
That's a good point. I'd also try assigning the selection to a variable to pass into copy(). See Lines 11 and 14.
import arcpy
def main():
arcpy.env.workspace = arcpy.env.scratchGDB
arcpy.env.overwriteOutput = True
poly = 'test.gdb/test_poly'
point = 'test.gdb/test_point'
sel =arcpy.SelectLayerByLocation_management(point, 'WITHIN', poly, '', 'NEW_SELECTION', 'INVERT')
point_select = 'point_select'
arcpy.CopyFeatures_management(sel, point_select)
#tbh I'm not sure this is needed
#arcpy.SelectLayerByAttribute_management(point, selection_type='CLEAR_SELECTION')
if __name__ == "__main__":
main()
Good suggestion, I will implement this as well!
This worked, thank you!