File size: 2,831 Bytes
76b3a0b
33e50c6
 
 
2a0373b
 
 
 
33e50c6
 
c698193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ec8d71
78d4a06
2a0373b
6ec8d71
2a0373b
78d4a06
6ec8d71
78d4a06
 
6ec8d71
78d4a06
 
6ec8d71
 
78d4a06
6ec8d71
 
78d4a06
6ec8d71
 
78d4a06
 
 
 
6ec8d71
c698193
 
 
33e50c6
78d4a06
 
 
33e50c6
6ec8d71
c698193
 
33e50c6
c698193
33e50c6
6ec8d71
 
33e50c6
 
6ec8d71
78d4a06
33e50c6
 
c698193
6ec8d71
c698193
a3a2fc3
 
 
6ec8d71
c698193
33e50c6
 
c698193
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
import gradio as gr
import random

locations = {
    "Downtown": {"lat": -1.95, "lon": 30.05},
    "Highway": {"lat": -1.92, "lon": 30.06},
    "Industrial Area": {"lat": -1.94, "lon": 30.07},
    "School Zone": {"lat": -1.93, "lon": 30.03}
}

tile_layers = {
    "Light": {
        "url": "https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
        "attribution": '&copy; <a href="https://carto.com/">CartoDB</a> contributors'
    },
    "Dark": {
        "url": "https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
        "attribution": '&copy; <a href="https://carto.com/">CartoDB</a> contributors'
    },
    "Satellite": {
        "url": "https://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
        "attribution": "Β© Google",
        "subdomains": ["mt0", "mt1", "mt2", "mt3"]
    }
}

def simulate_map_style(style):
    markers = ""
    alerts = []

    for name, info in locations.items():
        speed = round(random.gauss(30, 15), 1)
        vehicles = int(random.gauss(40, 15))

        if speed < 20 and vehicles > 50:
            status = "🚦 Heavy Traffic"
            color = "red"
        elif speed < 40 or vehicles > 40:
            status = "⚠️ Moderate Traffic"
            color = "orange"
        else:
            status = "βœ… Light Traffic"
            color = "green"

        alerts.append(f"πŸ“ {name}: {status} β€” {speed} km/h, {vehicles} cars")

        markers += f"""
        L.circleMarker([{info['lat']}, {info['lon']}], {{
            color: '{color}',
            radius: 10
        }}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicles}<br>Status: {status}");"""

    layer = tile_layers[style]
    subdomains_str = f", subdomains: {layer['subdomains']}" if "subdomains" in layer else ""

    html = f"""
    <div id="map" style="width:100%; height:500px;"></div>
    <link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
    <script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
    <script>
      var map = L.map('map').setView([-1.935, 30.05], 13);
      L.tileLayer('{layer['url']}', {{
        attribution: '{layer['attribution']}',
        maxZoom: 19
        {subdomains_str}
      }}).addTo(map);
      L.control.zoom({{ position: 'topright' }}).addTo(map);
      {markers}
    </script>
    """

    return "\n".join(alerts), html

with gr.Blocks() as demo:
    gr.Markdown("## πŸ—ΊοΈ Traffic Map with Style Selector (Light, Dark, Satellite)")

    style_dropdown = gr.Dropdown(label="Hitamo Style ya Map", choices=list(tile_layers.keys()), value="Light")
    btn = gr.Button("πŸ” Refresh Traffic Data")
    report = gr.Textbox(label="Traffic Report", lines=8)
    map_html = gr.HTML()

    btn.click(fn=simulate_map_style, inputs=style_dropdown, outputs=[report, map_html])

demo.launch()