Select to view content in your preferred language

Using ArcHydro 2.0 in a python script

7978
11
08-21-2012 01:28 PM
Labels (1)
StacieWolny
New Contributor II
Hi all -

I've used ArcHydro quite a bit over the years, done a lot of python geoprocessing too, but this is the first time I've tried to script ArcHydro in python.  I just don't seem to be able to run any ArcHydro tools from my python script, there's no explicit error as to why, just a generic script failure.  If I do ListToolboxes within the script, ArcHydro is there, if I do ListTools, the ArcHydro tools that I'm calling are there.  But when I try to run any of the tools, the script just fails.  This is with ArcGIS 10 and ArcHydro 2.0.

Since it looks like Arc already knows about ArcHydro (from being able to list the toolbox and tools), I'd think that I wouldn't need to use ImportToolbox (and would rather not have to, since I'm making a tool to provide to others so don't want to hard-code or ask for paths), but I tried that anyway and the script just hangs on this command:

arcpy.ImportToolbox("C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Arc Hydro Tools.tbx")

If I don't try to import, the first thing the script was doing is a Flow Direction:

workspace = arcpy.GetParameterAsText(0)
DEM = arcpy.GetParameterAsText(1)

interws = workspace + os.sep + "Intermediate" + os.sep
sshed_gdb = interws + "ssheds.gdb"
flow_dir = interws + "flow_dir"

arcpy.FlowDirection_archydro(DEM, flow_dir)

Maybe it doesn't like the flow_dir setting the whole path, since AH has its own default locations set, so try this:
arcpy.FlowDirection_archydro(DEM, "flow_dir")

Doesn't work.  Maybe it doesn't have its own default locations set, so try setting them:
arcpy.SetTargetLocations_archydro("HydroConfig", "Layers", interws, sshed_gdb)

Doesn't work, still no explicit error.  Try creating the gdb first, doesn't help.  Checked the XML Vector Location Type, it's set to 1 (gdb), Raster to 2 (can't find doc on that, but the RasterLocation is set to a folder by default, so I'm hoping 2 means folder.)  All of this works fine using these folders, file names and file types when I run the ArcToolbox tools manually outside of the script.

Put this through a debugger and get this error message:
AttributeError: 'module' object has no attribute 'FlowDirection_archydro'

I must be missing something basic.  Any clues as to what are greatly appreciated...

~ Stacie
Tags (2)
0 Kudos
11 Replies
PeterWilson
Regular Contributor
Hi Stacie

Thanks for the following, this is going to help greatly. I upload my final script once completed.

Regards
0 Kudos
xristosxristou
New Contributor
hello i need help

I have a proplem with arc hydro in arcpy,i want to create a simple script where automated some tools from archydro in arcpy.. I use arcmap 10.1 and python 2.7,my import is one dem and my export is a 6 raster file (fill,fdr,fac,cat,str,strlink) and run ok without proplem but if I add one more tool cachmented to polygon that converts a raster file to polygon feature class then show me error

ADDING AUTPUTS TO MAP FAILED TO EXECUTE SCRIPT

i import this python script on arcmap

import arcpy

arcpy.ImportToolbox("C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Toolboxes\Arc Hydro Tools.tbx", "archydrotools")
#Set Ouput Names
fill=arcpy.GetParameterAsText(0) + '\\fill'
Fdr=arcpy.GetParameterAsText(0) + '\\fdr'
Fac=arcpy.GetParameterAsText(0) + '\\fac'
Str=arcpy.GetParameterAsText(0) + '\\Str'
StrLnk=arcpy.GetParameterAsText(0) + '\\StrLnk'
Cat=arcpy.GetParameterAsText(0) + '\\Cat'
Catchment=arcpy.GetParameterAsText(0) + '\\Catchment'

# Local variables:
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
arcpy.env.workspace = arcpy.GetParameterAsText(0)
dem = arcpy.GetParameterAsText(1)
# Process: Fill Sinks
arcpy.FillSinks_archydrotools(dem, fill, "", "", "ISSINK_YES")
# Process: Flow Direction
arcpy.FlowDirection_archydrotools(fill, Fdr, "")
# Process: Flow Accumulation
arcpy.FlowAccumulation_archydrotools(Fdr, Fac)
# Process: Stream Definition
arcpy.StreamDefinition_archydrotools(Fac, "", Str, "1")
# Process: Stream Segmentation
arcpy.StreamSegmentation_archydrotools(Str, Fdr, StrLnk, "", "")
# Process: Catchment Grid Delineation
arcpy.CatchmentGridDelineation_archydrotools(Fdr, StrLnk, Cat)
# Process: Catchment Polygon Processing
arcpy.CatchmentPolyProcessing_archydrotools(Cat, Catchment)

ANY IDEA ?
0 Kudos