我创建了一个python程序,该程序使用ArcGIS的“ CostPath”函数自动在shapefile“ selected_pa​​tches.shp”中包含的多个多边形之间建立成本最低的路径(LCP)。我的python程序似乎可以运行,但是速度太慢。我必须构建275493个LCP。不幸的是,我不知道如何加快程序速度(我是Python编程语言和ArcGIS的初学者)。还是有另一个解决方案可以使用ArcGIS(我使用ArcGIS 10.1)快速计算多个面之间的最小成本路径?这是我的代码:

# Import system modules
 import arcpy
 from arcpy import env
 from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

 # Overwrite outputs
 arcpy.env.overwriteOutput = True

 # Set the workspace
 arcpy.env.workspace = "C:\Users\LCP"

 # Set the extent environment
 arcpy.env.extent = "costs.tif"


rowsInPatches_start = arcpy.SearchCursor("selected_patches.shp")

for rowStart in rowsInPatches_start:

ID_patch_start = rowStart.getValue("GRIDCODE")

expressionForSelectInPatches_start = "GRIDCODE=%s" % (ID_patch_start) ## Define SQL expression for the fonction Select Layer By Attribute

# Process: Select Layer By Attribute in Patches_start
arcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_start", expressionForSelectInPatches_start)

 # Process: Cost Distance
outCostDist=CostDistance("Selected_patch_start", "costs.tif", "", "outCostLink.tif")

# Save the output
outCostDist.save("outCostDist.tif")

rowsInSelectedPatches_end = arcpy.SearchCursor("selected_patches.shp")

for rowEnd in rowsInSelectedPatches_end:

    ID_patch_end = rowEnd.getValue("GRIDCODE")

    expressionForSelectInPatches_end = "GRIDCODE=%s" % (ID_patch_end) ## Define SQL expression for the fonction Select Layer By Attribute

    # Process: Select Layer By Attribute in Patches_end
    arcpy.MakeFeatureLayer_management("selected_patches.shp", "Selected_patch_end", expressionForSelectInPatches_end)

    # Process: Cost Path
    outCostPath = CostPath("Selected_patch_end", "outCostDist.tif", "outCostLink.tif", "EACH_ZONE","FID")

    # Save the output
    outCostPath.save('P_' +  str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")

    # Writing in file .txt
    outfile=open('P_' +  str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".txt", "w")
    rowsTxt = arcpy.SearchCursor('P_' +  str(int(ID_patch_start)) + '_' + str(int(ID_patch_end)) + ".tif")
    for rowTxt in rowsTxt:
        value = rowTxt.getValue("Value")
        count = rowTxt.getValue("Count")
        pathcost = rowTxt.getValue("PATHCOST")
        startrow = rowTxt.getValue("STARTROW")
        startcol = rowTxt.getValue("STARTCOL")
        print value, count, pathcost, startrow, startcol
        outfile.write(str(value) + " " + str(count) + " " + str(pathcost) + " " + str(startrow) + " " + str(startcol) + "\n")
    outfile.close()


非常感谢您的帮助。

最佳答案

写入磁盘所需的速度与计算成本可能会成为瓶颈,请考虑添加一个线程来处理所有写入操作。

这个:

for rowTxt in rowsTxt:
        value = rowTxt.getValue("Value")
        count = rowTxt.getValue("Count")
        pathcost = rowTxt.getValue("PATHCOST")
        startrow = rowTxt.getValue("STARTROW")
        startcol = rowTxt.getValue("STARTCOL")
        print value, count, pathcost, startrow, startcol
        outfile.write(str(value) + " " + str(count) + " " + str(pathcost) + " " + str(startrow) + " " + str(startcol) + "\n")


通过将rowsTxt设为全局变量,并使线程从rowsTxt写入磁盘,可以将其转换为线程函数。
完成所有处理后,您可以再添加一个全局布尔值,以便在完成所有编写操作后可以终止线程函数,并可以关闭线程。

我当前使用的示例线程函数:

import threading
class ThreadExample:
   def __init__(self):
      self.receiveThread = None

   def startRXThread(self):
      self.receiveThread = threading.Thread(target = self.receive)
      self.receiveThread.start()

   def stopRXThread(self):
      if self.receiveThread is not None:
         self.receiveThread.__Thread__stop()
         self.receiveThread.join()
         self.receiveThread = None

   def receive(self):
       while true:
          #do stuff for the life of the thread
          #in my case, I listen on a socket for data
          #and write it out


因此,对于您的情况,您可以将一个类变量添加到线程类中

self.rowsTxt


然后更新您的接收以检查self.rowsTxt,如果它不为空,请按照我上面从您那里获取的代码片段中的方式进行处理。处理完之后,将self.rowsTxt设置回None。您可以使用主要功能更新线程self.rowsTxt,因为它会获得rowsTxt。考虑为self.rowsTxt使用类似于列表的缓冲区,这样您就不会错过编写任何内容的机会。

08-25 03:03