-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx2map.py
96 lines (70 loc) · 2.55 KB
/
gpx2map.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""./ngrok http -region=eu 5000"""
import os
import gpxpy
import gpxpy.gpx
import folium
from flask import Flask, send_file, request, redirect, url_for, render_template, abort, flash
from werkzeug.utils import secure_filename
__version__ = 0.1
__author__ = "Artur Wronowski"
UPLOAD_FOLDER = 'uploads'
MAP_FOLDER = 'maps'
ALLOWED_EXTENSIONS = set(['gpx'])
app = Flask(__name__)
app.secret_key = 'some_secret'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def genmap(filename, _color='red', _weight='4'):
""" generuje mape"""
gpx_file = open(filename, 'r')
gpx = gpxpy.parse(gpx_file)
points = []
if gpx.tracks != 1:
flash('Brak tras')
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
points.append(tuple([point.latitude, point.longitude]))
ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)
map = folium.Map(location=[ave_lat, ave_lon], zoom_start=13)
folium.PolyLine(points, color=_color, opacity=0.7, weight=_weight).add_to(map)
map.save('maps/map.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
"""formatka upload pliku"""
# sprawdzanie czy katalogi istnieją
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(MAP_FOLDER):
os.makedirs(MAP_FOLDER)
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# jeżeli user nie wybrał pliku
if file.filename == '':
flash('Nie wybrano pliku')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
# save file in folder
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# generate map
genmap(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# remove file
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('show_map'))
return render_template('index.html')
@app.route('/maps/map.html')
def show_map():
"""pokaż mapę"""
return send_file('maps/map.html')
if __name__ == '__main__':
app.run(debug=True, host= '0.0.0.0')