Select to view content in your preferred language

Selecting points not within polygons in arcpy

168
4
Jump to solution
2 weeks ago
blob
by
New Contributor

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()
 
For which I also have created a new database with a point (5 features) and polygon (1 features) layer:
 
 blob_2-1719575567538.png

 

I have also created a new tool to test this with, for which I am getting the output (with some small print statements added):

 
blob_1-1719575418697.png

Which suggests that no selection is being done. Using the Select By Location tool within ArcGIS Pro (the same equivalent?) the selection works:

blob_4-1719575781576.png

 

blob_3-1719575741393.png

 

Things I have tried:

  • Substituting the operation ('INTERSECT', 'COMPLETELY_WITHIN')
  • Clearing the cache (Project > Options > Application > Display > Local Cache > Clear Cache Now)
  • Restarting ArcGIS / reloading tool
  • Clearing / not clearing the selection after copying features
  • Using layers in scratch.gdb / actual database
  • Setting the method arguments using their names (in_layer=..., overlap_type=..., etc.)
  • Switching 'INVERT' / 'NOT_INVERT' but none of them affect the results, i.e. no selection

Am I misunderstanding how this method works, or is there some other explanation for the selection not working?

 

0 Kudos
1 Solution

Accepted Solutions
RichardHowe
Occasional Contributor III

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

View solution in original post

0 Kudos
4 Replies
RichardHowe
Occasional Contributor III

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

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

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()

 

0 Kudos
blob
by
New Contributor

Good suggestion, I will implement this as well!

0 Kudos
blob
by
New Contributor

This worked, thank you!

0 Kudos