Fast, accurate watershed delineation for any point on Earth's land surface using hybrid vector/raster methods with MERIT-Hydro and MERIT-Basins datasets.
Citation: DOI 10.5281/zenodo.7314287
Online Demo: https://mghydro.com/watersheds/ (free, easy to use, good for most users)
Delineator is a Python CLI tool for watershed delineation that combines:
- High-resolution MERIT-Hydro raster data (flow direction and accumulation)
- MERIT-Basins vector data (unit catchments and river networks)
- Hybrid vector/raster algorithms for optimal speed and accuracy
The tool automatically downloads required data, handles large-scale batch processing, and outputs results in standard GIS formats.
git clone https://github.com/your-org/delineator.git
cd delineator
uv syncgit clone https://github.com/your-org/delineator.git
cd delineator
pip install -e .Requirements: Python 3.12+
# outlets.toml
[[outlets]]
gauge_id = "usgs_12345678"
lat = 47.6062
lng = -122.3321
gauge_name = "Green River near Seattle, WA" # optional# config.toml
[settings]
output_dir = "./output"
max_fails = 100 # optional
[[regions]]
name = "my_region"
outlets = "outlets.toml"delineator run config.tomlThe tool will automatically:
- Determine required MERIT-Hydro basins from outlet coordinates
- Download missing data
- Delineate watersheds
- Output shapefiles in
output/region=my_region/
delineator run config.toml
delineator run config.toml --dry-run # Validate config without processing
delineator run config.toml -o ./output # Override output directory
delineator run config.toml --max-fails 10 # Stop after 10 failures
delineator run config.toml --no-download # Fail if data is missing (no auto-download)
delineator run config.toml --fill-threshold 50 # Fill holes smaller than 50 pixels
delineator run config.toml --fill-threshold 0 # Fill ALL holes (no size limit)# Download by bounding box (min_lon,min_lat,max_lon,max_lat)
delineator download --bbox -125,45,-120,50 -o data/
# Download specific basins by Pfafstetter Level 2 code
delineator download --basins 71,72,73 -o data/
# Download only rasters (no Google Drive credentials needed)
delineator download --bbox -125,45,-120,50 --rasters-only
# Preview what would be downloaded
delineator download --bbox -125,45,-120,50 --dry-rundelineator list-basinsDisplays all 61 Pfafstetter Level 2 basin codes grouped by continent.
[settings]
output_dir = "./output" # Required: base output directory
data_dir = "~/data/merit-hydro" # Optional: path to MERIT-Hydro data (see below)
max_fails = 100 # Optional: stop after N failures (default: unlimited)
fill_threshold = 100 # Optional: fill holes smaller than N pixels (default: 100, 0 = fill all)
[[regions]]
name = "region_name" # Required: used for hive partitioning (region=name/)
outlets = "outlets.toml" # Required: path to outlets fileThe tool needs MERIT-Hydro data (rasters and vectors). You can configure where it looks for this data:
Option 1: Config file (per-project)
[settings]
data_dir = "~/data/merit-hydro"Option 2: Environment variable (global default)
export DELINEATOR_DATA_DIR="$HOME/data/merit-hydro"Fallback chain: config file > env var > {output_dir}/../data
For a central data directory used across all projects, add the env var to your shell profile (~/.zshrc):
export DELINEATOR_DATA_DIR="$HOME/data/merit-hydro"[[outlets]]
gauge_id = "unique_id" # Required: unique identifier (used in output filenames)
lat = 47.6062 # Required: latitude (decimal degrees, EPSG:4326)
lng = -122.3321 # Required: longitude (decimal degrees, EPSG:4326)
gauge_name = "River Name" # Optional: descriptive nameSee examples/ directory for complete configuration examples.
output/
├── region=my_region/
│ └── my_region.shp # Shapefile with all watersheds for this region
└── FAILED.csv # Log of failed outlets (if any)
Each watershed shapefile includes attributes:
gauge_id: Unique identifiergauge_name: Descriptive name (if provided)area: Watershed area (km²)country: Country code- Geometry: Watershed polygon
The tool uses data from:
- MERIT-Hydro: High-resolution flow direction and accumulation rasters (3-arcsecond, ~90m resolution)
- MERIT-Basins: Vector unit catchments and river networks
Data is organized by Pfafstetter Level 2 basins (61 continental-scale basins worldwide). The tool automatically downloads required data on first use.
For manual download or offline use:
- Rasters: https://mghydro.com/watersheds/rasters
- Vectors: https://www.reachhydro.org/home/params/merit-basins
Raster data (flow direction, accumulation) downloads automatically from mghydro.com — no credentials needed.
Vector data (MERIT-Basins catchments and rivers) is hosted on Google Drive and requires authentication. If you only need rasters, use --rasters-only to skip this setup.
The vector data is hosted in a Google Drive folder. Set this environment variable:
export MERIT_BASINS_FOLDER_ID="1owkvZQBMZbvRv3V4Ff3xQPEgmAC48vJo"This is the pfaf_level_02 folder from the MERIT-Basins bugfix1 release.
You need a service account to authenticate with Google Drive API.
Option A: Using gcloud CLI (recommended)
# If you have an existing project and service account:
gcloud iam service-accounts keys create ~/drive-credentials.json \
--iam-account=YOUR_SERVICE_ACCOUNT@YOUR_PROJECT.iam.gserviceaccount.com
# Or create everything from scratch:
gcloud projects create my-delineator-project --set-as-default
gcloud services enable drive.googleapis.com
gcloud iam service-accounts create delineator-sa \
--display-name="Delineator Service Account"
gcloud iam service-accounts keys create ~/drive-credentials.json \
--iam-account=delineator-sa@my-delineator-project.iam.gserviceaccount.comOption B: Using Google Cloud Console
- Go to Google Cloud Console
- Create or select a project
- Enable the Google Drive API
- Go to IAM & Admin → Service Accounts
- Create a service account and download the JSON key
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/drive-credentials.json"Add these lines to your ~/.zshrc or ~/.bashrc for persistence:
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/drive-credentials.json"
export MERIT_BASINS_FOLDER_ID="1owkvZQBMZbvRv3V4Ff3xQPEgmAC48vJo"Then reload: source ~/.zshrc
Test that everything works:
delineator download --basins 12 --dry-runThis should show the files that would be downloaded without errors.
src/delineator/
├── cli/ # Typer CLI (run, download, list-basins commands)
├── config/ # Pydantic configuration schema for TOML configs
├── core/ # Delineation logic (watershed algorithms, dissolve, raster ops)
└── download/ # MERIT-Hydro data download (HTTP and Google Drive)
uv run pytestuv run ruff format
uv run ruff check --fixSee CLAUDE.md for detailed development guidelines.
MIT License. See LICENSE file for details.
If you use this tool in research, please cite:
Heberger, M. (2022). delineator: Fast watershed delineation using MERIT-Hydro (Version 1.0.0) [Software]. Zenodo. https://doi.org/10.5281/zenodo.7314287
Original author: Matthew Heberger
Built using:
- MERIT-Hydro (Yamazaki et al., 2019)
- MERIT-Basins (Lin et al., 2021)
- GeoPandas, Shapely, pysheds