0

我一直在对此进行一些研究,但是所有问题(在我的新手理解中)似乎都解决了如何确定另一个程序是否正在运行,而不是 Python 本身。

因此,我目前正在编写一个代码,该代码围绕 shapefile 中的一组行生成缓冲区。这是一个批处理过程,其中涉及 20 个 shapefile 和其中多达 4000 行。这个过程需要很长时间,我开始怀疑它是否真的有效。我通常使用打印语句来跟踪我的代码的进度,但在这种情况下,我调用 ArcGIS 来执行该功能,所以我可以编写的唯一打印语句是在处理完所有行之后,而不是在它仍在运行时.

有什么方法可以确定该过程是否仍在进行(除了检查任务管理器以查看它是否被冻结)?

我想到的疯狂的做法(不知道这样的事情是否可能):只要脚本还没有完成运行,我就在考虑每隔 X 分钟在 txt 文件中写一些东西。

谢谢!!!!

我的代码:

def Buffer30m(self,InputFile,OutputBuffer, size):    
   arcpy.Buffer_analysis(InputFile,OutputBuffer,size,"FULL","ROUND","NONE","#")


TheList=os.listdir(SSFlinespath)    #read the files in the folder
os.mkdir(SSFlines+"SSFbuffers"      #create folder for the output
SSbuff=SSFlinespath+"SSFbuffers/"   #select the folder as destination 
try:
    size=30
    for TheFile in TheList:                                          #Identify each file
        TheFileName, TheFileExtension = os.path.splitext(TheFile)    #Break up the file name
        if (TheFileExtension==".shp"):                               #Identify the shapefiles
            TheLines=SSFlines+TheFile
            ##Generate a 30m buffer around the lines
            TheBuffer=SSFbuff+TheFileName+"_buff30.shp"
            TheStepBuffer.Buffer30m(TheLines,TheBuffer,size)
            print "SSF buffer done"  
 except Exception as TheError:
    print "Error with the SSF forest area calculation"
    print TheError

更新 部分解决方案,完整代码和新问题:

我应用了 qwwqwwq 的建议,这对于我之前提到的功能非常有用。现在的问题是,当我运行脚本的其余部分(我之前展示的只是其中的一部分,因此您不必阅读太多内容)时,其他一些功能不起作用。

这是完整的脚本:

class StepBuffer:
 def GetPairInfo (self, MainFile, SourceFile, WantFields, SSF):
    fields= WantFields   #Fields I will keep
    ##Extract the info and add to the main table
    if SSF==False:
        GRHE_proj.GetFieldInfo(MainFile,SourceFile,"Pair", "Pair", fields)
    elif SSF==True:
        GRHE_proj.GetFieldInfo(MainFile,SourceFile,"SAMPLEID", "SAMPLEID", fields)

 def Buffer30m(self,InputFile,OutputBuffer, size):
    arcpy.Buffer_analysis(InputFile,OutputBuffer,size,"FULL","ROUND","NONE","#")

 def AreaBuff (self,InputFile, OutputTable):      
    arcpy.CalculateAreas_stats(InputFile,OutputTable)

 def AreaForest (self,BufferFile,ForestFile, OutputTable, SSF):
    if SSF==False:
        arcpy.TabulateIntersection_analysis(BufferFile,"Pair",ForestFile,OutputTable,"type","#","#","HECTARES")
    elif SSF==True:
        arcpy.TabulateIntersection_analysis(BufferFile,"SAMPLEID",ForestFile,OutputTable,"type","#","#","HECTARES")



TheList=os.listdir(SSFlinespath)    #read the files in the folder
os.mkdir(SSFlines+"SSFbuffers"      #create folder for the output
SSbuff=SSFlinespath+"SSFbuffers/"   #select the folder as destination

try:
    size=30
    for TheFile in TheList:                                          #Identify each file
    TheFileName, TheFileExtension = os.path.splitext(TheFile)    #Break up the file name
    if (TheFileExtension==".shp"):                               #Identify the shapefiles
        TheLines=SSFlines+TheFile
        ##Generate a 30m buffer around the lines
                t = threading.Thread(target=TheStepBuffer.Buffer30m, args=(TheLines,TheBuffer,size))
                t.start() 
                while (t.is_alive()):
                    time.sleep(2)  ## sleep so that we don't execute the print statement too often
                    print "I'm alive!"                  
                print "SSF buffer done"


                ##Calculate area of buffer
                TableAreaBuff=OutputTables+TheFileName+"_Area_Buffers.dbf"         
                TheStepBuffer.AreaBuff(TheBuffer,TableAreaBuff)
                print "SSF area buffer done"

                ##Calculate the area of the forest inside the buffer
                TableAreaFor=OutputTables+TheFileName+"_Area_forest.dbf"
                TheStepBuffer.AreaForest(TheBuffer,ForestPath,TableAreaFor,True)
                print "SSF area forest done"

                ##Add info of the area of the buffer to the buffer layer
                TheStepBuffer.GetPairInfo(TheBuffer,TableAreaBuff, "F_AREA",True)

                ##Add info of area of forest within the buffers to the buffer layer
                TheStepBuffer.GetPairInfo(TheBuffer,TableAreaFor,["Area","Percentage"],True)
                print TheFileName+"Done"
    except Exception as TheError:
        print "Error with the SSF forest area calculation"
        print TheError
        TheErrorFile.writelines(format(TheError)+"\n")

具体来说,当它到达时出现错误:

TheStepBuffer.AreaForest(TheBuffer,ForestPath,TableAreaFor,True)

产生此错误:

arcgisscripting.ExecuteError:
Traceback (most recent call last):
File "c:\program files (x86)\arcgis\desktop10.1\
ArcToolbox\Scripts\CalculateAreas.py", line 76, in <module>
setupCalcAreas()
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\CalculateAreas.py", line 31, in setupCalcAreas
calculateAreas(inputFC, outputFC)
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\CalculateAreas.py", line 55, in calculateAreas
cnt = UTILS.getCount(inputFC)
File "c:\program files (x86)\arcgis\desktop10.1\ArcToolbox\Scripts\SSUtilities.py", line 502, in getCount
return int(countObject.getOutput(0))
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 986, in getOutput ... (truncated)
return convertArcObjectToPythonObject(self._arc_object.GetOutput(*gp_fixargs(args))) ... (truncated)
File "C:\Users\Noelia Volpe\Dropbox\Project\analisis\Python_Codes\Scripts\MovementPref\volpe_project_v2.py", line 464, in <module>
  TheStepBuffer.AreaBuff(TheBuffer,TableAreaBuff)
File "C:\Users\Noelia Volpe\Dropbox\Project\analisis\Python_Codes\ReUsable\Movement_preferences.py", line 58, in AreaBuff
  arcpy.CalculateAreas_stats(InputFile,OutputTable)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\stats.py", line 1564, in CalculateAreas
  raise e

有谁知道这个错误的来源可能是什么?谢谢!

4

1 回答 1

2

我会尝试使用多线程:

import threading
import time
t = threading.Thread(target=TheStepBuffer.Buffer30m, args=(TheLines,TheBuffer,size))
t.start()

while (t.is_alive()):
    time.sleep(2)  ## sleep so that we don't execute the print statement too often
    print "I'm alive!"

您的线程将执行该函数TheStepBuffer.Buffer30m,控制线程将监视该线程以确保它仍在执行您的目标函数,并执行一条print语句让您知道一切正常。

于 2013-05-14T04:38:51.517 回答