Select to view content in your preferred language

Error Trapping: ArcHydro LongestFlowPath

499
1
05-20-2013 09:31 AM
PeterWilson
Frequent Contributor
I'm looking for some assistance in error trapping. I have 650 watersheds that I need to calculate the Longest Flow Path for. I have incorporated the tool into a much larger model and currently its failing at Longest Flow Path. I would like to be able run the current tools and for any exception thrown by the tool print the name of the current watershed being processed and continue with the rest. At least then I can go back to see the watersheds that have thrown an exception to see why. Currently I have to manually look for which watershed is causing the error and rerun the tool from scratch.

import arcpy, ArcHydroTools
watshed = r'E:\Projects\H109355_2\ArcHydro\Model3\Nigeria03.gdb\Layers\Watershed'
fdr = r'E:\Projects\H109355_2\Archydro\GRID\layers.gdb\Fdr'
ArcHydroTools.LongestFlowpath(watshed,fdr,r'E:\Projects\H109355_2\ArcHyro\Model3\Nigeria03.gdb\Layers\LongestFlowPath')


I've tried the following approach, but overwriteoutput isn't working:

import arcpy, ArcHydroTools
outpath = arcpy.env.workspace = r'E:\Projects\H109355_2\ArcHydro\Model3\Nigeria03.gdb\Layers'
arcpy.env.overwriteOutput = True
fdr = r'E:\Projects\H109355_2\ArcHydro\GRID\Layers.gdb\Fdr'
watshed = "Watershed"
with arcpy.da.SearchCursor(watshed,("Name",)) as scur:
    for row in scur:
        cuindex = row[0]
        sqlexp = """{0} = '{1}'""".format(arcpy.AddFieldDelimiters(watshed,"Name"),cuindex)
        watshedind = arcpy.FeatureClassToFeatureClass_conversion(watshed,outpath,"watshed",sqlexp)
        try:
            longflowpath1 = ArcHydroTools.LongestFlowPath(watshedind,fdr,"in_memory\longestflowpath")
        except:
            arcpy.AddMessage
            print cuindex


Any help will be appreciated.

Regards
Tags (2)
0 Kudos
1 Reply
curtvprice
MVP Alum
You need to print a bit more:
import arcpy, ArcHydroTools
outpath = arcpy.env.workspace = \
     r'E:\Projects\H109355_2\ArcHydro\Model3\Nigeria03.gdb\Layers'
arcpy.env.overwriteOutput = True
fdr = r'E:\Projects\H109355_2\ArcHydro\GRID\Layers.gdb\Fdr'
watshed = "Watershed"
with arcpy.da.SearchCursor(watshed,("Name",)) as scur:
    for row in scur:
        cuindex = row[0]
        sqlexp = """{0} = '{1}'""".format(
             arcpy.AddFieldDelimiters(watshed,"Name"),cuindex)
        watshedind = arcpy.FeatureClassToFeatureClass_conversion(
             watshed,outpath,"watshed",sqlexp)
        try:
            longflowpath1 = ArcHydroTools.LongestFlowPath(
                watshedind,fdr,r"in_memory\longestflowpath")
        except Exception, msg:
            arcpy.AddWarning(
                "LongestFlowPath failed for cuindex {0}".format(cuindex) +
                 + "\n" + str(msg))
            
0 Kudos