pngtile/tile.py
changeset 167 16b600e927fe
parent 165 1dc09e81a4e2
child 171 0ff809f855f7
equal deleted inserted replaced
166:986052d7d0ce 167:16b600e927fe
     1 """
     1 """
     2     Raw tile handling.
     2     Raw tile handling.
     3 """
     3 """
     4 
     4 
     5 from pngtile.application import BaseApplication, url
       
     6 from werkzeug import Request, Response, exceptions
     5 from werkzeug import Request, Response, exceptions
     7 from werkzeug.utils import redirect
     6 from werkzeug.utils import redirect
       
     7 import werkzeug.urls
     8 
     8 
       
     9 import pngtile.application
     9 import pypngtile
    10 import pypngtile
    10 
    11 
    11 ## Coordinates
    12 ## Coordinates
    12 # width of a tile
    13 # width of a tile
    13 TILE_SIZE = 256
    14 TILE_SIZE = 256
    40         Scale value about center by zoom.
    41         Scale value about center by zoom.
    41     """
    42     """
    42 
    43 
    43     return scale(val, zoom) - dim / 2
    44     return scale(val, zoom) - dim / 2
    44 
    45 
    45 class TileApplication (BaseApplication):
    46 class TileApplication (pngtile.application.PNGTileApplication):
    46     def __init__ (self, image_server, **opts):
    47     def __init__ (self, image_server, **opts):
    47         """
    48         """
    48             image_server:       http://.../ url to image-server frontend
    49             image_server:       http://.../ url to image-server frontend
    49         """
    50         """
    50 
    51 
    51         BaseApplication.__init__(self, **opts)
    52         super(TileApplication, self).__init__(**opts)
    52 
    53 
    53         self.image_server = image_server
    54         self.image_server = image_server
    54         
    55         
       
    56     def image_url (self, name):
       
    57         return werkzeug.urls.Href(self.image_server)(path)
       
    58 
    55     def render_region (self, request, image):
    59     def render_region (self, request, image):
    56         """
    60         """
    57             Handle request for an image region
    61             Handle request for an image region
    58         """
    62         """
    59 
    63 
    75         
    79         
    76         try:
    80         try:
    77             return image.tile_mem(width, height, x, y, zoom)
    81             return image.tile_mem(width, height, x, y, zoom)
    78 
    82 
    79         except pypngtile.Error as error:
    83         except pypngtile.Error as error:
    80             raise exceptions.BadRequest(str(error))
    84             raise exceptions.InternalServerError(str(error))
    81         
    85         
    82     def render_tile (self, request, image):
    86     def render_tile (self, request, image):
    83         """
    87         """
    84             Handle request for image tile
    88             Handle request for image tile
    85         """
    89         """
    98 
   102 
    99         try:
   103         try:
   100             return image.tile_mem(width, height, x, y, zoom)
   104             return image.tile_mem(width, height, x, y, zoom)
   101 
   105 
   102         except pypngtile.Error as error:
   106         except pypngtile.Error as error:
   103             raise exceptions.BadRequest(str(error))
   107             raise exceptions.InternalServerError(str(error))
   104 
   108 
   105     def handle_dir (self, request, name, path):
   109     def handle_dir (self, request, name, path):
   106         """
   110         """
   107             Redirect to the image frontend for a non-tile request.
   111             Redirect to the image frontend for a non-tile request.
   108         """
   112         """
   109 
   113 
   110         if not name.endswith('/'):
   114         if not name.endswith('/'):
   111             # avoid an additional redirect
   115             # avoid an additional redirect
   112             name += '/'
   116             name += '/'
   113 
   117 
   114         return redirect(url(self.image_server, name))
   118         return redirect(self.image_url(name))
   115 
   119 
   116     def handle_image (self, request, name, path):
   120     def handle_image (self, request, name, path):
   117         """
   121         """
   118             Redirect to the image frontend for a non-tile request.
   122             Redirect to the image frontend for a non-tile request.
   119         """
   123         """
   120 
   124 
   121         return redirect(url(self.image_server, name))
   125         return redirect(self.image_url(name))
   122 
   126 
   123     def handle_region (self, request):
   127     def handle_region (self, request):
   124         """
   128         """
   125             Return image/png for given region.
   129             Return image/png for given region.
   126         """
   130         """
   127 
   131 
   128         try:
   132         image, name = self.open(request.path)
   129             image, name = self.get_image(request.path)
       
   130         except pypngtile.Error as error:
       
   131             raise exceptions.BadRequest(str(error))
       
   132  
   133  
   133         png = self.render_region(request, image)
   134         png = self.render_region(request, image)
   134         
   135         
   135         return Response(png, content_type='image/png')
   136         return Response(png, content_type='image/png')
   136 
   137 
   137     def handle_tile (self, request):
   138     def handle_tile (self, request):
   138         """
   139         """
   139             Return image/png for given tile.
   140             Return image/png for given tile.
   140         """
   141         """
   141 
   142 
   142         try:
   143         image, name = self.open(request.path)
   143             image, name = self.get_image(request.path)
       
   144         except pypngtile.Error as error:
       
   145             raise exceptions.BadRequest(str(error))
       
   146             
   144             
   147         png = self.render_tile(request, image)
   145         png = self.render_tile(request, image)
   148         
   146         
   149         return Response(png, content_type='image/png')
   147         return Response(png, content_type='image/png')
   150 
   148 
   151     def handle (self, request):
   149     def handle (self, request):
   152         """
   150         """
   153             Handle request for an image
   151             Handle request for an image
   154         """
   152         """
   155 
   153         
   156         name, path, type = self.lookup_path(request.path)
   154         name, path, type = self.lookup(request.path)
   157         
   155         
   158         # determine handler
   156         # determine handler
   159         if not type:
   157         if not type:
   160             return self.handle_dir(request, name, path)
   158             return self.handle_dir(request, name, path)
   161 
   159