【Arcpy学习实践教程】将阻力栅格上的53各点两两相连,算出各两点间的最小路径
【需求】将阻力栅格上面的53个点两两相连,求出他们两点间的最小距离
【解决初步思路】利用arcgis中的最小距离函数计算每两点间的最小阻力值,将生成栅格路径转化为矢量路径,将矢量路径进行链接,累加得到所有路径的合并路径
import os
import arcpy
arcpy.env.workspace="A:\\360data\\重要数据\\我的文档\\ArcGIS\\Packages\\最小树\\v101\\cost.gdb"#工作空间
shp="A:\\360data\\重要数据\\我的文档\\ArcGIS\\Packages\\最小树\\v101\\cost.gdb\\shi"
out_path="A:\\360data\\重要数据\\我的文档\\ArcGIS\\Packages\\最小树\\v101\\"
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:
cost = "cost"
te_po1 = "te_po1"
te_po2 = "te_po2"
julishange = "julishange"+shit[1]+"f"+row[1]+"t"
huisushange = "huisushange"+shit[1]+"f"+row[1]+"t"
lujingshangge = "lujingshangge"+shit[1]+"f"+row[1]+"t"
alline="alline"
tmp="tmp"
line="line"+shit[1]+"f"+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.DeleteFeatures_management(tmp)
arcpy.CopyFeatures_management(alline, tmp)
arcpy.DeleteFeatures_management(alline)
arcpy.Merge_management([line, tmp],alline)
最后实现将所有的最短路径合并。如果有机会将利用arcpy的相关工具制作一下add-in插件,使使用者用起来更加便捷。
转载自:https://blog.csdn.net/ldl250058/article/details/82147226