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)