Skip to content

added sanic app #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions mapshader/sanic_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()