Search code examples
pythonarcgisarcpyarcmap

SWITCH_SELECTION is not working with SelectLayerByLocation_management


 import arcpy,sys
 sdeConn = r"Database Connections\\Test.sde"
 muniLoc = "Municipalities"
 luLoc = "Land_Use"
 tempLoc = "tempMuniLuRatio"
 arcpy.env.workspace = sdeConn

 try:
    print "MakeFeatureLayer_management lu_lyr"
    arcpy.MakeFeatureLayer_management(luLoc, "lu_lyr") 
    prematchcount = int(arcpy.GetCount_management("lu_lyr").getOutput(0)) 
    print "MakeFeatureLayer_management muni_lyr"
    #arcpy.MakeFeatureLayer_management(muniLoc, "muni_lyr") 
    print "SelectLayerByLocation_management COMPLETELY_WITHIN"
    arcpy.SelectLayerByLocation_management("lu_lyr", "COMPLETELY_CONTAINS",muniLoc,"","SWITCH_SELECTION")
     postmatchcount = int(arcpy.GetCount_management("lu_lyr").getOutput(0)) 
    if prematchcount == postmatchcount:
        print "SelectLayerByLocation_management DID NOT WORK"
    else:
        print "SelectLayerByLocation_management LOOKS GOOD"
        if arcpy.Exists(tempLoc):
            print "Delete_management "
            arcpy.Delete_management(tempLoc)
        print "CopyFeatures_management "
        arcpy.CopyFeatures_management('lu_lyr',tempLoc)
 except Exception:
    e = sys.exc_info()[1]
    print(e.args[0])

So I add

if prematchcount == postmatchcount: 

to see SWITCH_SELECTION worked or not.

every time it returns same result as source feature class.

Have I miss anything in my code?


Solution

  • TL;DR

    Change this:

    arcpy.SelectLayerByLocation_management("lu_lyr", "COMPLETELY_CONTAINS",muniLoc,"","SWITCH_SELECTION")
    

    To this:

    arcpy.SelectLayerByLocation_management("lu_lyr", "COMPLETELY_CONTAINS",muniLoc)
    arcpy.SelectLayerByLocation_management("lu_lyr", None, None, "", "SWITCH_SELECTION")
    

    Details

    GetCount_management and SelectLayerByLocation_management are working as documented.

    From Get Count:

    If a selection is defined on the input, the count of the selected rows is returned.

    From Select Layer By Location:

    SWITCH_SELECTION —Switches the selection. All records that were selected are removed from the selection, and all records that were not selected are added to the selection. The select_features and overlap_type parameters are ignored when this option is selected.

    Let me explain what your code is doing and why it is correct.

    arcpy.MakeFeatureLayer_management(luLoc, "lu_lyr")
    

    You create a feature layer with no selection. Let's say the Land_Use feature class has 42 features in it.

    prematchcount = int(arcpy.GetCount_management("lu_lyr").getOutput(0))
    

    Since no selection is defined on lu_lyr, all features in the feature class are counted, and prematchcount now equals 42.

    arcpy.SelectLayerByLocation_management("lu_lyr", "COMPLETELY_CONTAINS",muniLoc,"","SWITCH_SELECTION")
    

    Since you're using SWITCH_SELECTION, COMPLETELY_CONTAINS and muniLoc are ignored, and the selection is simply switched. Before this call, zero features were selected. This call switches the selection so that all 42 features are selected.

    postmatchcount = int(arcpy.GetCount_management("lu_lyr").getOutput(0))
    

    Since a selection is defined on lu_lyr, only the selected features are counted. The previous line selected all 42 features, so postmatchcount now equals 42.

    if prematchcount == postmatchcount:
    

    True. They are both 42.

    Your fix depends on what you want to do, which you did not say. My guess is that you want to select all features in Land_Use that do not completely contain a feature in Municipalities and copy those selected features to tempMuniLuRatio. If so, make the change described at the top of this answer. If not, please edit your question to explain what you want to do.