correct the naming of the buildings and adding building_centroid as an attribute to the node

This commit is contained in:
Majid Rezaei 2024-01-30 10:07:35 -05:00
parent 10086215e3
commit d9b6201d5f

View File

@ -125,9 +125,17 @@ class DistrictHeatingNetworkCreator:
"""
G = nx.Graph()
# Add centroids and nearest points as nodes
for centroid in self.centroids_gdf.geometry:
G.add_node((centroid.x, centroid.y), type='centroid')
# Convert centroids to EPSG:4326 for Google Maps compatibility
centroids_4326 = self.centroids_gdf.to_crs(epsg=4326)
# Add centroids and nearest points as nodes with additional attributes
for idx, (centroid, centroid_4326) in enumerate(zip(self.centroids_gdf.geometry, centroids_4326.geometry)):
building_name = f"Building_{idx + 1}"
G.add_node((centroid.x, centroid.y),
type='centroid',
name=building_name,
building_centroid=(centroid_4326.x, centroid_4326.y))
for point in self.nearest_points:
G.add_node((point.x, point.y), type='nearest_point')
@ -162,25 +170,20 @@ class DistrictHeatingNetworkCreator:
plt.figure(figsize=(12, 12))
pos = {node: (node[0], node[1]) for node in network_graph.nodes()}
# Draw nodes with type 'centroid'
centroids = [node for node, attr in network_graph.nodes(data=True) if attr.get('type') == 'centroid']
nx.draw_networkx_nodes(network_graph, pos, nodelist=centroids, node_color='blue', node_size=50,
label='Centroids')
# Draw nodes with type 'nearest_point'
nearest_points = [node for node, attr in network_graph.nodes(data=True) if attr.get('type') == 'nearest_point']
nx.draw_networkx_nodes(network_graph, pos, nodelist=nearest_points, node_color='green', node_size=30,
label='Nearest Points')
# Draw other nodes
other_nodes = [node for node, attr in network_graph.nodes(data=True) if 'type' not in attr]
nx.draw_networkx_nodes(network_graph, pos, nodelist=other_nodes, node_color='red', node_size=20,
label='Other Nodes')
# Draw edges
# Draw nodes and edges
nx.draw_networkx_nodes(network_graph, pos, node_color='blue', node_size=50)
nx.draw_networkx_edges(network_graph, pos, edge_color='gray')
plt.legend()
# Create a dictionary for node labels for centroids only
node_labels = {node: data['name'] for node, data in network_graph.nodes(data=True) if
data.get('type') == 'centroid'}
# Adjust node label positions to reduce overlap
label_pos = {node: (coords[0], coords[1] + 0.03) for node, coords in pos.items()} # Shift labels up
# Draw node labels for centroids
nx.draw_networkx_labels(network_graph, label_pos, labels=node_labels, font_size=8, verticalalignment='bottom')
plt.title('District Heating Network Graph')
plt.axis('off')
plt.show()