arcpy项目实战将两两生成的最短路

白癜风分型 http://news.39.net/bjzkhbzy/180423/6185383.html

之前的两两点相连接的最短路径为单纯的pyhon代码,用户需要修改文件路径才能正常使用,为了给用户提供更多的方便,我将pyhon代码封装到arcgis的工具中,通过接受用户输入文件,实现可视的文件生成。

需要对代码进行封装,首先要需要用户选择输入的元素,程序运行的方法等,下面即为具体的分析。

接下来,讲一下这个封装过程。封装即为建立scripttool工具来实现复杂的gp运算。具体步骤为

新建toolbox工具,在其中添加script,然后导入pyhon.py的代码

根据python代码中需要输入的要素和顺序,依次定义类型和名称

确定生成工具,使用新建的工具对所需的要素进行选择,直接进行脚本运算即可

干货来了,python代码如下:

importos

importarcpy

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是其中一个字段

forshitincursor:

num=str(shit[1])#将class编号转换为字符串

out_name=num+"f.shp"#输出属性表中每条要素

#arcpy.CopyFeatures_management(row,"c:\\users\\wolfer\\desktop\\test\\new\\"+out_name)#将生成的要素复制一份

#printshp#输出要素名

arcpy.Select_analysis(shp,out_path+"\\"+out_name,"class"=\+num+\)#利用sql查询要素中的每一条字段,查询语句需要专制

other=arcpy.da.SearchCursor(shp,[shape

,class,市])

forrowinother:

ifrow[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")

#本地变量

#Localvariables:

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

#Foreachrow,evaluatetheWELL_YIELDvalue(indexposition

#of0),andupdateWELL_CLASS(indexpositionof1)

foroneincursor:

one[1]=one[0]

#Updatethecursorwiththeupdatedlist

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

#Foreachrow,evaluatetheWELL_YIELDvalue(indexposition

#of0),andupdateWELL_CLASS(indexpositionof1)

formincursor:

m[0]=shit[1]

m[1]=row[1]

#Updatethecursorwiththeupdatedlist

cursor.updateRow(m)

arcpy.RasterToPolyline_conversion(lujingshangge,line,"ZERO","0","SIMPLIFY","cost")

ifarcpy.Exists(alline)andarcpy.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)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这个样一个封装好的工具就能做好了

元凿坊



转载请注明地址:http://www.sanbaicaoasb.com/sctx/8427.html
  • 上一篇文章:
  • 下一篇文章:
  • 热点文章

    • 没有热点文章

    推荐文章

    • 没有推荐文章