错误处理Python arcpy脚本所需的建议

时间:2012-10-12 18:16:32

标签: python arcgis arcpy

我可以使用一些关于如何处理Python脚本中的错误的建议。通过阅读本站点上的所有Python错误处理帖子,我能够收集的是,除了continue语句之外,您不能简单地绕过for循环中的错误。相反,您必须直接处理每个错误。这是我遇到问题的原因。我附加了在for循环中间发生的错误消息。另外,我附上了我的脚本,该脚本通过以下工作流程进行:

  1. 在栅格周围放置一个多边形
  2. 在多边形的平均中心放置一个点
  3. 使用该点来标识与其关联的特定县 相应的栅格
  4. 根据选定的县多边形
  5. 剪辑栅格

    如何将错误消息中的信息合并到try除continue语句之外,以便脚本可以移动到列表中的下一个栅格而不是在处理过程中停止?

    # Import arcpy module
    import arcpy
    from arcpy import env
    from arcpy.sa import *
    arcpy.CheckOutExtension("3D")
    
    # Set Over write
    arcpy.env.overwriteOutput = 1
    
    # Set the workspace
    env.workspace = r"Z:\temp.gdb"
    outworkspace = r"Z:\location2\temp2.gdb"
    
    # Local variables:
    counties = r"Z:\temp.gdb\boundaries\Counties"
    counties_lyr = arcpy.MakeFeatureLayer_management(counties,"counties_lyr")
    
    # Get the list of rasters to process
    raster_list = arcpy.ListRasters("*_clp")
    print raster_list
    
    for raster in raster_list:
        # Define name and location for output raster
        name = outworkspace + "\\" + str(raster)
    
        # Process: Raster Domain
        arcpy.RasterDomain_3d(raster, "in_memory/temp", "POLYGON")
    
        # Process: Central Feature
        arcpy.MeanCenter_stats("in_memory/temp", "in_memory/temp1")
    
        # Process: Select Layer By Location
        arcpy.SelectLayerByLocation_management(counties_lyr, "intersect", "in_memory/temp1", "", "NEW_SELECTION")
    
        # Clip Raster
        arcpy.Clip_management(raster, "#", name,counties_lyr, "#", "ClippingGeometry")
    
        # Delete in_memory
        arcpy.Delete_management("in_memory")
    
        print "processing " + raster + " complete..."
    
    print "All processing is now finished"
    

    enter image description here

4 个答案:

答案 0 :(得分:1)

你可以处理for循环中的错误。将代码包装在try except块中将允许您在参数无效的情况下处理错误。

try:

# Process: Raster Domain
    arcpy.RasterDomain_3d(raster, "in_memory/temp", "POLYGON")

except ExcecuteError: 
  continue

这将发生在你的循环中,它基本上说如果发生异常会发出continue,这意味着继续下一个循环迭代。

你可以将try except块放在任何地方,正如Mark Amery建议你可以在try中包装整个for循环体。

答案 1 :(得分:1)

这是你需要的吗?如果for循环中任何单个栅格出现任何问题,它将打印出回溯,然后转到下一个。

import traceback

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("3D")

# Set Over write
arcpy.env.overwriteOutput = 1

# Set the workspace
env.workspace = r"Z:\temp.gdb"
outworkspace = r"Z:\location2\temp2.gdb"

# Local variables:
counties = r"Z:\temp.gdb\boundaries\Counties"
counties_lyr = arcpy.MakeFeatureLayer_management(counties,"counties_lyr")

# Get the list of rasters to process
raster_list = arcpy.ListRasters("*_clp")
print raster_list

for raster in raster_list:
    try:
        # Define name and location for output raster
        name = outworkspace + "\\" + str(raster)

        # Process: Raster Domain
        arcpy.RasterDomain_3d(raster, "in_memory/temp", "POLYGON")

        # Process: Central Feature
        arcpy.MeanCenter_stats("in_memory/temp", "in_memory/temp1")

        # Process: Select Layer By Location
        arcpy.SelectLayerByLocation_management(counties_lyr, "intersect", "in_memory/temp1", "", "NEW_SELECTION")

        # Clip Raster
        arcpy.Clip_management(raster, "#", name,counties_lyr, "#", "ClippingGeometry")

        # Delete in_memory
        arcpy.Delete_management("in_memory")

        print "processing " + raster + " complete..."

    except:
        print "Something went wrong handling " + str(raster) + ". Here's a traceback:"
        traceback.print_exc()
        continue

print "All processing is now finished"

这是一个懒惰的程序员的实现,将for循环的全部内容包装在try ... except ...块中,它将捕获块中任何类型的异常。根据你的需要和品味,你可能会发现它更有帮助(或者只是更优雅)来捕捉你在实践中遇到的特定错误,正如dm03514建议的那样 - 但是我认为对于像这样的小脚本,其输出是要被人类看待,你做什么并不重要。

正如您在问题中指定的那样,对您来说重要的是您在发生错误时会看到回溯。这就是traceback.print_exc()的用武之地。:)

答案 2 :(得分:1)

我不知道arcpy。但也许可以通过以下方式处理第969行中的异常:

try:
    your stuff

except arcpy.ExecuteError, error:
    print error

可以提供帮助。

答案 3 :(得分:1)

关于try / except / continue in for循环的内容并不十分清楚。以下内容基于您提供的代码。

def failing_func(raster):
    raise ValueError("raster <%s> function has failed" % (raster))

def working_func(raster):
    print("raster <%s> function has succeeded" % (raster))
    return True

raster_list = [0, 1, 2, 3, 4, 5]

for raster in raster_list:
    print("Processing <%s>" % (raster))

    try:
        v1 = working_func("raster_1")
        v2 = failing_func("raster_2")
        v3 = working_func("raster_3")
        v4 = failing_func("raster_4")
        v5 = working_func("raster_5")
    except ValueError, e:
        print("Exception was raised: %s when processing item %s" % (str(e), raster))
        continue

哪个输出:

Processing <0>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 0
Processing <1>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 1
Processing <2>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 2
Processing <3>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 3
Processing <4>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 4
Processing <5>
raster <raster_1> function has succeeded
Exception was raised: raster <raster_2> function has failed when processing item 5