1

How do you use python in a ArcGIS 10.3 script to list/print the shapefiles with a message from the result of a geoprocessing tool in the progress window/dialog box. For example;

abc.shp
1234.shp
zyx.shp
987.shp

The above 4 datasets have been projected to: WGS_1984_World_Mercator and saved in C:\GIS_Result

I have been looking at this example; http://resources.arcgis.com/en/help/main/10.2/index.html#//03q30000000q000000

  • 1
    Welcome to GIS SE! Would you be able to edit your question to include details of the GIS software and version that you are using to try and do this, please? Also, can you include details of what you have tried so far. For example, if you are using ArcGIS for Desktop to write a Python script tool then I suspect your code should include arcpy.ListFeatureClasses(). – PolyGeo Apr 13 '15 at 3:34
  • What sort of dialog box? Python does not natively have a dialog/message box interface, are you trying to print the names to be sure you're getting them all? – Michael Stimson Apr 13 '15 at 3:40
  • @MichaelMiles-Stimson I think the question is referring to the geoprocessing progress dialog that shows while an ArcGIS for Desktop tool is running in the foreground. – PolyGeo Apr 13 '15 at 4:09
2

I'm going to assume that you are using ArcPy from ArcGIS for Desktop to write a Python script tool.

To send any messages to the geoprocessing progress dialog you can use

arcpy.AddMessage() 

For example:

arcpy.AddMessage("abc.shp")

or:

fc = "abc.shp"
arcpy.AddMessage(fc)
  • works for me, there's also AddWarning and AddError. If you want to go to the next level then you can use open("file name",'w') and write() functions to write to a log file. Like I said previously there's no forms installed by default, that doesn't mean there aren't any, pyWin and QT both provide good options (and I'm sure there's more) of throwing up a dialog/message box without too much effort. – Michael Stimson Apr 13 '15 at 4:41
0

The arcpy.AddMessage is bread and butter for adding your own messages in ArcToolbox Scripts tools. Also you can get the geoprocessing messages with arcpy.GetMessage/s. These are very powerful when used in: if else and try statements.

Adding A Script Tool

    import arcpy
    import os

    arcpy.AddMessage(" ")
    arcpy.AddMessage("===================================================================")
    arcpy.AddMessage(" ")
    arcpy.AddMessage(" Data Walk: Listings Rasters & Repair Standalone Feature Classes")
    arcpy.AddMessage(" ")


    FolderOrWorkspace = arcpy.GetParameterAsText(0)

    RasterCount = 0
    FeatureClassCount = 0

    DataList = []
    for dirname, dirnames, filenames in os.walk(FolderOrWorkspace):
        for subdirname in dirnames:
            arcpy.env.workspace = os.path.join(dirname, subdirname)
            RasterList = arcpy.ListRasters()
            for raster in RasterList:
                RasterCount = (RasterCount +1)

                arcpy.AddMessage("       - RASTER:" + str(RasterCount) +" Name: "+raster +" Within: "+subdirname)
                desc = arcpy.Describe(raster)
                SR = desc.spatialReference
                try:
                    arcpy.AddMessage("         - Band Count:        %d" % desc.bandCount)
                except:
                    arcpy.AddMessage("         - Does Not Support Bands")
                try:
                    arcpy.AddMessage("         - Compression Type:  %s" % desc.compressionType)
                except:
                    arcpy.AddMessage("         - Does Not Support Compression")
                try:
                    arcpy.AddMessage("         - Raster Format:     "+desc.format)
                except:
                    arcpy.AddError("          - Raster Format: Error") #Error Message

                arcpy.AddMessage("         - Spatial Reference: "+SR.name)
                arcpy.AddMessage("         - Reference Type:    "+SR.type)
                arcpy.AddMessage(" ")

            FeatureClassList = arcpy.ListFeatureClasses()
            for Feature in FeatureClassList:
                desc = arcpy.Describe(Feature)
                FeaturePath = os.path.join(desc.path,Feature)
                SR = desc.spatialReference
                FeatureClassCount = (FeatureClassCount +1)
                arcpy.AddMessage("       - FEATURE CLASS:" + str(FeatureClassCount) +" Name: "+Feature +" Within: "+subdirname)
                arcpy.AddMessage("         - Shape Type:        "+desc.shapeType)
                arcpy.AddMessage("         - Spatial Reference: "+SR.name)
                arcpy.AddMessage("         - Reference Type:    "+SR.type)
                try:
                    arcpy.RepairGeometry_management(FeaturePath, "DELETE_NULL" )
                    MesseageText = arcpy.GetMessages(1)
                    if MesseageText =="":
                        arcpy.AddMessage("         - Repair: geometry is good: " )
                    else:
                        arcpy.AddMessage("         - Repair: was poor: " )
                        arcpy.AddMessage(" ")
                        arcpy.AddMessage(str(MesseageText))
                        arcpy.AddMessage(" ")
                except Exception as e:
                    arcpy.AddError(e.message) # prints the error messages(ie the geometry is empty)

                arcpy.AddMessage(" ")

    arcpy.AddMessage("===================================================================")

    #arcpy.GetMessages(0) # prints all the messages
    #arcpy.GetMessages(1) # prints warning messages
    #arcpy.GetMessages(2) # prints error messages

Screen Grab of GetMessages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.