network_snapping
network_snapping
Route link geometries of a Ribasim-scheme along the network of hydro-objects.
Connector nodes are never moved: they stay at the location of the structure they represent, see ribasim_nl.structure_locations. The geometries of the flow links between a Basin and a connector node are changed. Such a link is drawn as a straight line from the connector node to the nearest hydro-object within the basin area, followed by the cheapest route along the hydro-objects to the Basin node. Lines that are not hydro-objects of the water authority, such as the ditches of the Top10NL, cost more per metre, see OTHER_LINE_COST, so routes only use them where the hydro-objects leave a gap. A Basin node is moved to the center of its network, see _BasinNetwork.center, or else stays where it is, with a straight line to the network. Links that share a route can afterwards be merged with ribasim_nl.junctions.junctionify.
A connector node that cannot be reached within the basin area, such as one beyond a canal that splits the area, or one that the model connects over a distance, is routed along the lines and the water around the straight line to it as well, see reach_area, where a route costs more per metre, see BEYOND_AREA_COST. Links that are routed after the other links of their Basin, such as those from another model, follow the routes of these links where they can, see _BasinNetwork.add_routes.
This relies on the network of hydro-objects written by the network-based profile-generation: src/ribasim_nl/ribasim_nl/profiles/*.py.
Classes
| Name | Description |
|---|---|
| Waterways | The lines, culverts and water surfaces along which the links of Basins are routed, see route_basin_links. |
Waterways
network_snapping.Waterways(lines, culverts=None, water=None)The lines, culverts and water surfaces along which the links of Basins are routed, see route_basin_links.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| lines | gpd.GeoDataFrame | lines, with a boolean “main-route” column, and optionally a “cost” column with their cost per metre | required |
| culverts | gpd.GeoDataFrame | None | culverts (lines) that connect the lines, defaults to None | None |
| water | gpd.GeoDataFrame | None | water surfaces (polygons), across which the lines may be connected, defaults to None | None |
Methods
| Name | Description |
|---|---|
| water_in | The water surface within an area. |
| within | The lines, culverts and water within an area, as the arguments of route_basin_links. |
water_in
network_snapping.Waterways.water_in(area)The water surface within an area.
within
network_snapping.Waterways.within(area, home=None, home_water=None)The lines, culverts and water within an area, as the arguments of route_basin_links.
The lines are clipped to the area, which keeps those crossing its boundary connected. The part of a line beyond home, if given, costs BEYOND_AREA_COST times as much per metre.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| area | shapely.Geometry | the area | required |
| home | shapely.Geometry | None | the part of the area in which the lines keep their cost, such as the area of the Basin | None |
| home_water | shapely.Geometry | None | the water within home, if it is known, to which that of the rest of the area is added |
None |
Functions
| Name | Description |
|---|---|
| attach_to_tree | Attach points to the tree of routes from start, each with a straight line from the nearest point of the tree. |
| culverts_in | LineString parts of the culverts within an area. |
| insert_points | Insert the points that lie on a segment of a route, strictly between its coordinates, into that segment. |
| line_reader | Read the lines along which links are routed, within an area. |
| lines_in | Indices and LineString parts of the geometries clipped to an area. |
| reach_area | The strips around the straight lines from points to the nearest point of a target. |
| read_hydro_objects | Read the network of hydro-objects written by the profile-generation. |
| read_lake_waterways | The fairways within a mask, such as those of OpenStreetMap, and the lines of a main route, all as main route. |
| read_lines_on_water | The parts of the hydro-objects in the network.gpkg files of paths that lie on water, such as harbour channels. |
| read_main_waterways | The Top10NL water lines within a mask, and the lines of the main_route as its main route. |
| read_waterways | Read the water lines of the Top10NL, or of a dataset in its format, within a mask, to complement the hydro-objects. |
| route_basin | Route the links of a Basin along the waterways within an area, and beyond it to the nodes not reached in it. |
| route_basin_links | Route the links between a Basin node and its connector nodes along the hydro-objects in the basin. |
| route_over_water | Route the links between a Basin node and its connector nodes over the water surface in the basin area. |
| snap_model | Snap a model to the hydro-objects of the profile-generation, see snap_to_network. |
| snap_to_network | Route flow links between a Basin and a connector node along the hydro-objects, and move Basin nodes onto them. |
| snap_to_waterways | Route links along the Top10NL water lines and over the Top10NL water surfaces, without moving nodes. |
| straight_flow_link_ids | The flow links with a straight geometry. |
| straight_link_geometries | Make link geometries start at their from-node and end at their to-node. |
| with_branch_points | The lines that get a point where one of the routes joins or leaves them, by their number. |
attach_to_tree
network_snapping.attach_to_tree(routes, start, points)Attach points to the tree of routes from start, each with a straight line from the nearest point of the tree.
The points nearest to the tree are attached first, such that the others can branch off their lines. Where a line branches off a segment, a point is inserted in all routes along that segment, such that they share it.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| routes | dict[int, list[Coord]] | per key, the coordinates of a route from start |
required |
| start | Coord | the start of the routes, from which the first point is attached if there are no routes | required |
| points | dict[int, Coord] | per key, a point to attach | required |
Returns
| Name | Type | Description |
|---|---|---|
| dict[int, list[Coord]] | the routes, with a route to each point |
culverts_in
network_snapping.culverts_in(geometries, tree, area)LineString parts of the culverts within an area.
A culvert with both ends in the area is taken whole, also where it leaves the area in between, such as a siphon under a canal that is not part of the area: clipped, it would not connect the hydro-objects on either side. The other culverts are clipped to the area, see lines_in.
insert_points
network_snapping.insert_points(coords, points, tolerance=1e-06)Insert the points that lie on a segment of a route, strictly between its coordinates, into that segment.
line_reader
network_snapping.line_reader(waterway_paths=(), lines=None)Read the lines along which links are routed, within an area.
These are the water lines of waterway_paths, see read_waterways, and the lines, such as hydro-objects or a main route.
lines_in
network_snapping.lines_in(geometries, tree, area)Indices and LineString parts of the geometries clipped to an area.
reach_area
network_snapping.reach_area(
points,
target,
width=REACH,
ratio=REACH_RATIO,
max_width=MAX_REACH,
)The strips around the straight lines from points to the nearest point of a target.
A connector node that is not reached within the area of its Basin is routed in this area as well: along the waterways between the node and the routes of the Basin, or the Basin node. Each strip reaches width to both sides of its line and beyond its ends, or ratio times the length of the line if that is more, up to max_width.
read_hydro_objects
network_snapping.read_hydro_objects(profiles_path)Read the network of hydro-objects written by the profile-generation.
read_lake_waterways
network_snapping.read_lake_waterways(fairway_path, mask, main_route)The fairways within a mask, such as those of OpenStreetMap, and the lines of a main route, all as main route.
On open water the fairways cross each other and the main route without a common point, so the lines are split where they cross, to connect them.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| fairway_path | pathlib.Path | GeoPackage with the fairways | required |
| mask | shapely.Geometry | area to read | required |
| main_route | gpd.GeoDataFrame | lines of the main route, such as a network of main waterways | required |
Returns
| Name | Type | Description |
|---|---|---|
| gpd.GeoDataFrame | the waterways, with a boolean “main-route” column |
read_lines_on_water
network_snapping.read_lines_on_water(paths, water)The parts of the hydro-objects in the network.gpkg files of paths that lie on water, such as harbour channels.
See read_hydro_objects. They are not the main route, and are clipped to water.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| paths | typing.Sequence[pathlib.Path] | the network.gpkg files of the profile-generation of water authorities | required |
| water | shapely.Geometry | water surface, such as the area of a lake Basin | required |
read_main_waterways
network_snapping.read_main_waterways(top10nl_path, mask, main_route=None)The Top10NL water lines within a mask, and the lines of the main_route as its main route.
The water lines cost OTHER_LINE_COST times as much per metre as the main route, like next to the hydro-objects of a water authority, see snap_model: the Top10NL has every ditch, also those along a main waterway.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| top10nl_path | pathlib.Path | Top10NL GeoPackage | required |
| mask | shapely.Geometry | area to read | required |
| main_route | gpd.GeoDataFrame | None | lines of the main route, such as a network of main waterways, defaults to None | None |
Returns
| Name | Type | Description |
|---|---|---|
| gpd.GeoDataFrame | the waterways, with a boolean “main-route” column and their “cost” per metre |
read_waterways
network_snapping.read_waterways(path, mask)Read the water lines of the Top10NL, or of a dataset in its format, within a mask, to complement the hydro-objects.
See scripts/download_gsk3e.py for such a dataset.
route_basin
network_snapping.route_basin(
basin_point,
connectors,
waterways,
area,
*,
route_water=True,
routes=(),
**kwargs,
)Route the links of a Basin along the waterways within an area, and beyond it to the nodes not reached in it.
A connector node that is not reached along the lines within the area, see route_basin_links, lies far from them, beyond a gap in the area, such as a canal of another authority that splits it, or outside it, where the model connects it over a distance. All links are then routed again, also over the water surface, and with the waterways around the straight lines from these nodes to the routes found, see reach_area, where a line costs BEYOND_AREA_COST times as much per metre. These routes are used if they reach more nodes.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| basin_point | shapely.Point | location of the Basin node | required |
| connectors | dict[int, shapely.Point] | locations of the connector nodes, by key | required |
| waterways | Waterways | the lines, culverts and water to route along | required |
| area | shapely.Geometry | the area of the Basin within which it is routed | required |
| route_water | bool | route nodes far from the lines over the water surface, defaults to True | True |
| routes | typing.Sequence[shapely.LineString] | routes from the Basin node that other links of the Basin already follow | () |
| kwargs | typing.Any | other arguments of route_basin_links |
{} |
Returns
| Name | Type | Description |
|---|---|---|
| tuple[shapely.Point, dict[int, list[Coord]]] | the (possibly moved) location of the Basin node, and per routed connector key the coordinates from the Basin node to the connector node |
route_basin_links
network_snapping.route_basin_links(
basin_point,
connectors,
lines,
main_route,
*,
cost=None,
culverts=None,
water=None,
area=None,
basin_area=None,
max_gap=2.0,
max_water_gap=500.0,
culvert_snap=10.0,
max_attach_distance=100.0,
max_land=MAX_LAND,
max_straight_line_ratio=MAX_STRAIGHT_LINE_RATIO,
min_routed_fraction=0.5,
open_water=False,
routes=(),
)Route the links between a Basin node and its connector nodes along the hydro-objects in the basin.
The hydro-objects are connected by the culverts, and dangling ends are connected to other parts of the network if the gap is small or crossed over water, see _BasinNetwork.bridge_gaps. Then only the longest connected part of the network is used. Each connector node is attached to its nearest hydro-object within max_attach_distance. Where its route passes nearer to the node on another line near it, the route leaves the lines there. Connector nodes farther away are attached over the water within area instead, along the least-cost path over water that crosses at most max_land of land, see _WaterRaster. All routes follow the cheapest paths from the Basin node, by the length of the lines times their cost per metre, such that routes share their common parts exactly.
If the water is open_water, such as a lake, the other parts of the network are first connected to the longest one over it, see _BasinNetwork.connect_over_water, or else along the least-cost path over it, see _BasinNetwork.connect_over_raster, and connector nodes far from the network are attached by straight lines where possible, see _WaterRaster.attach_straight.
The routes that other links of the Basin already follow are part of the network, see _BasinNetwork.add_routes. Then the longest part of the network with these routes is used, such that the links share them, unless the longest part reaches more of the connector nodes, as when a route touches none of the lines.
If basin_area is given, the Basin node is moved to the center of the network within that area, see _BasinNetwork.center: the point from which the routes to its connector nodes, and to the point of the network nearest to where the Basin node was, are cheapest in total. That last point keeps a Basin with one connector node where it was, and one with two away from them. The Basin node is only moved if at least min_routed_fraction of the links can be routed from the center. Otherwise it stays where it is, attached to its nearest main route hydro-object (if any). A route is not used if it is not less than max_straight_line_ratio times the straight line, as in ribasim_nl.link_geometries.fix_link_geometries.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| basin_point | shapely.Point | location of the Basin node | required |
| connectors | dict[int, shapely.Point] | locations of the connector nodes, by key | required |
| lines | list[shapely.LineString] | hydro-objects within the basin area | required |
| main_route | list[bool] | per hydro-object, whether it is part of the main route | required |
| cost | list[float] | None | per hydro-object, its cost per metre, defaults to None (1.0 for all) | None |
| culverts | list[shapely.LineString] | None | culverts within the basin area, defaults to None | None |
| water | shapely.Geometry | None | water surface within the basin area, defaults to None | None |
| area | shapely.Geometry | None | area within which routes over water are found, defaults to None (no routes over water) | None |
| basin_area | shapely.Polygon | shapely.MultiPolygon | None | area within which the Basin node may be moved onto the network, defaults to None (not moved) | None |
| max_gap | float | maximum gap between hydro-objects to bridge | 2.0 |
| max_water_gap | float | maximum gap between hydro-objects to bridge over water | 500.0 |
| culvert_snap | float | maximum distance between a culvert end and the hydro-objects | 10.0 |
| max_attach_distance | float | maximum distance between a connector node and the network | 100.0 |
| max_land | float | maximum length of land crossed by a route over water | MAX_LAND |
| max_straight_line_ratio | float | maximum ratio between route length and straight line length | MAX_STRAIGHT_LINE_RATIO |
| min_routed_fraction | float | minimum fraction of the links that can be routed to move the Basin node | 0.5 |
| open_water | bool | whether the water is open water, such as a lake |
False |
| routes | typing.Sequence[shapely.LineString] | routes from the Basin node that other links of the Basin already follow | () |
Returns
| Name | Type | Description |
|---|---|---|
| tuple[shapely.Point, dict[int, list[Coord]]] | the (possibly moved) location of the Basin node, and per routed connector key the coordinates from the Basin node to the connector node |
route_over_water
network_snapping.route_over_water(
basin_point,
connectors,
water,
area,
*,
land_cost=20.0,
max_land=MAX_LAND,
max_cells=1000000,
max_straight_line_ratio=MAX_STRAIGHT_LINE_RATIO,
)Route the links between a Basin node and its connector nodes over the water surface in the basin area.
This is for basins without hydro-objects, such as lakes and harbours, see _WaterRaster. Routes that cross more than max_land of land, or are not less than max_straight_line_ratio times the straight line, are not used.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| basin_point | shapely.Point | location of the Basin node | required |
| connectors | dict[int, shapely.Point] | locations of the connector nodes, by key | required |
| water | shapely.Geometry | water surface within the basin area | required |
| area | shapely.Geometry | the basin area, outside which routes cannot go | required |
| land_cost | float | cost of crossing land relative to water | 20.0 |
| max_land | float | maximum length of land crossed by a route | MAX_LAND |
| max_cells | int | maximum number of raster cells, which sets the cell size | 1000000 |
| max_straight_line_ratio | float | maximum ratio between route length and straight line length | MAX_STRAIGHT_LINE_RATIO |
Returns
| Name | Type | Description |
|---|---|---|
| dict[int, list[Coord]] | per routed connector key, the coordinates from the Basin node to the connector node |
snap_model
network_snapping.snap_model(
model,
profiles_path,
*,
crossings_path=None,
water_path=None,
waterway_paths=(),
tolerance=10.0,
routing_areas=None,
)Snap a model to the hydro-objects of the profile-generation, see snap_to_network.
The water lines of the waterway_paths cost OTHER_LINE_COST times as much per metre as the hydro-objects.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Model | Ribasim model | required |
| profiles_path | pathlib.Path | directory with the network.gpkg of the profile-generation | required |
| crossings_path | pathlib.Path | None | GeoPackage with a duikersifonhevel layer of culverts, defaults to None | None |
| water_path | pathlib.Path | None | GeoParquet with water surfaces, see ribasim_nl.bgt_water, defaults to None |
None |
| waterway_paths | typing.Sequence[pathlib.Path] | GeoPackages of which the water lines complement the hydro-objects, the Top10NL and possibly others in its format, see read_waterways, defaults to none |
() |
| tolerance | float | buffer around the basin area within which hydro-objects are used, defaults to 10.0 | 10.0 |
| routing_areas | dict[int, shapely.Geometry] | None | per Basin, an area beyond its basin area in which it is routed, see snap_to_network |
None |
snap_to_network
network_snapping.snap_to_network(
model,
hydro_objects,
*,
culverts=None,
water=None,
tolerance=10.0,
move_basins=True,
route_water=True,
link_ids=None,
routing_areas=None,
)Route flow links between a Basin and a connector node along the hydro-objects, and move Basin nodes onto them.
Connector nodes are never moved, such that they stay at the location of the structure they represent. Basin nodes are moved to the center of their network if move_basins is True, see route_basin_links, and the nodes of their bergend part along with them. Links that cannot be routed, links to bergend basins and links to boundary nodes keep their geometry, unless it does not connect their nodes, see straight_link_geometries.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Model | Ribasim model | required |
| hydro_objects | gpd.GeoDataFrame | hydro-objects, with a boolean “main-route” column, and optionally a “cost” column with their cost per metre, see route_basin_links |
required |
| culverts | gpd.GeoDataFrame | None | culverts (lines) that connect hydro-objects, defaults to None | None |
| water | gpd.GeoDataFrame | None | water surfaces (polygons), across which hydro-objects may be connected, defaults to None | None |
| tolerance | float | buffer around the basin area within which hydro-objects are used, defaults to 10.0 | 10.0 |
| move_basins | bool | move non-bergend Basin nodes onto the network, defaults to True | True |
| route_water | bool | route links that cannot be routed along the hydro-objects over the water surface, see route_basin_links, defaults to True |
True |
| link_ids | list[int] | None | only route these links, defaults to None (all) | None |
| routing_areas | dict[int, shapely.Geometry] | None | per Basin, an area beyond its basin area in which the hydro-objects are used as well, for connector nodes that are connected to it over a long distance, for instance through Germany | None |
Returns
| Name | Type | Description |
|---|---|---|
| Model | updated Ribasim model |
snap_to_waterways
network_snapping.snap_to_waterways(
model,
top10nl_path,
link_ids,
*,
main_route=None,
tolerance=10.0,
)Route links along the Top10NL water lines and over the Top10NL water surfaces, without moving nodes.
This is a fallback for links that cannot be routed along the hydro-objects of a water authority, see snap_to_network.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Model | Ribasim model | required |
| top10nl_path | pathlib.Path | Top10NL GeoPackage | required |
| link_ids | list[int] | links to route | required |
| main_route | gpd.GeoDataFrame | None | lines of the main route, such as a network of main waterways, defaults to None | None |
| tolerance | float | buffer around the basin area within which waterways are used, defaults to 10.0 | 10.0 |
Returns
| Name | Type | Description |
|---|---|---|
| Model | updated Ribasim model |
straight_flow_link_ids
network_snapping.straight_flow_link_ids(model)The flow links with a straight geometry.
straight_link_geometries
network_snapping.straight_link_geometries(model, tolerance=1.0)Make link geometries start at their from-node and end at their to-node.
Link ends within tolerance of their node are moved onto it, other link geometries are replaced by straight lines.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | Model | Ribasim model | required |
| tolerance | float | maximum distance between a link end and its node to keep the link geometry | 1.0 |
Returns
| Name | Type | Description |
|---|---|---|
| Model | updated Ribasim model |
with_branch_points
network_snapping.with_branch_points(lines, routes)The lines that get a point where one of the routes joins or leaves them, by their number.
Routes that follow a line, see _BasinNetwork.add_routes, then share all its coordinates up to there, so ribasim_nl.flow_paths.merge_basin_trees can merge them.