Using ArcHydro 2.0 in a python script

7644
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
MarkBoucher
Occasional Contributor III
I'm extremely interested in this too...
0 Kudos
PeterWilson
Occasional Contributor III
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


Hi Stacie

Just wondering if you ever resolved this.

Regards
0 Kudos
ChristineDartiguenave
Esri Contributor
1. First I would recommend upgrading to Arc Hydro 2.1. We did some cleanup for Python in there and you will also get some python scripts in the AH toolbox (see download instructions below).

2. We have precompiled the tools from the toolbox in a module called ArcHydroTools so that you do not have to redo the import each time which takes a long time... Then in your script you will need to add:

import ArcHydroTools

and call the tools with:
ArcHydroTools.AssignHydroID(tmpPolyFC, "OVERWRITE_YES")

Christine Dartiguenave
Esri Water Resources Team

Connection instructions for water resources applications
Location for latest development code for water resources applications developed by the ESRI�??s Professional Services group:

address:                  https://mft.esri.com
user:                        ADSRiverHydraulics
password:                ADSRiver.2012
port:                            22

Make sure you correctly login (case sensitive). You have 3 options to connect to the ftp site:

1. Web Client
   Site link:  https://mft.esri.com 
   (Video tutorials available on the main page, prior to log in)


2. Third party SFTP client
   ** Download a preconfigured client
https://mft.esri.com/EFTClient/Account/mft.zip **



3. FTP address to configure a third party SFTP client of your choice. There are plenty of good free ones �?? I personally like �??Filezilla�?�.

  Site address:  mft.esri.com
  Username: ADSRiverHydraulics
  Password: ADSRiver.2012
   Port:  22
0 Kudos
PeterWilson
Occasional Contributor III
Hi Christine

Thanks for the following and all the best for 2013.

Regards
0 Kudos
StacieWolny
New Contributor II
Woohoo!  That helped - I can now call ArcHydro tools within the python script.  Thanks, Christine! 

So far, most tools that I've tried ran fine (I'm working up to creating watersheds and sub-watersheds), except AdjointCatchment, where I got this error:

System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
   at ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.Execute(String Name, IVariantArray ipValues, ITrackCancel pTrackCancel)
   at ESRI.APWR.ApHydro.ArcHydroOp.AdjointCatchmentProcessing(ApLayers apLayers, Int32 debug, ITrackCancel& trackcancel, IGPMessages& messages, String& exMessage, String& tempwkdir) in C:\Builds\HydroSolutions\10.0_ArcHydro\Sources\ArcHydroTools\src\ApHydro\ArcHydroOp.vb:line 48825
Failed to execute (AdjointCatchment).

Catchment polygons and Drainage lines ran fine (although some of the resulting columns contain all zeroes like DrainID and NextDownID) and the adjoint catchment shapefile was created but only contains column names.  Any idea what's happening?
I can provide additional info if necessary, just let me know what's helpful. 

Thanks again...

~ Stacie
0 Kudos
StacieWolny
New Contributor II
Christine reminded me that we cannot use shapefiles for either input or output.  So I just got past the AdjointCatchment error by directing everything to a geodatabase, and the whole script works.

~ Stacie
0 Kudos
PeterWilson
Occasional Contributor III
Hi Stacie

Thanks for the update, I'm about to write a python script to automate the entire terrain preprocessing steps and some additional analysis from HEC-GeoHMS that I had in a ModelBuilder.

Regards
0 Kudos
StacieWolny
New Contributor II
Hi Peter -

I've got a basic working version of my script that goes from DEM to watersheds, including formatting a user's outlet point shapefile to have the fields required for batch points.  Attaching it here in case it's of use...

~ Stacie
CaseyBox
New Contributor II

Wonderfully written script! Thanks

0 Kudos