如果不想批量处理,直接把dataname替换成需要处理的数据,把遍历的循环删掉。 需要替换的参数较多,没有写成函数…初学,写的不太好,供参考。
整体的思路,就是挑选出属性符合的线,计算这些线的长度,然后查询最大最小值,当满足长度等于最大值时,被挑选出来。最终的结果呢就是:选出符合条件的线里的最长的那个
import os
import arcpy
import np
from arcpy import env
from arcpy.sa import *
arcpy.env.workspace =""
path = ""
datanames = os.listdir(path)
for dataname in datanames:
if dataname.endswith(".shp"):
outpolyline = ""
print(dataname+"_800mmTABLE SQL")
arcpy.Select_analysis(dataname, outpolyline,'"CONTOUR" = 800')
arcpy.env.workspace =""
path =""
datanames = os.listdir(path)
for dataname in datanames:
if dataname.endswith(".shp"):
arcpy.AddField_management(dataname, "length", "SHORT", "", "", "#", "#", "#", "#")
print(dataname[-8:-4] + "_ADD_FIELD")
arcpy.CalculateField_management(dataname, "length", "!shape.geodesicLength@KILOMETERS!", "PYTHON_9.3")
field = "length"
max_value = []; k = 0
datanames = os.listdir(path)
for dataname in datanames:
if dataname.endswith(".shp"):
datapath = path + "/" + dataname
valuelist = []; i = 0
for row in arcpy.da.SearchCursor(datapath, "length"):
valuelist.append(row)
max_value.append(max(valuelist))
k += 1
max_value =np.array(max_value, dtype=int)
max_value = str([int(x) for item in max_value for x in item])
max_value = max_value[1:-1].split(',', k)
for i in range(20):
max_value[i]=max_value[i].strip()
print(max_value,len(max_value))
arcpy.env.workspace =""
path = ""
i=0
datanames = os.listdir(path)
for dataname in datanames:
if dataname.endswith(".shp"):
outpolyline = ""
print(dataname[-8:-4]+"_MAXTABLE SQL")
arcpy.Select_analysis(dataname, outpolyline,'"length"='+ max_value[i])
os.remove(""+dataname)
i+=1
print ("成功")
以以上!OwO
|