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
相同的结果。
我的代码中是否遗漏了任何内容?
最佳答案
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_management
和 SelectLayerByLocation_management
正在按文档说明工作。
来自Get Count :
If a selection is defined on the input, the count of the selected rows is returned.
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
。如果是这样,请进行此答案顶部描述的更改。如果没有,请编辑您的问题以解释您想要做什么。
关于python - SWITCH_SELECTION 不适用于 SelectLayerByLocation_management,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33354363/