rivabar.analysis¶
rivabar.analysis
¶
analyze_width_and_wavelength(D_primal, main_path, ax, delta_s=5, smoothing_factor=100000000.0, min_sinuosity=1.1, dx=30)
¶
Analyze the width and wavelength of a river channel based on input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D_primal
|
dict
|
Dictionary containing river channel data. |
required |
main_path
|
list of tuples
|
List of tuples representing the main path of the river channel. |
required |
ax
|
Axes
|
Matplotlib Axes object for plotting. |
required |
delta_s
|
float
|
Resampling interval for smoothing (default is 5). |
5
|
smoothing_factor
|
float
|
Smoothing factor for the spline (default is 1e8). |
100000000.0
|
min_sinuosity
|
float
|
Minimum sinuosity to consider (default is 1.1). |
1.1
|
dx
|
float
|
Spatial resolution of the data (default is 30). |
30
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
DataFrame containing wavelengths, sinuosities, mean widths, standard deviations of widths, and along-channel distances. |
curv |
ndarray
|
Array of curvature values. |
s |
ndarray
|
Array of along-channel distances. |
loc_zero_curv |
ndarray
|
Array of indices where curvature crosses zero. |
xsmooth |
ndarray
|
Smoothed x-coordinates of the river channel. |
ysmooth |
ndarray
|
Smoothed y-coordinates of the river channel. |
Source code in rivabar/analysis.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
compute_curvature(x, y)
¶
Compute the first derivatives and curvature of a curve (centerline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Cartesian x-coordinates of the curve. |
required |
y
|
array_like
|
Cartesian y-coordinates of the curve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
curvature |
ndarray
|
Curvature of the curve (in 1/units of x and y). |
s |
ndarray
|
Cumulative distance along the curve. |
Notes
The function calculates the first and second derivatives of the input coordinates to determine the curvature and cumulative distance along the curve.
Source code in rivabar/analysis.py
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 | |
find_zero_crossings(curve)
¶
Find zero crossings of a curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curve
|
array_like
|
A one-dimensional array that describes the curve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
loc_zero_curv |
ndarray
|
Indices of zero crossings. |
loc_max_curv |
ndarray
|
Indices of maximum values. |
Notes
Zero crossings are points where the curve changes sign. The function also identifies the indices of the maximum values between zero crossings.
Source code in rivabar/analysis.py
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 | |
filter_half_contours(contours, ch_map, threshold=0.25)
¶
Simple one-liner to keep only 0.5-level contours.
Source code in rivabar/analysis.py
198 199 200 201 202 | |
get_bank_coords(poly, mndwi, dataset, timer=False, filter_contours=False)
¶
This function calculates the coordinates of river banks from a given polygon and MNDWI dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
Polygon
|
The polygon representing the area of interest. |
required |
mndwi
|
ndarray
|
The Modified Normalized Difference Water Index (MNDWI) array. |
required |
dataset
|
DatasetReader
|
The raster dataset containing the spatial reference and transformation information. |
required |
timer
|
bool
|
If True, uses a progress bar to show the progress of the loop (default is False). |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
x_utm |
list
|
List of x coordinates of the river bank in UTM. |
y_utm |
list
|
List of y coordinates of the river bank in UTM. |
ch_map |
ndarray
|
A binary map where the river channel is marked. |
Source code in rivabar/analysis.py
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 | |
compute_mndwi_small_dist(poly, dataset, mndwi, tile_size=500)
¶
Computes the distance transform of a 'small' MNDWI tile, relative to a polygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
Polygon
|
The polygon defining the area of interest. |
required |
dataset
|
DatasetReader
|
The dataset containing the raster data. |
required |
mndwi
|
ndarray
|
The MNDWI (Modified Normalized Difference Water Index) array. |
required |
tile_size
|
int
|
The size of the tile to be extracted (default is 500). |
500
|
Returns:
| Name | Type | Description |
|---|---|---|
mndwi_small_dist |
ndarray
|
The distance transform of the small MNDWI tile. |
col1 |
int
|
The starting column index of the small tile in the original MNDWI array. |
col2 |
int
|
The ending column index of the small tile in the original MNDWI array. |
row1 |
int
|
The starting row index of the small tile in the original MNDWI array. |
row2 |
int
|
The ending row index of the small tile in the original MNDWI array. |
Notes
The function extracts a smaller tile from the MNDWI array based on the bounding box of the input polygon, rasterizes the polygon, and computes the distance transform of the small tile. The units of the distance transform are in pixels.
Source code in rivabar/analysis.py
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 | |
set_half_channel_widths(G_primal, G_rook, dataset, mndwi)
¶
Set half channel widths for edges in the 'G_primal' graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_primal
|
Graph
|
The primal graph where edges represent the centerlines of channels. |
required |
G_rook
|
Graph
|
The rook graph where nodes represent polygons and edges represent adjacency between polygons. |
required |
dataset
|
DatasetReader
|
The dataset containing the raster data. |
required |
mndwi
|
ndarray
|
The Modified Normalized Difference Water Index (MNDWI) array. |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in rivabar/analysis.py
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 | |
get_bank_coords_for_main_channel(D_primal, mndwi, edge_path, dataset, cline_buffer=2000)
¶
Extracts the coordinates of the banks for the main channel from the given dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D_primal
|
DiGraph
|
The directed graph representing the river network. |
required |
mndwi
|
ndarray
|
The Modified Normalized Difference Water Index (MNDWI) array. |
required |
edge_path
|
list of tuples
|
The list of edges representing the main channel path. |
required |
dataset
|
DatasetReader
|
The dataset containing the spatial information. |
required |
cline_buffer
|
int
|
The buffer distance around the centerline, by default 2000. |
2000
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
The x-coordinates of the main channel. |
y |
ndarray
|
The y-coordinates of the main channel. |
x_utm1 |
ndarray
|
The x-coordinates of the first bank. |
y_utm1 |
ndarray
|
The y-coordinates of the first bank. |
x_utm2 |
ndarray
|
The x-coordinates of the second bank. |
y_utm2 |
ndarray
|
The y-coordinates of the second bank. |
Source code in rivabar/analysis.py
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 | |
get_channel_widths_along_path(D_primal, path)
¶
Calculates the channel widths along a given path in the directed graph "D_primal".
The path is a list of edges, where each edge is a tuple of two nodes and a key. The function retrieves the 'half_widths' attribute of each edge, which is a dictionary with two keys. The values corresponding to these keys are lists of half-widths of the channel at various points along the edge. The function also calculates the cumulative distance along the path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D_primal
|
DiGraph
|
The directed graph. |
required |
path
|
list
|
The path, represented as a list of edges. Each edge is a tuple of two nodes and a key. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
xl |
list
|
The x-coordinates of the points along the path. |
yl |
list
|
The y-coordinates of the points along the path. |
w1l |
list
|
The half-widths corresponding to the first key for each edge. |
w2l |
list
|
The half-widths corresponding to the second key for each edge. |
w |
list
|
The full widths of the channel at various points along the path. |
s |
ndarray
|
The cumulative distance along the path. |
Source code in rivabar/analysis.py
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 | |
get_all_channel_widths(D_primal)
¶
Extract all channel widths from a directed multigraph.
This function iterates through all edges in the directed multigraph and extracts the channel widths by summing the half-widths from both sides of the channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D_primal
|
MultiDiGraph
|
A directed multigraph where edges contain 'half_widths' attributes. Each 'half_widths' attribute is a dictionary with two keys, each corresponding to a list of half-width measurements. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
widths |
list
|
A flattened list of all channel widths across all edges in the graph. |
Source code in rivabar/analysis.py
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 | |
get_channel_mouth_polygon(mndwi, dataset, points)
¶
Create a polygon that defines the coastline when multiple channels reach the sea/lake (e.g., in a delta). It uses a line drawn roughly parallel to the coastline (defined by 'points') to create the polygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mndwi
|
ndarray
|
A 2D array representing the Modified Normalized Difference Water Index (MNDWI). |
required |
dataset
|
DatasetReader
|
A rasterio dataset object representing the image. |
required |
points
|
list
|
A list of points defining a line that runs roughly parallel to the coastline. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x_utm |
list
|
The x-coordinates of the vertices of the channel mouth polygon in UTM coordinates. |
y_utm |
list
|
The y-coordinates of the vertices of the channel mouth polygon in UTM coordinates. |
ch_map |
ndarray
|
A 2D array representing the 'channel' map - in this case, it is a map of the distance of the line from the coastline. |
Example
points = plt.ginput(-1) # create a line that runs roughly parallel to the coastline x_utm, y_utm = get_channel_mouth_polygon(mndwi, dataset, points) # use this function to create the channel mouth polygon
Source code in rivabar/analysis.py
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 | |
filter_outlier_paths(rivers, outlier_threshold=2.0, min_overlap_ratio=0.3, resample_points=100, plot_analysis=False)
¶
Filter out main paths that are spatial outliers compared to the consensus.
This method identifies centerlines that deviate significantly from the overall spatial trend while preserving shorter paths that follow the same general route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rivers
|
list
|
List of River instances with processed main paths |
required |
outlier_threshold
|
float
|
Standard deviation threshold for outlier detection (default 2.0) |
2.0
|
min_overlap_ratio
|
float
|
Minimum overlap ratio with consensus path to be considered valid (default 0.3) |
0.3
|
resample_points
|
int
|
Number of points to resample each path to for comparison (default 100) |
100
|
plot_analysis
|
bool
|
Whether to plot the analysis results (default False) |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
filtered_rivers |
list
|
List of rivers with non-outlier paths |
outlier_rivers |
list
|
List of rivers identified as outliers |
analysis_results |
dict
|
Dictionary with analysis metrics |
Source code in rivabar/analysis.py
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 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | |
classify_confluences_and_splits(D_primal)
¶
Classifies nodes in a primal graph. If the graph is directed, classifies nodes as 'confluence', 'split', etc. If the graph is undirected, classifies nodes as 'junction', 'terminal', etc.
Args: D_primal (nx.Graph): The centerline graph. Can be directed or undirected.
Returns: dict: A dictionary mapping each node ID to its classification string.
Source code in rivabar/analysis.py
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | |