Spaces:
Sleeping

File size: 4,281 Bytes
90776a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from geopy.distance import geodesic
from geopy.geocoders import Nominatim
import asyncio

geolocator = Nominatim(user_agent="crowdroute_app")

# Average speeds (km/h) per transport in Indian cities
TRANSPORT_SPEEDS = {
    "bus":        18,   # city bus avg including stops
    "metro":      35,   # metro avg
    "train":      45,   # suburban train avg
    "chigari":    20,   # electric bus
    "ferry":      15,   # ferry
    "tram":       12,   # tram
    "toy_train":  20,   # mountain railway
    "shared_cab": 25,   # shared cab
    "shikara":    8,    # shikara
}

# Crowd level β†’ speed reduction factor
CROWD_SPEED_FACTOR = {
    "LOW":    1.0,    # full speed
    "MEDIUM": 0.75,   # 25% slower
    "HIGH":   0.5,    # 50% slower
}


def get_coords(location: str, city: str) -> tuple:
    """Get lat/lon for a location in a city."""
    try:
        query    = f"{location}, {city}, India"
        loc      = geolocator.geocode(query, timeout=10)
        if loc:
            return (loc.latitude, loc.longitude)
    except Exception:
        pass
    return None


def calculate_distance(source: str, destination: str, city: str) -> float:
    """Calculate straight-line distance in km between source and destination."""
    src_coords  = get_coords(source, city)
    dst_coords  = get_coords(destination, city)

    if src_coords and dst_coords:
        dist = geodesic(src_coords, dst_coords).km
        # Road distance is typically 1.3x straight line
        return round(dist * 1.3, 2)

    # Fallback β€” return None if geocoding fails
    return None


def estimate_travel_time(
    distance_km:     float,
    transport_type:  str,
    crowd_level:     str
) -> str:
    """Estimate travel time based on distance, transport, and crowd."""
    if distance_km is None:
        return "N/A"

    base_speed    = TRANSPORT_SPEEDS.get(transport_type, 20)
    crowd_factor  = CROWD_SPEED_FACTOR.get(crowd_level, 1.0)
    effective_speed = base_speed * crowd_factor

    # Time in minutes
    time_minutes  = (distance_km / effective_speed) * 60

    # Add boarding/waiting time per transport
    waiting_times = {
        "bus":        8,
        "metro":      5,
        "train":      10,
        "chigari":    8,
        "ferry":      15,
        "shared_cab": 5,
        "tram":       6,
        "toy_train":  10,
        "shikara":    5,
    }
    total_minutes = time_minutes + waiting_times.get(transport_type, 5)
    total_minutes = round(total_minutes)

    if total_minutes < 60:
        return f"{total_minutes} mins"
    else:
        hours   = total_minutes // 60
        minutes = total_minutes % 60
        return f"{hours}h {minutes}m"


def get_route_note(
    transport_type: str,
    source:         str,
    destination:    str,
    crowd_level:    str
) -> str:
    """Generate a helpful route note."""
    notes = {
        "bus": {
            "LOW":    f"Direct bus from {source} to {destination} β€” smooth ride!",
            "MEDIUM": f"Buses running but expect some crowding near stops.",
            "HIGH":   f"Buses very crowded β€” consider waiting for next one.",
        },
        "metro": {
            "LOW":    f"Metro is the fastest option right now.",
            "MEDIUM": f"Metro is moderately busy β€” stand near doors for easy exit.",
            "HIGH":   f"Metro is packed β€” wait for the next train if possible.",
        },
        "train": {
            "LOW":    f"Train is comfortable and quick.",
            "MEDIUM": f"Train is filling up β€” board from less busy coaches.",
            "HIGH":   f"Train is very crowded β€” consider general vs reserved coaches.",
        },
        "chigari": {
            "LOW":    f"Chigari electric bus is running smoothly β€” eco-friendly choice!",
            "MEDIUM": f"Chigari is moderately occupied.",
            "HIGH":   f"Chigari is crowded β€” next bus in ~10 mins.",
        },
        "ferry": {
            "LOW":    f"Ferry is calm and comfortable.",
            "MEDIUM": f"Ferry is filling up β€” book your ticket early.",
            "HIGH":   f"Ferry is at capacity β€” next departure recommended.",
        },
    }
    transport_notes = notes.get(transport_type, {})
    return transport_notes.get(crowd_level, f"Travel from {source} to {destination}.")