add building ids as attributes

This commit is contained in:
Majid Rezaei 2024-02-02 17:22:35 -05:00
parent d9b6201d5f
commit 64618af861
2 changed files with 13 additions and 11 deletions

View File

@ -35,18 +35,22 @@ class DistrictHeatingNetworkCreator:
with open(self.buildings_file, 'r') as file:
city = json.load(file)
# Extract centroids from building data
# Extract centroids and building IDs from building data
centroids = []
building_ids = [] # List to store building IDs
buildings = city['features']
for building in buildings:
coordinates = building['geometry']['coordinates'][0]
building_polygon = Polygon(coordinates)
centroid = building_polygon.centroid
centroids.append(centroid)
building_ids.append(building['id']) # Extract building ID
# Convert centroids to a GeoDataFrame
self.centroids_gdf = gpd.GeoDataFrame(geometry=[Point(centroid.x, centroid.y) for centroid in centroids],
crs='EPSG:4326')
# Convert centroids to a GeoDataFrame and include building IDs
self.centroids_gdf = gpd.GeoDataFrame({
'geometry': [Point(centroid.x, centroid.y) for centroid in centroids],
'building_id': building_ids # Add building IDs as a column
}, crs='EPSG:4326')
# Load road data
self.gdf_road = gpd.read_file(self.roads_file)
@ -126,15 +130,12 @@ class DistrictHeatingNetworkCreator:
G = nx.Graph()
# 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)):
for idx, row in self.centroids_gdf.iterrows():
building_name = f"Building_{idx + 1}"
G.add_node((centroid.x, centroid.y),
G.add_node((row.geometry.x, row.geometry.y),
type='centroid',
name=building_name,
building_centroid=(centroid_4326.x, centroid_4326.y))
building_id=row['building_id']) # Add building ID as an attribute
for point in self.nearest_points:
G.add_node((point.x, point.y), type='nearest_point')

View File

@ -10,4 +10,5 @@ network_creator = DistrictHeatingNetworkCreator(
network_graph = network_creator.run()
# Plot the network graph (optional)
network_creator.plot_network_graph(network_graph)
for node, attrs in network_graph.nodes(data=True):
print(f"Node: {node}, Attributes: {attrs}")