From 6d39d181528ffd1e2252deef7d0f13b77b6deccb Mon Sep 17 00:00:00 2001 From: Majid Rezaei Date: Tue, 30 Jan 2024 09:01:27 -0500 Subject: [PATCH] buildings as buildings and the rest as junction nodes --- DistrictHeatingNetworkCreator.py | 60 +++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/DistrictHeatingNetworkCreator.py b/DistrictHeatingNetworkCreator.py index 4123709..2871e8a 100644 --- a/DistrictHeatingNetworkCreator.py +++ b/DistrictHeatingNetworkCreator.py @@ -126,37 +126,57 @@ class DistrictHeatingNetworkCreator: G = nx.Graph() # Add building nodes - building_idx = 1 - for centroid in self.centroids_gdf.geometry: - label = f"Building_{building_idx}" - G.add_node(label, pos=(centroid.x, centroid.y), type='building') - building_idx += 1 + for idx, centroid in enumerate(self.centroids_gdf.geometry): + node_label = f"Building_{idx + 1}" + G.add_node(node_label, pos=(centroid.x, centroid.y), type='building') - # Add junction nodes for the points along the line strings - junction_idx = 1 - junction_positions = {} # To keep track of junction positions + # Add junction nodes and keep track of them + junction_counter = 1 + junction_points = set() # To avoid duplicating junctions + + # Process each line in gdf_cleanest for line in self.gdf_cleanest.geometry: for point in line.coords: + # Convert tuple to Point object for comparison point_obj = Point(point) - if point_obj not in junction_positions: - label = f"Junction_{junction_idx}" - G.add_node(label, pos=(point_obj.x, point_obj.y), type='junction') - junction_positions[point_obj] = label - junction_idx += 1 - # Add edges + # Check if this point is already a centroid (building) + if point_obj not in [centroid for centroid in self.centroids_gdf.geometry]: + # Check if not already added as a junction + if point_obj not in junction_points: + node_label = f"Junction_{junction_counter}" + G.add_node(node_label, pos=(point_obj.x, point_obj.y), type='junction') + junction_points.add(point_obj) + junction_counter += 1 + + # Add edges between nodes for line in self.gdf_cleanest.geometry: points = list(line.coords) for start, end in zip(points[:-1], points[1:]): - start_point_obj = Point(start) - end_point_obj = Point(end) - start_label = junction_positions.get(start_point_obj) - end_label = junction_positions.get(end_point_obj) - if start_label and end_label: - G.add_edge(start_label, end_label, weight=LineString([start, end]).length) + start_node = self._find_closest_node(G, Point(start)) + end_node = self._find_closest_node(G, Point(end)) + if start_node and end_node: + G.add_edge(start_node, end_node, weight=LineString([start, end]).length) return G + def _find_closest_node(self, graph, point): + """ + Find the closest node to a given point. + :param graph: The NetworkX graph. + :param point: The point for which to find the closest node. + :return: The label of the closest node. + """ + closest_node = None + min_dist = float('inf') + for node, data in graph.nodes(data=True): + node_point = Point(data['pos']) + dist = point.distance(node_point) + if dist < min_dist: + closest_node = node + min_dist = dist + return closest_node + def plot_network_graph(self, network_graph): """ Plot the network graph using matplotlib and networkx.