Core primitives#
Shared, hydrology-agnostic building blocks that every domain package rests on. Nothing here imports from the rest
of digitalrivers, which is what makes it safe for everything else to import from here.
Direction convention#
The 8-neighbour convention every routing, ordering and walk kernel indexes: 0=S, 1=SW, 2=W, 3=NW, 4=N, 5=NE,
6=E, 7=SE. It is exported in two shapes — a (col, row) dict and parallel (row, col) arrays — and the arrays
come in both int8 and int32, because Numba compiles a separate specialisation per argument dtype and the two
halves of the package use different ones.
digitalrivers.core.directions
#
The 8-neighbour direction convention shared by every routing, ordering, and walk kernel.
Direction codes run 0=S, 1=SW, 2=W, 3=NW, 4=N, 5=NE, 6=E, 7=SE. This is the convention
digital-rivers writes into rasters tagged DR_ENCODING="digitalrivers", and the one every
docstring in the package means when it says "DIR_OFFSETS order".
Two shapes of the same table are exported, because callers genuinely need different ones:
DIR_OFFSETS—{direction: (column_offset, row_offset)}. Note the(col, row)order: it is the transpose of the array form below. Both encode the same convention (direction0is south, i.e.row + 1); they differ only in argument order. Do not re-order either form —DEM.accumulate_flowandDEM.convert_flow_direction_to_cell_indicesunpack the tuples positionally.DIR_DR_* / DIR_DC_*— parallel(row, col)offset arrays, one pair per dtype. Numba compiles a separate specialisation per argument dtype, so theint8andint32variants are both real and neither is redundant: the vectorised routing kernels inflow._kernels.routingwork inint8, while every@njitkernel and every pure-Python walk works inint32. Pass the variant the call site already used — swapping one for the other silently changes a kernel's compiled signature.
INV_DIR[k] is the direction code a neighbour sitting at offset k would carry if it
flowed into the centre cell. Every upstream (reverse) walk indexes it.
These arrays are module-level and therefore shared by every importer. Treat them as
immutable — nothing in the package writes to them. They are deliberately left writable
rather than flagged read-only, because ndarray.flags.writeable is part of the type Numba
compiles against, and flipping it would change the signature of every @njit kernel that
takes them.
Not every 8-direction table in the package belongs here:
dem._kernels.morphometry.horizon_walk_kernel walks in azimuth order
(E, NE, N, NW, W, SW, S, SE), a different labelling that happens to cover the same eight
neighbours. It stays local to that kernel.
The forms are cross-checked by tests/core/test_directions.py; the examples below are
the same invariants written so they run as doctests.
Examples:
-
The dict form and the array form describe the same eight neighbours:
from digitalrivers.core.directions import DIR_OFFSETS from digitalrivers.core.directions import DIR_DR_I32, DIR_DC_I32 pairs = list(zip(DIR_DC_I32.tolist(), DIR_DR_I32.tolist())) [DIR_OFFSETS[k] for k in range(8)] == pairs True
-
Reversing a direction twice is the identity:
from digitalrivers.core.directions import INV_DIR INV_DIR[INV_DIR].tolist() == list(range(8)) True
-
And a direction's offsets cancel against its inverse's:
all( ... DIR_DR_I32[k] + DIR_DR_I32[INV_DIR[k]] == 0 ... and DIR_DC_I32[k] + DIR_DC_I32[INV_DIR[k]] == 0 ... for k in range(8) ... ) True
Result metadata#
The DR_* GeoTIFF tag keys and valid-value sets the typed rasters write, so a raster carries the routing scheme
and encoding that produced it and a downstream reader cannot silently misinterpret it.
digitalrivers.core.metadata
#
Shared metadata keys and valid-value sets for typed result classes.
These constants are the single source of truth for GeoTIFF metadata keys
written by FlowDirection.persist_metadata (and equivalent methods on
Accumulation/StreamRaster). Keeping them in one module avoids
string-typo bugs across the typed-class files.
resolve_no_val(dataset)
#
Return the dataset's no-data sentinel or None if not set.
Pyramids exposes Dataset.no_data_value as a tuple-per-band. Callers
that want the band-0 sentinel (or None for "no sentinel set") would
otherwise hand-roll
ds.no_data_value[0] if ds.no_data_value else None everywhere; this
helper deduplicates that pattern across the typed-class files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
A pyramids |
required |
Returns:
| Type | Description |
|---|---|
float | int | None
|
The band-0 no-data sentinel as |
float | int | None
|
when no sentinel has been set (the attribute is |
float | int | None
|
empty tuple). |
Examples:
-
A pyramids Dataset built with
no_data_valuereturns that sentinel:import numpy as np from pyramids.dataset import Dataset, GeoReference from digitalrivers.core.metadata import resolve_no_val ds = Dataset.from_array( ... np.ones((2, 2), dtype=np.float32), ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), ... cell_size=1.0, ... epsg=4326, ... ), ... no_data_value=-9999.0, ... ) float(resolve_no_val(ds)) -9999.0
-
An object whose
no_data_valueattribute isNonereturnsNone:from digitalrivers.core.metadata import resolve_no_val class _Ds: ... no_data_value = None resolve_no_val(_Ds()) is None True
-
An empty tuple is also treated as "no sentinel":
from digitalrivers.core.metadata import resolve_no_val class _Ds: ... no_data_value = () resolve_no_val(_Ds()) is None True
Source code in src/digitalrivers/core/metadata.py
Numba availability#
The single place that decides whether the JIT path is live. Every kernel decorates with the njit exported here
rather than importing numba directly, so DIGITALRIVERS_DISABLE_NUMBA=1 turns the whole package's fast path off
in one step.
digitalrivers.core.numba
#
Numba availability shim: the njit / prange decorators and the enabled predicate.
Every JIT kernel in the package decorates with the njit exported here rather than
importing numba directly, so there is exactly one place that decides whether the JIT
path is live.
Disabling Numba#
Set DIGITALRIVERS_DISABLE_NUMBA=1 (or simply have no Numba installed) and the decorators
degrade to no-ops, so the decorated functions run as plain Python and produce bit-for-bit
identical output. Used for debugging (step-through in an IDE) and for CI on platforms
without Numba wheels.
The flag is read once, at import. Tests that flip the env var must therefore drop this
module from sys.modules — along with any kernel module that imported these decorators —
and import it again, not merely re-read the variable.
This module imports numba eagerly. import digitalrivers must not pull Numba into
the process, so nothing may import it at module scope outside the _kernels layer; the
kernel modules that do are themselves only ever imported lazily, inside the function that
needs them.
njit(*args, **kwargs)
#
No-op decorator used when Numba is unavailable / disabled.