Select to view content in your preferred language

Issue with select by location arcpy with ArcGIS Pro

3399
11
02-12-2020 06:49 AM
deleted-user-3K8mKpoPb261
Deactivated User

I am running an arcpy script in command prompt with ArcGIS Pro installed. For some reason the Select By location tool produce different result every time I run it.

Here is the selection code:

arcpy.SelectLayerByLocation_management('lowland_layer', 'INTERSECT', 'buildings_layer', selection_type='NEW_SELECTION')
arcpy.CopyFeatures_management('lowland_layer', 'lowlands_with_buildings.shp')

I have no idea why this would produce different results everytime I run it. I have already made sure that all files has the same coordinate system, as well as the outputs.

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

From the help topic

Select Layer By Location—Data Management toolbox | Documentation 

Determines how the selection will be applied to the input and how to combine it with an existing selection. Note that there is no option here to clear an existing selection. To clear a selection, use the CLEAR_SELECTION option on the Select Layer By Attribute tool.

deleted-user-3K8mKpoPb261
Deactivated User

I'm actually creating the layers right before the selection (with MakeFeatureLayer). So I'm assuming a preselected layer shouldn't be the issue

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It would be good to show additional code then.  Given that we can't see what workspaces are set or how you are creating and managing layers, it is difficult to give much more feedback than what Dan has already done.

DanPatterson_Retired
MVP Emeritus

It wouldn't hurt to see if clearing the selection to rule it out... you mentioned running tool multiple times, but you don't indicate whether map/pro is shut down between... things that you don't think persist within a session might surprise you

deleted-user-3K8mKpoPb261
Deactivated User

The script is very long and uses functions from other .py file. But long script short, here is the script config:

arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False

Then I run multiple functions imported from another python file but based on arcpy. Loops are done using the 'in_memory' workspace.

And I'm using .shp to save all files.

Everything is working fine except the part where I do this selection. I'm doing other selections as well and they all work fine. I really have no idea what going on. Should I delete/clear selection before running a selection? (I never had to that before though)

Another thing is that I saved these selection files at the end of the script. And I do the selection manually, it works fine. It's only when I run it as a script that there's this issue.

0 Kudos
DanPatterson_Retired
MVP Emeritus

running it manually and it works fine does suggest that the selection isn't being cleared out in the script.  Only you can test this

deleted-user-3K8mKpoPb261
Deactivated User

OK. I'll try to make this fix and follow-up. Thanks!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Also... I always Delete_management anything that is in_memory/memory at the end of my scripts.  Don't want to tempt remnants lurking around

deleted-user-3K8mKpoPb261
Deactivated User

So the 'clear_selection' did not fix the issue but I think I figure out the solution after multiple tests.

If I'm using this workflow the selection is messed up:

this_feature = arcpy.CopyFeatures_management('this_feature0.shp', 'this_feature1.shp')

with arcpy.da.UpdateCursor(this_feature, ["some_field"]) as features:
    for afeature in features:
        if condition_met:
            features.deleteRow()‍‍‍‍‍‍

arcpy.MakeFeatureLayer_management(this_feature, 'this_feature_lyr') # creating the layer right after UpdateCursor
arcpy.MakeFeatureLayer_management(this_selecting_feature, 'this_selecting_feature_lyr')

arcpy.SelectLayerByLocation_management('this_feature_lyr', 'INTERSECT', 'this_selecting_feature_lyr', selection_type='NEW_SELECTION')
# this final output will change each time I run the script
updated_features = arcpy.CopyFeatures_management('this_feature_lyr' ,'updated_features.shp')‍‍‍‍‍‍‍‍‍‍‍‍‍

But if I make a copy after the UpdateCursor then everything seems fine:

with arcpy.da.UpdateCursor('this_feature0.shp', ["some_field"]) as features:
    for afeature in features:
        if condition_met:
            features.deleteRow()‍‍‍‍‍‍

arcpy.CopyFeatures_management('this_feature0.shp', 'this_feature1.shp') # copy after UpdateCursor

arcpy.MakeFeatureLayer_management('this_feature1.shp', 'this_feature_lyr') 
arcpy.MakeFeatureLayer_management(this_selecting_feature, 'this_selecting_feature_lyr')

arcpy.SelectLayerByLocation_management('this_feature_lyr', 'INTERSECT', 'this_selecting_feature_lyr', selection_type='NEW_SELECTION')
# this final output will be correct
updated_features = arcpy.CopyFeatures_management('this_feature_lyr' ,'updated_features.shp')‍‍‍‍‍‍‍‍‍‍‍‍‍

I don't really know why to be honest. Maybe its an issue with memory management. But this has fixed the issue.

Thanks!

0 Kudos