2015-10-19 56 views
1

我在ArcGIS中编写了一个python脚本,用于选择相交的功能。它需要不断重复,直到选择了所有相关功能。此时选择将停止改变。是否可以设置一个循环来保持重复,直到所选功能的数量与最后一次循环的时间相同为止?我可以使用arcpy.GetCount_management()方法获得选定的功能。我可以设置python while循环来运行,直到没有任何更改?

我给自己定的选择要素的数量是一个变量:

selectCount = arcpy.GetCount_management("StreamT_StreamO1") 

那么这是

mylist = [] 
with arcpy.da.SearchCursor("antiRivStart","ORIG_FID") as mycursor: 
    for feat in mycursor: 
     mylist.append(feat[0]) 
     liststring = str(mylist) 
     queryIn1 = liststring.replace('[','(') 
     queryIn2 = queryIn1.replace(']',')') 

arcpy.SelectLayerByAttribute_management('StreamT_StreamO1',"ADD_TO_SELECTION",'OBJECTID IN '+ queryIn2) 

arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","StreamT_StreamO1","","ADD_TO_SELECTION") 

所以我想要做的实际上将:

while selectcount == previousselectcount: 
     do stuff 

但我不知道while循环应该如何构造

回答

1

你很接近你将如何监视功能数量的变化。考虑以下。

previousselectcount = -1 
selectcount = arcpy.GetCount_management("StreamT_StreamO1") 
while selectcount != previousselectcount: 
     do stuff 
     # update both counts at the end of what you want to do in the while loop 
     previousselectcount = selectcount 
     selectcount = arcpy.GetCount_management("StreamT_StreamO1") 

注意while循环条件中的不等号运算符(!=)。

0

python wiki

如果selectcount或previousselectcount是float类型的,你可能想要做一个范围 又名

while selectcount >= previousselectcount+c: 
     .... 

用C正常数非常接近于零。