在ArcGIS地理处理结果窗口中向用户显示引发的错误消息?


13

是否可以在“地理处理结果”窗口中显示从自定义例外引发的打印语句?

我修改了一个脚本,发现该脚本从用户处获取两个输入,找到关联的功能,然后缩放到该功能。该工具工作正常。我试图通过自动格式化输入以使其具有正确数量的字符来使其更加用户友好。我添加了一个消息框,告知用户输入框中是否有太多字符,但是我使用easygui创建了该消息框(用于GUI的Python库)。相反,我想使用gp结果窗口显示消息。我以为我正确设置了raise和except语句的格式,但是该工具运行并且得到完整的结果,而不是错误消息。我尝试了esri的arcpy帮助,但没有得到我想要的结果。

class BadInputError(Exception):
    pass

# Import arcpy module
import arcpy, sys, string
from arcpy import env

try:
    def checkInput(inp):
        mNCount = len(inp)
        trigger = 0
        while trigger == 0:
            if  mNCount == 4:
                trigger = 1
            elif mNCount > 4:
                inp = ''
                trigger = -1
                raise BadInputError
            elif mNCount == 3:
                inp = '0' + inp
                trigger = 1
            elif mNCount == 2:
                inp = '00' + inp
                trigger = 1
            elif mNCount == 1:
                inp = '000' + inp
                trigger = 1
        return inp


    # Script arguments
    MapNumber = arcpy.GetParameterAsText(0)
    MapNumber = checkInput(MapNumber)
    ParcelNumber = arcpy.GetParameterAsText(1)
    ParcelNumber = checkInput(ParcelNumber)

    Expression = (""" "MAP" = '%s' AND "PARCEL" = '%s'""") %(MapNumber,ParcelNumber)

    # Local variables:
    GISData_GISDATA_CAD_PARCEL2009_WHOLE = "Database Connections\\GISUser.sde\\GISData.GISDATA.CAD_PARCEL2009_WHOLE"
    Output_Layer_Name = Expression
    Output_Layer = "ParcelSelection"

    # Process: Select Layer By Attribute
    arcpy.SelectLayerByAttribute_management("2009 Parcel Points", "NEW_SELECTION", Expression)

    # Zoom to Selected Features
    mxd = arcpy.mapping.MapDocument('CURRENT') 
    df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0] 
    df.zoomToSelectedFeatures()
    df.scale = 8000
    arcpy.RefreshActiveView()

except BadInputError:
    print "Too many characters.  Run again"

except Exception, e:
    # If an error occurred, print line number and error message  
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

Answers:


13

我认为您正在寻找arcpy.AddMessage

编写消息的四个ArcPy函数如下:

  • AddMessage(“ message”)-用于常规信息性消息(严重性= 0)。
  • AddWarning(“ message”)-用于警告(严重性= 1)。
  • AddError(“ message”)-错误(严重性= 2)。
  • AddIDMessage(MessageType,MessageID,AddArgument1,AddArgument2)-用于错误和警告(MessageType参数确定严重性)。

1
这就对了。谢谢。我认为我将使用arcpy.AddError
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.