pngtile/tile.py
changeset 165 1dc09e81a4e2
parent 144 5b2b866ad0a3
child 167 16b600e927fe
equal deleted inserted replaced
164:e1e0c8099c8b 165:1dc09e81a4e2
     1 """
     1 """
     2     Raw tile handling.
     2     Raw tile handling.
     3 """
     3 """
     4 
     4 
     5 from pngtile.application import BaseApplication
     5 from pngtile.application import BaseApplication, url
     6 from werkzeug import Request, Response, exceptions
     6 from werkzeug import Request, Response, exceptions
       
     7 from werkzeug.utils import redirect
     7 
     8 
     8 import pypngtile
     9 import pypngtile
     9 
    10 
    10 ## Coordinates
    11 ## Coordinates
    11 # width of a tile
    12 # width of a tile
    40     """
    41     """
    41 
    42 
    42     return scale(val, zoom) - dim / 2
    43     return scale(val, zoom) - dim / 2
    43 
    44 
    44 class TileApplication (BaseApplication):
    45 class TileApplication (BaseApplication):
    45     def __init__ (self, **opts):
    46     def __init__ (self, image_server, **opts):
       
    47         """
       
    48             image_server:       http://.../ url to image-server frontend
       
    49         """
       
    50 
    46         BaseApplication.__init__(self, **opts)
    51         BaseApplication.__init__(self, **opts)
       
    52 
       
    53         self.image_server = image_server
    47         
    54         
    48     def render_region (self, request, image):
    55     def render_region (self, request, image):
    49         """
    56         """
    50             Handle request for an image region
    57             Handle request for an image region
    51         """
    58         """
    93             return image.tile_mem(width, height, x, y, zoom)
   100             return image.tile_mem(width, height, x, y, zoom)
    94 
   101 
    95         except pypngtile.Error as error:
   102         except pypngtile.Error as error:
    96             raise exceptions.BadRequest(str(error))
   103             raise exceptions.BadRequest(str(error))
    97 
   104 
       
   105     def handle_dir (self, request, name, path):
       
   106         """
       
   107             Redirect to the image frontend for a non-tile request.
       
   108         """
       
   109 
       
   110         if not name.endswith('/'):
       
   111             # avoid an additional redirect
       
   112             name += '/'
       
   113 
       
   114         return redirect(url(self.image_server, name))
       
   115 
       
   116     def handle_image (self, request, name, path):
       
   117         """
       
   118             Redirect to the image frontend for a non-tile request.
       
   119         """
       
   120 
       
   121         return redirect(url(self.image_server, name))
       
   122 
       
   123     def handle_region (self, request):
       
   124         """
       
   125             Return image/png for given region.
       
   126         """
       
   127 
       
   128         try:
       
   129             image, name = self.get_image(request.path)
       
   130         except pypngtile.Error as error:
       
   131             raise exceptions.BadRequest(str(error))
       
   132  
       
   133         png = self.render_region(request, image)
       
   134         
       
   135         return Response(png, content_type='image/png')
       
   136 
       
   137     def handle_tile (self, request):
       
   138         """
       
   139             Return image/png for given tile.
       
   140         """
       
   141 
       
   142         try:
       
   143             image, name = self.get_image(request.path)
       
   144         except pypngtile.Error as error:
       
   145             raise exceptions.BadRequest(str(error))
       
   146             
       
   147         png = self.render_tile(request, image)
       
   148         
       
   149         return Response(png, content_type='image/png')
       
   150 
    98     def handle (self, request):
   151     def handle (self, request):
    99         """
   152         """
   100             Handle request for an image
   153             Handle request for an image
   101         """
   154         """
       
   155 
       
   156         name, path, type = self.lookup_path(request.path)
   102         
   157         
   103         try:
   158         # determine handler
   104             image, name = self.get_image(request.path)
   159         if not type:
   105         except pypngtile.Error as error:
   160             return self.handle_dir(request, name, path)
   106             raise exceptions.BadRequest(str(error))
       
   107 
   161 
   108         if 'w' in request.args and 'h' in request.args and 'x' in request.args and 'y' in request.args:
   162         elif not request.args:
   109             png = self.render_region(request, image)
   163             return self.handle_image(request, name, path)
       
   164 
       
   165         elif 'w' in request.args and 'h' in request.args and 'x' in request.args and 'y' in request.args:
       
   166             return self.handle_region(request)
   110 
   167 
   111         elif 'x' in request.args and 'y' in request.args:
   168         elif 'x' in request.args and 'y' in request.args:
   112             png = self.render_tile(request, image)
   169             return self.handle_tile(request)
   113 
   170 
   114         else:
   171         else:
   115             raise exceptions.BadRequest("Unknown args")
   172             raise exceptions.BadRequest("Unknown args")
   116 
   173 
   117         return Response(png, content_type='image/png')