【arcpy項目實戰】將兩兩生成的最短路徑pyhon代碼封裝入script中,作爲arcgis的工具直接調用

【需求】之前的兩兩點相連接的最短路徑爲單純的pyhon代碼,用戶需要修改文件路徑才能正常使用,爲了給用戶提供更多的方便,我將pyhon代碼封裝到arcgis的工具中,通過接受用戶輸入文件,實現可視的文件生成。

【分析】需要對代碼進行封裝,首先要需要用戶選擇輸入的元素,程序運行的方法等,下面即爲具體的分析。

 接下來,講一下這個封裝過程。封裝即爲建立script tool工具來實現複雜的gp運算。具體步驟爲

1.新建toolbox工具,在其中添加script,然後導入pyhon.py的代碼

2.根據python代碼中需要輸入的要素和順序,依次定義類型和名稱

3.確定生成工具,使用新建的工具對所需的要素進行選擇,直接進行腳本運算即可

乾貨來了,python代碼如下:

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)



這個樣一個封裝好的工具就能做好了

如果有需要更加詳細交流的可以加微信taohualangzili,如果有地理編程需求的可以訪問淘寶網站https://shop525251073.taobao.com/?spm=a230r.7195193.1997079397.2.ESlFER,直接與店家聯繫。此貼由元鑿坊獨創,如需轉載請註明出處,謝謝。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章