FlowDirection#
Typed flow-direction raster — produced by DEM.flow_direction(method=...) for any of the five routing
schemes (d8 / dinf / mfd_quinn / mfd_holmgren / rho8). Carries a routing tag persisted into
the raster's metadata (DR_ROUTING) so downstream consumers stay aware of which scheme produced the
grid.
Top-level surface:
accumulate(weights=...)— Kahn topological-sort flow accumulation (single kernel for all five routings).watershed(points, ...)— pour-point watershed delineation under D8 / Rho8.basins(...)— partition the DEM into one basin per terminal outlet.subbasins_pfafstetter(accumulation, streams, level=N)— hierarchical Pfafstetter coding to the requested level (1 or 2).isobasins(streams, accumulation, target_area_km2)— equal-area sub-basin partition (W-7).upslope_flowpath_length()— per-cell longest upslope flow path (W-9).upscale(method=...)/upscale_ihu(...)— COTAT / EAM / DMM / IHU (Eilander 2021) upscalers.
digitalrivers.flow.direction.FlowDirection
#
Bases: UpscaleMixin, PfafstetterMixin, Dataset
Flow-direction raster with routing-scheme metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Dataset
|
GDAL dataset wrapping a flow-direction raster. |
required |
access
|
str
|
|
'read_only'
|
routing
|
str
|
Routing scheme used to produce this raster. One of
|
required |
encoding
|
str
|
Cell-value encoding convention. One of |
'digitalrivers'
|
gdal_env
|
dict[str, str] | None
|
GDAL config (cloud credentials, HTTP knobs) captured on
the dataset and re-installed around its reads, so the paths that
reopen the file authenticate the same way. Default |
None
|
open_options
|
tuple[str, ...] | list[str] | None
|
GDAL open options captured on the dataset and reapplied
when it is reopened. Default |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/digitalrivers/flow/direction.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 | |
__init__(src, access='read_only', *, routing, encoding='digitalrivers', gdal_env=None, open_options=None)
#
Wrap a GDAL dataset as a flow-direction raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Dataset
|
Open GDAL dataset to wrap. The handle is adopted, not copied. |
required |
access
|
str
|
|
'read_only'
|
gdal_env
|
dict[str, str] | None
|
GDAL config (cloud credentials, HTTP knobs) captured on the
dataset and re-installed around its reads. Default |
None
|
open_options
|
tuple[str, ...] | list[str] | None
|
GDAL open options captured on the dataset and reapplied
when it is reopened. Default |
None
|
routing
|
str
|
Routing scheme the raster encodes. Required keyword-only — there is no default, because reading a D-infinity raster as D8 silently corrupts every derivative. |
required |
encoding
|
str
|
Cell-value convention. Defaults to |
'digitalrivers'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/digitalrivers/flow/direction.py
from_dataset(ds, *, routing, encoding='digitalrivers')
classmethod
#
Promote a plain Dataset into a FlowDirection.
The source's access mode, gdal_env and open_options are carried onto
the wrapper. Dropping them left a promoted file-backed raster unable to
write its own metadata tags, and stripped the credentials a signed remote
raster needs when pyramids reopens it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Dataset wrapping the flow-direction raster. Its raster handle is reused, not copied. |
required |
routing
|
str
|
Routing scheme. Required keyword-only. |
required |
encoding
|
str
|
Cell-value encoding convention. |
'digitalrivers'
|
Returns:
| Type | Description |
|---|---|
FlowDirection
|
A |
FlowDirection
|
configuration. |
Examples:
- Promote an in-memory raster and read the provenance back, and confirm the handle carries through:
>>> import numpy as np >>> from pyramids.dataset import Dataset, GeoReference >>> from digitalrivers import FlowDirection >>> plain = Dataset.from_array( ... np.array([[0, 1], [2, 3]], dtype=np.int32), ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), cell_size=1.0, epsg=4326 ... ), ... ) >>> wrapped = FlowDirection.from_dataset(plain, routing="d8") >>> wrapped.routing, wrapped.encoding ('d8', 'digitalrivers') >>> FlowDirection.from_dataset( ... plain, routing="d8" ... ).access == plain.access True
Source code in src/digitalrivers/flow/direction.py
to_dataset()
#
Drop the typed wrapper and return the underlying Dataset.
Symmetric with from_dataset: the access mode, gdal_env and
open_options come back out with the raster, so a round trip does not
silently downgrade a writable handle to a read-only one.
Returns:
| Type | Description |
|---|---|
Dataset
|
A plain |
Examples:
- Unwrap and read the grid straight off the plain
Dataset, and confirm the handle carries through:>>> import numpy as np >>> from pyramids.dataset import Dataset, GeoReference >>> from digitalrivers import FlowDirection >>> plain = Dataset.from_array( ... np.array([[1, 2], [3, 4]], dtype=np.float32), ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), cell_size=1.0, epsg=4326 ... ), ... ) >>> wrapped = FlowDirection.from_dataset(plain, routing="d8") >>> plain_again = wrapped.to_dataset() >>> plain_again.read_array().tolist() [[1.0, 2.0], [3.0, 4.0]] >>> wrapped.to_dataset().access == wrapped.access True
Source code in src/digitalrivers/flow/direction.py
persist_metadata()
#
Write routing and encoding to the underlying raster tags.
Stored under DR_CLASS / DR_ROUTING / DR_ENCODING GeoTIFF
metadata keys so FlowDirection.open(path) can recover them.
Source code in src/digitalrivers/flow/direction.py
open(path, *, routing=None, encoding=None, read_only=True, gdal_env=None, open_options=None)
classmethod
#
Open a FlowDirection GeoTIFF.
Resolution order for the routing/encoding tags:
- Explicit
routing=/encoding=kwargs win unconditionally (caller knows what the file is). - Otherwise,
DR_ROUTING/DR_ENCODINGmetadata tags are used if present. - Otherwise, raise
ValueError. There is no silent fallback to"d8"— a D∞ raster on disk is float32 in[0, 2π]and reinterpreting it as int D8 codes silently corrupts every downstream computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the GeoTIFF. |
required |
routing
|
str | None
|
Explicit routing override. If |
None
|
encoding
|
str | None
|
Explicit encoding override. If |
None
|
read_only
|
bool
|
Open the file read-only (default). Pass |
True
|
gdal_env
|
dict[str, str] | None
|
GDAL config (cloud credentials, HTTP knobs) installed
for the open and captured on the result, so pyramids' reopen
paths re-authenticate. Default |
None
|
open_options
|
tuple[str, ...] | list[str] | None
|
GDAL open options forwarded to the driver and
captured on the result. Default |
None
|
Returns:
| Type | Description |
|---|---|
FlowDirection
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in src/digitalrivers/flow/direction.py
accumulate(weights=None, *, engine='auto', out_path=None, tile_size=2048, cache='evict', workers=1, scratch_dir=None, scheduler='threads', client=None)
#
Run flow accumulation over this raster's routing scheme.
Implements a Kahn topological-sort sweep that handles all five routing
schemes (D8, Rho8, D∞, MFD-Quinn, MFD-Holmgren) via a single algorithm,
dispatched by self.routing.
Output semantics: out[cell] = sum of weights over strictly-upstream
cells — the cell's own weight does not contribute to its own count.
This matches the legacy DEM.flow_accumulation convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Dataset | None
|
Per-cell weight raster (rainfall, runoff coefficient,
whatever). Must align with this FlowDirection's shape. |
None
|
Returns:
| Type | Description |
|---|---|
Accumulation
|
Accumulation carrying this object's |
Out-of-core
engine="tiled" streams a Barnes-2017 tiled accumulation to out_path (D8/Rho8 only). engine="auto"
(default) only switches to it when the raster is large enough to risk exhausting RAM.
Source code in src/digitalrivers/flow/direction.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
upslope_flowpath_length()
#
Return a per-cell raster of the longest upslope flow path.
For every cell in the raster, the returned value is the longest
planimetric flow path from any upstream source to that cell —
following D8 / Rho8 receivers, with cardinal steps contributing
cell_size and diagonal steps contributing cell_size * sqrt(2).
Source cells (no upstream neighbour pointing at them) hold 0.0.
Only single-direction routings (d8 / rho8) are supported.
Returns:
| Type | Description |
|---|---|
Dataset
|
|
Dataset
|
|
Dataset
|
sentinel. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/digitalrivers/flow/direction.py
isobasins(streams, accumulation, target_area_km2)
#
Partition the catchment into sub-basins of approximately equal area.
Walks the stream network from heads to outlet via the accumulation
raster. At every stream cell whose floor-divided accumulation quantile
(accumulation // target_cells) is strictly greater than the maximum
quantile of its stream-upstream neighbours, a virtual sub-basin
outlet is placed. The final sub-basin labels are produced by
first-claim-wins reverse-BFS watershed delineation on those seeds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
streams
|
|
required | |
accumulation
|
|
required | |
target_area_km2
|
float
|
Target sub-basin area in km². Converted to target cell count via the dataset's cell size; must be positive. |
required |
Returns:
| Type | Description |
|---|---|
WatershedRaster
|
|
WatershedRaster
|
(or 0 for cells outside any sub-basin) and whose |
WatershedRaster
|
mapping carries |
Raises:
| Type | Description |
|---|---|
ValueError
|
If routing is multi-direction, shapes mismatch, or
|
Source code in src/digitalrivers/flow/direction.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
basins(*, min_area_cells=None, min_area_km2=None, merge_small='drop')
#
Partition the entire DEM into basins, one label per terminal outlet.
Detects every cell whose flow direction is the no-data sentinel (cells with no defined downstream — either at the data envelope or at internal sinks that survived the fill phase) and seeds a reverse BFS from each. The result labels every valid cell with the ID of the outlet it drains to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_area_cells
|
int | None
|
Optional minimum basin area in cells; basins
smaller than this are post-processed via |
None
|
min_area_km2
|
float | None
|
Same threshold expressed in map km². Mutually
exclusive with |
None
|
merge_small
|
str
|
|
'drop'
|
Returns:
| Type | Description |
|---|---|
WatershedRaster
|
class: |
WatershedRaster
|
routing. The |
WatershedRaster
|
basin with the outlet |
WatershedRaster
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both area kwargs are supplied or
|
Source code in src/digitalrivers/flow/direction.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
watershed(pour_points, require_unique_basins=False)
#
Delineate the upstream watershed of each pour point.
Reverse-BFS from every pour-point cell, labelling every contributing cell with the pour point's 1-based basin ID. Multi-point inputs produce a labelled raster (one ID per pour point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pour_points
|
|
required | |
require_unique_basins
|
bool
|
If False (default), inner pour points overwrite the outer basin's cells along shared upstream paths — the outer basin contains a hole around the inner basin. If True, the first seed to claim a cell keeps it; the outer basin contains no inner-basin cells. |
False
|
Returns:
| Type | Description |
|---|---|
WatershedRaster
|
class: |
WatershedRaster
|
The |
WatershedRaster
|
|
Source code in src/digitalrivers/flow/direction.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
__repr__()
#
Return a one-line summary of the raster.
Names its shape, routing scheme and cell-value encoding.
Both tags are shown, because a direction grid is only interpretable given the pair: the same cell value means different neighbours under different encodings.
Returns:
| Type | Description |
|---|---|
str
|
A string of the form |
Source code in src/digitalrivers/flow/direction.py
subbasins_pfafstetter(accumulation, streams, level=1, encoding='packed_int')
#
Compute Pfafstetter (Verdin & Verdin 1999) hierarchical codes.
Single-basin level-1 implementation: identifies the main stem (the
path with the largest downstream-accumulation), finds the four
tributaries with the largest accumulation at confluence with the main
stem, and labels every cell with one of the nine Pfafstetter codes:
2/4/6/8 for the four main tributaries (downstream order) and
1/3/5/7/9 for the inter-basin segments between them.
The multi-level recursive descent (level > 1) and the HydroBASINS
iso-basin pre-split are out of scope for this initial implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accumulation
|
|
required | |
streams
|
|
required | |
level
|
int
|
Hierarchy depth. Only |
1
|
encoding
|
str
|
|
'packed_int'
|
Returns:
| Type | Description |
|---|---|
WatershedRaster
|
class: |
WatershedRaster
|
codes are in |
WatershedRaster
|
|
WatershedRaster
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
NotImplementedError
|
If |
Examples:
-
Level-1 coding on a small east-flowing DEM yields codes within the canonical
[1, 9]Pfafstetter range:import numpy as np from pyramids.dataset import Dataset, GeoReference from digitalrivers import DEM z = np.array( ... [ ... [9, 9, 9, 9, 9, 9], ... [9, 5, 4, 3, 2, 1], ... [9, 9, 9, 9, 9, 9], ... ], ... dtype=np.float32, ... ) ds = Dataset.from_array( ... z, ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), ... cell_size=1.0, ... epsg=4326, ... ), ... no_data_value=-9999.0, ... ) dem = DEM(ds.raster) fd = dem.flow_direction(method="d8") acc = fd.accumulate() sr = acc.streams(threshold=1) ws = fd.subbasins_pfafstetter(acc, sr, level=1) arr = ws.read_array() codes = sorted({int(v) for v in np.unique(arr) if v != 0}) bool(set(codes).issubset(set(range(1, 10)))) True
-
Level-2 coding produces two-digit
parent*10 + childcodes (P16 multi-level backfill):import numpy as np from pyramids.dataset import Dataset, GeoReference from digitalrivers import DEM z = np.array( ... [ ... [9, 9, 9, 9, 9, 9], ... [9, 5, 4, 3, 2, 1], ... [9, 9, 9, 9, 9, 9], ... ], ... dtype=np.float32, ... ) ds = Dataset.from_array( ... z, ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), ... cell_size=1.0, ... epsg=4326, ... ), ... no_data_value=-9999.0, ... ) dem = DEM(ds.raster) fd = dem.flow_direction(method="d8") acc = fd.accumulate() sr = acc.streams(threshold=1) ws = fd.subbasins_pfafstetter(acc, sr, level=2) arr = ws.read_array() codes = sorted({int(v) for v in np.unique(arr) if v != 0}) bool(all(11 <= c <= 99 for c in codes)) True
-
level < 1is rejected:import numpy as np from pyramids.dataset import Dataset, GeoReference from digitalrivers import DEM z = np.array( ... [[9, 9, 9], [9, 5, 9], [9, 9, 9]], dtype=np.float32 ... ) ds = Dataset.from_array( ... z, ... geo_ref=GeoReference( ... top_left_corner=(0.0, 0.0), ... cell_size=1.0, ... epsg=4326, ... ), ... no_data_value=-9999.0, ... ) dem = DEM(ds.raster) fd = dem.flow_direction(method="d8") acc = fd.accumulate() sr = acc.streams(threshold=1) fd.subbasins_pfafstetter(acc, sr, level=0) Traceback (most recent call last): ... ValueError: level must be >= 1; got 0
See Also
FlowDirection.basins: terminal-outlet partitioning of the DEM. FlowDirection.accumulate: upstream-area accumulation needed for tributary ranking.
Source code in src/digitalrivers/flow/pfafstetter.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
upscale_ihu(scale_factor, accumulation, dem, max_iter=20, report=False)
#
Iterative Hydrography Upscaling (Eilander 2021).
The state-of-the-art D8 upscaling method that builds an initial coarse network with COTAT-style outlet selection and then refines boundary mismatches by swapping outlets between adjacent coarse cells until convergence.
v1 status: scale_factor=1 is a no-op (passes through the input
unchanged). All other scale_factor values raise
NotImplementedError. The roadmap recommends vendoring pyflwdir
as the first-release backend; a native swap-search implementation
is deferred to Phase 4.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_factor
|
int
|
Integer aggregation factor (>= 1). |
required |
accumulation
|
|
required | |
dem
|
|
required | |
max_iter
|
int
|
Maximum refinement iterations. |
20
|
report
|
bool
|
When True, the third return slot carries Eilander 2021
validation metrics ( |
False
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
For |
Source code in src/digitalrivers/flow/upscale.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 179 180 181 182 183 184 | |
upscale(scale_factor, method='cotat', accumulation=None, dem=None, area_threshold_cells=None)
#
Upscale the flow-direction raster by an integer factor.
Three classical methods are specified by P18; this initial
implementation ships "cotat" (Reed 2003 — Cell Outlet Tracing
with an Area Threshold). EAM (Olivera 2002) and DMM raise
NotImplementedError pending a follow-up.
COTAT algorithm (per coarse cell):
- Find the fine cell with the highest accumulation in the scale_factor × scale_factor block. This is the coarse cell's outlet.
- Trace downstream from that fine outlet along the fine
fdiruntil leaving the block. - The direction from the source coarse cell to the destination coarse cell becomes the coarse cell's D8 flow direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale_factor
|
int
|
Integer aggregation factor (>= 1). |
required |
method
|
str
|
|
'cotat'
|
accumulation
|
|
None
|
|
dem
|
Optional |
None
|
|
area_threshold_cells
|
int | None
|
Reserved for COTAT+ branch-cutoff refinement; currently ignored. |
None
|
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple |
tuple
|
|
tuple
|
expected to recompute elevations from a coarsened DEM. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
For methods other than |
ValueError
|
If |
Source code in src/digitalrivers/flow/upscale.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |