API Reference¶
This reference documents the Python API for the overflow library.
Core Functions¶
overflow.breach
¶
breach(input_path, output_path, chunk_size=DEFAULT_CHUNK_SIZE, search_radius=DEFAULT_SEARCH_RADIUS, max_cost=float('inf'), progress_callback=None)
Breach pits in a DEM using least-cost paths.
This function identifies pits (local minima) in the DEM and creates breach paths to allow water to flow out, minimizing the total elevation change.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the input DEM raster file. |
required |
output_path
|
str
|
Path for the output breached DEM raster file. |
required |
chunk_size
|
int
|
Size of processing chunks in pixels. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
search_radius
|
int
|
Maximum search radius for finding breach paths in cells. Default is 50. |
DEFAULT_SEARCH_RADIUS
|
max_cost
|
float
|
Maximum allowed cost (total elevation change) for breach paths. Default is infinity (no limit). |
float('inf')
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Source code in src/overflow/__init__.py
overflow.burn
¶
burn(dem_path, mask_path, output_path, method, mask_values=None, burn_values=None, statistic='min', burn_offset=0.0, connectivity=8, chunk_size=DEFAULT_CHUNK_SIZE, working_dir=None, progress_callback=None)
Burn elevations into a DEM inside the regions of a mask raster.
A region is a maximal connected set of cells that share the same mask value and whose value is selected. Cells of two different selected values stay in separate regions even where they touch. The statistic method gives each contiguous region its own statistic, computed over whole regions even when they straddle tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dem_path
|
str
|
Path to the input DEM raster file. |
required |
mask_path
|
str
|
Path to a binary or classified mask raster, co-registered with the DEM (same shape and geotransform). |
required |
output_path
|
str
|
Path for the output burned DEM raster file. Its data type and nodata value are carried over from the input DEM, so fractional burn values are truncated when the DEM has an integer type. |
required |
method
|
str
|
How to choose the elevation to burn. "constant" writes the burn value for the cell's mask value. "relative" subtracts the burn value from the DEM, preserving relief inside the region. "statistic" writes each region's own statistic of the DEM beneath it. |
required |
mask_values
|
str | list | tuple | None
|
Which mask values identify regions, as a comma separated string such as "1,3" or a sequence. If None, the keys of burn_values are used; if those are absent too, every non zero, non nodata mask value is selected and each distinct value still forms its own regions. |
None
|
burn_values
|
str | float | dict | None
|
Required for the constant and relative methods, ignored by the statistic method. Either a mapping of mask value to burn value, given as a string such as "1:225.5,3:210.0" or as a dict, or a single number applied to every selected mask value. |
None
|
statistic
|
str
|
Which statistic the statistic method computes per region, one of "min", "max" or "mean". Default is "min". |
'min'
|
burn_offset
|
float
|
Subtracted from each computed statistic, so statistic="min" with burn_offset=1.0 writes each region's minimum elevation less one. Only used by the statistic method. Default is 0.0. |
0.0
|
connectivity
|
int
|
8 (default) to treat diagonally touching cells as one region, or 4 to require a shared edge. |
8
|
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
working_dir
|
str | None
|
Directory for temporary files during tiled processing with the statistic method. If None, uses system temp directory. |
None
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the DEM and mask are not co-registered, if the DEM has no nodata value, or if any of the method, statistic, connectivity, mask value or burn value arguments are unrecognized or inconsistent. |
Note
DEM nodata cells are never written and are excluded from region statistics. A region lying entirely over DEM nodata is left untouched.
Source code in src/overflow/__init__.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
overflow.fill
¶
fill(input_path, output_path=None, chunk_size=DEFAULT_CHUNK_SIZE, working_dir=None, fill_holes=False, progress_callback=None)
Fill depressions in a DEM using priority flood algorithm.
This function fills local depressions (sinks) in the DEM to create a hydrologically conditioned surface where water can flow to the edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the input DEM raster file. |
required |
output_path
|
str | None
|
Path for the output filled DEM raster file. If None, the input file is modified in place. |
None
|
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing (suitable for smaller DEMs). Default is 2048. |
DEFAULT_CHUNK_SIZE
|
working_dir
|
str | None
|
Directory for temporary files during tiled processing. If None, uses system temp directory. |
None
|
fill_holes
|
bool
|
If True, also fills holes (nodata regions) in the DEM. |
False
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Source code in src/overflow/__init__.py
overflow.flow_direction
¶
flow_direction(input_path, output_path, chunk_size=DEFAULT_CHUNK_SIZE, working_dir=None, resolve_flats=True, progress_callback=None, flat_resolution_chunk_size_max=512)
Compute D8 flow directions from a DEM and optionally resolve flat areas.
This function calculates the steepest descent direction for each cell using the D8 algorithm, then optionally resolves flat areas to ensure continuous flow paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the input DEM raster file (should be hydrologically conditioned, e.g., filled or breached). |
required |
output_path
|
str
|
Path for the output flow direction raster file. |
required |
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
working_dir
|
str | None
|
Directory for temporary files during tiled processing. If None, uses system temp directory. |
None
|
resolve_flats
|
bool
|
If True (default), resolve flat areas in the flow direction raster to ensure water flows toward lower terrain. |
True
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
flat_resolution_chunk_size_max
|
int
|
Maximum chunk size for flat resolution processing. Default is 512. This caps the chunk size used during flat resolution to prevent performance issues in areas with large undefined flow regions. When chunk_size exceeds this value, flat resolution will use this smaller chunk size instead. |
512
|
Source code in src/overflow/__init__.py
overflow.accumulation
¶
accumulation(input_path, output_path, chunk_size=DEFAULT_CHUNK_SIZE, weights_path=None, weights_nodata_mode='zero', progress_callback=None)
Calculate flow accumulation from a flow direction raster.
This function computes the number of upstream cells that flow into each cell, representing drainage area in cell units. If a weights raster is given, it instead sums the weights raster over each cell's upstream contributing area (e.g. precipitation depth, imperviousness fraction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the input flow direction raster file. |
required |
output_path
|
str
|
Path for the output flow accumulation raster file. |
required |
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
weights_path
|
str | None
|
Optional path to a weights raster, co-registered with the flow direction raster (same shape and geotransform). When given, the output is a float64 weighted-sum accumulation raster instead of the default int64 cell-count raster. |
None
|
weights_nodata_mode
|
str
|
Only used when weights_path is given. "zero" (default) treats a nodata weight cell as contributing 0. "propagate" poisons that cell's accumulation and everything downstream of it with NaN. |
'zero'
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Source code in src/overflow/__init__.py
overflow.streams
¶
streams(fac_path, fdr_path, output_dir, threshold, chunk_size=DEFAULT_CHUNK_SIZE, progress_callback=None)
Extract stream networks from flow accumulation and direction rasters.
This function identifies stream cells based on a flow accumulation threshold and creates vector stream lines and junction points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fac_path
|
str
|
Path to the input flow accumulation raster file. |
required |
fdr_path
|
str
|
Path to the input flow direction raster file. |
required |
output_dir
|
str
|
Directory for output stream files (streams.gpkg will be created). |
required |
threshold
|
int
|
Minimum flow accumulation value (cell count) to define a stream. Cells with accumulation >= threshold are considered streams. |
required |
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Source code in src/overflow/__init__.py
overflow.basins
¶
basins(fdr_path, drainage_points_path, output_path, chunk_size=DEFAULT_CHUNK_SIZE, all_basins=False, fac_path=None, snap_radius=0, layer_name=None, progress_callback=None)
Delineate drainage basins from a flow direction raster and drainage points.
This function labels each cell with the ID of its downstream drainage point, effectively delineating basin boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fdr_path
|
str
|
Path to the input flow direction raster file. |
required |
drainage_points_path
|
str
|
Path to the drainage points vector file. |
required |
output_path
|
str
|
Path for the output basins raster file. |
required |
chunk_size
|
int
|
Size of processing chunks in pixels. Use chunk_size <= 1 for in-memory processing. Default is 2048. |
DEFAULT_CHUNK_SIZE
|
all_basins
|
bool
|
If True, label all basins including those not draining to specified points. Default is False. |
False
|
fac_path
|
str | None
|
Path to flow accumulation raster for snapping drainage points. If None, no snapping is performed. |
None
|
snap_radius
|
int
|
Radius in cells to search for nearest drainage point. If 0, no snapping is performed. |
0
|
layer_name
|
str | None
|
Name of the layer in the drainage points file to use. If None, uses the first layer. |
None
|
progress_callback
|
ProgressCallback | None
|
Optional callback function for progress reporting. Receives a float value between 0 and 1. |
None
|
Source code in src/overflow/__init__.py
overflow.flow_length
¶
flow_length(fdr_path, drainage_points_path, output_raster, output_vector=None, fac_path=None, snap_radius=0, layer_name=None)
Calculate upstream flow length (longest flow path) from drainage points.
This function calculates the distance from each cell to its downstream drainage point, measured along the flow path. The cell with the maximum flow length in each basin represents the longest flow path origin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fdr_path
|
str
|
Path to the input flow direction raster file. |
required |
drainage_points_path
|
str
|
Path to the drainage points vector file. |
required |
output_raster
|
str
|
Path for the output flow length raster file (GeoTIFF). Values represent upstream flow distance in map units (or meters for geographic CRS). |
required |
output_vector
|
str | None
|
Path for the output longest flow path vectors (GeoPackage). If None, vector output is not created. |
None
|
fac_path
|
str | None
|
Path to flow accumulation raster for snapping drainage points. If None, no snapping is performed. |
None
|
snap_radius
|
int
|
Radius in cells to search for maximum flow accumulation when snapping drainage points. If 0 or fac_path is None, no snapping. |
0
|
layer_name
|
str | None
|
Name of the layer in the drainage points file to use. If None, uses the first layer. |
None
|
Source code in src/overflow/__init__.py
Utilities & Types¶
overflow.FlowDirection
¶
Bases: IntEnum
D8 flow direction codes.
These codes represent the eight cardinal and intercardinal directions plus special values for undefined flow and nodata cells.
The numeric values correspond to the index in the neighbor offset array, starting from East (0) and going counter-clockwise.
| 3 | 2 | 1 |
|---|---|---|
| 4 | 8 | 0 |
| 5 | 6 | 7 |
overflow.ProgressCallback
¶
Bases: Protocol
Protocol for progress reporting callbacks.
This protocol implements a hierarchical progress structure designed to track long-running hydrological operations.
The hierarchy levels are:
- Phase: High-level operation (e.g., 'Breaching paths').
- Step: Named sub-operation within a phase (e.g., 'Process chunks').
- Message: Detail within a step (e.g., 'Chunk 28/36').
- Progress: Float (0.0-1.0) representing completion of the current step.
Implementations should be callable with the following signature:
def callback(
phase: str | None = None,
step_name: str | None = None,
step_number: int = 0,
total_steps: int = 0,
message: str = "",
progress: float = 0.0,
) -> None: ...
Where:
- phase: The name of the high-level phase. If
None, the previously set phase is preserved. - step_name: The name of the current step within the phase. If
None, the previously set step is preserved. - step_number: The current step number (1-indexed).
- total_steps: The total number of steps expected in this phase.
- message: A detailed status message regarding the current activity.
- progress: The normalized progress of the current step (0.0 to 1.0).