-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·118 lines (86 loc) · 3.02 KB
/
app.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
#!/usr/bin/env python3
from flask import Flask, render_template, Response, request, redirect
import json
import markdown
from lib.cache import cache
from lib.config import Config
from lib.i18n import messages, set_lang, get_lang
from lib.utils import *
from flask_compress import Compress
app = Flask(__name__)
app.config.update(Config.__dict__)
cache.init_app(app)
Compress(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
with open("templates/about_%s.md" % get_lang(), "r") as f :
about_text = markdown.markdown(f.read())
return render_template("about.html", about_text=about_text)
@app.route("/api/itineraries")
def get_itineraries():
start = list(map(lambda s: float(s), request.args["start"].split(",")))
end = list(map(lambda s: float(s), request.args["end"].split(",")))
moutain = str2bool(request.args.get("mountain", "false"))
elec = str2bool(request.args.get("elec", "false"))
best_only = str2bool(request.args.get("best_only", "true"))
try :
itis = get_all_itineraries(start, end, best_only, electric=elec, mountainBike=moutain)
except HttpError as e:
if e.status == 400:
return "No itinerary found", 400
error("Error happened", e)
car_distance = None
try :
car_iti = get_route_safe(start, end, "car-fast", 1)
car_distance = car_iti.length
except:
pass
return js_response(dict(
itineraries=itis,
car_distance=car_distance))
def export(template, mime) :
start = list(map(lambda s: float(s), request.args["start"].split(",")))
end = list(map(lambda s: float(s), request.args["end"].split(",")))
profile = request.args["profile"]
alternative = int(request.args["alt"])
iti = get_route_safe(start, end, profile, alternative)
points = []
for path in iti.paths:
points += path.coords[0:-1]
points.append(iti.paths[-1].coords[-1])
out = render_template(template, points=points)
return Response(
out,
mimetype=mime,
headers={'Content-Disposition': 'attachment;filename=' + template})
@app.route("/api/gpx")
def gpx():
return export("route.gpx", "application/gpx+xml")
@app.route("/api/kml")
def kml():
return export("route.kml", "application/vnd.google-earth.kml+xml")
@app.route("/lang/<lang>")
def set_lang_route(lang):
set_lang(lang)
return redirect("/")
@app.context_processor
def global_jinja_context():
return dict(
_=messages(),
js_conf=dict(
center=Config.CENTER,
init_zoom=Config.INIT_ZOOM,
country=Config.COUNTRY),
lang=get_lang())
if __name__ == '__main__':
app.run()
#res = get_route(from_latlon, to_latlon, "safety")
#print(dumps(res, indent=2))
#with open("res/itinerary.json") as f :
# iti = process_message(json.load(f))
# print(to_json(iti))
# print("nb Paths", len(iti.paths))
# print("nb Coords", sum(len(path.coords) for path in iti.paths))