SWITCH_SELECTION 不适用于 SelectLayerByLocation_management

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

所以我添加

if prematchcount == postmatchcount: 

查看 SWITCH_SELECTION 是否有效。

每次 returns 与源特征相同的结果 class

我的 代码 中是否遗漏了任何内容?

TL;DR

改变这个:

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

为此:

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

详情

GetCount_managementSelectLayerByLocation_management 正在按记录工作。

来自 Get Count:

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

来自 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.

让我解释一下您的代码在做什么以及为什么它是正确的。

arcpy.MakeFeatureLayer_management(luLoc, "lu_lyr")

您创建了一个没有 selection 的特征层。假设 Land_Use 特征 class 有 42 个特征。

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

由于 selection 没有在 lu_lyr 上定义,特征 class 中的所有特征都被计算在内,prematchcount 现在等于 42。

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

由于您使用的是 SWITCH_SELECTIONCOMPLETELY_CONTAINSmuniLoc 将被忽略,并且 selection 被简单地切换。在此调用之前,select 编辑了零个功能。此调用切换 selection,以便所有 42 个特征都被 selected。

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

由于 selection 是在 lu_lyr 上定义的,因此仅计算 selected 特征。上一行 select 编辑了所有 42 个特征,因此 postmatchcount 现在等于 42。

if prematchcount == postmatchcount:

没错。他们都是42.

您的解决方法取决于您想做什么,但您没有说。我的猜测是,您想要 select Land_Use 中不完全包含 Municipalities 中某个特征的所有特征,并将那些 selected 特征复制到 tempMuniLuRatio。如果是这样,请进行此答案顶部所述的更改。如果不是,请编辑您的问题以解释您想要做什么。