1

Is there a way to use a "for" loop to print the success messages for all of the following geoprocesses? In this example, I am clipping the entire river system of Kansas to a pre-selected ecoregion, and then calculating the total miles of rivers within that ecoregion.

You can see that I have used the "print" function to display a custom completed message after each geoprocess succeeds, but that can be a lot of work; I think a "for" loop would work better.

You'll notice that I've used the arcpy.GetMessageCount() to print the last message, but it only prints the last message of the last geoprocessing item (I believe)

Here's the code for what I've tried. Once again, I would like to get rid of the print statements underneath each geoprocessing step:

import arcpy, sys
from arcpy import env
env.workspace = "C://whatever_workspace"
env.overwriteOutput = True

inFc = "ks_ecoregions.shp"
clipFc = "ks_major_rivers.shp"
outTable = "out_table"

#Processes
try:
    #Check out extension
    arcpy.CheckOutExtension("Spatial")
    print "Extension checked out\n"

    #Make feature layer
    arcpy.MakeFeatureLayer_management(inFc, "ks_ecoregions_layer")
    print "Feature Layer Made\n"

    #Select ecoregion
    arcpy.Select_analysis("ks_ecoregions_layer", "ks_ecoregions_select", where_clause = "US_L3NAME = 'Flint Hills'")
    print "Region selected\n"

    #Perform buffer around the selected ecoregion
    arcpy.Buffer_analysis("ks_ecoregions_select", "buffer_layer", "10 Kilometers")
    print "Buffer created\n"

    #Clip rivers to region
    arcpy.Clip_analysis(clipFc, "ks_ecoregions_select", "riverClip")
    print "Clip created\n"

    #Add table to attributes, converting "kilometers" to "miles
    arcpy.AddGeometryAttributes_management("riverClip", "LENGTH", "MILES_US")
    print "Table added\n"

    #Get summary of total miles of rivers in selected region
    arcpy.Statistics_analysis("riverclip", outTable, [["LENGTH", "SUM"]])
    print "Summation computed\n"

    #Delete intermediate data
    arcpy.Delete_management("ks_ecoregions_layer")
    print "Intermediate feature class deleted\n"
    arcpy.Delete_management("ks_ecoregions_select")
    print "Intermediate feature class deleted\n"
    arcpy.Delete_management("buffer_layer")
    print "Intermediate feature class deleted\n"

    #Get projection of original dataset
    desc = arcpy.Describe("riverClip")
    print "Data information for original projection: \nName: {0} \nData Type: {1} \nProjection: {2}\n".format(desc.name, desc.datasetType, desc.spatialReference.name)

   #Check extension back in
    arcpy.CheckInExtension("Spatial")
    print "Extension checked back in"
except arcpy.ExecuteError:
    pass

severity = arcpy.GetMaxSeverity()

if severity == 2:
    print "Error occurred: \n{0}".format(arcpy.GetMessages(2))
elif severity == 1:
    print "Warning raised: \n{1}".format(arcpy.GetMessages(1))
else:
    count = arcpy.GetMessageCount()
    print arcpy.GetMessage(count-1)
  • 2
    This looks like a use case for Python (rather than ArcPy) functions so I suggest using Stack Overflow and other resources to learn how to define and call them. Also, I recommend not using try/except in code presented here - see gis.meta.stackexchange.com/questions/4312/… – PolyGeo Oct 17 at 21:57
  • 1
    I suspect the function you would have to write to accomplish this would not be worth the time, and may not save you any lines of code. There are only 13 print statements. Even if you function ends up being only 5 lines, will it be worth the time spent to reduce your code by 8 lines? You'd also be adding unnecessary complexity to your code. – jbalk Oct 17 at 23:43
2

You could use Result.GetMessage/GetMessages, instead of arcpy.GetMessage/GetMessages

For example:

import arcpy

def log(result):
        # Last message
        print(result.getMessage(result.messageCount-1))

        # Or all messages...
        # print(result.getMessages())

log(arcpy.SomeTool_toolbox(some_input))
log(arcpy.AnotherTool_toolbox(some_input))
log(arcpy.DifferentTool_toolbox(some_input))

Or:

import arcpy

results = []

results.append(arcpy.SomeTool_toolbox(some_input))
results.append(arcpy.AnotherTool_toolbox(some_input))
results.append(arcpy.DifferentTool_toolbox(some_input))

for result in results:
    print(result.getMessage(result.messageCount-1))

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.