在多个多边形之间构建成本最低的路径的Python脚本:如何提高速度?

在多个多边形之间构建成本最低的路径的Python脚本:如何提高速度?,python,arcgis,arcpy,Python,Arcgis,Arcpy,我创建了一个python程序,它使用ArcGIS的函数“CostPath”在shapefile“selected_patches.shp”中包含的几个多边形之间自动构建最小成本路径(LCP)。我的python程序似乎可以工作,但速度太慢了。我必须建造275493个LCP。不幸的是,我不知道如何加速我的程序(我是Python编程语言和ArcGIS的初学者)。或者,是否有其他解决方案可以使用ArcGIS快速计算多个多边形之间的最小成本路径(我使用ArcGIS 10.1)?这是我的密码: # Impo

我创建了一个python程序,它使用ArcGIS的函数“CostPath”在shapefile“selected_patches.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()

非常感谢你的帮助。

< p>写磁盘到计算成本的速度可能是一个瓶颈,考虑添加一个线程来处理所有的写入。

这:

可以通过使rowsText成为全局变量,并让线程从rowsText写入磁盘,将其转换为线程函数。 完成所有处理后,您可以拥有一个额外的全局布尔值,这样当您完成编写所有内容并关闭线程时,您的线程函数可以结束

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

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
因此,对于您的情况,您可以向thread类添加一个类变量

self.rowsTxt

然后更新您的receive以检查self.rowsText,如果它不是空的,请像我在上面从您那里获取的代码片段中那样处理它。处理后,将self.rowsText设置回无。您可以使用main函数更新threads self.rowsTxt,因为它获取了rowsTxt。考虑使用一个类似于Self.RoStxt的缓冲区列表,这样您就不会错过写入任何东西。

< P>最快的更改,您可以使其显著提高速度,即切换到(例如,代码> ARCYP.DA.SKCHCURSORE())/>代码>。为了说明这一点,我在前一段时间运行了一个基准测试,以查看数据访问游标与旧游标相比的性能

附图显示了新da方法UpdateCursor与旧UpdateCursor方法的基准测试结果。基本上,基准测试执行以下工作流:

  • 创建随机点(10、100、1000、10000、100000)
  • 从正态分布中随机抽取样本,并向新样本添加值 带有光标的随机点属性表中的列
  • 对新场景和新场景的每个随机点场景运行5次迭代 旧的UpdateCursor方法,并将平均值写入列表
  • 绘制结果


  • 非常感谢亚伦的回答。我不知道它是否正确,但在上面的代码中,我用arcpy.da.SearchCursor(“selected_patches.shp”)替换了
    中的rowsInPatches\u start=arcpy.SearchCursor(“selected_patches.shp”),(“*”)作为rowsInPatches\u start:
    ID_patch\u start=rowStart.getValue(“GRIDCODE”)
    by
    ID_patch\u start=int(rowStart[3])
    。不幸的是,我不知道为什么我的脚本还太长。谢谢你的帮助。
    self.rowsTxt
    
    import arcpy, os, numpy, time
    arcpy.env.overwriteOutput = True
    
    outws = r'C:\temp'
    fc = os.path.join(outws, 'randomPoints.shp')
    
    iterations = [10, 100, 1000, 10000, 100000]
    old = []
    new = []
    
    meanOld = []
    meanNew = []
    
    for x in iterations:
        arcpy.CreateRandomPoints_management(outws, 'randomPoints', '', '', x)
        arcpy.AddField_management(fc, 'randFloat', 'FLOAT')
    
        for y in range(5):
    
            # Old method ArcGIS 10.0 and earlier
            start = time.clock()
    
            rows = arcpy.UpdateCursor(fc)
    
            for row in rows:
                # generate random float from normal distribution
                s = float(numpy.random.normal(100, 10, 1))
                row.randFloat = s
                rows.updateRow(row)
    
            del row, rows
    
            end = time.clock()
            total = end - start
            old.append(total)
    
            del start, end, total
    
            # New method 10.1 and later
            start = time.clock()
    
            with arcpy.da.UpdateCursor(fc, ['randFloat']) as cursor:
                for row in cursor:
                    # generate random float from normal distribution
                    s = float(numpy.random.normal(100, 10, 1))
                    row[0] = s
                    cursor.updateRow(row)
    
            end = time.clock()
            total = end - start
            new.append(total)
            del start, end, total
        meanOld.append(round(numpy.mean(old),4))
        meanNew.append(round(numpy.mean(new),4))
    
    #######################
    # plot the results
    
    import matplotlib.pyplot as plt
    plt.plot(iterations, meanNew, label = 'New (da)')
    plt.plot(iterations, meanOld, label = 'Old')
    plt.title('arcpy.da.UpdateCursor -vs- arcpy.UpdateCursor')
    plt.xlabel('Random Points')
    plt.ylabel('Time (minutes)')
    plt.legend(loc = 2)
    plt.show()