Geometry
DHyDAMO Geometry API¶
common
¶
as_linestring_list(linestring: LineString | MultiLineString | list[LineString | MultiLineString]) -> list[LineString]
¶
Returns a list of LineStrings from a given LineString or MultiLineString. Useful for iterating over varying multi of single type geometries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linestring
|
Union[LineString, MultiLineString]
|
LineString of MultiLineString shapely geometry object |
required |
Returns:
| Type | Description |
|---|---|
list[LineString]
|
List[LineString]: list of shapely geometry LineString objects |
Source code in hydrolib/dhydamo/geometry/common.py
42 43 44 45 46 47 48 49 50 51 52 53 54 | |
as_point_list(point: Point | MultiPoint | list[Point | MultiPoint]) -> list[Point]
¶
Returns a list of Point from a given Point or MultiPoint. Useful for iterating over varying multi of single type geometries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linestring
|
Union[Point, MultiPoint]
|
Point of MultiPoint shapely geometry object |
required |
Returns:
| Type | Description |
|---|---|
list[Point]
|
List[Point]: list of shapely geometry Point objects |
Source code in hydrolib/dhydamo/geometry/common.py
72 73 74 75 76 77 78 79 80 81 82 83 84 | |
as_polygon_list(polygon: Polygon | MultiPolygon | list[Polygon | MultiPolygon]) -> list[Polygon]
¶
Returns a list of Polygons from a given Polygon or MultiPolygon. Useful for iterating over varying multi of single type geometries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linestring
|
Union[Polygon, MultiPolygon]
|
Polygon of MultiPolygon shapely geometry object |
required |
Returns:
| Type | Description |
|---|---|
list[Polygon]
|
List[Polygon]: list of shapely geometry Polygon objects |
Source code in hydrolib/dhydamo/geometry/common.py
57 58 59 60 61 62 63 64 65 66 67 68 69 | |
mesh
¶
links1d2d_add_links_1d_to_2d(network: Network, branchids: list[str] | None = None, within: Polygon | MultiPolygon | None = None, max_length: float = np.inf) -> None
¶
Function to add 1d2d links to network, by generating them from 1d to 2d. Branchids can be specified for 1d branches that need to be linked. A (Multi)Polygon can be provided were links should be made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network in which the connections are made |
required |
branchids
|
list[str] | None
|
List of branchid's to connect. If None, all branches are connected. Defaults to None. |
None
|
within
|
Polygon | MultiPolygon | None
|
Area within which connections are made. Defaults to None. |
None
|
max_length
|
float
|
Max edge length. Defaults to np.inf. |
inf
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
links1d2d_add_links_2d_to_1d_embedded(network: Network, branchids: list[str] | None = None, within: Polygon | MultiPolygon | None = None) -> None
¶
Generates links from 2d to 1d, where the 2d mesh intersects the 1d mesh: the 'embedded' links.
To find the intersecting cells in an efficient way, we follow we the next steps. 1) Get the maximum length of a face edge. 2) Buffer the branches with this length. 3) Find all face nodes within this buffered geometry. 4) Check for each of the corresponding faces if it crossed the branches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network in which the links are made. Should contain a 1d and 2d mesh |
required |
branchids
|
list[str] | None
|
List is branch id's for which the connections are made. Defaults to None. |
None
|
within
|
Polygon | MultiPolygon | None
|
Clipping polygon for 2d mesh that is. Defaults to None. |
None
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
links1d2d_add_links_2d_to_1d_lateral(network: Network, dist_factor: float | None = 2.0, branchids: list[str] | None = None, within: Polygon | MultiPolygon | None = None, max_length: float = np.inf) -> None
¶
Generate 1d2d links from the 2d mesh to the 1d mesh, with a lateral connection. If a link is kept, is determined based on the distance between the face center and the intersection with the 2d mesh exterior. By default, links with an intersection distance larger than 2 times the center to edge distance of the cell, are removed. Note that for a square cell with a direct link out of the cell (without passing any other cells) this max distance is sqrt(2) = 1.414. The default value of 2 provides some flexibility. Note that a link with more than 1 intersection with the 2d mesh boundary is removed anyway.
Furthermore: - Branch ids can be specified to connect only specific branches. - A 'within' polygon can be given to only connect 2d cells within this polygon. - A max link length can be given to limit the link length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network in which the links are made. Should contain a 1d and 2d mesh |
required |
dist_factor
|
float | None
|
Factor to determine which links are kept (see description above). Defaults to 2.0. |
2.0
|
branchids
|
list[str] | None
|
List is branch id's for which the conncetions are made. Defaults to None. |
None
|
within
|
Polygon | MultiPolygon | None
|
Clipping polygon for 2d mesh that is. Defaults to None. |
None
|
max_length
|
float
|
Max edge length. Defaults to np.inf. |
inf
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
links1d2d_remove_1d_endpoints(network: Network) -> None
¶
Method to remove 1d2d links from end points of the 1d mesh. The GUI will interpret every endpoint as a boundary conditions, which does not allow a 1d 2d link at the same node. To avoid problems with this, use this method.
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
links1d2d_remove_within(network: Network, within: Polygon | MultiPolygon) -> None
¶
Remove 1d2d links within a given polygon or multipolygon
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The network from which the links are removed |
required |
within
|
Polygon | MultiPolygon
|
The polygon that indicates which to remove |
required |
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh1d_add_branch_from_linestring(network: Network, linestring: LineString, node_distance: float, name: str | None = None, structure_chainage: list[float] | None = None, max_dist_to_struc: float | None = None) -> str
¶
Add branch to 1d mesh, from a LineString geometry. The branch is discretized with the given node distance. The position of a structure can be provided, just like the max distance of a mesh node to a structure
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network to which the branch is added |
required |
linestring
|
LineString
|
The geometry of the new branch |
required |
node_distance
|
float | int
|
Preferred node distance between branch nodes |
required |
name
|
str | None
|
Name of the branch. If not given, a name is generated. Defaults to None. |
None
|
structure_chainage
|
list[float] | None
|
Positions of structures along the branch. Defaults to None. |
None
|
max_dist_to_struc
|
float | None
|
Max distance of a mesh point to a structure. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
name of added branch |
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh1d_add_branches_from_gdf(network: Network, branches: gpd.GeoDataFrame, branch_name_col: str, node_distance: float, max_dist_to_struc: float | None = None, structures: gpd.GeoDataFrame | None = None) -> None
¶
Function to generate branches from geodataframe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
The network to which the branches are added |
required |
branches
|
GeoDataFrame
|
GeoDataFrame with branches |
required |
branch_name_col
|
str
|
Name of the column in the GeoDataFrame with the branchnames |
required |
node_distance
|
float
|
Preferred 1d mesh distance |
required |
max_dist_to_struc
|
float | None
|
Maximum distance to structure. Defaults to None. |
None
|
structures
|
GeoDataFrame | None
|
GeoDataFrame with structures. Must contain a column branchid and chainage. Defaults to None. |
None
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh1d_order_numbers_from_attribute(branches: gpd.GeoDataFrame, missing: list[str], order_attribute: str, network: Network, exceptions: list[str] | None = None) -> list[str]
¶
Method to allocate order numbers to branches based on a common attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branches
|
GeoDataFrame
|
HyDAMO GeoDataFrame containing branches. |
required |
missing
|
list[str]
|
list of branches that have so far not been allocated a cross section. |
required |
order_attribute
|
str
|
Attribute of branches based on which order numbers are allocated. |
required |
network
|
Network
|
FM network geometry to which the order numbers should be written |
required |
exceptions
|
list[str] | None
|
List of branch codes that should not be given an order number. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: list of branches that still do not have a cross section |
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh1d_set_branch_order(network: Network, branchids: list[str], idx: int | None = None) -> None
¶
Group branch ids so that the cross sections are interpolated along the branch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network in which the branch order is set |
required |
branchids
|
list[str]
|
List of branches to group |
required |
idx
|
int | None
|
Order number with which to update a branch. If None, the group is appended after the current highest order. Defaults to None. |
None
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_add_rectilinear(network: Network, polygon: Polygon | MultiPolygon, dx: float, dy: float, deletemeshoption: mk.DeleteMeshOption = mk.DeleteMeshOption.INSIDE_NOT_INTERSECTED) -> None
¶
Add 2d rectilinear mesh to network. A new network is created, clipped, and merged with the existing network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network object to which the mesh is added |
required |
polygon
|
Polygon | MultiPolygon
|
Geometry within which the mesh is generated |
required |
dx
|
float
|
Horizontal mesh spacing |
required |
dy
|
float
|
Vertical mesh spacing |
required |
deletemeshoption
|
DeleteMeshOption
|
Option for clipping mesh. Defaults to mk.DeleteMeshOption.INSIDE_NOT_INTERSECTED. |
INSIDE_NOT_INTERSECTED
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_add_triangular(network: Network, polygon: Polygon | MultiPolygon, edge_length: float | None = None) -> None
¶
Add triangular mesh to existing network. An orthogonal mesh is generated by the meshkernel, which likely means that the given geometry is not completely filled. The triangle size follows only from the spacing of the coordinates on the boundary of the provided geometry: triangles are small where boundary points are close together and larger where they are far apart. Giving an edge_length will discretize the polygon for you, but you can also do this yourself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network object to which the mesh is added |
required |
polygon
|
Polygon | MultiPolygon
|
Geometry within which the mesh is generated |
required |
edge_length
|
float | None
|
Distance for which the polygon boundary is discretized (by approximation). This is also the approximate edge length of the triangles. If None, the existing boundary coordinates determine the triangle size. Defaults to None. |
None
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_altitude_from_raster(network: Network, rasterpath: Path | str, where: RasterStatPosition | str = 'face', stat: str = 'mean', fill_option: FillOption | str = 'fill_value', fill_value: float | None = None, *, min_coverage_frac: float = 0.0, coverage_weight: zonal.CoverageWeight = 'fraction') -> None
¶
Method to determine level of nodes
This function works faster for large amounts of cells, since it does not draw polygons but checks for nearest neighbours (voronoi) based on interpolation.
Note that the raster is not clipped. Any values outside the bounds are also taken into account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network for which the altitude is determined. Should contain a 2d mesh |
required |
rasterpath
|
Path | str
|
Path to the raster file from which altitude values are read |
required |
where
|
RasterStatPosition | str
|
Whether to determine altitude at cell "face" centers or "node" locations. Defaults to "face". |
'face'
|
stat
|
str
|
exactextract statistic to derive from the raster cells per face/node, such as "mean", "median" or "quantile(q=0.9)". Defaults to "mean". |
'mean'
|
fill_option
|
FillOption | str
|
How to fill locations without a raster value: "fill_value", "nearest", or "interpolate". Defaults to "fill_value". |
'fill_value'
|
fill_value
|
float | None
|
Value used when fill_option is "fill_value", or the fallback for values that cannot be interpolated. Defaults to None. |
None
|
min_coverage_frac
|
float
|
Smallest fraction of a raster cell a face or node polygon must cover for the cell to count. Defaults to 0, every touched cell. |
0.0
|
coverage_weight
|
str
|
"fraction" counts raster cells by the fraction of them the polygon covers, "none" counts them fully. Defaults to "fraction". |
'fraction'
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_clip(network: Network, polygon: GeometryList | Polygon | MultiPolygon, deletemeshoption: mk.DeleteMeshOption = mk.DeleteMeshOption.INSIDE_AND_INTERSECTED, inside: bool = True) -> None
¶
Clip the mesh (currently implemented for 2d) and clean remaining hanging edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network for which the mesh is clipped |
required |
polygon
|
GeometryList | Polygon | MultiPolygon
|
Polygon within which the mesh is clipped |
required |
deletemeshoption
|
DeleteMeshOption
|
Option for deleting nodes inside/outside polygon. Defaults to mk.DeleteMeshOption.INSIDE_AND_INTERSECTED. |
INSIDE_AND_INTERSECTED
|
inside
|
bool
|
Whether to clip inside or outside the polygon. Defaults to True. |
True
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_from_netcdf(network: Network, path: Path | str) -> None
¶
Read a 2d mesh from a UGRID netCDF file into the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network object to which the mesh is added |
required |
path
|
Path | str
|
Path to the UGRID netCDF file |
required |
Source code in hydrolib/dhydamo/geometry/mesh.py
101 102 103 104 105 106 107 108 109 | |
mesh2d_refine(network: Network, polygon: Polygon | MultiPolygon, steps: int, refine_parameters: dict | None = None) -> None
¶
Refine mesh 2d within (list of) polygon or multipolygon, with a certain number of refinement steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
Network
|
Network for which the mesh is refined |
required |
polygon
|
Polygon | MultiPolygon
|
Polygon within which the mesh is refined |
required |
steps
|
int
|
Number of steps in the refinement |
required |
refine_parameters
|
dict | None
|
Overrides for the default mk.MeshRefinementParameters attributes. Defaults to None. |
None
|
Source code in hydrolib/dhydamo/geometry/mesh.py
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 | |
mesh2d_gridgeom
¶
Mesh2D_GG
¶
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 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 | |
altitude_from_raster(rasterpath, where='face', stat='mean', missing='default')
¶
Method to determine level of nodes
This function works faster for large amounts of cells, since it does not draw polygons but checks for nearest neighbours (voronoi) based on interpolation.
Note that the raster is not clipped. Any values outside the bounds are also taken into account.
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
clean_nodes(xnodes, ynodes, edge_nodes, face_nodes)
¶
Method to clean the nodes. Edges that do not form a cell are deleted. Alternative method based on checking tuple pairs
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
clip_mesh_by_polygon(polygon, outside=True)
¶
Removes part of the existing mesh within the given polygon.
Parameters¶
polygon : list, Polygon, MultiPolygon A polygon, multi-polygon or list of multiple polygons or multipolygons. The faces within the polygons are removed.
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
clip_nodes(xnodes, ynodes, edge_nodes, polygon, keep_outside=False)
¶
Method that returns a set of nodes that is clipped within a given polygon.
Parameters¶
xnodes : X-coordinates of mesh nodes ynodes : Y-coordinates of mesh nodes edge_nodes : node ids of edges. These are assumed in 1-based numbering polygon : Polygon, Multipolygon or list of
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
faces_to_centroid()
¶
The find_cells function does not always return face coordinates that are all accepted by the interactor. The coordinates are placed on the circumcenters, and placed on the face border in case of a coordinate that is outside the cells.
This function shifts the face coordinates towards the centroid (center of gravity) of the cell.
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
Rectangular
¶
Bases: Mesh2D_GG
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
generate_grid(x0, y0, dx, dy, ncols, nrows, clipgeo=None, rotation=0)
¶
Generate rectangular grid based on the origin (x0, y0) the cell sizes (dx, dy), the number of columns and rows (ncols, nrows) and a rotation in degrees (default=0) A geometry (clipgeo) can be given to clip the grid.
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
generate_within_polygon(polygon, cellsize, rotation=0)
¶
Function to generate a grid within a polygon. It uses the function 'generate_grid' but automatically detects the extent.
Parameters¶
polygon : (list of) shapely.geometry.Polygon or a shapely.geometry.MultiPolygon
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
refine(polygon, level, cellsize, debug=False, dflowfm_path=None)
¶
Parameters¶
polygon : list of tuples Polygon in which to refine level : int Number of times to refine
Source code in hydrolib/dhydamo/geometry/mesh2d_gridgeom.py
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 | |
models
¶
GeometryList
¶
Bases: GeometryList
Source code in hydrolib/dhydamo/geometry/models.py
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 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 | |
geoms
property
¶
Returns GeometryList objects based on spliited by geometries
spatial
¶
find_nearest_branch(branches, geometries, method='overal', maxdist=5)
¶
Method to determine nearest branch for each geometry. The nearest branch can be found by finding t from both ends (ends) or the nearest branch from the geometry as a whole (overal), the centroid (centroid), or intersecting (intersect).
Parameters¶
branches : geopandas.GeoDataFrame Geodataframe with branches geometries : geopandas.GeoDataFrame Geodataframe with geometries to snap method='overal' : str Method for determine branch maxdist=5 : int or float Maximum distance for finding nearest geometry minoffset : int or float Minimum offset from the end of the corresponding branch in case of method=equal
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
get_voronoi_around_nodes(nodes: np.ndarray, facedata: gpd.GeoDataFrame) -> gpd.GeoDataFrame
¶
Creates voronoi polygons around face nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
ndarray
|
xy coordinates of face nodes |
required |
facedata
|
GeoDataFrame
|
GeoDataFrame with face properties |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
gpd.GeoDataFrame: Creating GeoDataFrame with created polygons and their properties |
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
line_parts(geometry) -> list[LineString]
¶
All LineStrings in a geometry, including those in a GeometryCollection; points are ignored.
Source code in hydrolib/dhydamo/geometry/spatial.py
311 312 313 314 315 316 317 | |
minimum_bounds_fixed_rotation(polygon, angle)
¶
Get the minimum box for a polygon with a given axes rotation.
Parameters¶
polygon : shapely.geometry.Polygon Polygon that is rotated angle : int or float Rotation of the polygon in degrees
Returns¶
tuple Tuple with origin (x, y), xsize and ysize
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
orthogonal_line(line: LineString, offset: float, width: float = 1.0) -> list[tuple[float]]
¶
Parameters¶
line : shapely.geometry.LineString Line geometry object on which the orthogonal line is drawn offset : float Offset of the orthogonal line along line width : float Width of the orthogonal line
Returns¶
line : list List with coordinate tuples
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
points_to_branch_values(branches: gpd.GeoDataFrame, points: gpd.GeoDataFrame, column: str, max_distance: float = 5.0, junction_tolerance: float = 0.1) -> dict[str, tuple[list[float], list[float]]]
¶
Convert point values to values per branch and chainage, e.g. for a 1dField file.
If the points have columns 'branchid' and 'chainage' (e.g. from a model result on the same network),
these are used. Otherwise each point is snapped to the nearest branch within max_distance and its
chainage is the projection on that branch. D-Flow FM interpolates linearly between the chainages of a
branch and keeps the value constant beyond the first and last chainage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branches
|
GeoDataFrame
|
branches, with the branch id as index |
required |
points
|
GeoDataFrame
|
points with values |
required |
column
|
str
|
column with the values |
required |
max_distance
|
float
|
maximum distance to snap a point to a branch. Defaults to 5.0. |
5.0
|
junction_tolerance
|
float
|
distance within which a point at a branch end is also used for the other branches at that junction. Defaults to 0.1. |
0.1
|
Returns:
| Type | Description |
|---|---|
dict[str, tuple[list[float], list[float]]]
|
dict[str, tuple[list[float], list[float]]]: chainages and values per branch id |
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
polygons_to_branch_values(branches: gpd.GeoDataFrame, polygons: gpd.GeoDataFrame, column: str, step: float = 0.01) -> dict[str, tuple[list[float], list[float]]]
¶
Convert polygon values to values per branch and chainage, e.g. for a 1dField file.
Each part of a branch inside a polygon gets the value of that polygon. At polygon boundaries two
chainages are written, step apart, so the value changes stepwise. Where polygons overlap, the last
polygon in the GeoDataFrame determines the value. Parts of a branch outside all polygons get no
chainage: D-Flow FM interpolates linearly between the given chainages and keeps the value constant
beyond the first and last chainage of a branch. Branches that do not intersect any polygon are left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branches
|
GeoDataFrame
|
branches, with the branch id as index |
required |
polygons
|
GeoDataFrame
|
(multi)polygons with values |
required |
column
|
str
|
column with the values |
required |
step
|
float
|
distance between the two chainages at a polygon boundary. Defaults to 0.01. |
0.01
|
Returns:
| Type | Description |
|---|---|
dict[str, tuple[list[float], list[float]]]
|
dict[str, tuple[list[float], list[float]]]: chainages and values per branch id |
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
rasterize_polygons(polygons: gpd.GeoDataFrame, column: str, cellsize: float, fill: float = -999.0) -> tuple[np.ndarray, rasterio.Affine]
¶
Rasterize the values in a column of a polygon GeoDataFrame.
The raster extent is aligned to a multiple of the cellsize. Where polygons overlap, the last polygon in the GeoDataFrame determines the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygons
|
GeoDataFrame
|
(multi)polygons with values |
required |
column
|
str
|
column with the values to burn into the raster |
required |
cellsize
|
float
|
raster cell size |
required |
fill
|
float
|
value for cells outside the polygons. Defaults to -999.0. |
-999.0
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, Affine]
|
tuple[np.ndarray, rasterio.Affine]: float32 array and its transform |
Source code in hydrolib/dhydamo/geometry/spatial.py
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 | |
rotate_coordinates(origin, theta, xcrds, ycrds)
¶
Rotate coordinates around origin (x0, y0) with a certain angle (radians)
Source code in hydrolib/dhydamo/geometry/spatial.py
27 28 29 30 31 32 33 34 | |
viz
¶
zonal
¶
Zonal statistics computed with exactextract.
exactextract computes the exact fraction of every pixel covered by a zone. Two of its arguments, applied to every statistic, use that fraction:
min_coverage_fracselects the pixels: those covered less are ignored.coverage_weightsets how they count:"none"counts every selected pixel fully,"fraction"by the fraction of it the zone covers.
The defaults, min_coverage_frac=0 and coverage_weight="fraction", give
exact coverage weighting: every touched pixel counts by the fraction of it the
zone covers. coverage_weight="none" counts every touched pixel fully, like
all_touched=True in GDAL and rasterstats; adding min_coverage_frac=0.5
counts only pixels that are at least half covered, close to their default
pixel-centre rule.
Raster files are read through rasterio. exactextract reads windows of at most
max_cells_in_memory cells, so rasters may be larger than memory.
ZonalCache
¶
Reuses the results of identical :func:zonal_stats and
:func:zonal_category_counts calls on raster files.
A result is reused only when the zones (index, CRS and geometries), the
raster file (path, size and modification time), and every argument that
affects the result are the same. Arrays and open datasets are never cached.
Results are copied in and out, so changing a returned result does not
change the cache. Entries stay until :meth:clear.
Source code in hydrolib/dhydamo/geometry/zonal.py
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 | |
clear()
¶
Remove all cached results.
Source code in hydrolib/dhydamo/geometry/zonal.py
354 355 356 | |
zonal_category_counts(zones, raster, **kwargs) -> pd.DataFrame
¶
:func:zonal_category_counts, reusing an identical earlier result.
Source code in hydrolib/dhydamo/geometry/zonal.py
366 367 368 | |
zonal_stats(zones, raster, statistics, **kwargs) -> pd.DataFrame
¶
:func:zonal_stats, reusing an identical earlier result.
Source code in hydrolib/dhydamo/geometry/zonal.py
358 359 360 361 362 363 364 | |
zonal_category_counts(zones: gpd.GeoDataFrame | gpd.GeoSeries, raster: Path | str | np.ndarray, *, strategy: Strategy, min_coverage_frac: float = 0.0, coverage_weight: CoverageWeight = 'fraction', affine=None, raster_crs=None, nodata: float | None = None, max_cells_in_memory: int = 30000000, progress=False) -> pd.DataFrame
¶
Count the cells of each raster class within every polygonal zone.
Parameters are those of :func:zonal_stats. With
coverage_weight="fraction", partly covered cells count for their covered
fraction, so counts are not whole numbers.
Returns¶
pandas.DataFrame
One float column per class code, aligned to zones.index. Integer
class codes are ints. Classes absent from a zone count zero.
Source code in hydrolib/dhydamo/geometry/zonal.py
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 | |
zonal_stats(zones: gpd.GeoDataFrame | gpd.GeoSeries, raster: Path | str | np.ndarray, statistics: str | list[str] | tuple[str, ...], *, strategy: Strategy, min_coverage_frac: float = 0.0, coverage_weight: CoverageWeight = 'fraction', affine=None, raster_crs=None, nodata: float | None = None, max_cells_in_memory: int = 30000000, progress=False) -> pd.DataFrame
¶
Calculate scalar raster statistics for every polygonal zone.
Parameters¶
zones : geopandas.GeoDataFrame or geopandas.GeoSeries
Polygon zones. Zones without a CRS, such as mesh polygons, are taken to
be in the raster CRS.
raster : pathlib.Path, str, numpy.ndarray, or rasterio dataset
Raster file, an open rasterio dataset, or a two-dimensional array
together with affine. Only band 1 is used.
statistics : str or sequence of str
Scalar exactextract statistics, such as "mean", "median",
"mode", "sum", or "quantile(q=0.9)". exactextract names the
result columns, e.g. "mean" and "quantile_90". Set the coverage
arguments below, not in these strings.
strategy : {'feature-sequential', 'raster-sequential'}
exactextract processing strategy. 'feature-sequential' reads the
raster under each zone in turn, which suits few, large zones.
'raster-sequential' reads the raster once, which suits many small
zones such as mesh cells.
min_coverage_frac : float, default 0
Smallest fraction of a pixel the zone must cover for the pixel to
count, from 0 (every touched pixel) to 1.
coverage_weight : {'fraction', 'none'}, default 'fraction'
How the selected pixels count: 'fraction' by the fraction of them
the zone covers, 'none' fully.
affine : affine.Affine, optional
North-up transform of an array raster.
raster_crs : pyproj.CRS or str, optional
CRS of a raster that has none of its own, such as an array.
nodata : float, optional
Value to exclude from an array raster. Files use their own nodata.
max_cells_in_memory : int, default 30_000_000
Largest number of raster cells exactextract reads at once.
progress : bool or callable, default False
Show a progress bar, or call exactextract's progress(fraction,
message) as the calculation proceeds.
Returns¶
pandas.DataFrame
One float column per statistic, aligned to zones.index. Zones
without valid pixels get NaN.
Notes¶
Reads use GDAL_NUM_THREADS=ALL_CPUS to decompress the raster, unless
GDAL_NUM_THREADS is set in the environment.
Source code in hydrolib/dhydamo/geometry/zonal.py
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 | |
zonal_stats_per_raster(zones: gpd.GeoDataFrame | gpd.GeoSeries, rasters: list, statistic: str, *, strategy: Strategy, min_coverage_frac: float = 0.0, coverage_weight: CoverageWeight = 'fraction', affine=None, raster_crs=None, nodata: float | None = None, max_cells_in_memory: int = 30000000, progress=False) -> pd.DataFrame
¶
Calculate one scalar raster statistic for every zone, in each raster.
Faster than calling :func:zonal_stats per raster, as for a time series of
rasters: exactextract handles up to _RASTERS_PER_CALL rasters at once.
Parameters¶
rasters : list of pathlib.Path, str, rasterio dataset, or array
Raster files, open rasterio datasets, or two-dimensional arrays that
share affine and nodata. Lazy arrays, such as xarray's, are
read only when their turn comes, so the list may hold more than fits in
memory. Only band 1 of a file is used.
statistic : str
One scalar exactextract statistic, such as "mean".
Other parameters are those of :func:zonal_stats.
Returns¶
pandas.DataFrame
One float column per raster, numbered by position in rasters, and
aligned to zones.index.
Source code in hydrolib/dhydamo/geometry/zonal.py
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 | |