SevenhuijsenM commited on
Commit
59d42eb
1 Parent(s): 271c68d

Initial commit with map

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import plotly.graph_objects as go
3
+ import json
4
+ import requests
5
+ import os
6
+
7
+ API_KEY = os.getenv("API-KEY-TOMTOM")
8
+
9
+ api_params_incidents = {
10
+ 'base_url': 'api.tomtom.com',
11
+ 'API_KEY': "XyqEVsk8ELwIx6GGPMOqyAIE8dJ22vZM",
12
+ 'min_lon': 18.00,
13
+ 'max_lon': 18.16,
14
+ 'min_lat': 59.25,
15
+ 'max_lat': 59.40,
16
+ 'version_number': 5,
17
+ 'category_filter': '0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C14',
18
+ 'time_validity_filter': 'present',
19
+ 'language': 'en-GB',
20
+ 'fields': '%7Bincidents%7Btype%2Cgeometry%7Bcoordinates%7D%2Cproperties%7Bid%2CmagnitudeOfDelay%2Cevents%7Bdescription%2Ccode%2CiconCategory%7D%2CstartTime%2CendTime%7D%7D%7D'
21
+ }
22
+
23
+ def get_incident_details(params):
24
+ url = f"https://{params['base_url']}/traffic/services/{params['version_number']}/incidentDetails?bbox={params['min_lon']}%2C{params['min_lat']}%2C{params['max_lon']}%2C{params['max_lat']}&fields={params['fields']}&language={params['language']}&categoryFilter={params['category_filter']}&timeValidityFilter={params['time_validity_filter']}&key={params['API_KEY']}"
25
+ return json.loads(requests.get(url).text)
26
+
27
+
28
+ def filter_map():
29
+ latitude_list = []
30
+ longitude_list = []
31
+ index_list = []
32
+ # Get the data from the json
33
+ incidents = get_incident_details(api_params_incidents)['incidents']
34
+ for (_, i) in enumerate(incidents):
35
+ print(i)
36
+
37
+ # Add the coordinates to the list
38
+ if any(isinstance(j, list) for j in i['geometry']['coordinates']):
39
+ latitude_list.append(i['geometry']['coordinates'][0][1])
40
+ longitude_list.append(i['geometry']['coordinates'][0][0])
41
+ else:
42
+ latitude_list.append(i['geometry']['coordinates'][1])
43
+ longitude_list.append(i['geometry']['coordinates'][0])
44
+ index_list.append(i['properties']['id'])
45
+
46
+ fig = go.Figure(go.Scattermapbox(
47
+ customdata=index_list,
48
+ lat=latitude_list,
49
+ lon=longitude_list,
50
+ mode='markers',
51
+ marker=go.scattermapbox.Marker(
52
+ size=6
53
+ ),
54
+ hoverinfo="text",
55
+ hovertemplate='<b>Index</b>: %{customdata}'
56
+ ))
57
+
58
+ fig.update_layout(
59
+ mapbox_style="open-street-map",
60
+ hovermode='closest',
61
+ mapbox=dict(
62
+ bearing=0,
63
+ center=go.layout.mapbox.Center(
64
+ lat=59.32,
65
+ lon=18.08
66
+ ),
67
+ pitch=0,
68
+ zoom=9
69
+ ),
70
+ )
71
+
72
+ return fig
73
+
74
+ with gr.Blocks() as demo:
75
+ with gr.Column():
76
+ btn = gr.Button(value="Update Filter")
77
+ map = gr.Plot()
78
+ demo.load(filter_map, outputs=map)
79
+ btn.click(filter_map, outputs=map)
80
+
81
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy
2
+ pandas
3
+