From 62c70b9c9f7c6c3f6f6762828766af947d032cd4 Mon Sep 17 00:00:00 2001 From: Majid Rezaei Date: Wed, 31 Jul 2024 17:37:30 -0400 Subject: [PATCH] feature: add dhn factory to enrich graph with city.buildings --- .../district_heating_factory.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 scripts/district_heating_network/district_heating_factory.py diff --git a/scripts/district_heating_network/district_heating_factory.py b/scripts/district_heating_network/district_heating_factory.py new file mode 100644 index 00000000..55a3785a --- /dev/null +++ b/scripts/district_heating_network/district_heating_factory.py @@ -0,0 +1,32 @@ +import networkx as nx +import logging + +class DistrictHeatingFactory: + """ + DistrictHeatingFactory class + """ + + def __init__(self, city, graph): + self._city = city + self._network_graph = graph + + def enrich(self): + """ + Enrich the network graph nodes with attributes from the city buildings. + """ + + for node in self._network_graph.nodes(data=True): + node_id, node_attrs = node + if node_attrs.get('type') == 'building': + building_name = node_attrs.get('name') + building_found = False + for building in self._city.buildings: + if building.name == building_name: + building_attrs = vars(building) + for attr, value in building_attrs.items(): + if attr not in self._network_graph.nodes[node_id]: + self._network_graph.nodes[node_id][attr] = value + building_found = True + break + if not building_found: + logging.error(msg=f"Building with name '{building_name}' not found in city.") \ No newline at end of file