-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (48 loc) · 2.1 KB
/
main.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
#!/usr/bin/env python
import os, sys; sys.executable = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'venv', 'bin', 'python')
import folium
import gpxpy
import os
import glob
def convert_gpx_to_map(gpx_file, output_html):
# Parse GPX file
gpx = gpxpy.parse(open(gpx_file))
# Extract latitude and longitude coordinates
points = []
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
points.append([point.latitude, point.longitude])
# Calculate the midpoint between the minimum and maximum latitude and longitude values (to center the map)
midpoint_lat = (min(p[0] for p in points) + max(p[0] for p in points)) / 2
midpoint_lon = (min(p[1] for p in points) + max(p[1] for p in points)) / 2
# Create map object using Folium
resolution = 2048
map_obj = folium.Map(
tiles = 'https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}{r}.png?api_key=6d83354b-5801-4255-ac4b-6b41df50cf8c',
attr = 'Massilia Bastards',
location=[midpoint_lat, midpoint_lon],
width=resolution,
height=resolution,
zoom_control=False,
prefer_canvas=True,
zoom_start = 14)
# Calculate the bounds of the GPX track
min_lat = min(p[0] for p in points)
max_lat = max(p[0] for p in points)
min_lon = min(p[1] for p in points)
max_lon = max(p[1] for p in points)
# Fit the map to the bounds of the GPX track
map_obj.fit_bounds([(min_lat, min_lon), (max_lat, max_lon)], padding = (40,40))
# Plot coordinates on the map
folium.PolyLine(points, color="#FF0000", weight=5, line_cap="round", line_join="round", opacity=1).add_to(
map_obj)
# Save the map as an HTML file
map_obj.save(output_html)
# Print
print(f"{os.path.basename(gpx_file)} done")
gpx_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'gpx_files')
gpx_files = glob.glob(gpx_dir + '/*.gpx')
for gpx in gpx_files:
output_html = f"output_html/map_{os.path.basename(gpx).split('.')[0]}.html"
convert_gpx_to_map(gpx, output_html)