Delineator
Fast, accurate watershed delineation using hybrid vector- and raster-based methods and data from MERIT-Hydro
Install / Use
/learn @mheberger/DelineatorREADME
delineator.py
Python scripts for fast, accurate watershed delineation for any point on the Earth's land surface, using a hybrid of vector- and raster-based methods and the datasets MERIT-Hydro and MERIT-Basins.
Citation DOI: 10.5281/zenodo.7314287
Online demo: https://mghydro.com/watersheds/
(The web app is free and easy to use, and probably good enough for most users.)
Using these scripts
This repository includes sample data covering Iceland. To delineate watersheds in other locations, you will need to download datasets from MERIT-Hydro and MERIT-Basins. Instructions on how to get the data and run the script are provided below.
To get started, download the latest release from this GitHub repository. If you're a GitHub user, you can fork the repository.
These scripts were developed with Python version 3.9, and I later tested them with versions 3.10 and 3.11.
I recommend creating a Python virtual environment in which to run delineator.py. Here is a good introduction to virtual environments -- why they are useful, and how to use them. You can create and activate the virtual environment, then install all the required packages with the following commands.
Open the Terminal (Linux and Mac) or Command Prompt (Windows), cd to the directory containing delineator.py and related files, then enter these commands.
To create the virtual environment:
python -m venv venv
To activate the virtual environment:
# Windows Command Prompt or PowerShell
venv\Scripts\activate.bat
# Windows PowerShell
venv\Scripts\Activate.ps1
#Linux and MacOS venv activation
$ source venv/bin/activate
Next, install required packages:
$ pip install -r requirements.txt
This script uses the latest versions of Python packages circa November 2023, with one exception. I had to use an older version of GeoPandas (0.13.2 instead of 0.14), because of this: Issue #3054.
It seems that many of the libraries I'm using are under very active development, and upgrading packages usually breaks the code, requiring lots of careful debugging.
After this, follow the instructions below for how to get the input data, configure the settings, and run delineator.py.
Overview of using delineator.py
The major steps are the following, with more detailed instructions below.
- Download basin-scale MERIT-Hydro raster data (mghydro.com)
- Download MERIT-Basins vector data (reachhydro.com)
- Download simplified MERIT-Basins data (mghydro.com)
- Create a CSV file with your desired watershed outlet points
- Edit settings in
config.py - Run
delineator.pyto delineate watersheds - Review output
- Run again to fix mistakes (repeat steps 4 – 7)
Before you begin downloading data in steps 1 and 2, determine which files you need based on your region of interest. The data files are organized into continental-scale river basins, or Pfafstetter Level 2 basins. There are 61 of these basins in total. Basins are identified by a 2-digit code, with values from 11 to 91.

Detailed Instructions
<a name="step1">1. Download MERIT-Hydro raster data</a>
To delineate watersheds in the default "high-precision mode," you will need two sets of MERIT-Hydro gridded (or raster) data: flow accumulation and flow direction. The authors of the MERIT-Hydro created 5-degree tiles, but this script needs seamless layers that cover entire river basins. Download these data here: https://mghydro.com/watersheds/rasters
Update config.py to tell the script where to find these data files. Modify the variables:
MERIT_FDIR_DIR(for flow direction)MERIT_ACCUM_DIR(for flow accumulation)
2. <a name="step2">Download MERIT-Basins vector data</a>
Download the shapefiles for unit catchments and rivers. Follow the instructions here: https://www.reachhydro.org/home/params/merit-basins
In the folder pfaf_level_02 , download two sets of files:
- unit catchment shapefiles:
cat_pfaf_##_MERIT_Hydro_v07_Basins_v01.shp - river flowline shapefiles:
riv_pfaf_##_MERIT_Hydro_v07_Basins_v01.shp
In these files, ## is the Pfafstetter Level 2 basin code. See the figure above to determine which of the 61 level 2 basins you need, depending on your region of interest.
Unzip these files and save them to a folder on your hard drive. Then, in config.py, update the variables HIGHRES_CATCHMENTS_DIR and RIVERS_DIR.
<a name="step3">3. Download simplified MERIT-Basins data</a>
In "low-resolution" mode, the script will look for shapefiles with simplified unit catchment boundaries. I have created these files, and you can download them here:
https://mghydro.com/watersheds/share/catchments_simplified.zip
Unzip and save these files to a folder on your computer. In config.py, update the variable LOWRES_CATCHMENTS_DIR.
<a name="step4">4. Create a CSV file with your desired watershed outlet points</a>
The script reads information about your desired watershed outlet points from a plain-text comma-delimited (CSV) file. Edit this file carefully, as the script will not run if this file is not formatted correctly.
The CSV file must contain three required fields or columns, and another two optional fields that are useful when reviewing the results.
-
id - required: a unique identifier for your watershed or outlet point, an alphanumeric string. May be any length, but shorter is better. The script uses the id as the filename for output, so avoid using any forbidden characters. On Linux, do not use the forward slash /. On Windows, the list of forbidden characters is slightly longer (
\< \> : " / \ | ? \*). -
lat - required: latitude in decimal degrees of the watershed outlet. Avoid using a whole number without a decimal in the first row. For example, use 23.0 instead of 23.
-
lng - required: longitude in decimal degrees
-
name - optional: a name for the watershed outlet point. The text in this field should be surrounded by straight quotes. For example, "Elwha River at McDonald Bridge near Port Angeles, WA"
-
area - optional: if you have an a priori estimate of the watershed area, provide it here in square kilometers, km².
All latitude and longitude coordinates should be in decimal degrees (EPSG: 4326, https://spatialreference.org/ref/epsg/wgs-84/).
<a name="step5">5. Update config.py</a>
Read through the options and set the variables as you wish. Detailed instructions are below.
<a name="step6">6. Run delineator.py to delineate watersheds</a>
Once you have downloaded the datasets listed above, and updated config.py, you are ready to delineate watersheds.
Run delineate.py from your favorite Python IDE or from the command line:
>> python delineate.py
You will get a couple of warnings about an older version of the library Shapely. I haven't had time to migrate to Shapely 0.20, which will require some reprogramming.
<a name="step7">7. Review results</a>
The script can output watersheds in several different geodata formats, as long as it is supported by GeoPandas. Shapefiles are popular, but I recommend GeoPackage, as it is a more modern and open format. To get a full list of available formats, follow the directions here.
The script also can create web page to review your results on an interactive map. In config.py, set MAKE_MAP = True, and enter your desired file path in MAP_FOLDER. The script will create .js files for each watershed with the geodata in GeoJSON format readable by the web browser.
To view your watersheds, open the file _viewer.html in a web browser and click on a watershed ID. The table is sortable and searchable.
In the example dataset I included, the results for "Blanda River at Langamyri" appear to be totally wrong. I think there is a typo in the coordinates. The remainder of the results look quite reasonable.
<a name="step8">8. Run again to fix mistakes</a>
Automated watershed delineation often makes mistakes. The good news is that these errors can often be fixed by slightly moving the location of your watershed outlet.
Repeat steps 4 to 7 by creating a new CSV file, or modifying your existing file, using revised coordinates. The script will automatically overwrite existing files, so make sure to back up anything you want to save.
To quickly test different outlet locations, consider using the free Global Watersheds web app, which uses the same data and methods, but is often much faster: https://mghydro.com/watersheds/
Configuration Notes
Here are some more details on the variables in the file config.py.
Watershed Outlets CSV file
This GitHub repository contains example files to delineate several watersheds in Iceland. To delineate watersheds in other regions, follow the instructions above to download the input data files.
(The locations in outlets_sample.csv correspond to flow measurement gages in the GRDC database. I chose Iceland because the data files for this region are small. In addition, Iceland has some astonishingly beautiful rivers!)
You can create your own CSV file and name it anything you like. In config.py, enter the file name in the variable OUTLETS_CSV. Your CSV file should have a header row with these fields written exactly like this:
id, lat, lng, name, area
It does not matter
