Skip to content

Commit

Permalink
Merge pull request #276 from Deltares/275/fix/store-intermediate-prod…
Browse files Browse the repository at this point in the history
…uct-when-network-is-broken

275/fix/store intermediate product when network is broken
  • Loading branch information
LiekeMeijer authored Jan 11, 2024
2 parents feebe0f + b8dd9bd commit 6380354
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"""
import logging
from pathlib import Path
from typing import Any
from typing import Any, Union

import networkx as nx
import osmnx
import pandas as pd
from geopandas import GeoDataFrame
from networkx import MultiDiGraph, MultiGraph
from shapely.geometry import LineString
from shapely.geometry.base import BaseGeometry

import ra2ce.graph.networks_utils as nut
Expand Down Expand Up @@ -191,7 +192,7 @@ def get_clean_graph(complex_graph: MultiDiGraph):
graph=complex_graph, geom_name="geometry"
).to_directed()
complex_graph = OsmNetworkWrapper.snap_nodes_to_nodes(
graph=complex_graph, threshold=0.000025
graph=complex_graph, threshold=0.00005
)
return complex_graph

Expand Down Expand Up @@ -309,3 +310,4 @@ def snap_nodes_to_nodes(graph: MultiDiGraph, threshold: float) -> MultiDiGraph:
@staticmethod
def snap_nodes_to_edges(graph: MultiDiGraph, threshold: float):
raise NotImplementedError("Next thing to do!")

18 changes: 12 additions & 6 deletions ra2ce/graph/networks_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import warnings
from pathlib import Path
from statistics import mean
from typing import Optional
from typing import Optional, Union

import geopandas as gpd
import networkx as nx
Expand Down Expand Up @@ -1089,20 +1089,26 @@ def graph_create_unique_ids(graph: nx.Graph, new_id_name: str = "rfid") -> nx.Gr

def add_missing_geoms_graph(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
def get_node_by_attribute(graph: nx.Graph, node_number: int) -> dict:
for node, data in graph.nodes(data=True):
if node == node_number:
return data
return dict()

nodes_without_geom = [
n[0] for n in graph.nodes(data=True) if geom_name not in n[-1]
]
for nd in nodes_without_geom:
graph.nodes[nd][geom_name] = Point(graph.nodes[nd]["x"], graph.nodes[nd]["y"])

edges_without_geom = [
e for e in graph.edges.data(data=True) if geom_name not in e[-1]
e for e in graph.edges.data(keys=True, data=True) if geom_name not in e[-1]
]
for ed in edges_without_geom:
graph[ed[0]][ed[1]][0][geom_name] = LineString(
[graph.nodes[ed[0]][geom_name], graph.nodes[ed[1]][geom_name]]
)

if graph.nodes[ed[0]].get(geom_name, None) and graph.nodes[ed[1]].get(geom_name, None):
graph[ed[0]][ed[1]][ed[2]][geom_name] = LineString(
[graph.nodes[ed[0]][geom_name], graph.nodes[ed[1]][geom_name]]
)
return graph


Expand Down

0 comments on commit 6380354

Please sign in to comment.