Skip to content

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
def 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.

    Args:
        linestring (Union[LineString, MultiLineString]): LineString of MultiLineString shapely geometry object

    Returns:
        List[LineString]: list of shapely geometry LineString objects
    """
    return _as_geometry_list(linestring, LineString, MultiLineString)

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
def 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.

    Args:
        linestring (Union[Point, MultiPoint]): Point of MultiPoint shapely geometry object

    Returns:
        List[Point]: list of shapely geometry Point objects
    """
    return _as_geometry_list(point, Point, MultiPoint)

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
def 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.

    Args:
        linestring (Union[Polygon, MultiPolygon]): Polygon of MultiPolygon shapely geometry object

    Returns:
        List[Polygon]: list of shapely geometry Polygon objects
    """
    return _as_geometry_list(polygon, Polygon, MultiPolygon)

mesh

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
def 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.

    Args:
        network (Network): Network in which the connections are made
        branchids (list[str] | None, optional): List of branchid's to connect. If None, all branches are connected. Defaults to None.
        within (Polygon | MultiPolygon | None, optional): Area within which connections are made. Defaults to None.
        max_length (float, optional): Max edge length. Defaults to np.inf.
    """
    # Fetch the 2d mesh once and reuse it below - each network._mesh2d.mesh2d_*
    # property otherwise re-fetches and re-validates the whole mesh from meshkernel.
    mesh2d_output = network._mesh2d.get_mesh2d()

    if within is None:
        # If not provided, create a box from the maximum bounds
        xmin = min(network._mesh1d.mesh1d_node_x.min(), mesh2d_output.node_x.min())
        xmax = max(network._mesh1d.mesh1d_node_x.max(), mesh2d_output.node_x.max())
        ymin = min(network._mesh1d.mesh1d_node_y.min(), mesh2d_output.node_y.min())
        ymax = max(network._mesh1d.mesh1d_node_y.max(), mesh2d_output.node_y.max())

        within = box(xmin, ymin, xmax, ymax)

    # If a 'within' polygon was provided, convert it to a geometrylist
    geometrylist = _geomlist_from_polygon(within)

    # Get the nodes for the specific branch ids
    node_mask = network._mesh1d.get_node_mask(branchids)

    # Snapshot the already present links. They are restored after the call below
    # regardless of whether that call preserves them itself (see _filter_links_on_idx).
    present_links = network._link1d2d.link1d2d

    # Generate links
    network._link1d2d._link_from_1d_to_2d(node_mask, polygon=geometrylist)

    # Filter the newly generated links that are longer than the max distance.
    # Links that were already present are never subject to this filter.
    current_links = network._link1d2d.link1d2d
    is_new = _new_link_mask(current_links, present_links)
    id1d = current_links[is_new, 0]
    id2d = current_links[is_new, 1]
    nodes1d = np.stack(
        [network._mesh1d.mesh1d_node_x[id1d], network._mesh1d.mesh1d_node_y[id1d]],
        axis=1,
    )
    faces2d = np.stack(
        [mesh2d_output.face_x[id2d], mesh2d_output.face_y[id2d]],
        axis=1,
    )
    lengths = np.hypot(nodes1d[:, 0] - faces2d[:, 0], nodes1d[:, 1] - faces2d[:, 1])
    keep_mask = ~is_new
    keep_mask[is_new] = lengths < max_length
    keep = np.nonzero(keep_mask)[0]
    _filter_links_on_idx(network, keep, present_links)

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
def 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.

    Args:
        network (Network): Network in which the links are made. Should contain a 1d and 2d mesh
        branchids (list[str] | None, optional): List is branch id's for which the connections are made. Defaults to None.
        within (Polygon | MultiPolygon | None, optional): Clipping polygon for 2d mesh that is. Defaults to None.
    """
    # Fetch the 2d mesh once and reuse it below - each network._mesh2d.mesh2d_*
    # property otherwise re-fetches and re-validates the whole mesh from meshkernel.
    mesh2d_output = network._mesh2d.get_mesh2d()

    # Get the max edge distance
    nodes2d = np.stack([mesh2d_output.node_x, mesh2d_output.node_y], axis=1)
    edge_node_crds = nodes2d[network._mesh2d.mesh2d_edge_nodes]

    diff = edge_node_crds[:, 0, :] - edge_node_crds[:, 1, :]
    maxdiff = np.hypot(diff[:, 0], diff[:, 1]).max()

    # Create multilinestring from branches
    nodes1d = np.stack(
        [network._mesh1d.mesh1d_node_x, network._mesh1d.mesh1d_node_y], axis=1
    )

    # Buffer the branches with the max cell distances
    mls = MultiLineString(nodes1d[network._mesh1d.mesh1d_edge_nodes].tolist())
    area = mls.buffer(maxdiff)

    # If a within polygon is provided, clip the buffered area with this polygon.
    if within is not None:
        area = area.intersection(within)

    # Create an array with 2d facecenters and check which intersect the (clipped) area
    faces2d = np.stack([mesh2d_output.face_x, mesh2d_output.face_y], axis=1)
    mpgl = mk.GeometryList(*faces2d.T.copy())
    idx = np.zeros(len(faces2d), dtype=bool)

    for subarea in common.as_polygon_list(area):
        subarea = GeometryList.from_polygon(subarea)
        sub_idx = network.meshkernel.polygon_get_included_points(subarea, mpgl)
        idx |= sub_idx.values == 1

    # Check for each of the remaining faces, if it actually crosses the branches
    where = np.nonzero(idx)[0]

    # Return early if there are no candidates
    if not where.size:
        return

    # Check which face crosses a branch
    candidate_face_nodes = network._mesh2d.mesh2d_face_nodes[idx]
    face_polygons = []
    for face_idxs in candidate_face_nodes:
        # nodes2d always has 4 columns if the grid is a mix of triangular and 
        # rectangular, but a triangular cell only has 3 nodes. A triangular 
        # cell will then have a column of invalid indices. This needs to be
        # checked per cell.
        # Use a polygon instead of linestring to enforce a closed ring and to
        # test both boundary and/or face intersection.
        valid_face_idxs = face_idxs[face_idxs >= 0]
        face_polygons.append(Polygon(nodes2d[valid_face_idxs]))
    idx[where] = shapely.intersects(face_polygons, mls)

    # Use the remaining points to create the links
    multipoint = mk.GeometryList(
        x_coordinates=faces2d[idx, 0], y_coordinates=faces2d[idx, 1]
    )

    # Get the nodes for the specific branch ids
    node_mask = network._mesh1d.get_node_mask(branchids)

    # Snapshot the already present links. They are restored after the call below
    # regardless of whether that call preserves them itself (see _filter_links_on_idx).
    present_links = network._link1d2d.link1d2d

    # Generate links
    network._link1d2d._link_from_2d_to_1d_embedded(node_mask, polygons=multipoint)

    # Nothing computed here is filtered on its own merits, so keep it all
    keep = np.arange(len(network._link1d2d.link1d2d))
    _filter_links_on_idx(network, keep, present_links)

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
def 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.

    Args:
        network (Network): Network in which the links are made. Should contain a 1d and 2d mesh
        dist_factor (float | None, optional): Factor to determine which links are kept (see description above). Defaults to 2.0.
        branchids (list[str] | None, optional): List is branch id's for which the conncetions are made. Defaults to None.
        within (Polygon | MultiPolygon | None, optional): Clipping polygon for 2d mesh that is. Defaults to None.
        max_length (float, optional): Max edge length. Defaults to np.inf.
    """

    geometrylist = network.meshkernel.mesh2d_get_mesh_boundaries_as_polygons()
    mpboundaries = GeometryList(**geometrylist.__dict__).to_geometry()
    if within is not None:
        # If a 'within' polygon was provided, get the intersection with the meshboundaries
        # and convert it to a geometrylist
        # Note that the provided meshboundaries is a (list of) polygon(s). Holes are provided
        # as polygons as well, which dont make it a valid MultiPolygon
        if isinstance(mpboundaries, Polygon):
            geom = MultiPolygon([mpboundaries.intersection(within)])
            geometrylist = GeometryList.from_geometry(geom)
        else:
            geom = [geom.intersection(within) for geom in mpboundaries.geoms]
            geom = MultiPolygon(common.as_polygon_list(geom))
            geometrylist = GeometryList.from_geometry(geom)

    # Get the nodes for the specific branch ids
    node_mask = network._mesh1d.get_node_mask(branchids)

    # Snapshot the already present links. They are restored after the call below
    # regardless of whether that call preserves them itself (see _filter_links_on_idx).
    present_links = network._link1d2d.link1d2d

    # Generate links
    network._link1d2d._link_from_2d_to_1d_lateral(
        node_mask, polygon=geometrylist, search_radius=max_length
    )

    # If the provided distance factor was None, no further selection is needed, all links are kept.
    if dist_factor is None:
        return

    # Create multilinestring
    if isinstance(mpboundaries, Polygon):
        multilinestring = MultiLineString([mpboundaries.exterior])
    else:
        multilinestring = MultiLineString([poly.exterior for poly in mpboundaries.geoms])

    # Find the links that intersect the boundary close to the origin    
    id1d = network._link1d2d.link1d2d[:, 0]
    id2d = network._link1d2d.link1d2d[:, 1]

    nodes1d = np.stack(
        [network._mesh1d.mesh1d_node_x[id1d], network._mesh1d.mesh1d_node_y[id1d]],
        axis=1,
    )
    # Fetch the 2d mesh once and reuse it below - each network._mesh2d.mesh2d_*
    # property otherwise re-fetches and re-validates the whole mesh from meshkernel.
    mesh2d_output = network._mesh2d.get_mesh2d()
    faces2d = np.stack(
        [mesh2d_output.face_x[id2d], mesh2d_output.face_y[id2d]],
        axis=1,
    )
    nodes2d = np.stack([mesh2d_output.node_x, mesh2d_output.node_y], axis=1)
    nodes2d_idx = network._mesh2d.mesh2d_face_nodes[id2d]
    keep = []
    for i, (node1d, face2d, node2d) in enumerate(
        zip(nodes1d, faces2d, nodes2d_idx)
    ):
        # nodes2d always has 4 columns if the grid is a mix of triangular and 
        # rectangular, but a triangular cell only has 3 nodes. A triangular 
        # cell will then have a column of invalid indices. This needs to be
        # checked per cell.
        face_node_crds = nodes2d[node2d[node2d >= 0]]                

        # Calculate distance between face edge and face center
        x1 = face_node_crds[:,0]
        y1 = face_node_crds[:,1]
        x0, y0 = face2d[0], face2d[1]
        distance = np.hypot(x1-x0, y1-y0).mean() 

        isect = multilinestring.intersection(LineString([face2d, node1d]))

        # If the intersection is for some reason not a Point of Multipoint, skip it.
        if not isinstance(isect, (Point, MultiPoint)):
            continue

        # Skip the link if it has more than one intersection with the boundary
        isect_list = common.as_point_list(isect)
        if len(isect_list) != 1:
            continue

        # If the distance to the mesh 2d exterior intersection is smaller than
        # the compared distance, keep it.
        dist = np.hypot(*(face2d - isect_list[0].coords[:][0]))
        if dist < dist_factor * distance:
            keep.append(i)

    # Select the remaining links
    _filter_links_on_idx(network, keep, present_links=present_links)

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
def 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.
    """
    # Can only be done after links have been generated
    if not list(network._mesh1d.network1d_node_id) or not list(
        network._mesh2d.mesh2d_face_nodes
    ):
        logger.warning("1d nodes or 2d faces are not present.")
        return

    # Fetch the current 1d2d links once - like network._mesh2d.mesh2d_*, this
    # property re-fetches and re-validates the contacts from meshkernel on every access.
    link1d2d = network._link1d2d.link1d2d
    if link1d2d.shape[0] == 0:
        logger.warning("No 1d2d-links present.")
        return

    # Select mesh1d nodes that are only present in a single edge.
    # link1d2d[:, 0] stores mesh1d node indices, so endpoint detection must
    # use the same mesh1d index space.
    edge_nodes = network._mesh1d.mesh1d_edge_nodes
    edgeid, counts = np.unique(edge_nodes, return_counts=True)
    to_remove = edgeid[counts == 1]

    # Remove links connected to 1d endpoint nodes.
    link_nodes = link1d2d[:, 0]
    keep = ~np.isin(link_nodes, to_remove)

    _filter_links_on_idx(network, keep)

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
def links1d2d_remove_within(
    network: Network, within: Polygon | MultiPolygon
) -> None:
    """Remove 1d2d links within a given polygon or multipolygon

    Args:
        network (Network): The network from which the links are removed
        within (Polygon | MultiPolygon): The polygon that indicates which to remove
    """

    # Create an array with 2d facecenters and 1d nodes, that form the links
    links = network._link1d2d.link1d2d
    nodes1d = np.stack(
        [
            network._mesh1d.mesh1d_node_x[links[:, 0]],
            network._mesh1d.mesh1d_node_y[links[:, 0]],
        ], axis=1
    )
    # Fetch the 2d mesh once and reuse it below - each network._mesh2d.mesh2d_*
    # property otherwise re-fetches and re-validates the whole mesh from meshkernel.
    mesh2d_output = network._mesh2d.get_mesh2d()
    faces2d = np.stack(
        [
            mesh2d_output.face_x[links[:, 1]],
            mesh2d_output.face_y[links[:, 1]],
        ], axis=1
    )

    # Create GeometryList MultiPoint object (dedupe to avoid MK duplicate-point issues)
    uniq_faces2d, inv_faces2d = np.unique(faces2d, axis=0, return_inverse=True)
    uniq_nodes1d, inv_nodes1d = np.unique(nodes1d, axis=0, return_inverse=True)
    mpgl_faces2d = GeometryList(*uniq_faces2d.T.copy())
    mpgl_nodes1d = GeometryList(*uniq_nodes1d.T.copy())
    idx_faces = np.zeros(len(uniq_faces2d), dtype=bool)
    idx_nodes = np.zeros(len(uniq_nodes1d), dtype=bool)

    # Check which links intersect the provided area
    for polygon in common.as_polygon_list(within):
        subarea = GeometryList.from_geometry(polygon)
        idx_faces |= (
            network.meshkernel.polygon_get_included_points(subarea, mpgl_faces2d).values
            == 1
        )
        idx_nodes |= (
            network.meshkernel.polygon_get_included_points(subarea, mpgl_nodes1d).values
            == 1
        )

    # Remove these links
    idx = idx_faces[inv_faces2d] | idx_nodes[inv_nodes1d]
    keep = ~idx
    _filter_links_on_idx(network, keep)

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
def 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

    Args:
        network (Network): Network to which the branch is added
        linestring (LineString): The geometry of the new branch
        node_distance (float | int): Preferred node distance between branch nodes
        name (str | None, optional): Name of the branch. If not given, a name is generated. Defaults to None.
        structure_chainage (list[float] | None, optional): Positions of structures along the branch. Defaults to None.
        max_dist_to_struc (float | None, optional): Max distance of a mesh point to a structure. Defaults to None.

    Returns:
        str: name of added branch
    """

    branch = Branch(geometry=np.array(linestring.coords[:]))
    branch.generate_nodes(
        mesh1d_edge_length=node_distance,
        structure_chainage=structure_chainage,
        max_dist_to_struc=max_dist_to_struc,
    )
    branchid = network.mesh1d_add_branch(branch, name=name)

    return branchid

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
def 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

    Args:
        network (Network): The network to which the branches are added
        branches (gpd.GeoDataFrame): GeoDataFrame with branches
        branch_name_col (str): Name of the column in the GeoDataFrame with the branchnames
        node_distance (float): Preferred 1d mesh distance
        max_dist_to_struc (float | None, optional): Maximum distance to structure. Defaults to None.
        structures (gpd.GeoDataFrame | None, optional): GeoDataFrame with structures. Must contain a column branchid and chainage. Defaults to None.
    """

    # Create empty dictionary for structure chainage
    structure_chainage = {}

    # If structures are given, collect offsets per branch
    if structures is not None:
        # Get structure data from dfs
        ids_offsets = structures[["branchid", "chainage", "id"]].copy()
        idx = structures["branchid"] != ""
        missing = structures.branchid.isna()
        if missing.any():
            n_missing = int(missing.sum())
            if n_missing > 5:
                logger.warning(
                    "%d structures are not linked to a branch (too many to show).",
                    n_missing,
                )
            else:
                logger.warning(
                    "%d structures (%s) are not linked to a branch.",
                    n_missing,
                    structures.loc[missing, "id"].to_list(),
                )
        ids_offsets = ids_offsets.loc[idx, :]

        # For each branch
        for branchid, group in ids_offsets.groupby("branchid")[["chainage", "id"]]:
            # Check if structures are located at the same offset
            u, c = np.unique(group.chainage, return_counts=True)
            if any(c > 1):
                logger.warning(
                    "Structures %s have the same location.",
                    ", ".join(str(v) for v in group.loc[group.chainage.isin(u[c > 1]), "id"].tolist()),
                )
            # Add to dictionary
            structure_chainage[branchid] = u

    # Loop over all branches, and add structures
    for branchname, geometry in zip(
        branches[branch_name_col].tolist(), branches["geometry"].tolist()
    ):
        # Create branch
        branch = Branch(geometry=np.array(geometry.coords[:]))
        # Generate nodes on branch
        branch.generate_nodes(
            mesh1d_edge_length=node_distance,
            structure_chainage=structure_chainage.get(branchname, None),
            max_dist_to_struc=max_dist_to_struc,
        )
        network.mesh1d_add_branch(branch, name=branchname)

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
def 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.

    Args:
        branches (gpd.GeoDataFrame): HyDAMO GeoDataFrame containing branches.
        missing (list[str]): list of branches that have so far not been allocated a cross section.
        order_attribute (str): Attribute of branches based on which order numbers are allocated.
        network (Network): FM network geometry to which the order numbers should be written
        exceptions (list[str] | None, optional): List of branch codes that should not be given an order number. Defaults to None.

    Returns:
        list[str]: list of branches that still do not have a cross section
    """
    if exceptions is None:
        exceptions = []

    j = 0
    branches["order"] = np.nan
    for value in branches[order_attribute].unique():
        if value is not None and not all(x in missing for x in branches.loc[branches.loc[:, order_attribute] == value, "code"]):
            branches.loc[branches.loc[:, order_attribute] == value, "order"] = int(j)
            j = j + 1
    for exception in exceptions:
        branches.loc[branches.code == exception, 'order']  = -1

    interpolation = []
    for ordernr in branches.order.unique():
        if ordernr > 0:
            mesh1d_set_branch_order(
                network,
                branches.code[branches.order == ordernr].to_list(),
                idx=int(ordernr),
            )
            interpolation = (
                interpolation + branches.code[branches.order == ordernr].to_list()
            )
    missing_after_interpolation = np.setdiff1d(missing, interpolation)
    return missing_after_interpolation

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
def 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.

    Args:
        network (Network): Network in which the branch order is set
        branchids (list[str]): List of branches to group
        idx (int | None, optional): Order number with which to update a branch. If
            None, the group is appended after the current highest order. Defaults to None.
    """
    # Get the ids (integers) of the branch names given by the user
    branchidx = np.isin(network._mesh1d.network1d_branch_id, branchids)
    # Get current order
    branchorder = network._mesh1d.network1d_branch_order
    # Update
    if idx is None:
        branchorder[branchidx] = branchorder.max() + 1
    else:
        if not isinstance(idx, int):
            raise TypeError("Expected integer.")
        branchorder[branchidx] = idx
    # Save
    network._mesh1d.network1d_branch_order = branchorder

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
def 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.

    Args:
        network (Network): Network object to which the mesh is added
        polygon (Polygon | MultiPolygon): Geometry within which the mesh is generated
        dx (float): Horizontal mesh spacing
        dy (float): Vertical mesh spacing
        deletemeshoption (mk.DeleteMeshOption, optional): Option for clipping mesh.
            Defaults to mk.DeleteMeshOption.INSIDE_NOT_INTERSECTED.
    """

    # Loop over polygons and create a grid for each polygon bounds
    mesh2d_hc = Mesh2d(meshkernel=mk.MeshKernel())
    polygon_list = GeometryList.from_geometry(polygon)
    for part in polygon_list.geoms:
        xmin, ymin, xmax, ymax = part.bounds
        rows = int((ymax - ymin) / dy)
        columns = int((xmax - xmin) / dx)
        if rows > 0 and columns > 0:
            mesh2d_hc.create_rectilinear(extent=part.bounds, dx=dx, dy=dy)

    # Store present 2d mesh (to be able to add)
    existing_mesh2d = network._mesh2d.get_mesh2d()

    # Create new network
    mesh2d_output = mesh2d_hc.get_mesh2d()
    network._mesh2d._set_mesh2d(
        mesh2d_output.node_x,
        mesh2d_output.node_y,
        mesh2d_output.edge_nodes,
    )

    # Clip and clean
    mesh2d_clip(
        network=network,
        polygon=polygon_list,
        deletemeshoption=deletemeshoption,
        inside=False,
    )

    # Merge with existing network
    if existing_mesh2d.node_x.size > 0:
        # Add all variables to existing mesh
        new_mesh2d = network._mesh2d.get_mesh2d()
        network._mesh2d._set_mesh2d(
            np.concatenate([existing_mesh2d.node_x, new_mesh2d.node_x]),
            np.concatenate([existing_mesh2d.node_y, new_mesh2d.node_y]),
            np.concatenate([
                existing_mesh2d.edge_nodes,
                new_mesh2d.edge_nodes + existing_mesh2d.edge_nodes.max() + 1,
            ]),
        )

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
def 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.

    Args:
        network (Network): Network object to which the mesh is added
        polygon (Polygon | MultiPolygon): Geometry within which the mesh is generated
        edge_length (float | None, optional): 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.
    """

    meshkernel = network._mesh2d.meshkernel
    for poly in common.as_polygon_list(polygon):
        # Interpolate coordinates on polygon with edge_length distance
        if edge_length is not None:
            poly = common.interp_polygon(poly, dist=edge_length)

        # Add triangular mesh within polygon
        # No scale_factor: meshkernel>=8.4 ignores it (Deltares/MeshKernelPy#291)
        meshkernel.mesh2d_make_triangular_mesh_from_polygon(_geomlist_from_polygon(poly))

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
def 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.

    Args:
        network (Network): Network for which the altitude is determined. Should contain a 2d mesh
        rasterpath (Path | str): Path to the raster file from which altitude values are read
        where (RasterStatPosition | str, optional): Whether to determine altitude at cell
            "face" centers or "node" locations. Defaults to "face".
        stat (str, optional): exactextract statistic to derive from the raster cells per
            face/node, such as "mean", "median" or "quantile(q=0.9)". Defaults to "mean".
        fill_option (FillOption | str, optional): How to fill locations without a raster value:
            "fill_value", "nearest", or "interpolate". Defaults to "fill_value".
        fill_value (float | None, optional): Value used when fill_option is "fill_value", or the
            fallback for values that cannot be interpolated. Defaults to None.
        min_coverage_frac (float, optional): Smallest fraction of a raster cell a face or
            node polygon must cover for the cell to count. Defaults to 0, every touched cell.
        coverage_weight (str, optional): "fraction" counts raster cells by the fraction of
            them the polygon covers, "none" counts them fully. Defaults to "fraction".
    """

    if isinstance(fill_option, str):
        fill_option = FillOption(fill_option)

    if isinstance(where, str):
        where = RasterStatPosition(where)

    # Select points on faces or nodes
    logger.info("Creating GeoDataFrame of cell faces.")

    # Fetch the 2d mesh once and reuse it below - each network._mesh2d.mesh2d_*
    # property otherwise re-fetches and re-validates the whole mesh from meshkernel.
    mesh2d_output = network._mesh2d.get_mesh2d()

    # Get coordinates where z-values are derived or centered
    xy = np.stack(
        [
            getattr(mesh2d_output, f"{where.value}_x"),
            getattr(mesh2d_output, f"{where.value}_y"),
        ],
        axis=1,
    )

    # Create cells as polygons
    xy_facenodes = np.stack([mesh2d_output.node_x, mesh2d_output.node_y], axis=1)
    # Use raw MeshKernel connectivity instead of mesh2d_face_nodes, whose
    # fixed-width rows contain padding for triangles and other shorter faces.
    # nodes_per_face marks each face boundary in the flattened coordinates.
    face_node_counts = mesh2d_output.nodes_per_face
    face_coords = xy_facenodes[mesh2d_output.face_nodes]
    # Shapely's ``indices`` groups coordinates into rings. Repeat each face ID
    # by its node count so every coordinate is assigned to the correct face.
    face_ids = np.arange(len(face_node_counts))
    face_idx = np.repeat(face_ids, face_node_counts)
    facedata = gpd.GeoDataFrame(
        geometry=shapely.polygons(shapely.linearrings(face_coords, indices=face_idx))
    )

    if where == RasterStatPosition.NODE:
        logger.info(
            "Generating voronoi polygons around cell centers for determining raster statistics."
        )
        facedata = spatial.get_voronoi_around_nodes(xy, facedata)

    df = zonal.zonal_stats(
        facedata,
        rasterpath,
        stat,
        strategy="raster-sequential",
        min_coverage_frac=min_coverage_frac,
        coverage_weight=coverage_weight,
    )
    # By position: exactextract names e.g. "quantile(q=0.9)" "quantile_90".
    zvalues = df.iloc[:, 0].to_numpy()

    isnan = np.isnan(zvalues)
    if isnan.any():
        # If interpolation or fill_option, but all are NaN, raise error.
        if isnan.all() and fill_option in [FillOption.NEAREST, FillOption.INTERPOLATE]:
            raise ValueError(
                "Only NaN values found, interpolation or nearest not possible."
            )

        # Fill fill_option values
        if fill_option == FillOption.FILL_VALUE:
            if fill_value is None:
                raise ValueError("Provide a fill_value (keyword argument).")
            # With default value
            zvalues[isnan] = fill_value

        elif fill_option == FillOption.NEAREST:
            # By looking for the nearest value in the grid
            # Create a KDTree of the known points
            tree = KDTree(data=xy[~isnan])
            idx = tree.query(x=xy[isnan])[1]
            zvalues[isnan] = zvalues[~isnan][idx]

        elif fill_option == FillOption.INTERPOLATE:
            if fill_value is None:
                raise ValueError(
                    "Provide a fill_value (keyword argument) to fill values that cannot be interpolated."
                )
            # By interpolating
            isnan = np.isnan(zvalues)
            interp = LinearNDInterpolator(
                xy[~isnan], zvalues[~isnan], fill_value=fill_value
            )
            zvalues[isnan] = interp(xy[isnan])

    # Set values to mesh geometry
    setattr(network._mesh2d, f"mesh2d_{where.value}_z", zvalues)

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
def 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.

    Args:
        network (Network): Network for which the mesh is clipped
        polygon (GeometryList | Polygon | MultiPolygon): Polygon within which the mesh is clipped
        deletemeshoption (mk.DeleteMeshOption, optional): Option for deleting nodes inside/outside
            polygon. Defaults to mk.DeleteMeshOption.INSIDE_AND_INTERSECTED.
        inside (bool, optional): Whether to clip inside or outside the polygon. Defaults to True.
    """

    if isinstance(polygon, GeometryList):
        geo = polygon.to_geometry()
        if not isinstance(geo, (Polygon, MultiPolygon)):
            raise TypeError(
                f"Expected to provided geometrylist to be interpreted as Polygon or MultiPolygon, not a {type(geo)}."
            )
    elif isinstance(polygon, (Polygon, MultiPolygon)):
        polygon = GeometryList.from_geometry(polygon)

    if inside:
        # Clip each single polygon
        for part in polygon.geoms:
            network.mesh2d_clip_mesh(_geomlist_from_polygon(part), deletemeshoption, inside)
    else:
        # Keep exterior
        clip_pol_ext = _geomlist_from_multipolygon(polygon, "exterior")
        if clip_pol_ext.x_coordinates.size > 0:
            network._mesh2d.meshkernel.mesh2d_delete(
                geometry_list=clip_pol_ext,
                delete_option=deletemeshoption,
                invert_deletion=True,
            )
        # # Clip interior
        clip_pol_int = _geomlist_from_multipolygon(polygon, "interior")
        if clip_pol_int.x_coordinates.size > 0:
            network._mesh2d.meshkernel.mesh2d_delete(
                geometry_list=clip_pol_int,
                delete_option=deletemeshoption,
                invert_deletion=False,
            )

    # Remove hanging edges
    network._mesh2d.meshkernel.mesh2d_delete_hanging_edges()    
    network._mesh2d.meshkernel.mesh2d_get()

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
def mesh2d_from_netcdf(network: Network, path: Path | str) -> None:
    """Read a 2d mesh from a UGRID netCDF file into the network.

    Args:
        network (Network): Network object to which the mesh is added
        path (Path | str): Path to the UGRID netCDF file
    """
    reader = UgridReader(path)
    reader.read_mesh2d(network._mesh2d)

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
def 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.

    Args:
        network (Network): Network for which the mesh is refined
        polygon (Polygon | MultiPolygon): Polygon within which the mesh is refined
        steps (int): Number of steps in the refinement
        refine_parameters (dict | None, optional): Overrides for the default
            mk.MeshRefinementParameters attributes. Defaults to None.
    """
    parameters = mk.MeshRefinementParameters(   
                refine_intersected=True,
                use_mass_center_when_refining=True,
                min_edge_size=1.0,
                refinement_type=2,
                connect_hanging_nodes=True,
                account_for_samples_outside_face=True,
                max_refinement_iterations=1,
                smoothing_iterations=5,
                max_courant_time=120.0,
                directional_refinement=False
    )
    if refine_parameters is not None:
        for key in refine_parameters:
            setattr(parameters, key, refine_parameters[key])

    for poly in common.as_polygon_list(polygon):
        network.mesh2d_refine_mesh(
            _geomlist_from_polygon(poly),
            level=steps,            
            parameters=parameters)                      

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
class Mesh2D_GG:

    def __init__(self, dll_path=None):
        self.fill_value_z = -999.0
        self.missing_z_value = None
        self.dll_path = dll_path
        self.meshgeomdim = meshgeomdim(pointer(c_char()), 2, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0)
        self.meshgeom = meshgeom(self.meshgeomdim)

    def clip_nodes(self, 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

        """

        # Find nodes within one of the polygons
        logger.info('Selecting nodes within polygon.')
        index = np.zeros(len(xnodes), dtype=bool)
        for poly in geometry.as_polygon_list(polygon):
            index = index | geometry.points_in_polygon(np.c_[xnodes, ynodes], poly)
        if keep_outside:
            index = ~index

        # Filter edge nodes, keep the ones that are within a polygon with both points
        nodeids_in_polygon = np.where(index)[0] + 1
        edge_nodes = edge_nodes[np.isin(edge_nodes, nodeids_in_polygon).all(axis=1)]

        # Remove edges that intersect the clipping polygons. This is needed for when the clipping polygon is narrow,
        # So it crosses an edge without containing one of the edge nodes
        logger.info('Creating LineString for each edge.')
        edges = [LineString(line) for line in np.c_[xnodes, ynodes][edge_nodes - 1]]

        logger.info('Checking for intersections.')
        isect = np.zeros(len(edge_nodes), dtype=bool)
        for poly in geometry.as_polygon_list(polygon):
            prepped = prep(poly.exterior)
            isect = isect | np.array([prepped.intersects(e) for e in edges])
            for interior in poly.interiors:
                prepped = prep(interior)
                isect = isect | np.array([prepped.intersects(e) for e in edges])
        edge_nodes = edge_nodes[~isect]

        # Remove edges that have a 1 count, until none are left
        logger.info('Remove edges with only a single node within the clip area.')
        unique, count = np.unique(edge_nodes, return_counts=True)
        while any(count == 1):
            count1nodes = unique[count == 1]
            edge_nodes = edge_nodes[~np.isin(edge_nodes, count1nodes).any(axis=1)]
            unique, count = np.unique(edge_nodes, return_counts=True)

        # Select remaining nodes
        xnodes = xnodes[unique - 1]
        ynodes = ynodes[unique - 1]

        # Make mapping for new id's
        new_id_mapping = {old_id: new_id + 1 for new_id, old_id in enumerate(unique)}
        edge_nodes = np.reshape([new_id_mapping[old_id] for old_id in edge_nodes.ravel()], edge_nodes.shape)

        return xnodes, ynodes, edge_nodes

    def clip_mesh_by_polygon(self, 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.
        """
        logger.info('Clipping 2D mesh by polygon.')

        # Get xnodes, ynodes and edge_nodes
        xnodes = self.meshgeom.get_values('nodex', as_array=True)
        ynodes = self.meshgeom.get_values('nodey', as_array=True)
        edge_nodes = self.meshgeom.get_values('edge_nodes', as_array=True)

        # Clip
        logger.info('Clipping nodes.')
        xnodes, ynodes, edge_nodes = self.clip_nodes(xnodes, ynodes, edge_nodes, polygon, keep_outside=outside)

        # Update dimensions
        self.meshgeomdim.numnode = len(xnodes)
        self.meshgeomdim.numedge = len(edge_nodes)

        # Add nodes and links
        self.meshgeom.set_values('nodex', xnodes)
        self.meshgeom.set_values('nodey', ynodes)
        self.meshgeom.set_values('edge_nodes', np.ravel(edge_nodes).tolist())

        # Determine what the cells are
        logger.info('Finding cell faces within clipped nodes.')
        self._find_cells(self.meshgeom, dll_path=self.dll_path)

        # Clean nodes, this function deletes nodes based on no longer existing cells
        face_nodes = self.meshgeom.get_values('face_nodes', as_array=True)
        logger.info('Removing incomplete faces.')
        cleaned = self.clean_nodes(xnodes, ynodes, edge_nodes, face_nodes)

        # If cleaning leaves no whole cells, return None
        if cleaned is None:
            raise ValueError('Clipping the grid does not leave any nodes.')
        # Else, get the arguments from the returned tuple
        else:
            xnodes, ynodes, edge_nodes, face_nodes = cleaned

        logger.info('Update mesh with clipped nodes, edges and faces.')
        # Update dimensions
        self.meshgeomdim.numnode = len(xnodes)
        self.meshgeomdim.numedge = len(edge_nodes)

        # Add nodes and links
        self.meshgeom.set_values('nodex', xnodes)
        self.meshgeom.set_values('nodey', ynodes)
        self.meshgeom.set_values('edge_nodes', np.ravel(edge_nodes).tolist())
        self.meshgeom.set_values('face_nodes', np.ravel(face_nodes).tolist())

    def clean_nodes(self, 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
        """

        # Select nodes the occur in any face
        node_selection = np.unique(face_nodes)
        node_selection = node_selection[node_selection!=-999]
        # If no nodes (because no faces), return None
        if not node_selection.any():
            return None

        # Remove all edges that are not in this selection
        edge_nodes = edge_nodes[np.isin(edge_nodes, node_selection).all(axis=1)]

        # Remove all segments that are not part of any face
        # Direction is irrelevant, sort to compare in ascending order
        # Convert to a contiguous array for faster comparison
        sorted_edges = np.ascontiguousarray(np.sort(edge_nodes, axis=1)).view(np.dtype((np.void, edge_nodes.dtype.itemsize * edge_nodes.shape[1])))
        idx = np.zeros(len(sorted_edges), dtype=bool)

        # Get combinations of face nodes that can form an edge
        combs = list(combinations(range(len(face_nodes[0])), 2))
        for comb in combs:
            # Get the first, second, third, etc. possible edge of every face
            arr = face_nodes[:, comb]
            # Remove nodata (so for example the non-existing fourth edge of a triangular face)
            arr = arr[(arr != -999).all(axis=1)]
            arr = np.sort(arr, axis=1)
            # Convert to a contiguous array for faster comparison
            sorted_face_edges = np.ascontiguousarray(arr).view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[1])))
            # Check if face edges are in edges
            idx |= np.isin(sorted_edges, sorted_face_edges)[:, 0]

        # Select the nodes that are present in faces
        edge_nodes = edge_nodes[idx]

        xnodes = xnodes[node_selection - 1]
        ynodes = ynodes[node_selection - 1]

            # Make mapping for new id's
        new_id_mapping = {old_id: new_id + 1 for new_id, old_id in enumerate(node_selection)}    

        edge_nodes = np.reshape([-999 if old_id == -999 else new_id_mapping[old_id] for old_id in edge_nodes.ravel()], edge_nodes.shape)
        face_nodes = np.reshape([-999 if old_id == -999 else new_id_mapping[old_id] for old_id in face_nodes.ravel()], face_nodes.shape)

        return xnodes, ynodes, edge_nodes, face_nodes

    @staticmethod
    def _find_cells(geometries, maxnumfacenodes=None, dll_path=None):
        """
        Determine what the cells are in a grid.
        """

        dimensions = geometries.meshgeomdim
        os.add_dll_directory(dll_path)
        wrapperGridgeom = CDLL(os.path.join(os.path.dirname(__file__), '..', 'resources', 'lib', 'gridgeom', 'gridgeom.dll'))
        ierr = wrapperGridgeom.ggeo_deallocate()
        assert ierr == 0

        start_index = c_int(1)

        meshdimout = meshgeomdim()
        meshout = meshgeom(meshdimout)

        ierr = wrapperGridgeom.ggeo_find_cells(
            byref(dimensions),
            byref(geometries),
            byref(meshdimout),
            byref(meshout),
            byref(start_index)
        )
        assert ierr == 0

        dimensions.numface = meshdimout.numface
        if maxnumfacenodes is None:
            dimensions.maxnumfacenodes = meshdimout.maxnumfacenodes
        else:
            dimensions.maxnumfacenodes = maxnumfacenodes

        # Allocate
        for mesh in [geometries, meshout]:
            for var in ['facex', 'facey', 'face_nodes']:
                mesh.allocate(var)

        ierr = wrapperGridgeom.ggeo_find_cells(
            byref(dimensions),
            byref(geometries),
            byref(meshdimout),
            byref(meshout),
            byref(start_index)
        )
        assert ierr == 0

        # Copy content to self meshgeom
        for var in ['facex', 'facey', 'face_nodes']:
            # Create array of allocated size. In case of deviating maxnumfacenodes
            if (maxnumfacenodes is not None) and (var == 'face_nodes'):
                # Create an empty integer. Empty values should only be present in the integer arrays
                arr = np.full(geometries.get_dimensions(var), -999, dtype=int)
                # Get array
                arr[:, :meshout.meshgeomdim.maxnumfacenodes] = meshout.get_values(var, as_array=True)
                # Set array
                geometries.set_values(var, arr.ravel())
            else:
                # Set array
                geometries.set_values(var, meshout.get_values(var))


        ierr = wrapperGridgeom.ggeo_deallocate()

    def altitude_constant(self, constant, where='face'):

        zvalues = np.ones(getattr(self.meshgeomdim, f'num{where}')) * constant
        self.meshgeom.set_values(f'{where}z', zvalues)

    def altitude_from_raster(self, 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.
        """

        # Select points on faces or nodes
        logger.info('Creating GeoDataFrame of cell faces.')
        xy = np.c_[self.meshgeom.get_values(f'{where}x'), self.meshgeom.get_values(f'{where}y')]
        cells = self.meshgeom.get_faces()
        facedata = gpd.GeoDataFrame(geometry=[Polygon(cell) for cell in cells])   

        if where == 'face':
            # Get raster statistics
            facedata.index = np.arange(len(xy), dtype=np.uint32) + 1
            # facedata.to_file('test_face.shp')
            facedata['crds'] = [cell for cell in cells]

            df = geometry.raster_stats_fine_cells(rasterpath, facedata, stats=[stat])
            # Get z values
            zvalues = df[stat].values

        elif where == 'node':

            logger.info('Generating voronoi polygons around cell centers for determining raster statistics.')

            # Creat voronoi polygon
            # Add border to limit polygons
            border = box(xy[:, 0].min(), xy[:, 1].min(), xy[:, 0].max(), xy[:, 1].max()).buffer(1000).exterior
            borderpts = [border.interpolate(dist).coords[0] for dist in np.linspace(0, border.length, max(20, border.length//100))]
            vor = Voronoi(points=xy.tolist()+borderpts)
            clippoly = facedata.union_all()
            # Get lines
            lines = []
            for poly in geometry.as_polygon_list(clippoly):
                lines.append(poly.exterior)
                lines.extend([line for line in poly.interiors])
            linesprep = prep(MultiLineString(lines))
            clipprep = prep(clippoly)

            # Collect polygons
            data = []
            for (pr, (i, pt)) in zip(vor.point_region, enumerate(xy)):
                region = vor.regions[pr]
                if pr == -1:
                    break
                while -1 in region:
                    region.remove(-1)
                if len(region) < 3:
                    continue
                crds = vor.vertices[region]
                if clipprep.intersects(Point(pt)):
                    poly = Polygon(crds)
                    if linesprep.intersects(poly):
                        poly = poly.intersection(clippoly)
                        if isinstance(poly, MultiPolygon):
                            poly = poly.buffer(0.001)
                        if isinstance(poly, MultiPolygon):
                            logger.warning('Got multipolygon when clipping voronoi polygon. Only adding coordinates for largest of the polygons.')
                            poly = poly[np.argmax([p.area for p in geometry.as_polygon_list(poly)])]
                        crds = np.vstack(poly.exterior.coords[:])
                    data.append({'geometry': poly, 'crds': crds})

            # Limit to model extend
            facedata = gpd.GeoDataFrame(data)
            facedata.index=np.arange(len(xy), dtype=np.uint32) + 1
            # facedata.drop('crds', axis=1).to_file('test_node.shp')
            # Get raster statistics
            df = geometry.raster_stats_fine_cells(rasterpath, facedata, stats=[stat])
            # Get z values
            zvalues = df[stat].values

        else:
            raise NotImplementedError()


        # If there are no NaN's, return the answer
        isnan = np.isnan(zvalues)
        if not isnan.any():
            # Set values to mesh geometry
            self.meshgeom.set_values(f'{where}z', zvalues)
            return None

        # If interpolation or missing, but all are NaN, raise error.
        elif isnan.all() and missing in ['nearest', 'interpolation']:
            raise ValueError('Only NaN values found, interpolation or nearest not possible.')

        # Fill missing values
        if missing.lower() == 'default':
            # With default value
            zvalues[isnan] = self.fill_value_z

        elif isinstance(missing, (float, int)):
            # With a given number
            zvalues[isnan] = missing

        elif missing.lower() == 'nearest':
            # By looking for the nearest value in the grid
            # Create a KDTree of the known points
            tree = KDTree(data=xy[~isnan])
            idx = tree.query(x=xy[isnan])[1]
            zvalues[isnan] = zvalues[~isnan][idx]

        elif missing.lower() == 'interpolation':
            # By interpolating
            isnan = np.isnan(zvalues)
            interp = LinearNDInterpolator(xy[~isnan], zvalues[~isnan], fill_value=self.fill_value_z)
            zvalues[isnan] = interp(xy[isnan])

        else:
            raise ValueError(f'{missing} not recognized. Choose \'default\', \'nearest\', \'interpolation\' or a number with which to fill the missing data.')

        # Set values to mesh geometry
        self.meshgeom.set_values(f'{where}z', zvalues)

    def set_missing_z_value(self, value):
        self.missing_z_value = value

    def geom_from_netcdf(self, file, only2d=False):
        gridio.from_netcdf_old(self.meshgeom, file, only2d=only2d)
        self._find_cells(self.meshgeom, dll_path=self.dll_path)

    def faces_to_centroid(self):
        """
        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.
        """
        # Calculate centroids
        centroids = np.vstack([cell.mean(axis=0) for cell in self.meshgeom.get_faces()]).T

        # Set values back to geometry
        self.meshgeom.set_values('facex', centroids[0])
        self.meshgeom.set_values('facey', centroids[1])
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
def altitude_from_raster(self, 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.
    """

    # Select points on faces or nodes
    logger.info('Creating GeoDataFrame of cell faces.')
    xy = np.c_[self.meshgeom.get_values(f'{where}x'), self.meshgeom.get_values(f'{where}y')]
    cells = self.meshgeom.get_faces()
    facedata = gpd.GeoDataFrame(geometry=[Polygon(cell) for cell in cells])   

    if where == 'face':
        # Get raster statistics
        facedata.index = np.arange(len(xy), dtype=np.uint32) + 1
        # facedata.to_file('test_face.shp')
        facedata['crds'] = [cell for cell in cells]

        df = geometry.raster_stats_fine_cells(rasterpath, facedata, stats=[stat])
        # Get z values
        zvalues = df[stat].values

    elif where == 'node':

        logger.info('Generating voronoi polygons around cell centers for determining raster statistics.')

        # Creat voronoi polygon
        # Add border to limit polygons
        border = box(xy[:, 0].min(), xy[:, 1].min(), xy[:, 0].max(), xy[:, 1].max()).buffer(1000).exterior
        borderpts = [border.interpolate(dist).coords[0] for dist in np.linspace(0, border.length, max(20, border.length//100))]
        vor = Voronoi(points=xy.tolist()+borderpts)
        clippoly = facedata.union_all()
        # Get lines
        lines = []
        for poly in geometry.as_polygon_list(clippoly):
            lines.append(poly.exterior)
            lines.extend([line for line in poly.interiors])
        linesprep = prep(MultiLineString(lines))
        clipprep = prep(clippoly)

        # Collect polygons
        data = []
        for (pr, (i, pt)) in zip(vor.point_region, enumerate(xy)):
            region = vor.regions[pr]
            if pr == -1:
                break
            while -1 in region:
                region.remove(-1)
            if len(region) < 3:
                continue
            crds = vor.vertices[region]
            if clipprep.intersects(Point(pt)):
                poly = Polygon(crds)
                if linesprep.intersects(poly):
                    poly = poly.intersection(clippoly)
                    if isinstance(poly, MultiPolygon):
                        poly = poly.buffer(0.001)
                    if isinstance(poly, MultiPolygon):
                        logger.warning('Got multipolygon when clipping voronoi polygon. Only adding coordinates for largest of the polygons.')
                        poly = poly[np.argmax([p.area for p in geometry.as_polygon_list(poly)])]
                    crds = np.vstack(poly.exterior.coords[:])
                data.append({'geometry': poly, 'crds': crds})

        # Limit to model extend
        facedata = gpd.GeoDataFrame(data)
        facedata.index=np.arange(len(xy), dtype=np.uint32) + 1
        # facedata.drop('crds', axis=1).to_file('test_node.shp')
        # Get raster statistics
        df = geometry.raster_stats_fine_cells(rasterpath, facedata, stats=[stat])
        # Get z values
        zvalues = df[stat].values

    else:
        raise NotImplementedError()


    # If there are no NaN's, return the answer
    isnan = np.isnan(zvalues)
    if not isnan.any():
        # Set values to mesh geometry
        self.meshgeom.set_values(f'{where}z', zvalues)
        return None

    # If interpolation or missing, but all are NaN, raise error.
    elif isnan.all() and missing in ['nearest', 'interpolation']:
        raise ValueError('Only NaN values found, interpolation or nearest not possible.')

    # Fill missing values
    if missing.lower() == 'default':
        # With default value
        zvalues[isnan] = self.fill_value_z

    elif isinstance(missing, (float, int)):
        # With a given number
        zvalues[isnan] = missing

    elif missing.lower() == 'nearest':
        # By looking for the nearest value in the grid
        # Create a KDTree of the known points
        tree = KDTree(data=xy[~isnan])
        idx = tree.query(x=xy[isnan])[1]
        zvalues[isnan] = zvalues[~isnan][idx]

    elif missing.lower() == 'interpolation':
        # By interpolating
        isnan = np.isnan(zvalues)
        interp = LinearNDInterpolator(xy[~isnan], zvalues[~isnan], fill_value=self.fill_value_z)
        zvalues[isnan] = interp(xy[isnan])

    else:
        raise ValueError(f'{missing} not recognized. Choose \'default\', \'nearest\', \'interpolation\' or a number with which to fill the missing data.')

    # Set values to mesh geometry
    self.meshgeom.set_values(f'{where}z', zvalues)
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
def clean_nodes(self, 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
    """

    # Select nodes the occur in any face
    node_selection = np.unique(face_nodes)
    node_selection = node_selection[node_selection!=-999]
    # If no nodes (because no faces), return None
    if not node_selection.any():
        return None

    # Remove all edges that are not in this selection
    edge_nodes = edge_nodes[np.isin(edge_nodes, node_selection).all(axis=1)]

    # Remove all segments that are not part of any face
    # Direction is irrelevant, sort to compare in ascending order
    # Convert to a contiguous array for faster comparison
    sorted_edges = np.ascontiguousarray(np.sort(edge_nodes, axis=1)).view(np.dtype((np.void, edge_nodes.dtype.itemsize * edge_nodes.shape[1])))
    idx = np.zeros(len(sorted_edges), dtype=bool)

    # Get combinations of face nodes that can form an edge
    combs = list(combinations(range(len(face_nodes[0])), 2))
    for comb in combs:
        # Get the first, second, third, etc. possible edge of every face
        arr = face_nodes[:, comb]
        # Remove nodata (so for example the non-existing fourth edge of a triangular face)
        arr = arr[(arr != -999).all(axis=1)]
        arr = np.sort(arr, axis=1)
        # Convert to a contiguous array for faster comparison
        sorted_face_edges = np.ascontiguousarray(arr).view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[1])))
        # Check if face edges are in edges
        idx |= np.isin(sorted_edges, sorted_face_edges)[:, 0]

    # Select the nodes that are present in faces
    edge_nodes = edge_nodes[idx]

    xnodes = xnodes[node_selection - 1]
    ynodes = ynodes[node_selection - 1]

        # Make mapping for new id's
    new_id_mapping = {old_id: new_id + 1 for new_id, old_id in enumerate(node_selection)}    

    edge_nodes = np.reshape([-999 if old_id == -999 else new_id_mapping[old_id] for old_id in edge_nodes.ravel()], edge_nodes.shape)
    face_nodes = np.reshape([-999 if old_id == -999 else new_id_mapping[old_id] for old_id in face_nodes.ravel()], face_nodes.shape)

    return xnodes, ynodes, edge_nodes, face_nodes
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
def clip_mesh_by_polygon(self, 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.
    """
    logger.info('Clipping 2D mesh by polygon.')

    # Get xnodes, ynodes and edge_nodes
    xnodes = self.meshgeom.get_values('nodex', as_array=True)
    ynodes = self.meshgeom.get_values('nodey', as_array=True)
    edge_nodes = self.meshgeom.get_values('edge_nodes', as_array=True)

    # Clip
    logger.info('Clipping nodes.')
    xnodes, ynodes, edge_nodes = self.clip_nodes(xnodes, ynodes, edge_nodes, polygon, keep_outside=outside)

    # Update dimensions
    self.meshgeomdim.numnode = len(xnodes)
    self.meshgeomdim.numedge = len(edge_nodes)

    # Add nodes and links
    self.meshgeom.set_values('nodex', xnodes)
    self.meshgeom.set_values('nodey', ynodes)
    self.meshgeom.set_values('edge_nodes', np.ravel(edge_nodes).tolist())

    # Determine what the cells are
    logger.info('Finding cell faces within clipped nodes.')
    self._find_cells(self.meshgeom, dll_path=self.dll_path)

    # Clean nodes, this function deletes nodes based on no longer existing cells
    face_nodes = self.meshgeom.get_values('face_nodes', as_array=True)
    logger.info('Removing incomplete faces.')
    cleaned = self.clean_nodes(xnodes, ynodes, edge_nodes, face_nodes)

    # If cleaning leaves no whole cells, return None
    if cleaned is None:
        raise ValueError('Clipping the grid does not leave any nodes.')
    # Else, get the arguments from the returned tuple
    else:
        xnodes, ynodes, edge_nodes, face_nodes = cleaned

    logger.info('Update mesh with clipped nodes, edges and faces.')
    # Update dimensions
    self.meshgeomdim.numnode = len(xnodes)
    self.meshgeomdim.numedge = len(edge_nodes)

    # Add nodes and links
    self.meshgeom.set_values('nodex', xnodes)
    self.meshgeom.set_values('nodey', ynodes)
    self.meshgeom.set_values('edge_nodes', np.ravel(edge_nodes).tolist())
    self.meshgeom.set_values('face_nodes', np.ravel(face_nodes).tolist())
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
def clip_nodes(self, 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

    """

    # Find nodes within one of the polygons
    logger.info('Selecting nodes within polygon.')
    index = np.zeros(len(xnodes), dtype=bool)
    for poly in geometry.as_polygon_list(polygon):
        index = index | geometry.points_in_polygon(np.c_[xnodes, ynodes], poly)
    if keep_outside:
        index = ~index

    # Filter edge nodes, keep the ones that are within a polygon with both points
    nodeids_in_polygon = np.where(index)[0] + 1
    edge_nodes = edge_nodes[np.isin(edge_nodes, nodeids_in_polygon).all(axis=1)]

    # Remove edges that intersect the clipping polygons. This is needed for when the clipping polygon is narrow,
    # So it crosses an edge without containing one of the edge nodes
    logger.info('Creating LineString for each edge.')
    edges = [LineString(line) for line in np.c_[xnodes, ynodes][edge_nodes - 1]]

    logger.info('Checking for intersections.')
    isect = np.zeros(len(edge_nodes), dtype=bool)
    for poly in geometry.as_polygon_list(polygon):
        prepped = prep(poly.exterior)
        isect = isect | np.array([prepped.intersects(e) for e in edges])
        for interior in poly.interiors:
            prepped = prep(interior)
            isect = isect | np.array([prepped.intersects(e) for e in edges])
    edge_nodes = edge_nodes[~isect]

    # Remove edges that have a 1 count, until none are left
    logger.info('Remove edges with only a single node within the clip area.')
    unique, count = np.unique(edge_nodes, return_counts=True)
    while any(count == 1):
        count1nodes = unique[count == 1]
        edge_nodes = edge_nodes[~np.isin(edge_nodes, count1nodes).any(axis=1)]
        unique, count = np.unique(edge_nodes, return_counts=True)

    # Select remaining nodes
    xnodes = xnodes[unique - 1]
    ynodes = ynodes[unique - 1]

    # Make mapping for new id's
    new_id_mapping = {old_id: new_id + 1 for new_id, old_id in enumerate(unique)}
    edge_nodes = np.reshape([new_id_mapping[old_id] for old_id in edge_nodes.ravel()], edge_nodes.shape)

    return xnodes, ynodes, edge_nodes
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
def faces_to_centroid(self):
    """
    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.
    """
    # Calculate centroids
    centroids = np.vstack([cell.mean(axis=0) for cell in self.meshgeom.get_faces()]).T

    # Set values back to geometry
    self.meshgeom.set_values('facex', centroids[0])
    self.meshgeom.set_values('facey', centroids[1])

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
class Rectangular(Mesh2D_GG):

    def __init__(self, dll_path=None):
        Mesh2D_GG.__init__(self,dll_path=dll_path)
        self.meshgeomdim.maxnumfacenodes = 4
        self.dll_path=dll_path
        self.rotated = False

    def generate_grid(self, 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.
        """

        # Generate x and y spacing
        x = np.linspace(x0, x0 + dx * (ncols), ncols+1)
        y = np.linspace(y0, y0 + dy * (nrows), nrows+1)

        # Get nodes
        xnodes, ynodes = np.meshgrid(x, y)

        # Rotate
        if rotation != 0:
            self.rotated = True
            xnodes, ynodes = geometry.rotate_coordinates((x0, y0), np.radians(rotation), xnodes, ynodes)

        # Get nodes as list
        nodes = {crd: i+1 for i, crd in enumerate(zip(xnodes.ravel(), ynodes.ravel()))}

        # Create segments
        segments = list(zip(
            zip(*np.c_[xnodes[:, :-1].ravel(), ynodes[:, :-1].ravel()].T),
            zip(*np.c_[xnodes[:, 1:].ravel(), ynodes[:, 1:].ravel()].T)
        ))
        segments += list(zip(
            zip(*np.c_[xnodes[1:, :].ravel(), ynodes[1:, :].ravel()].T),
            zip(*np.c_[xnodes[:-1, :].ravel(), ynodes[:-1, :].ravel()].T)
        ))

        # Get links
        edge_nodes = np.asarray([(nodes[s[0]], nodes[s[1]]) for s in segments])

        # Rvael the nodes
        xnodes = xnodes.ravel()
        ynodes = ynodes.ravel()

        # Add to mesh
        dimensions = meshgeomdim()
        dimensions.dim = 2
        geometries = meshgeom(dimensions)

        # Clip
        if clipgeo is not None:
            xnodes, ynodes, edge_nodes = self.clip_nodes(xnodes, ynodes, edge_nodes, clipgeo)

        # Update dimensions
        dimensions.numnode = len(xnodes)
        dimensions.numedge = len(edge_nodes)

        # Add nodes and links
        geometries.set_values('nodex', xnodes)
        geometries.set_values('nodey', ynodes)
        geometries.set_values('edge_nodes', np.ravel(edge_nodes).tolist())

        # Determine what the cells are
        self._find_cells(geometries, dll_path=self.dll_path)

        # Clip
        if clipgeo is not None:
            # Clean nodes, this function deletes nodes based on no longer existing cells
            face_nodes = geometries.get_values('face_nodes', as_array=True)
            cleaned = self.clean_nodes(xnodes, ynodes, edge_nodes, face_nodes)
            # If cleaning leaves no whole cells, return None
            if cleaned is None:
                return None
            # Else, get the arguments from the returned tuple
            else:
                xnodes, ynodes, edge_nodes, face_nodes = cleaned

        # Update dimensions
        dimensions.numnode = len(xnodes)
        dimensions.numedge = len(edge_nodes)
        dimensions.maxnumfacenodes = 4

        # Add nodes and links
        geometries.set_values('nodex', xnodes)
        geometries.set_values('nodey', ynodes)
        geometries.set_values('edge_nodes', np.ravel(edge_nodes).tolist())
        geometries.set_values('face_nodes', np.ravel(face_nodes).tolist())

        # Add to mesh
        self.meshgeom.add_from_other(geometries)

    def generate_within_polygon(self, 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
        """

        polygons = geometry.as_polygon_list(polygon)

        logger.info(
            "Generating grid with cellsize %s m and rotation %s degrees.",
            cellsize,
            rotation,
        )
        convex = MultiPolygon(polygons).convex_hull

        # In case of a rotation, extend the grid far enough to make sure
        if rotation != 0:
            # Find a box that contains the whole polygon
            (x0, y0), xsize, ysize = geometry.minimum_bounds_fixed_rotation(convex, rotation)
        else:
            xsize = convex.bounds[2] - convex.bounds[0]
            ysize= convex.bounds[3] - convex.bounds[1]
            x0, y0 = convex.bounds[0], convex.bounds[1]

        self.generate_grid(
            x0=x0,
            y0=y0,
            dx=cellsize,
            dy=cellsize,
            ncols=int(xsize / cellsize) + 1,
            nrows=int(ysize / cellsize) + 1,
            clipgeo=polygon,
            rotation=rotation
        )

    def refine(self, 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
        """

        if self.rotated:
            raise NotImplementedError('Mesh refinement does not work for rotation grids.')

        # checks.check_argument(polygon, 'polygon', (list, Polygon, MultiPolygon))

        # Save network as grid
        temppath = 'tempgrid_net.nc'
        gridio.to_netcdf_old(self.meshgeom, temppath)

        if isinstance(level, int):
            level = [level]
            polygon = [polygon]

        factor = 2 ** max(level)
        stepsize = cellsize / factor
        dxmin = stepsize
        dtmax = stepsize / (9.81) ** 0.5


        xnodes = self.meshgeom.get_values('nodex')
        ynodes = self.meshgeom.get_values('nodey')
        x0 = min(xnodes)
        ncols = int((max(xnodes) - x0) / cellsize)
        y0 = min(ynodes)
        nrows = int((max(ynodes) - y0) / cellsize)

        arr = np.ones((nrows * factor, ncols * factor)) * 4**max(level)

        for poly, lvl in zip(polygon, level):
            mask = geometry.geometry_to_mask(poly, (x0, y0), stepsize, shape=(nrows * factor, ncols * factor))
            arr[mask] = 4 ** (max(level) - lvl)

        # Create ascii grid based on polygon
        header = (
            'nCols        {0:d}\n'
            'nRows        {1:d}\n'
            'xllCorner    {2:.6f}\n'
            'yllCorner    {3:.6f}\n'
            'CellSize     {4:.6f}\n'
            'nodata_value 0\n'
        ).format(ncols * factor, nrows * factor, x0, y0 + stepsize, stepsize)

        values = '\n'.join(' '.join('%d' %x for x in y) for y in arr.squeeze())

        with open('temp_ascgrid.asc', 'w') as f:
            f.write(header + values)

        # Get dflow fm on system path
        if dflowfm_path is None or not os.path.exists(dflowfm_path):
            raise OSError('dflowfm.exe not provided or not found.')        

        # Construct statement        
        statement = f'"{dflowfm_path}" --refine:hmin={dxmin}:dtmax={dtmax}:connect=1:outsidecell=1 {temppath} temp_ascgrid.asc'
        if debug:
            statement += ' > log.txt'
        # Execute statement
        os.system(statement)

        if not debug:
            os.remove('temp_ascgrid.asc')

        # Read geometry and dimensions again
        if not os.path.exists('out_net.nc'):
            raise OSError(
                (f'Could not find "out_net.nc" in directory "{os.getcwd()}". '
                 f'The file should have been generated with the statement: "{statement}". '
                 'You can test this manually by running this statement from a command prompt launched from the directory named in this message.'))
        gridio.from_netcdf_old(self.meshgeom, 'out_net.nc')

        # Find cells
        self._find_cells(self.meshgeom, dll_path=self.dll_path)

        os.remove('out_net.nc')
        os.remove(temppath)
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
def generate_grid(self, 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.
    """

    # Generate x and y spacing
    x = np.linspace(x0, x0 + dx * (ncols), ncols+1)
    y = np.linspace(y0, y0 + dy * (nrows), nrows+1)

    # Get nodes
    xnodes, ynodes = np.meshgrid(x, y)

    # Rotate
    if rotation != 0:
        self.rotated = True
        xnodes, ynodes = geometry.rotate_coordinates((x0, y0), np.radians(rotation), xnodes, ynodes)

    # Get nodes as list
    nodes = {crd: i+1 for i, crd in enumerate(zip(xnodes.ravel(), ynodes.ravel()))}

    # Create segments
    segments = list(zip(
        zip(*np.c_[xnodes[:, :-1].ravel(), ynodes[:, :-1].ravel()].T),
        zip(*np.c_[xnodes[:, 1:].ravel(), ynodes[:, 1:].ravel()].T)
    ))
    segments += list(zip(
        zip(*np.c_[xnodes[1:, :].ravel(), ynodes[1:, :].ravel()].T),
        zip(*np.c_[xnodes[:-1, :].ravel(), ynodes[:-1, :].ravel()].T)
    ))

    # Get links
    edge_nodes = np.asarray([(nodes[s[0]], nodes[s[1]]) for s in segments])

    # Rvael the nodes
    xnodes = xnodes.ravel()
    ynodes = ynodes.ravel()

    # Add to mesh
    dimensions = meshgeomdim()
    dimensions.dim = 2
    geometries = meshgeom(dimensions)

    # Clip
    if clipgeo is not None:
        xnodes, ynodes, edge_nodes = self.clip_nodes(xnodes, ynodes, edge_nodes, clipgeo)

    # Update dimensions
    dimensions.numnode = len(xnodes)
    dimensions.numedge = len(edge_nodes)

    # Add nodes and links
    geometries.set_values('nodex', xnodes)
    geometries.set_values('nodey', ynodes)
    geometries.set_values('edge_nodes', np.ravel(edge_nodes).tolist())

    # Determine what the cells are
    self._find_cells(geometries, dll_path=self.dll_path)

    # Clip
    if clipgeo is not None:
        # Clean nodes, this function deletes nodes based on no longer existing cells
        face_nodes = geometries.get_values('face_nodes', as_array=True)
        cleaned = self.clean_nodes(xnodes, ynodes, edge_nodes, face_nodes)
        # If cleaning leaves no whole cells, return None
        if cleaned is None:
            return None
        # Else, get the arguments from the returned tuple
        else:
            xnodes, ynodes, edge_nodes, face_nodes = cleaned

    # Update dimensions
    dimensions.numnode = len(xnodes)
    dimensions.numedge = len(edge_nodes)
    dimensions.maxnumfacenodes = 4

    # Add nodes and links
    geometries.set_values('nodex', xnodes)
    geometries.set_values('nodey', ynodes)
    geometries.set_values('edge_nodes', np.ravel(edge_nodes).tolist())
    geometries.set_values('face_nodes', np.ravel(face_nodes).tolist())

    # Add to mesh
    self.meshgeom.add_from_other(geometries)
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
def generate_within_polygon(self, 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
    """

    polygons = geometry.as_polygon_list(polygon)

    logger.info(
        "Generating grid with cellsize %s m and rotation %s degrees.",
        cellsize,
        rotation,
    )
    convex = MultiPolygon(polygons).convex_hull

    # In case of a rotation, extend the grid far enough to make sure
    if rotation != 0:
        # Find a box that contains the whole polygon
        (x0, y0), xsize, ysize = geometry.minimum_bounds_fixed_rotation(convex, rotation)
    else:
        xsize = convex.bounds[2] - convex.bounds[0]
        ysize= convex.bounds[3] - convex.bounds[1]
        x0, y0 = convex.bounds[0], convex.bounds[1]

    self.generate_grid(
        x0=x0,
        y0=y0,
        dx=cellsize,
        dy=cellsize,
        ncols=int(xsize / cellsize) + 1,
        nrows=int(ysize / cellsize) + 1,
        clipgeo=polygon,
        rotation=rotation
    )
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
def refine(self, 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
    """

    if self.rotated:
        raise NotImplementedError('Mesh refinement does not work for rotation grids.')

    # checks.check_argument(polygon, 'polygon', (list, Polygon, MultiPolygon))

    # Save network as grid
    temppath = 'tempgrid_net.nc'
    gridio.to_netcdf_old(self.meshgeom, temppath)

    if isinstance(level, int):
        level = [level]
        polygon = [polygon]

    factor = 2 ** max(level)
    stepsize = cellsize / factor
    dxmin = stepsize
    dtmax = stepsize / (9.81) ** 0.5


    xnodes = self.meshgeom.get_values('nodex')
    ynodes = self.meshgeom.get_values('nodey')
    x0 = min(xnodes)
    ncols = int((max(xnodes) - x0) / cellsize)
    y0 = min(ynodes)
    nrows = int((max(ynodes) - y0) / cellsize)

    arr = np.ones((nrows * factor, ncols * factor)) * 4**max(level)

    for poly, lvl in zip(polygon, level):
        mask = geometry.geometry_to_mask(poly, (x0, y0), stepsize, shape=(nrows * factor, ncols * factor))
        arr[mask] = 4 ** (max(level) - lvl)

    # Create ascii grid based on polygon
    header = (
        'nCols        {0:d}\n'
        'nRows        {1:d}\n'
        'xllCorner    {2:.6f}\n'
        'yllCorner    {3:.6f}\n'
        'CellSize     {4:.6f}\n'
        'nodata_value 0\n'
    ).format(ncols * factor, nrows * factor, x0, y0 + stepsize, stepsize)

    values = '\n'.join(' '.join('%d' %x for x in y) for y in arr.squeeze())

    with open('temp_ascgrid.asc', 'w') as f:
        f.write(header + values)

    # Get dflow fm on system path
    if dflowfm_path is None or not os.path.exists(dflowfm_path):
        raise OSError('dflowfm.exe not provided or not found.')        

    # Construct statement        
    statement = f'"{dflowfm_path}" --refine:hmin={dxmin}:dtmax={dtmax}:connect=1:outsidecell=1 {temppath} temp_ascgrid.asc'
    if debug:
        statement += ' > log.txt'
    # Execute statement
    os.system(statement)

    if not debug:
        os.remove('temp_ascgrid.asc')

    # Read geometry and dimensions again
    if not os.path.exists('out_net.nc'):
        raise OSError(
            (f'Could not find "out_net.nc" in directory "{os.getcwd()}". '
             f'The file should have been generated with the statement: "{statement}". '
             'You can test this manually by running this statement from a command prompt launched from the directory named in this message.'))
    gridio.from_netcdf_old(self.meshgeom, 'out_net.nc')

    # Find cells
    self._find_cells(self.meshgeom, dll_path=self.dll_path)

    os.remove('out_net.nc')
    os.remove(temppath)

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
class GeometryList(GeometryListMK):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @classmethod
    def from_geometry(cls, geometry):
        if isinstance(geometry, Polygon):
            return cls.from_polygon(geometry)
        elif isinstance(geometry, MultiPolygon):
            return cls.from_multipolygon(geometry)
        elif isinstance(geometry, LineString):
            return cls.from_linestring(geometry)
        elif isinstance(geometry, MultiLineString):
            return cls.from_multilinestring(geometry)
        elif isinstance(geometry, Point):
            return cls.from_point(geometry)
        elif isinstance(geometry, MultiPoint):
            return cls.from_multipoint(geometry)
        else:
            raise TypeError(f"Geometry type {type(geometry)} not understood.")

    @classmethod
    def _from_simple(cls, geometry: LineString | Point):
        # Extract coordinates from geometry
        x_crds, y_crds = np.array(geometry.coords[:]).T
        gl = cls(x_coordinates=x_crds, y_coordinates=y_crds)
        return gl

    @classmethod
    def from_linestring(cls, linestring: LineString):
        return cls._from_simple(linestring)

    @classmethod
    def from_point(cls, point: Point):
        return cls._from_simple(point)

    @classmethod
    def from_polygon(cls, polygon: Polygon):
        # Create a list of coordinate lists
        # Add exterior
        x_ext, y_ext = np.array(polygon.exterior.coords[:]).T
        x_crds = [x_ext]
        y_crds = [y_ext]


        if not hasattr(cls, 'inner_outer_separator'):
            cls.inner_outer_separator = -998

        # Add interiors, seperated by inner_outer_separator
        for interior in polygon.interiors:
            x_int, y_int = np.array(interior.coords[:]).T
            x_crds.append([cls.inner_outer_separator])
            x_crds.append(x_int)
            y_crds.append([cls.inner_outer_separator])
            y_crds.append(y_int)
        gl = cls(
            x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds)
        )
        return gl

    @classmethod
    def _from_multigeometry(cls, multigeometry: MultiLineString | MultiPolygon):
        x_crds = []
        y_crds = []
        cls.inner_outer_separator = -998
        cls.geometry_separator = -999
        # Create a GeometryList for every polygon in the multipolygon
        for i, geometry in enumerate(multigeometry.geoms):
            gl = cls.from_geometry(geometry)
            # Add geometry seperator for every geometrylist, except the first one
            if i != 0:
                x_crds.append([cls.geometry_separator])
                y_crds.append([cls.geometry_separator])
            # Add coordinates of polygon GeometryList
            x_crds.append(gl.x_coordinates)
            y_crds.append(gl.y_coordinates)

        gl = cls(
            x_coordinates=np.concatenate(x_crds), y_coordinates=np.concatenate(y_crds)
        )
        return gl

    @classmethod
    def from_multipolygon(cls, multipolygon: MultiPolygon):
        return cls._from_multigeometry(multipolygon)

    @classmethod
    def from_multilinestring(cls, multilinestring: LineString):
        return cls._from_multigeometry(multilinestring)

    @classmethod
    def from_multipoint(cls, multipoint: Point):
        return cls._from_multigeometry(multipoint)

    def _to_polygon(
        self, geometries: list[GeometryListMK], is_multi: bool
    ) -> Polygon | MultiPolygon:
        polygons = []
        for geometry in geometries:
            parts = [
                np.stack([p.x_coordinates, p.y_coordinates], axis=1)
                for p in split_by(geometry, self.inner_outer_separator)
            ]
            polygons.append(Polygon(shell=parts[0], holes=parts[1:]))
        if is_multi:
            return MultiPolygon(polygons)
        else:
            return polygons[0] if len(polygons)==1 else None

    def _to_linestring(
        self, geometries: list[GeometryListMK], is_multi: bool
    ) -> LineString | MultiLineString:
        linestrings = [
            LineString(np.stack([p.x_coordinates, p.y_coordinates], axis=1))
            for p in geometries
        ]
        if is_multi:
            return MultiLineString(linestrings)
        else:
            return linestrings[0]

    def _to_points(
        self, geometries: list[GeometryListMK], is_multi: bool
    ) -> Point | MultiPoint:
        points = [
            Point(np.stack([p.x_coordinates, p.y_coordinates], axis=1))
            for p in geometries
        ]
        if is_multi:
            return MultiPoint(points)
        else:
            return points[0]

    def to_geometry(self):
        geometries = [
            geo
            for geo in split_by(self, self.geometry_separator)
            if geo.x_coordinates.size > 0
        ]
        is_multi = len(geometries) > 1

        for geometry_list in geometries:
            # Check if polygon, by comparing first and last coordinates
            exterior = split_by(geometry_list, self.inner_outer_separator)[0]
            is_polygon = exterior.x_coordinates[0] == exterior.x_coordinates[-1]
            # Check if linestring, by checking the length of coordinate sequences
            is_linestring = (len(geometry_list.x_coordinates) > 1) and (not is_polygon)

            if is_polygon:
                return self._to_polygon(geometries, is_multi)

            elif is_linestring:
                return self._to_linestring(geometries, is_multi)

            else:
                return self._to_point(geometries, is_multi)

    @property
    def geoms(self):
        """Returns GeometryList objects based on spliited by geometries"""
        for geometrylist in split_by(self, self.geometry_separator):
            # yield as shapely geometry
            yield GeometryList(**geometrylist.__dict__).to_geometry()
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
def 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
    """
    # Check if method is in allowed methods
    allowed_methods = ['intersecting', 'overal', 'centroid', 'ends']
    if method not in allowed_methods:
        raise NotImplementedError(f'Method "{method}" not implemented.')

    # Add columns if not present
    if 'branch_id' not in geometries.columns:
        geometries['branch_id'] = ''
    if 'branch_offset' not in geometries.columns:
        geometries['branch_offset'] = np.nan

    if method == 'intersecting':
        # Determine intersection geometries per branch
        sindex = geometries.sindex
        for branch in branches.itertuples():
            intersecting = geometries.iloc[sindex.query(branch.geometry, predicate="intersects")]

            # For each geometrie, determine offset along branch
            for geometry in intersecting.itertuples():
                # Determine distance of profile line along branch
                geometries.at[geometry.Index, 'branch_id'] = branch.Index

                # Calculate offset
                branchgeo = branch.geometry
                mindist = min(0.1, branchgeo.length / 2.)
                offset = round(branchgeo.project(branchgeo.intersection(geometry.geometry).centroid), 3)
                offset = max(mindist, min(branchgeo.length - mindist, offset))
                geometries.at[geometry.Index, 'branch_offset'] = offset

    else:
        sindex = branches.sindex
        # In case of looking for the nearest, it is easier to iteratie over the geometries instead of the branches
        for geometry in geometries.itertuples():
            # Find near branches
            nearidx = sindex.query(geometry.geometry, predicate="dwithin", distance=maxdist)
            selectie = branches.iloc[nearidx]

            if method == 'overal':
                # Determine distances to branches
                dist = selectie.distance(geometry.geometry)
            elif method == 'centroid':
                # Determine distances to branches
                dist = selectie.distance(geometry.geometry.centroid)
            elif method == 'ends':
                # Since a culvert can cross a channel, it is
                crds = geometry.geometry.coords[:]
                dist = (selectie.distance(Point(*crds[0])) + selectie.distance(Point(*crds[-1]))) * 0.5

            # Enforce a point
            geo = geometry.geometry
            if not isinstance(geo, Point):
                geo = geo.centroid

            # Log-friendly name with coordinates
            geom_name = f"at x={geo.x:.4f} and y={geo.y:.4f}"
            if hasattr(geometry, "code"):
                geom_name = f"for {geometry.code} {geom_name}"

            # Determine nearest
            if dist.min() < maxdist:
                branchidxmin = dist.idxmin()
                geometries.at[geometry.Index, 'branch_id'] = branchidxmin

                nbranches = (dist < maxdist).sum()
                if nbranches > 1:
                    logger.info(
                        "Centroid %s has %d branches with maxdist=%s and method=%s, chosing closest branch %s",
                        geom_name,
                        nbranches,
                        maxdist,
                        method,
                        branchidxmin,
                    )

                # Calculate offset
                branchgeo = branches.at[branchidxmin, 'geometry']
                mindist = min(0.1, branchgeo.length / 2.)
                offset = max(mindist, min(branchgeo.length - mindist, round(branchgeo.project(geo), 3)))
                geometries.at[geometry.Index, 'branch_offset'] = offset
            else:
                logger.info(
                    "Centroid %s has 0 branches with maxdist=%s and method=%s",
                    geom_name,
                    maxdist,
                    method,
                )

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
def get_voronoi_around_nodes(nodes: np.ndarray, facedata: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    """Creates voronoi polygons around face nodes.

    Args:
        nodes (np.ndarray): xy coordinates of face nodes
        facedata (gpd.GeoDataFrame): GeoDataFrame with face properties

    Returns:
        gpd.GeoDataFrame: Creating GeoDataFrame with created polygons and their properties
    """
    # Creat voronoi polygon
    # Add border to limit polygons
    border = box(nodes[:, 0].min(), nodes[:, 1].min(), nodes[:, 0].max(), nodes[:, 1].max()).buffer(1000).exterior
    borderpts = [border.interpolate(dist).coords[0] for dist in np.linspace(0, border.length, max(20, int(border.length / 100)))]
    vor = Voronoi(points=np.vstack([nodes, borderpts]))
    clippoly = facedata.union_all()
    # Get lines
    lines = []
    for poly in common.as_polygon_list(clippoly):
        lines.append(poly.exterior)
        lines.extend([line for line in poly.interiors])
    linesprep = prep(MultiLineString(lines))

    # Determine for all nodes at once whether they lie within the clip polygon
    inside = shapely.intersects(clippoly, shapely.points(nodes))

    # Collect polygons
    data = []
    for (pr, pt_inside) in zip(vor.point_region, inside):
        region = vor.regions[pr]
        if pr == -1:
            break
        region = [v for v in region if v != -1]
        if len(region) < 3:
            continue
        if pt_inside:
            crds = vor.vertices[region]
            poly = Polygon(crds)
            if linesprep.intersects(poly):
                poly = poly.intersection(clippoly)
                if isinstance(poly, MultiPolygon):
                    poly = poly.buffer(0.001)
                if isinstance(poly, MultiPolygon):
                    logger.warning('Got multipolygon when clipping voronoi polygon. Only adding coordinates for largest of the polygons.')
                    poly = poly[np.argmax([p.area for p in common.as_polygon_list(poly)])]
                crds = np.vstack(poly.exterior.coords[:])
            data.append({'geometry': poly, 'crds': crds})

    # Limit to model extend
    facedata = gpd.GeoDataFrame(data)
    facedata.index=np.arange(len(nodes), dtype=np.uint32) + 1

    return facedata

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
def line_parts(geometry) -> list[LineString]:
    """All LineStrings in a geometry, including those in a GeometryCollection; points are ignored."""
    if isinstance(geometry, LineString):
        return [geometry]
    if hasattr(geometry, "geoms"):
        return [part for geom in geometry.geoms for part in line_parts(geom)]
    return []

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
def 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
    """
    # Determine spinning point
    spinpt = (polygon.envelope.bounds[0], polygon.envelope.bounds[1])

    # Rotate clip polygon with rotation, get envelope and rotate back.
    rotbox1 = affinity.rotate(polygon, angle=angle, origin=spinpt).envelope

    # Determine size of grid
    xsize = rotbox1.bounds[2] - rotbox1.bounds[0]
    ysize = rotbox1.bounds[3] - rotbox1.bounds[1]

    # Rotate again, and get origin
    rotbox2 = affinity.rotate(rotbox1, angle=-angle, origin=spinpt)
    origin = rotbox2.exterior.coords[0]

    return origin, xsize, ysize

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
def 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
    """

    # Determine angle at offset
    angle = np.angle(complex(*np.diff([
        line.interpolate(offset - 0.001).coords[0][:2],
        line.interpolate(offset + 0.001).coords[0][:2]
    ], axis=0)[0])) + 0.5 * np.pi

    # Create new line
    pt = line.interpolate(offset).coords[0]

    f = 0.5 * width
    line = [(pt[0] + np.cos(angle) * f, pt[1] + np.sin(angle) * f), (pt[0] - np.cos(angle) * f, pt[1] - np.sin(angle) * f)]

    return line

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
def 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.

    Args:
        branches (gpd.GeoDataFrame): branches, with the branch id as index
        points (gpd.GeoDataFrame): points with values
        column (str): column with the values
        max_distance (float, optional): maximum distance to snap a point to a branch. Defaults to 5.0.
        junction_tolerance (float, optional): distance within which a point at a branch end is also used for the
            other branches at that junction. Defaults to 0.1.

    Returns:
        dict[str, tuple[list[float], list[float]]]: chainages and values per branch id
    """
    if {"branchid", "chainage"} <= set(points.columns):
        data = points[["branchid", "chainage", column]].copy()
        unknown = ~data["branchid"].isin(branches.index)
        if unknown.any():
            logger.warning(f"{unknown.sum()} points refer to branches that are not in the model and are left out.")
            data = data[~unknown]
    else:
        # first row: index of the point, second row: index of the nearest branch
        point_idx, branch_idx = branches.sindex.nearest(points.geometry, max_distance=max_distance, return_all=False)
        if len(point_idx) < len(points):
            logger.warning(
                f"{len(points) - len(point_idx)} points are further than {max_distance} m from a branch and are left out."
            )
        geometries = branches.geometry.to_numpy()
        data = points.iloc[point_idx][[column]].copy()
        data["branchid"] = branches.index.to_numpy()[branch_idx]
        data["chainage"] = [
            geometries[b].project(p) for b, p in zip(branch_idx, points.geometry.to_numpy()[point_idx])
        ]

    result = {}
    for branch_id, group in data.groupby("branchid", sort=False):
        group = group.sort_values("chainage").drop_duplicates("chainage", keep="last")
        result[branch_id] = (group["chainage"].astype(float).tolist(), group[column].astype(float).tolist())

    _add_junction_end_values(result, branches, points, column, junction_tolerance)
    return result

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
def 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.

    Args:
        branches (gpd.GeoDataFrame): branches, with the branch id as index
        polygons (gpd.GeoDataFrame): (multi)polygons with values
        column (str): column with the values
        step (float, optional): distance between the two chainages at a polygon boundary. Defaults to 0.01.

    Returns:
        dict[str, tuple[list[float], list[float]]]: chainages and values per branch id
    """
    polygons = polygons.reset_index(drop=True)
    values = polygons[column].astype(float).to_numpy()
    result = {}
    for branch_id, line in zip(branches.index, branches.geometry):
        intervals = _polygon_intervals(line, polygons, step)
        if not intervals:
            continue
        segments = _covering_segments(intervals, values, line.length, step)
        result[branch_id] = _segments_to_chainages(segments, line.length, step)
    return result

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
def 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.

    Args:
        polygons (gpd.GeoDataFrame): (multi)polygons with values
        column (str): column with the values to burn into the raster
        cellsize (float): raster cell size
        fill (float, optional): value for cells outside the polygons. Defaults to -999.0.

    Returns:
        tuple[np.ndarray, rasterio.Affine]: float32 array and its transform
    """
    xmin, ymin, xmax, ymax = polygons.total_bounds
    xmin = np.floor(xmin / cellsize) * cellsize
    ymin = np.floor(ymin / cellsize) * cellsize
    xmax = np.ceil(xmax / cellsize) * cellsize
    ymax = np.ceil(ymax / cellsize) * cellsize
    ncols = max(round((xmax - xmin) / cellsize), 1)
    nrows = max(round((ymax - ymin) / cellsize), 1)

    transform = from_origin(xmin, ymax, cellsize, cellsize)
    array = features.rasterize(
        zip(polygons.geometry, polygons[column].astype(float)),
        out_shape=(nrows, ncols),
        transform=transform,
        fill=fill,
        dtype="float32",
    )
    return array, transform

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
def rotate_coordinates(origin, theta, xcrds, ycrds):
    """
    Rotate coordinates around origin (x0, y0) with a certain angle (radians)
    """
    x0, y0 = origin
    xcrds_rot = x0 + (xcrds - x0) * np.cos(theta) + (ycrds - y0) * np.sin(theta)
    ycrds_rot = y0 - (xcrds - x0) * np.sin(theta) + (ycrds - y0) * np.cos(theta)
    return xcrds_rot, ycrds_rot

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_frac selects the pixels: those covered less are ignored.
  • coverage_weight sets 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
class 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`.
    """

    # Arguments that do not affect the result.
    _NOT_IN_KEY = frozenset(
        {"zones", "raster", "strategy", "max_cells_in_memory", "progress"}
    )

    def __init__(self):
        self._results = {}

    def __len__(self):
        return len(self._results)

    def clear(self):
        """Remove all cached results."""
        self._results.clear()

    def zonal_stats(self, zones, raster, statistics, **kwargs) -> pd.DataFrame:
        """:func:`zonal_stats`, reusing an identical earlier result."""
        if isinstance(statistics, str):
            statistics = [statistics]
        return self._get(
            zonal_stats, zones, raster, statistics=tuple(statistics), **kwargs
        )

    def zonal_category_counts(self, zones, raster, **kwargs) -> pd.DataFrame:
        """:func:`zonal_category_counts`, reusing an identical earlier result."""
        return self._get(zonal_category_counts, zones, raster, **kwargs)

    def _get(self, function, zones, raster, **kwargs):
        if not isinstance(raster, (str, os.PathLike)):
            return function(zones, raster, **kwargs)
        try:
            path = Path(raster).resolve()
            status = path.stat()
        except OSError:  # not a local file, such as a /vsicurl/ URL
            return function(zones, raster, **kwargs)

        arguments = inspect.signature(function).bind(zones, raster, **kwargs)
        arguments.apply_defaults()
        key = (
            function.__name__,
            str(path),
            status.st_size,
            status.st_mtime_ns,
            _zones_key(zones),
            *(
                (name, _crs_key(value) if name == "raster_crs" else value)
                for name, value in arguments.arguments.items()
                if name not in self._NOT_IN_KEY
            ),
        )
        if key in self._results:
            logger.debug("Reusing %s of %s.", function.__name__, path.name)
        else:
            self._results[key] = function(zones, raster, **kwargs)
        return self._results[key].copy()
clear()

Remove all cached results.

Source code in hydrolib/dhydamo/geometry/zonal.py
354
355
356
def clear(self):
    """Remove all cached results."""
    self._results.clear()
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
def zonal_category_counts(self, zones, raster, **kwargs) -> pd.DataFrame:
    """:func:`zonal_category_counts`, reusing an identical earlier result."""
    return self._get(zonal_category_counts, zones, raster, **kwargs)
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
def zonal_stats(self, zones, raster, statistics, **kwargs) -> pd.DataFrame:
    """:func:`zonal_stats`, reusing an identical earlier result."""
    if isinstance(statistics, str):
        statistics = [statistics]
    return self._get(
        zonal_stats, zones, raster, statistics=tuple(statistics), **kwargs
    )

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
def 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 = 30_000_000,
    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.
    """
    result = _extract(
        zones,
        [raster],
        ["unique", "frac", "count"],
        strategy=strategy,
        min_coverage_frac=min_coverage_frac,
        coverage_weight=coverage_weight,
        affine=affine,
        raster_crs=raster_crs,
        nodata=nodata,
        max_cells_in_memory=max_cells_in_memory,
        progress=progress,
    )
    rows = [
        {
            int(code) if float(code).is_integer() else code: share * count
            for code, share in zip(classes.tolist(), np.asarray(frac).tolist())
        }
        if isinstance(classes, np.ndarray)  # None for zones outside the raster
        else {}
        for classes, frac, count in zip(
            result["unique"], result["frac"], result["count"]
        )
    ]
    counts = pd.DataFrame(rows, index=zones.index, dtype=np.float64)
    counts = counts.sort_index(axis=1).fillna(0.0)
    # Unweighted counts are whole cells; drop floating-point noise.
    return counts.round() if coverage_weight == "none" else counts

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
def 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 = 30_000_000,
    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.
    """
    statistics = [statistics] if isinstance(statistics, str) else list(statistics)
    result = _extract(
        zones,
        [raster],
        statistics,
        strategy=strategy,
        min_coverage_frac=min_coverage_frac,
        coverage_weight=coverage_weight,
        affine=affine,
        raster_crs=raster_crs,
        nodata=nodata,
        max_cells_in_memory=max_cells_in_memory,
        progress=progress,
    )
    if result.shape[1] != len(statistics):
        raise ValueError(
            f"Statistics {statistics} give clashing column names "
            f"{list(result.columns)}; request them in separate calls."
        )
    return result.apply(pd.to_numeric, errors="coerce").astype(np.float64)

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
def 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 = 30_000_000,
    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``.
    """
    rasters = list(rasters)
    results = []
    with ExitStack() as stack:
        if progress is True:
            progress = _progress_bar(stack)
        for start in range(0, len(rasters), _RASTERS_PER_CALL):
            chunk = rasters[start : start + _RASTERS_PER_CALL]
            chunk_progress = False
            if progress:
                chunk_progress = _rescale_progress(
                    progress, start, len(chunk), len(rasters)
                )
            result = _extract(
                zones,
                chunk,
                [statistic],
                strategy=strategy,
                min_coverage_frac=min_coverage_frac,
                coverage_weight=coverage_weight,
                affine=affine,
                raster_crs=raster_crs,
                nodata=nodata,
                max_cells_in_memory=max_cells_in_memory,
                progress=chunk_progress,
            )
            results.append(result)
    result = pd.concat(results, axis=1).set_axis(range(len(rasters)), axis=1)
    return result.apply(pd.to_numeric, errors="coerce").astype(np.float64)