convert graph edge weights to meters

This commit is contained in:
Majid Rezaei 2024-06-11 10:31:52 -04:00
parent 60ce8c03f6
commit 6a887b8f09
2 changed files with 2956 additions and 2 deletions

View File

@ -4,6 +4,26 @@ from shapely.geometry import Polygon, Point, LineString
import networkx as nx
from typing import List, Tuple
from rtree import index
import math
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great-circle distance between two points
on the Earth specified by their longitude and latitude.
"""
R = 6371000 # Radius of the Earth in meters
phi1 = math.radians(lat1)
phi2 = math.radians(lat2)
delta_phi = math.radians(lat2 - lat1)
delta_lambda = math.radians(lon2 - lon1)
a = math.sin(delta_phi / 2.0) ** 2 + \
math.cos(phi1) * math.cos(phi2) * \
math.sin(delta_lambda / 2.0) ** 2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c # Output distance in meters
class DistrictHeatingNetworkCreator:
@ -29,7 +49,8 @@ class DistrictHeatingNetworkCreator:
self._create_graph()
self._create_mst()
self._iteratively_remove_edges()
self.add_centroids_to_mst() # Add centroids to the MST
self._add_centroids_to_mst()
self._convert_edge_weights_to_meters()
return self.final_mst
def _load_and_process_data(self):
@ -217,7 +238,7 @@ class DistrictHeatingNetworkCreator:
single_connection_street_nodes = find_single_connection_street_nodes(self.final_mst)
self.final_mst_steps.append(list(self.final_mst.edges(data=True)))
def add_centroids_to_mst(self):
def _add_centroids_to_mst(self):
"""
Add centroids to the final MST graph and connect them to their associated node on the graph.
"""
@ -238,6 +259,16 @@ class DistrictHeatingNetworkCreator:
if nearest_point:
self.final_mst.add_edge(centroid_tuple, nearest_point, weight=min_distance)
def _convert_edge_weights_to_meters(self):
"""
Convert all edge weights in the final MST graph to meters using the Haversine formula.
"""
for u, v, data in self.final_mst.edges(data=True):
lon1, lat1 = u
lon2, lat2 = v
distance = haversine(lon1, lat1, lon2, lat2)
self.final_mst[u][v]['weight'] = distance
def plot_network_graph(self):
"""
Plot the network graph using matplotlib and networkx.

File diff suppressed because it is too large Load Diff