Skip to content

Commit

Permalink
small fixes that was needed to build model from urban drainage system…
Browse files Browse the repository at this point in the history
… data derived, need to check if needed in main
  • Loading branch information
xldeltares committed Jan 9, 2024
1 parent 8bdf2df commit c4a2382
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
8 changes: 7 additions & 1 deletion hydromt_delft3dfm/dflowfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ def setup_rivers(

# Read data and filter within region
region = workflows.parse_region_geometry(region, self.crs)
rivers_fn = rivers_fn.name if isinstance(rivers_fn, Path) else rivers_fn
gdf_br = self.data_catalog.get_geodataframe(
rivers_fn, geom=region, buffer=0, predicate="intersects"
)
Expand Down Expand Up @@ -981,6 +982,7 @@ def setup_urban_sewer_network_from_osm(
graph=graph,
data_catalog=self.data_catalog,
dem_fn=dem_fn,
fill_method="nearest",
logger=self.logger,
)

Expand Down Expand Up @@ -1027,8 +1029,11 @@ def setup_urban_sewer_network_from_osm(

# 6. any additional steps to add the network to delft3dfm model
rivers = graph_utils.graph_to_network(graph_rivers)[0]
# reindex
rivers["branchid"] = [f"river_{i}" for i in rivers.index]
self.set_geoms(rivers, "rivers")
pipes = graph_utils.graph_to_network(graph_pipe_dag_with_dimention)[0]
pipes["branchid"] = [f"pipe_{i}" for i in pipes.index]
self.set_geoms(pipes, "pipes")
self.write_geoms()

Expand Down Expand Up @@ -1215,11 +1220,12 @@ def setup_pipes(
"width", # rectangle
"height", # rectangle
"invlev_up",
"inlev_dn",
"invlev_dn",
]

# Read data and filter within region
region = workflows.parse_region_geometry(region, self.crs)
pipes_fn = pipes_fn.name if isinstance(pipes_fn, Path) else pipes_fn
gdf_br = self.data_catalog.get_geodataframe(
pipes_fn, geom=region, buffer=0, predicate="intersects"
)
Expand Down
24 changes: 13 additions & 11 deletions hydromt_delft3dfm/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import Tuple, Union

import geopandas as gpd
import momepy
import networkx as nx
import pandas as pd
from pyproj import CRS, Transformer
from shapely.geometry import LineString, Point, box
from shapely.ops import transform
Expand Down Expand Up @@ -39,13 +39,13 @@
graph property ["crs"]
edge property ["id", "geometry", "node_start", "node_end"]
node property ["id", "geometry"]
graph_edges: gpd.GeoDataFrame
columns: ["id", "geometry", "node_start", "node_end"]
graph_nodes: gpd.GeoDataFrame
columns: ["id", "geometry"]
graph_region: gpd.GeoDataFrame
"""

Expand All @@ -66,7 +66,6 @@ def gpd_to_digraph(data: gpd.GeoDataFrame) -> nx.DiGraph():
nx.DiGraph
The converted directed graph.
"""

_ = data.copy()

_["from_node"] = [row.geometry.coords[0] for index, row in _.iterrows()]
Expand Down Expand Up @@ -104,7 +103,9 @@ def preprocess_graph(graph: nx.Graph, to_crs: CRS = None) -> nx.Graph:
edge properties ['id', 'geometry','node_start', and 'node_end']
node properties ['id', 'geometry']
"""
# TODO Allow for more preprocessing steps, like cleaning up isolated nodes, adding weights to edges based on distance, etc.
# TODO Allow for more preprocessing steps,
# like cleaning up isolated nodes,
# adding weights to edges based on distance, etc.

crs = graph.graph.get("crs")
if not crs:
Expand All @@ -131,7 +132,8 @@ def preprocess_graph(graph: nx.Graph, to_crs: CRS = None) -> nx.Graph:
def _add_missing_geoms_one_by_one(
graph: nx.Graph, geom_name: str = "geometry"
) -> nx.Graph:
# Not all nodes have geometry attributed (some only x and y coordinates) so add a geometry columns
# Not all nodes have geometry attributed (some only x and y coordinates)
# so add a geometry columns
nodes_without_geom = [n[0] for n in graph.nodes(data=geom_name) if n[1] is None]
for nd in nodes_without_geom:
graph.nodes[nd][geom_name] = Point(graph.nodes[nd]["x"], graph.nodes[nd]["y"])
Expand Down Expand Up @@ -304,7 +306,6 @@ def network_to_graph(
edge properties ['id', 'geometry','node_start', and 'node_end']
node properties ['id', 'geometry']
"""

# create graph from edges
edges["source"] = edges["node_start"]
edges["target"] = edges["node_end"]
Expand Down Expand Up @@ -351,7 +352,8 @@ def graph_to_network(
Tuple
gpd.GeoDataFrame
edges of the graph.
Contains attributes ['id', 'geometry', node_start', 'node_end', '_graph_edge_index']
Contains attributes ['id', 'geometry', node_start', 'node_end',
'_graph_edge_index']
gpd.GeoDataFrame
nodes of the graph.
Contains attributes ['id', 'geometry', '_graph_node_index']
Expand Down Expand Up @@ -527,17 +529,17 @@ def write_graph(graph: nx.Graph, graph_fn: str) -> None:

# properties
def graph_edges(graph: nx.Graph) -> gpd.GeoDataFrame:
"""Get graph edges as geodataframe"""
"""Get graph edges as geodataframe."""
return graph_to_network(graph)[0]


def graph_nodes(graph: nx.Graph) -> gpd.GeoDataFrame:
"""Get graph nodes as geodataframe"""
"""Get graph nodes as geodataframe."""
return graph_to_network(graph)[1]


def graph_region(graph: nx.Graph) -> gpd.GeoDataFrame:
"""Get graph region as geodataframe"""
"""Get graph region as geodataframe."""
edges = graph_to_network(graph)[0]
region = gpd.GeoDataFrame(geometry=[box(*edges.total_bounds)], crs=edges.crs)
return region
Expand Down
2 changes: 1 addition & 1 deletion hydromt_delft3dfm/workflows/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ def _remove_branches_with_ring_geometries(
) -> gpd.GeoDataFrame:
first_nodes = [l.coords[0] for l in branches.geometry]
last_nodes = [l.coords[-1] for l in branches.geometry]
duplicate_ids = np.isclose(first_nodes, last_nodes)
duplicate_ids = np.isclose(first_nodes, last_nodes, atol=0, rtol=1e-10)
duplicate_ids = [
branches.index[i] for i in range(len(branches)) if np.all(duplicate_ids[i])
]
Expand Down

0 comments on commit c4a2382

Please sign in to comment.