-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·121 lines (104 loc) · 4.97 KB
/
server.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
#!/usr/bin/env python3
import shutil
from datetime import datetime
from importlib.metadata import version
import random
import string
import json
import traceback
import os
from schematization import *
from flask import Flask, request, send_file
def log(uid, lat, lng, c0, c1, c2, ignore_pp, fixed_width, turns, layout, margins, scale, draw_all_island, normalize_angles, angle_normalization, snap_aligned_streets, threshold_small_island, error=None, comment=None):
xml = ""
for file in shutil.os.listdir("static/cache/"+uid):
if file.endswith('.xml'):
xml = "static/cache/%s/%s"%(uid,file)
break
libraries = ""
for lib in ["osmnx","crossroads-segmentation","crmodel", "crossroads-schematization"]:
libraries += "%s %s\n"%(lib, version(lib))
logfile = "DATE : %s\nPOSITION : %s %s\nC0 C1 C2 : %s %s %s\nignore_pp : %s\nfixed_width: %s\nturns: %s\nlayout: %s\nmargins: %s\nscale: %s\ndraw_all_island: %s\nnormalize_angles: %s\nangle_normalization: %s\nsnap_aligned_streets: %s\nthreshold_small_island: %s\nIBRARIES : \n%s"%(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), lat, lng, c0, c1, c2, ignore_pp, fixed_width, turns, layout, margins, scale, draw_all_island, normalize_angles, angle_normalization, snap_aligned_streets, threshold_small_island, libraries)
if comment:
logfile += "COMMENT : \n%s\n"%comment
if error:
logfile += "ERROR : \n%s"%error
if xml:
with open(xml, 'r') as f:
content = f.read()
logfile += "OSMXML CONTENT : \n%s"%content
fname = ("error_%s%s.log" if error else "%s%s.log")%(''.join(random.choice(string.ascii_letters) for i in range(10)), datetime.now().timestamp())
with open("log/"+fname, 'w') as f:
f.write(logfile)
# clear folders
shutil.rmtree("static/cache", ignore_errors=True), shutil.os.mkdir("static/cache") , shutil.os.makedirs("log", exist_ok=True)
app = Flask(__name__)
@app.route("/")
def html():
return app.send_static_file('index.html')
def build_schematization():
args = request.args
lat, lng, c0, c1, c2, uid, comment = args.get("lat"), args.get("lng"), args.get("c0"), args.get("c1"), args.get("c2"), args.get("uid"), args.get("comment")
if c0 is None:
c0 = 2
if c1 is None:
c1 = 2
if c2 is None:
c2 = 4
turns, ignore_pp, fixed_width, layout = args.get("turns"), args.get("ignore_pp"), args.get("fixed_width"), args.get("layout")
ignore_pp = ignore_pp == "true"
fixed_width = fixed_width == "true"
margins = args.get("margins")
scale = args.get("scale")
draw_all_island = args.get("draw_all_island") == "true"
normalize_angles = args.get("normalize_angles") == "true"
angle_normalization = args.get("angle_normalization")
snap_aligned_streets = args.get("snap_aligned_streets") == "true"
threshold_small_island = args.get("threshold_small_island")
if lat and lng and uid:
result, directory = generate_schematization_if_required(uid, float(lat), float(lng), float(c0), float(c1), float(c2),
bool(ignore_pp), bool(fixed_width), str(turns), bool(draw_all_island), str(layout), float(margins), int(scale),
bool(normalize_angles), int(angle_normalization), bool(snap_aligned_streets), int(threshold_small_island),
app.logger)
log(uid, lat, lng, c0, c1, c2, ignore_pp, fixed_width, turns, layout, margins, scale, draw_all_island,
normalize_angles, angle_normalization, snap_aligned_streets, threshold_small_island,
result, comment)
return directory
else:
return ""
@app.route("/schematization")
def send_schematization():
url_prefix = "/"
try:
directory = build_schematization()
except Exception as e:
app.logger.info("cannot build schematization (exception: %s)", str(e))
return json.dumps({"error": str(e)})
if directory != "":
try:
result = {
"tif-96": url_prefix + directory + "/" + "schematization-96.tif",
"pdf-300": url_prefix + directory + "/" + "schematization-300.pdf"
}
return json.dumps(result)
except Exception as e:
app.logger.info("cannot send file (exception: %s)", str(e))
return json.dumps({"error": str(e)})
else:
app.logger.info("fichier manquant")
return "fichier manquant"
@app.route("/profile")
def send_profile():
name = request.args.get("name")
if name is None:
app.logger.info("nom de profil manquant")
return json.dumps({"error": "no profile name defined"})
filename = "profiles/" + name + ".json"
if os.path.isfile(filename):
with open(filename) as profile_file:
return profile_file.read()
else:
app.logger.info("profile inconnu")
return json.dumps({"error": "unknown profile"})
if __name__ == '__main__':
app.run(debug=True, use_reloader=False, host='0.0.0.0', port=int(shutil.os.environ.get("PORT", 8080)))