rivabar.polygon_processing¶
rivabar.polygon_processing
¶
plot_polygon(ax, poly, **kwargs)
¶
Plot a polygon on a given matplotlib axes.
This function creates a compound path from the exterior coordinates and any interior coordinates (for polygons with holes), creates a patch from this path, adds it to a collection, and then adds this collection to the axes. It then autoscales the view of the axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
The axes on which to plot the polygon. |
required |
poly
|
Polygon
|
The polygon to plot. |
required |
**kwargs
|
dict
|
Arbitrary keyword arguments to be passed to the PathPatch and PatchCollection constructors. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
collection |
PatchCollection
|
The collection containing the patch created from the polygon. |
Source code in rivabar/polygon_processing.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
one_time_step(chs, ind, plotting=False)
¶
Calculate erosion, deposition, and unchanged channel areas between two channel polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chs
|
list
|
List of channel polygons (Shapely Polygon or MultiPolygon objects) for different time steps. |
required |
ind
|
int
|
Index of the first time step to compare. The function compares chs[ind] with chs[ind+1]. |
required |
plotting
|
bool
|
If True, creates a map of the erosion, deposition, and unchanged channel areas. Default is False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
erosion |
Polygon or MultiPolygon
|
Areas that were not water in time step ind but became water in time step ind+1. |
deposition |
Polygon or MultiPolygon
|
Areas that were water in time step ind but became land in time step ind+1. |
channel |
Polygon or MultiPolygon
|
Areas that remained as water in both time steps. |
Source code in rivabar/polygon_processing.py
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 | |
create_main_channel_banks(G_rook, G_primal, D_primal, dataset)
¶
Create main channel banks from graph structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_rook
|
Graph
|
Graph containing bank polygons. |
required |
G_primal
|
Graph
|
Graph containing node geometries. |
required |
D_primal
|
DiGraph
|
Directed graph containing the main path information. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Four arrays representing x and y coordinates of the two main banks. |
Source code in rivabar/polygon_processing.py
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 | |
extend_line(x, y, ratio)
¶
Extend a line by extrapolating its endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
The x-coordinates of the points defining the line. |
required |
y
|
array - like
|
The y-coordinates of the points defining the line. |
required |
ratio
|
float
|
The ratio by which to extend the line at both ends. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
line |
LineString
|
A LineString object representing the extended line. |
Notes
This function uses the first two points and the last two points of the input coordinates to extrapolate the line at both ends.
Source code in rivabar/polygon_processing.py
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 | |
smooth_polygon(poly, savgol_window=21, remove_count=1)
¶
Smooth a polygon boundary using Savitzky-Golay filter and remove edge artifacts.
This function applies Savitzky-Golay smoothing to the x and y coordinates of a polygon's exterior boundary, removes potentially problematic edge points, and returns a simplified smooth polygon. The smoothing helps eliminate noise while preserving the overall shape characteristics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
Polygon
|
Input polygon whose exterior boundary will be smoothed. The polygon should have a valid exterior ring with sufficient points for smoothing. |
required |
savgol_window
|
Window length used in the Savitzky-Golay filter. Default is 21. |
21
|
|
remove_count
|
int
|
Number of points to remove from both the beginning and end of the smoothed coordinate arrays to eliminate edge artifacts from the filtering process. Default is 1. Must be less than half the number of boundary points. |
1
|
Returns:
| Type | Description |
|---|---|
Polygon
|
A new smoothed polygon with simplified geometry. The polygon is automatically closed (first point equals last point) and simplified with a tolerance of 3 coordinate units to reduce unnecessary vertices while preserving shape. |
Notes
- Uses Savitzky-Golay filter with default window length 21 and polynomial order 3
- Automatically closes the polygon by appending the first coordinate to the end
- Applies simplification with tolerance=3 to reduce vertex count
- Edge points are removed to avoid artifacts from the filtering boundary effects
- The function assumes the input polygon has at least 25+ points for effective smoothing
Examples:
>>> from shapely.geometry import Polygon
>>> import numpy as np
>>>
>>> # Create a noisy polygon
>>> theta = np.linspace(0, 2*np.pi, 100)
>>> x = np.cos(theta) + 0.1 * np.random.random(100)
>>> y = np.sin(theta) + 0.1 * np.random.random(100)
>>> noisy_poly = Polygon(zip(x, y))
>>>
>>> # Smooth the polygon
>>> smooth_poly = smooth_polygon(noisy_poly, remove_count=1)
>>>
>>> # Compare areas
>>> print(f"Original area: {noisy_poly.area:.3f}")
>>> print(f"Smoothed area: {smooth_poly.area:.3f}")
Source code in rivabar/polygon_processing.py
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 | |
smooth_line(x, y, savgol_window=21, multiplier=1.5)
¶
Smooths the given line data using Savitzky-Golay filter and spline resampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
The x-coordinates of the data points. |
required |
y
|
array_like
|
The y-coordinates of the data points. |
required |
spline_ds
|
int
|
The distance between points in the resampled spline, by default 25. |
required |
spline_smoothing
|
int
|
The smoothing factor for the spline, by default 10000. |
required |
savgol_window
|
int
|
The length of the filter window for the Savitzky-Golay filter, by default 21. |
21
|
savgol_poly_order
|
int
|
The order of the polynomial used in the Savitzky-Golay filter, by default 3. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
xs |
ndarray
|
The smoothed x-coordinates. |
ys |
ndarray
|
The smoothed y-coordinates. |
Source code in rivabar/polygon_processing.py
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 | |
vertex_density_tolerance(geometry, multiplier=1.5)
¶
Tolerance based on average edge length - adapts to geometry resolution.
Parameters:
geometry : shapely.geometry.Polygon, LineString, or LinearRing Input geometry to analyze multiplier : float Multiplier for average edge length (default 1.5)
Returns:
float Tolerance value based on average edge length
Source code in rivabar/polygon_processing.py
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 | |
smooth_banklines(G_rook, dataset, mndwi, save_smooth_lines=False)
¶
Smooth banklines and return them as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_rook
|
Graph
|
Graph containing nodes with 'bank_polygon' attributes representing the banklines. |
required |
dataset
|
DatasetReader
|
Dataset containing the spatial reference and transformation information. |
required |
mndwi
|
ndarray
|
Array representing the Modified Normalized Difference Water Index (MNDWI) values. |
required |
save_smooth_lines
|
bool
|
If True, the smoothed banklines will be saved back to the graph nodes (default is False). |
False
|
spline_smoothing
|
int
|
Smoothing factor for the spline interpolation (default is 1000). |
required |
Returns:
| Type | Description |
|---|---|
list of shapely.geometry.Polygon
|
List of smoothed bankline polygons. |
Source code in rivabar/polygon_processing.py
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 | |
smooth_main_bankline(bank_polygon, im_boundary)
¶
Smooth one of the two main bank polygons.
Isolates the bankline (the part of the polygon boundary away from the image edges), smooths and extends it, and splits the image boundary polygon with it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bank_polygon
|
Polygon
|
The (unsmoothed) main bank polygon (G_rook node 0 or 1). |
required |
im_boundary
|
Polygon
|
Polygon outlining the image extent. |
required |
Returns:
| Type | Description |
|---|---|
Polygon or None
|
The smoothed bank polygon, or None when the smoothed bankline failed to split the image boundary into two pieces. |
Source code in rivabar/polygon_processing.py
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 | |
simplify_if_needed(geom, simplify_tolerance)
¶
Apply simplification to reduce vertices.
Source code in rivabar/polygon_processing.py
419 420 421 422 423 | |
create_channel_nw_polygon(G_rook, buffer=10, ch_mouth_poly=None, dataset=None)
¶
Creates a polygon representing the channel network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G_rook
|
Graph
|
A graph where nodes contain 'bank_polygon' attributes representing the bank polygons. |
required |
buffer
|
int
|
The buffer distance to apply around the bank polygons (default is 10). |
10
|
ch_mouth_poly
|
Polygon
|
A polygon representing the channel mouth to be subtracted from the channel network polygon (default is None). |
None
|
dataset
|
DatasetReader
|
A dataset object used to get the boundary coordinates (default is None). |
None
|
mndwi
|
ndarray
|
An array representing the Modified Normalized Difference Water Index (default is None). |
required |
Returns:
| Type | Description |
|---|---|
Polygon
|
A polygon representing the channel network. |
Source code in rivabar/polygon_processing.py
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 | |
straighten_channel(xl, yl, xls, yls)
¶
Straighten a channel while preserving local shapes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xl
|
array_like
|
The x-coordinates of the original sinuous channel. |
required |
yl
|
array_like
|
The y-coordinates of the original sinuous channel. |
required |
xls
|
array_like
|
The x-coordinates of the smoothed centerline. |
required |
yls
|
array_like
|
The y-coordinates of the smoothed centerline. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
xl_straight |
ndarray
|
The x-coordinates of the straightened channel. |
yl_straight |
ndarray
|
The y-coordinates of the straightened channel. |
Source code in rivabar/polygon_processing.py
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 | |
straighten_polygon(polygon, xls, yls)
¶
Straighten a polygon along a centerline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
Polygon
|
The polygon to straighten. |
required |
xls
|
array_like
|
The x-coordinates of the smoothed centerline. |
required |
yls
|
array_like
|
The y-coordinates of the smoothed centerline. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
straight_polygon |
Polygon
|
The straightened polygon. |
Source code in rivabar/polygon_processing.py
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 | |
count_vertices(geometry)
¶
Count total number of vertices in a geometry
Source code in rivabar/polygon_processing.py
652 653 654 655 656 657 658 659 660 661 662 663 664 | |
remove_endpoints(poly_sm, remove_count=3)
¶
remove problematic start/end points
Source code in rivabar/polygon_processing.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
polygon_to_svg(geometry, filename, width=4000, height=800, fill_color='white', stroke_color='black', stroke_width=0.25, interior_fill='black', interior_stroke='black', smooth=True, savgol_window=7, scalebar=True, scalebar_length_km=100, scalebar_position='bottom-left', scalebar_color='black', scalebar_width=3, scalebar_text_size=32, slice_polygon=False, num_slices=4)
¶
Save a Shapely Polygon or MultiPolygon to SVG file with improved smoothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Polygon or MultiPolygon
|
The geometry to save |
required |
filename
|
str
|
Output SVG filename |
required |
width
|
int
|
Dimensions of the SVG canvas |
4000
|
height
|
int
|
Dimensions of the SVG canvas |
4000
|
fill_color
|
str or list
|
Fill color(s) for polygons. If a list, colors are assigned to each polygon in a MultiPolygon |
'white'
|
stroke_color
|
str or list
|
Stroke color(s) for polygons |
'black'
|
stroke_width
|
int
|
Stroke width for all paths |
0.25
|
interior_fill
|
str
|
Fill color for interior holes |
'black'
|
interior_stroke
|
str
|
Stroke color for interior holes |
'black'
|
smooth
|
bool
|
Whether to apply smoothing |
True
|
ds
|
float
|
Distance between points after resampling (for smoothing) |
required |
smoothing
|
float
|
Smoothing factor for splines (higher values = smoother curves) |
required |
scalebar
|
bool
|
Whether to include a scalebar |
True
|
scalebar_length_km
|
float
|
Length of scalebar in kilometers |
100
|
scalebar_position
|
str
|
Position of scalebar: "bottom-left", "bottom-right", "top-left", "top-right" |
'bottom-left'
|
scalebar_color
|
str
|
Color of the scalebar |
'black'
|
scalebar_width
|
int
|
Width of the scalebar line |
3
|
scalebar_text_size
|
int
|
Size of the scalebar text |
32
|
slice_polygon
|
bool
|
Whether to slice the polygon into multiple pieces |
False
|
num_slices
|
int
|
Number of slices to create (4-10 recommended) |
4
|
Source code in rivabar/polygon_processing.py
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 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 | |