forked from Noctem/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 17
/
raidex.py
93 lines (78 loc) · 2.74 KB
/
raidex.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
#!/usr/bin/env python3
from datetime import datetime
from pkg_resources import resource_filename
try:
from ujson import dumps
from flask import json as flask_json
flask_json.dumps = lambda obj, **kwargs: dumps(obj, double_precision=6)
except ImportError:
from json import dumps
from flask import Flask, jsonify, Markup, render_template, request
from monocle import db, sanitized as conf
from monocle.web_utils import *
from monocle.bounds import area, center
from shapely.geometry import Polygon, Point, LineString
import s2sphere
app = Flask(__name__, template_folder=resource_filename('monocle', 'templates'), static_folder=resource_filename('monocle', 'static'))
def render_map():
template = app.jinja_env.get_template('raidex.html')
return template.render(
area_name=conf.AREA_NAME,
map_center=center,
map_provider_url=conf.MAP_PROVIDER_URL,
map_provider_attribution=conf.MAP_PROVIDER_ATTRIBUTION
)
@app.route('/')
def fullmap(map_html=render_map()):
return map_html
@app.route('/gym_data')
def gym_data():
gyms = []
parks = get_all_parks()
for g in get_gym_markers():
gym_point = Point(g['lat'], g['lon'])
cell = Polygon(get_s2_cell_as_polygon(g['lat'], g['lon'], 20)) # s2 lvl 20
for p in parks:
coords = p['coords']
# osm polygon can be a line
if len(coords) == 2:
shape = LineString(coords)
if shape.within(cell.centroid):
gyms.append(g)
break
if len(coords) > 2:
shape = Polygon(coords)
if shape.contains(cell.centroid):
gyms.append(g)
break
return jsonify(gyms)
@app.route('/parks_cells')
def parks_cells():
markers = []
parks = get_all_parks()
for g in get_gym_markers():
gym_point = Point(g['lat'], g['lon'])
cell = Polygon(get_s2_cell_as_polygon(g['lat'], g['lon'], 20)) # s2 lvl 20
for p in parks:
coords = p['coords']
if len(coords) > 2:
shape = Polygon(coords)
if shape.contains(gym_point) or cell.intersects(shape):
bounds = shape.bounds
markers += get_s2_cells(bounds[0], bounds[1], bounds[2], bounds[3], 20)
break
return jsonify(markers)
@app.route('/parks')
def parks():
return jsonify(get_all_parks())
@app.route('/cells')
def cells():
return jsonify(get_s2_cells(level=13))
@app.route('/scan_coords')
def scan_coords():
return jsonify(get_scan_coords())
def main():
args = get_args()
app.run(debug=args.debug, threaded=True, host=args.host, port=args.port)
if __name__ == '__main__':
main()