-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsanic_app.py
More file actions
95 lines (70 loc) · 2.83 KB
/
sanic_app.py
File metadata and controls
95 lines (70 loc) · 2.83 KB
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
from functools import partial
try:
from sanic import Sanic
from sanic import response
except ImportError:
raise ImportError('You must install sanic `pip install sanic` to use this module')
from mapshader.core import render_map
from mapshader.core import render_geojson
from mapshader.sources import get_all_sources
def get_tile_func(source):
async def to_tile(request, z=0, x=0, y=0):
img = render_map(source, x=int(x), y=int(y), z=int(z))
bites = img.to_bytesio()
return response.raw(bites.getvalue(), content_type='image/png')
return to_tile
def get_image_func(source):
async def to_image(request,
xmin=-20e6, ymin=-20e6,
xmax=20e6, ymax=20e6,
height=500, width=500):
img = render_map(source, xmin=float(xmin), ymin=float(ymin),
xmax=float(xmax), ymax=float(ymax),
height=int(height), width=int(width))
bites = img.to_bytesio()
return response.raw(bites.getvalue(), content_type='image/png')
return to_image
def get_wms_func(source):
async def to_wms(request):
height = request.args.get('height')
width = request.args.get('width')
bbox = request.args.get('bbox')
xmin, ymin, xmax, ymax = bbox.split(',')
img = render_map(source, xmin=float(xmin), ymin=float(ymin),
xmax=float(xmax), ymax=float(ymax),
height=int(height), width=int(width))
bites = img.to_bytesio()
return response.raw(bites.getvalue(), content_type='image/png')
return to_wms
def get_geojson_func(source):
async def to_geojson(request):
resp = render_geojson(source)
return response.text(resp, content_type='application/json')
return to_geojson
def configure_app(app, user_source_filepath=None):
sources = get_all_sources(user_source_filepath)
for source in sources.values():
app.add_route(get_tile_func(source),
source.tile_url)
app.add_route(get_image_func(source),
source.image_url)
app.add_route(get_wms_func(source),
source.wms_url)
app.add_route(get_geojson_func(source),
source.geojson_url)
return app
def create_app(user_source_filepath=None):
app = Sanic(__name__)
return configure_app(app, user_source_filepath)
if __name__ == '__main__':
from argparse import ArgumentParser
from os import path
parser = ArgumentParser()
parser.add_argument('-f')
user_file = parser.parse_args().f
if user_file:
user_file_path = path.abspath(path.expanduser(user_file))
else:
user_file_path = None
app = create_app(user_file).run(host='0.0.0.0', port=5000, workers=4, debug=True)
app.run()