-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_map.py
322 lines (282 loc) · 12.4 KB
/
load_map.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import folium
from folium.plugins import MarkerCluster, Fullscreen
from folium.features import CustomIcon
import json
import os
import utils.js_helpers
import utils.MadBoulderDatabase
POPUP_WIDTH = 100
WIDTH_MULTIPLIER = 15
DEFAULT_AREA_ZOOM = 10
SECTOR_OPACITY = 0.6
MARKER_SIZE = 32
ICON_SIZE = 24
PLACEHOLDER = '_placeholder'
APPROX_PLACEHOLDER = 'approx_placeholder'
BETA_VIDEOS_TEXT = 'Beta Videos: '
COUNTRY_CODE_FIELD = 'country'
STATE_CODE_FIELD = 'state'
#####################
### GENERATE MAPS ###
#####################
def load_map(areaCode, areaData, datafile, generate_ids, return_html=True):
"""
Create a map for a bouldering zone that shows the GEOJSON data, names and
links to video playlists of its sectors as well as the parking areas. All
this data should be provided via a JSON file
"""
generate_ids.reset_seed()
area_map = folium.Map(location=[areaData['latitude'], areaData['longitude']],
zoom_start=areaData['zoom'])
area_map._id = generate_ids.next_id() # reassign id
for child in area_map._children.values():
child._id = generate_ids.next_id()
tile_layer = folium.TileLayer(
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
name='Satellite',
attr='Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
)
tile_layer._id = generate_ids.next_id() # reassign id
tile_layer.add_to(area_map)
# Add fullscreen button to map
fs = Fullscreen()
fs._id = generate_ids.next_id()
fs.add_to(area_map)
sectors = areaData.get('sectors', [])
# Create a Folium feature group for this layer, since we will be displaying multiple layers
sector_lyr = folium.FeatureGroup(name='Parking Markers')
sector_lyr._id = generate_ids.next_id() # reassign id
#for sector in sectors:
# if not sector['sector_data'] or not sector['link']:
# continue
# sector_map = folium.GeoJson(
# os.path.dirname(os.path.abspath(datafile))+sector['sector_data'],
# name=sector['name'],
# tooltip=sector['name'],
# style_function=lambda x: {
# 'color': x['properties']['stroke'],
# 'weight': x['properties']['stroke-width'],
# 'opacity': SECTOR_OPACITY,
# 'fillColor': x['properties']['stroke'],
# }
# )
# sector_map._id = generate_ids.next_id() # reassign id
# sector_html = utils.js_helpers.generate_sector_html(
# sector['name'], sector['link'])
# sector_popup = folium.Popup(
# sector_html,
# max_width=POPUP_WIDTH,
# min_width=POPUP_WIDTHº
# )
# sector_popup._id = generate_ids.next_id() # reassign id
# for child in sector_popup._children.values():
# child._id = generate_ids.next_id()
#
# sector_map.add_child(sector_popup)
#
# sector_lyr.add_child(sector_map)
#
# Parking areas
if 'parkings' in areaData and areaData['parkings']:
for parking in areaData['parkings']:
parking_icon = CustomIcon(
'static/images/icons/parking.png',
icon_size=(ICON_SIZE, ICON_SIZE)
)
parking_icon._id = generate_ids.next_id() # reassign id
parking_marker = folium.Marker(
location=[parking['parking_latitude'],
parking['parking_longitude']],
popup=utils.js_helpers.generate_parking_html([parking['parking_latitude'],
parking['parking_longitude']]),
tooltip='Parking',
icon=parking_icon
)
parking_marker._id = generate_ids.next_id() # reassign id
for child in parking_marker._children.values():
child._id = generate_ids.next_id()
sector_lyr.add_child(parking_marker)
# Approximation
if areaData.get('approximation', None) is not None:
import gpxpy
import gpxpy.gpx
approximation_geojson = {
'type': 'Feature',
'properties': {
'stroke': '#1f1a95',
'stroke-opacity': 1,
'stroke-width': 2
},
'geometry': {
'type': 'LineString',
'coordinates': []
}
}
gpx_path = 'data/zones/' + areaCode + '/' + areaData.get('approximation')
with open(gpx_path, 'r') as gpx_file:
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
approximation_geojson['geometry']['coordinates'].append(
[point.longitude, point.latitude])
zone_approximation = folium.GeoJson(
approximation_geojson,
name='Approximation',
tooltip=APPROX_PLACEHOLDER,
style_function=lambda x: {
'color': x['properties']['stroke'],
'weight': x['properties']['stroke-width'],
'opacity': SECTOR_OPACITY,
'fillColor': x['properties']['stroke'],
}
)
zone_approximation._id = generate_ids.next_id() # reassign id
zone_approx_html = utils.js_helpers.generate_file_download_html(
areaCode, areaData.get('approximation'), 'Track')
track_popup = folium.Popup(
zone_approx_html,
max_width=POPUP_WIDTH,
min_width=POPUP_WIDTH
)
track_popup._id = generate_ids.next_id() # reassign id
for child in track_popup._children.values():
child._id = generate_ids.next_id()
zone_approximation.add_child(track_popup)
sector_lyr.add_child(zone_approximation)
# Sectors
zoomed_out_lyr = folium.FeatureGroup(name='Zone Marker')
zoomed_out_lyr._id = generate_ids.next_id() # reassign id
zoomed_out_icon = CustomIcon(
'static/images/marker/marker.webp', icon_size=(MARKER_SIZE, MARKER_SIZE))
zoomed_out_icon._id = generate_ids.next_id() # reassign id
sectors_marker = folium.Marker(
location=[areaData['latitude'], areaData['longitude']],
tooltip=areaData['name'],
icon=zoomed_out_icon
)
sectors_marker._id = generate_ids.next_id() # reassign id
zoomed_out_lyr.add_child(sectors_marker)
area_map.add_child(zoomed_out_lyr)
area_map.add_child(sector_lyr)
layer_control = folium.LayerControl()
layer_control._id = generate_ids.next_id() # reassign id
layer_control.add_to(area_map)
# Since folium does not support all the functionalities we need
# we obtain them by injecting JavaScript code in the map html
map_html = area_map.get_root().render()
map_html = utils.js_helpers.make_layer_that_hides(
map_html, area_map.get_name(), sector_lyr.get_name(), DEFAULT_AREA_ZOOM)
#map_html = utils.js_helpers.make_layer_that_hides(
# map_html, area_map.get_name(), zoomed_out_lyr.get_name(), DEFAULT_AREA_ZOOM, False, True)
# Zoom into area when clicking
map_html = utils.js_helpers.zoom_on_click(
map_html, area_map.get_name(), sectors_marker.get_name(), DEFAULT_AREA_ZOOM+1)
map_html = utils.js_helpers.enable_links_from_iframe(map_html)
map_html = utils.js_helpers.replace_maps_placeholder(map_html)
map_html = utils.js_helpers.replace_approx_placeholders_for_translations(
map_html, APPROX_PLACEHOLDER)
# Avoid zooming in when clicking on a sector area
map_html = utils.js_helpers.remove_geojson_zoom_on_click(map_html)
# replace the ids of all the html tags
map_html = utils.js_helpers.replace_tag_ids(
map_html, ['html'], generate_ids)
return map_html if return_html else area_map
def load_general_map(areaData, generate_ids, return_html=True):
"""
Create a map that contains all the zones provided by the list of zone_data
i.e. all areas combined in one map. This map only shows the markers that
indicate an existing area.
"""
generate_ids.reset_seed()
area_map = folium.Map(
location=[
15.417195,
-15.184135
],
zoom_start=3
)
area_map._id = generate_ids.next_id() # reassign id
for child in area_map._children.values():
child._id = generate_ids.next_id()
tile_layer = folium.TileLayer(
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
name='Satellite',
attr='Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
)
tile_layer._id = generate_ids.next_id() # reassign id
tile_layer.add_to(area_map)
# Add fullscreen button to map
fs = Fullscreen()
fs._id = generate_ids.next_id()
fs.add_to(area_map)
# Areas layer
zoomed_out_lyr = folium.FeatureGroup(name='Areas Markers')
zoomed_out_lyr._id = generate_ids.next_id() # reassign id
areas_cluster = MarkerCluster()
areas_cluster._id = generate_ids.next_id() # reassign id
# Get countries data
countriesData = utils.MadBoulderDatabase.getCountriesData()
playlistsData = utils.MadBoulderDatabase.getPlaylistsData()
for areaCode, area in areaData.items():
zoomed_out_icon = CustomIcon(
'static/images/marker/marker.webp', icon_size=(MARKER_SIZE, MARKER_SIZE))
zoomed_out_icon._id = generate_ids.next_id() # reassign id
if areaCode in playlistsData:
playlists = playlistsData[areaCode]
# Get the area thumbnail from playlists, using fallbacks
thumbnail = (playlists.get('thumbnails', {}).get('medium') or
playlists.get('thumbnails', {}).get('default') or
'/static/images/placeholder.webp')
else:
thumbnail = '/static/images/placeholder.webp'
playlists = {'video_count': 0}
# Get location info
country_code = area.get(COUNTRY_CODE_FIELD, '')
state_code = area.get(STATE_CODE_FIELD, '')
# Get country and state names from codes
country = countriesData.get(country_code, {}).get('name', '') if country_code else ''
state = (countriesData.get(country_code, {})
.get('states', {})
.get(state_code, {})
.get('name', '')) if country_code and state_code else ''
popup_html = folium.Html(utils.js_helpers.generate_area_popup_html(
area['name'], areaCode, playlists['video_count'], thumbnail,
country=country, state=state), script=True)
popup_html._id = generate_ids.next_id() # reassign id
zone_popup = folium.Popup(
popup_html, max_width=350) # Increased max_width to accommodate the image
zone_popup._id = generate_ids.next_id() # reassign id
# Create marker with permanent tooltip
area_marker = folium.Marker(
location=[area['latitude'], area['longitude']],
tooltip=folium.Tooltip(
area['name'],
permanent=True,
direction='bottom',
className='area-label',
sticky=False,
interactive=False
),
icon=CustomIcon(
'static/images/marker/marker.webp',
icon_size=(MARKER_SIZE, MARKER_SIZE),
icon_anchor=(MARKER_SIZE/2, MARKER_SIZE)
),
popup=zone_popup,
)
area_marker._id = generate_ids.next_id() # reassign id
# Group areas' markers when zoomed out
areas_cluster.add_child(area_marker)
zoomed_out_lyr.add_child(areas_cluster)
area_map.add_child(zoomed_out_lyr)
layer_control = folium.LayerControl()
layer_control._id = generate_ids.next_id() # reassign id
layer_control.add_to(area_map)
# Add CSS link for the map styles
map_html = area_map.get_root().render()
css_link = '<link rel="stylesheet" href="/static/css/map.css">'
map_html = map_html.replace('</head>', f'{css_link}</head>')
map_html = utils.js_helpers.replace_tag_ids(
map_html, ['html'], generate_ids)
return map_html if return_html else area_map