import os
import arcpy
arcpy.env.workspace=arcpy.GetParameterAsText(0)#选取环境
cost =arcpy.GetParameterAsText(1) #成本栅格
shp=arcpy.GetParameterAsText(2) #选取要素的要素类
out_path=arcpy.GetParameterAsText(3) #命名字段裁剪后输出目录
cursor=arcpy.da.SearchCursor(shp,['shape@','class','市'])#shape@代表单个要输,class是其中一个字段
for shit in cursor:
num=str(shit[1]) #将class编号转换为字符串
out_name=num+"f.shp"#输出属性表中每条要素
#arcpy.CopyFeatures_management(row,"c:\\users\\wolfer\\desktop\\test\\new\\"+out_name)#将生成的要素复制一份
#print shp#输出要素名
arcpy.Select_analysis(shp,out_path+"\\"+out_name,'"class"=\''+num+ '\'')#利用sql查询要素中的每一条字段,查询语句需要专制
other=arcpy.da.SearchCursor(shp,['shape@','class','市'])
for row in other:
if row[1]>shit[1]:
out_name1=str(shit[1])+"f"+str(row[1])+"t.shp"
num1=str(row[1])
arcpy.Select_analysis(shp, out_path+"\\"+out_name1,'CLASS=\''+ num1+ '\'') # 利用sql查询要素中的每一条字段,查询语句需要专制
arcpy.CheckOutExtension("spatial")
#本地变量
# Local variables:
julishange = "juli"+shit[1]+"f"+row[1]+"t"
huisushange ="huisu"+shit[1]+"f"+row[1]+"t"
lujingshangge = "lujing"+shit[1]+"f"+row[1]+"t"
alline="alline"
tmp="tmp"
line="line"+str(shit[1])+"f"+str(row[1])+"t"
# Process: 成本回溯链接
arcpy.gp.CostBackLink_sa(out_path+"\\"+out_name, cost, huisushange, "", julishange)
# Process: 成本路径
arcpy.gp.CostPath_sa(out_path+"\\"+out_name1, julishange, huisushange, lujingshangge, "EACH_CELL", "FID")
arcpy.AddField_management(lujingshangge,"cost", "LONG")
cursor = arcpy.da.UpdateCursor(lujingshangge, ["PATHCOST", "cost"])
# For each row, evaluate the WELL_YIELD value (index position
# of 0), and update WELL_CLASS (index position of 1)
for one in cursor:
one[1] =one[0]
# Update the cursor with the updated list
cursor.updateRow(one)
# Process: 栅格转折线
arcpy.RasterToPolyline_conversion(lujingshangge,line, "ZERO", "0", "SIMPLIFY", "cost")
arcpy.AddField_management(line, "frm", "TEXT")
arcpy.AddField_management(line, "to", "TEXT")
cursor = arcpy.da.UpdateCursor(line, ["frm", "to"])
# For each row, evaluate the WELL_YIELD value (index position
# of 0), and update WELL_CLASS (index position of 1)
for m in cursor:
m[0] = shit[1]
m[1] = row[1]
# Update the cursor with the updated list
cursor.updateRow(m)
arcpy.RasterToPolyline_conversion(lujingshangge,line, "ZERO", "0", "SIMPLIFY", "cost")
if arcpy.Exists(alline) and arcpy.Exists(tmp): #判断是否为第一次循环
arcpy.Delete_management(tmp)
arcpy.CopyFeatures_management(alline, tmp)
arcpy.Delete_management(alline)
arcpy.Merge_management([line, tmp], alline)
else:
arcpy.CopyFeatures_management(line, tmp)
arcpy.CopyFeatures_management(line, alline)