Python script for watershed delineation with the USGS NLDI API

In November 2022, I posted a tutorial on how to use the US Geological Survey’s web service NLDI to delineated watersheds in the continental United States. Since then, they updated the API, and that old code no longer works. Here is an updated Python script. I’m using the library pynhd, which gives convenient access to the NLDI. You could also use the API directly with the requests library.

This NLDI gives access to data from the National Hydrography Dataset Plus version 2. This is an older version of the NHD that is no longer being updated. At this time, the USGS does not have an API to access the latest version of its geodata, the 3DHP. So if you wanted to use those data for watershed delineation, I suppose you would have to download the data and use GIS or write your own routines.

Let me know what you think. Would you be interested in a Python package that you could install with pip? What kind of output is most useful?

Download the script here: https://gist.github.com/mheberger/9b99aa7c185bbb070c9eff7f8e497091

Global Watersheds Updated with USGS Data and Methods

Now the Global Watersheds web app can use USGS data and methods (NLDI) to delineate watersheds in the continental United States. This means more accurate watersheds and flow paths in the US.

To access it, make sure to do a full reload of the page: https://mghydro.com/watersheds. In most browsers, you can hit: Ctrl + Shift + R or Ctrl + F5. 

To use the USGS option, click on Options, then under Data Source, select “USGS.” 

Here, the app is using a free API from the USGS called the Hydro Network Linked Data Index, or NLDI. This option was available a couple of years ago, but at one point, the USGS updated their API and it broke my code. It took me a while to get around to fixing it!

A few notes: Sometimes the USGS API will time out without returning results. I’ve set up the app to automatically retry 3 times, but if you get an error message, please wait a moment and try again. You can also try clicking on a slightly different point, like 0.001° from your original request. Be patient — their server can take a few minutes sometimes. 

I’ve also added another feature that should help you to get good results — the NHDv2 Flowlines layer on the map. (The National Hydrography Dataset version 2 is the dataset used by USGS for the NLDI.) In the map layer selector at the upper right, choose this layer at the bottom of the list. (See image below.)

To get good watershed delineation results, click on the flowline for the river whose watershed you are seeking. This will help snap the pour point correctly. This should help reduce the spurious small tributary watersheds that often come up. 

More about the National Hydrography Dataset

The NHD is “the nation’s water data backbone,” and gives access to geodata that has been compiled by state and federal agencies over the course of decades. Unfortunately, the version used by the NLDI is not the latest hydrography dataset created by the USGS. The naming of the different versions of the NHD is confusing, but as best as I can tell, the NLDI is based on NHD v2. This version was released in 2012, and was digitized from maps at the 1:100,000-scale. There is a later version called NHDPlus HR, for “high-resolution,” which was never quite completed for the whole nation. As of 2023, this is no longer being developed, and the USGS is now focused on creating a hydrography data product called 3DHP. There is not yet an API to access these datasets, but they are available for download.

One quirky thing about the NHD is that it is only for the continental United States, affectionately referred to as CONUS. So you will see missing data around the border with Mexico and Canada. For example, here is the watershed of the Tijuana River — the watershed boundaries are complete, but most of the rivers in Mexico are missing.

Let me know how this new feature works for you. I always like hearing from folks using the app. If you found the app helpful, consider supporting the site by buying me a coffee! ☕☺️