gpt4 book ai didi

python - SWITCH_SELECTION 不适用于 SelectLayerByLocation_management

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:11 27 4
gpt4 key购买 nike

 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_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")

您创建了一个没有选择的要素图层。假设 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_SELECTIONCOMPLETELY_CONTAINSmuniLoc 将被忽略,并且选择被简单地切换。在此调用之前,选择了零个功能。此调用会切换选择,以便选择所有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com