Skip to main content
 首页 » 编程设计

python之SWITCH_SELECTION 不适用于 SelectLayerByLocation_management

2024年10月24日13cmt

 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 是否有效。

每次返回与源特征 class 相同的结果。

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

请您参考如下方法:

长话短说

改变这个:

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_management 和 SelectLayerByLocation_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") 

您创建了一个没有选择的要素图层。假设 Land_Use 要素类中有 42 个要素。

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

由于在 lu_lyr 上没有定义任何选择,要素类中的所有要素都被计算在内,prematchcount 现在等于 42。

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

由于您使用的是 SWITCH_SELECTION,COMPLETELY_CONTAINS 和 muniLoc 将被忽略,并且选择被简单地切换。在此调用之前,选择了零个功能。此调用会切换选择,以便选择所有 42 个特征。

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

由于选择是在 lu_lyr 上定义的,因此仅计算所选功能。上一行选择了所有 42 个特征,因此 postmatchcount 现在等于 42。

if prematchcount == postmatchcount: 

没错。他们都是 42 岁。

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