pngtile/wsgi.py
author Tero Marttila <terom@fixme.fi>
Wed, 06 Jan 2010 14:03:11 +0200
changeset 30 53e99e552122
child 31 7eec7486a0af
permissions -rw-r--r--
move the python/web code in
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Our WSGI web interface, which can serve the JS UI and any .png tiles via HTTP.
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
from werkzeug import Request, Response, responder
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
from werkzeug import exceptions
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import os.path
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
import pypngtile as pt
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
DATA_ROOT = os.path.abspath('data')
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
IMAGE_CACHE = {}
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
TILE_WIDTH = 256
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
TILE_HEIGHT = 256
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
def image_view (req, image_path, image) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    image_name = os.path.basename(image_path)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    return """\
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    <head>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        <title>%(title)s</title>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        <script src="%(prefix)s/static/prototype.js" type="text/javascript"></script>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        <script src="%(prefix)s/static/dragdrop.js" type="text/javascript"></script>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        <script src="%(prefix)s/static/builder.js" type="text/javascript"></script>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        <script src="%(prefix)s/static/tiles2.js" type="text/javascript"></script>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        <link rel="Stylesheet" type="text/css" href="%(prefix)s/static/style.css">
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    </head>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    <body>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        <div id="wrapper">
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
            <div id="viewport" style="width: 100%%; height: 100%%">
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
                <div class="substrate"></div>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
            </div>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        </div>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        <script type="text/javascript">
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
            var tile_source = new Source("%(tile_url)s", %(tile_width)d, %(tile_height)d, 0, 0);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
            var main = new Viewport(tile_source, "viewport");
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        </script>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    </body>
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
</html>""" % dict(
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        title           = image_name,
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        prefix          = os.path.dirname(req.script_root).rstrip('/'),
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        tile_url        = req.url,
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        tile_width      = TILE_WIDTH,
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        tile_height     = TILE_HEIGHT,
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    )
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
def render_tile (image, x, y) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    return image.tile_mem(TILE_WIDTH, TILE_HEIGHT, x, y)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
def handle_main (req) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    # path to image
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    image = req.path.lstrip('/')
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    # check a .png filename was given
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    if not image or not image.endswith('.png') :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        raise exceptions.BadRequest("no .png path given")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    # build absolute path
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    image_path = os.path.abspath(os.path.join(DATA_ROOT, image))
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    # ensure the path points inside the data root
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
    if not image_path.startswith(DATA_ROOT) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        raise exceptions.NotFound(image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    # get Image object
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    if image_path in IMAGE_CACHE :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        # get from cache
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        image = IMAGE_CACHE[image_path]
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    else :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        # ensure exists
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
        if not os.path.exists(image_path) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
            raise exceptions.NotFound(image)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
        # cache
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        image = IMAGE_CACHE[image_path] = pt.Image(image_path)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    # what view?
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
    if not req.args :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
        # viewport
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
        return Response(image_view(req, image_path, image), content_type="text/html")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    elif 'x' in req.args and 'y' in req.args :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        # tile
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
        x = int(req.args['x'])
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        y = int(req.args['y'])
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        # yay render
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
        return Response(render_tile(image, x, y), content_type="image/png")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
   
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    else :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        raise exceptions.BadRequest("Unknown args")
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
   
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
@responder
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
def application (env, start_response) :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    req = Request(env, start_response)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    try :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        return handle_main(req)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    except exceptions.HTTPException, e :
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
        return e
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109