File size: 5,709 Bytes
a08a4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from geopandas import GeoDataFrame
from networkx import MultiDiGraph
import pandas as pd
import numpy as np
import osmnx as ox
from shapely.geometry import LineString, MultiLineString
from sklearn.neighbors import BallTree
import requests
from sklearn.cluster import KMeans
from datetime import datetime

def filter_by_direction(selected_road: GeoDataFrame, road_direction: str) -> GeoDataFrame:
    if road_direction == 'North':
        return selected_road[
            (selected_road['bearing'] >= 270) | (selected_road['bearing'] <= 90)
        ]
    elif road_direction == 'South':
        return selected_road[
            (selected_road['bearing'] > 90) & (selected_road['bearing'] < 270)
        ]
    elif road_direction == 'East':
        return selected_road[
            (selected_road['bearing'] >= 0) & (selected_road['bearing'] <= 180)
        ]
    elif road_direction == 'West':
        return selected_road[
            (selected_road['bearing'] > 180) & (selected_road['bearing'] < 360)
        ]
    else:
        raise ValueError(f"Invalid road_direction: {road_direction}. Must be one of: North, South, East, West.")

def add_weather_to_df(df: pd.DataFrame, num_clusters: int = 4 , api_key = 'FLMEW5QEEB8WT8YGUJXF6KAPK', time: datetime | None = None) -> pd.DataFrame:
    if df.empty:
        df['weather'] = None
        return df
    
    if time is None:
        time = datetime.now()
    
    coords = df[['Latitude', 'Longitude']].dropna().values
    kmeans = KMeans(n_clusters=min(num_clusters, len(coords)), random_state=42)
    df['weather_cluster'] = kmeans.fit_predict(coords)

    weather_data = {}
    date_str = time.strftime("%Y-%m-%d")
    target_hour = time.strftime("%H:%M:%S")

    for cluster_id in range(kmeans.n_clusters): # type: ignore
        lat, lon = kmeans.cluster_centers_[cluster_id]
        url = f"https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{lat},{lon}/{date_str}"
        params = {
            "key": api_key,
            "unitGroup": "metric",
            "contentType": "json"
        }

        try:
            response = requests.get(url=url, params=params)
            response.raise_for_status()
            data = response.json()
            hours = data.get("days", [{}])[0].get("hours", [])

            def hour_diff(hour_entry):
                try:
                    return abs(datetime.strptime(hour_entry["datetime"], "%H:%M:%S") - datetime.strptime(target_hour, "%H:%M:%S"))
                except:
                    return datetime.max

            if hours:
                best_match = min(hours, key=hour_diff)
                weather = best_match.get("conditions", "Unknown")
                weather_time = best_match.get("datetime", None)
            else:
                weather = "Unknown"
                weather_time = None

        except Exception as e:
            print(f"Weather api error for cluster {cluster_id}: {e}")
            weather = "Unknown"
            weather_time = None
        
        weather_data[cluster_id] = {
            "conditions": weather,
            "datetime": weather_time
        }

    df['time'] = time
    df['weather'] = df['weather_cluster'].map(lambda x: weather_data[x]["conditions"])
    df['weather_time'] = df['weather_cluster'].map(lambda x: weather_data[x]["datetime"])
    df.drop(columns=['weather_cluster'], inplace=True)
    return df

def get_coordinates_from_network(G : MultiDiGraph, road_name: str, road_direction: str):

    edges = ox.graph_to_gdfs(G, nodes=False, edges=True)

    edges_motorway = edges[edges['highway'].isin(['motorway', 'motorway_link'])]

    selected_road = edges_motorway[
        edges_motorway['ref'].str.contains(road_name, na=False, case=False)
    ]

    selected_road = filter_by_direction(selected_road, road_direction)

    rows = []

    for _, row in selected_road.iterrows():
        lanes = row.get("lanes", None)
        maxspeed = row.get("maxspeed", None)
        road_name = row.get("name", None) # type: ignore
        ref = row.get("ref", None)
        geometry = row.geometry

        if isinstance(geometry, LineString):
            coords = geometry.coords
        elif isinstance(geometry, MultiLineString):
            coords = [pt for line in geometry.geoms for pt in line.coords]
        else:
            continue

        for lon, lat in coords:
            rows.append({
                "Longitude": lon,
                "Latitude": lat,
                "lanes": lanes,
                "maxspeed": maxspeed,
                "road_name": road_name,
                "ref": ref,
                "direction" : road_direction
            })

    # Step 6: Build DataFrame
    road_df = pd.DataFrame(rows)
    print(f"Total points in {road_name} - {road_direction}: {len(road_df)}")
    return road_df


def sort_gps_by_greedy_path(df: pd.DataFrame) -> pd.DataFrame:
    """
    Greedy nearest-neighbor sorting of GPS coordinates.
    
    Args:
        df (pd.DataFrame): DataFrame with 'Latitude' and 'Longitude' columns.
    
    Returns:
        pd.DataFrame: Reordered DataFrame.
    """
    coords_rad = np.radians(df[['Latitude', 'Longitude']].values)
    tree = BallTree(coords_rad, metric='haversine')

    visited = np.zeros(len(df), dtype=bool)
    path = []
    current_idx = 0  # or use farthest-point-start logic

    for _ in range(len(df)):
        visited[current_idx] = True
        path.append(current_idx)

        dist, ind = tree.query([coords_rad[current_idx]], k=len(df))

        for next_idx in ind[0]:
            if not visited[next_idx]:
                current_idx = next_idx
                break

    return df.iloc[path].reset_index(drop=True)