pngtile/tile.py
changeset 140 a3bbecbd31e7
parent 138 59d61da2b64f
child 144 5b2b866ad0a3
equal deleted inserted replaced
139:8eff4a9fdd5e 140:a3bbecbd31e7
     7 
     7 
     8 import pypngtile
     8 import pypngtile
     9 
     9 
    10 ## Coordinates
    10 ## Coordinates
    11 # width of a tile
    11 # width of a tile
    12 TILE_WIDTH = 256
    12 TILE_SIZE = 256
    13 TILE_HEIGHT = 256
    13 
       
    14 # maximum zoom out
       
    15 MAX_ZOOM = 4
    14 
    16 
    15 # max. output resolution to allow
    17 # max. output resolution to allow
    16 MAX_PIXELS = 1920 * 1200
    18 MAX_PIXELS = 1920 * 1200
    17 
    19 
    18 def scale (val, zoom):
    20 def scale (val, zoom):
    52         height = int(request.args['h'])
    54         height = int(request.args['h'])
    53         x = int(request.args['x'])
    55         x = int(request.args['x'])
    54         y = int(request.args['y'])
    56         y = int(request.args['y'])
    55         zoom = int(request.args.get('zoom', "0"))
    57         zoom = int(request.args.get('zoom', "0"))
    56 
    58 
    57         # safely limit
    59         # safety limit
    58         if width * height > MAX_PIXELS:
    60         if width * height > MAX_PIXELS:
    59             raise exceptions.BadRequest("Image size: %d * %d > %d" % (width, height, MAX_PIXELS))
    61             raise exceptions.BadRequest("Image size: %d * %d > %d" % (width, height, MAX_PIXELS))
       
    62 
       
    63         if zoom > MAX_ZOOM:
       
    64             raise exceptions.BadRequest("Image zoom: %d > %d" % (zoom, MAX_ZOOM))
    60 
    65 
    61         x = scale(x, zoom)
    66         x = scale(x, zoom)
    62         y = scale(y, zoom)
    67         y = scale(y, zoom)
    63         
    68         
    64         try:
    69         try:
    70     def render_tile (self, request, image):
    75     def render_tile (self, request, image):
    71         """
    76         """
    72             Handle request for image tile
    77             Handle request for image tile
    73         """
    78         """
    74 
    79 
    75         width = TILE_WIDTH
    80         width = TILE_SIZE
    76         height = TILE_HEIGHT
    81         height = TILE_SIZE
    77         row = int(request.args['row'])
    82         x = int(request.args['x'])
    78         col = int(request.args['col'])
    83         y = int(request.args['y'])
    79         zoom = int(request.args.get('zoom', "0"))
    84         zoom = int(request.args.get('zoom', "0"))
       
    85         
       
    86         if zoom > MAX_ZOOM:
       
    87             raise exceptions.BadRequest("Image zoom: %d > %d" % (zoom, MAX_ZOOM))
    80 
    88 
    81         x = scale(row * width, zoom)
    89         x = scale(x * width, zoom)
    82         y = scale(col * height, zoom)
    90         y = scale(y * height, zoom)
    83 
    91 
    84         try:
    92         try:
    85             return image.tile_mem(width, height, x, y, zoom)
    93             return image.tile_mem(width, height, x, y, zoom)
    86 
    94 
    87         except pypngtile.Error as error:
    95         except pypngtile.Error as error:
    98             raise exceptions.BadRequest(str(error))
   106             raise exceptions.BadRequest(str(error))
    99 
   107 
   100         if 'w' in request.args and 'h' in request.args and 'x' in request.args and 'y' in request.args:
   108         if 'w' in request.args and 'h' in request.args and 'x' in request.args and 'y' in request.args:
   101             png = self.render_region(request, image)
   109             png = self.render_region(request, image)
   102 
   110 
   103         elif 'row' in request.args and 'col' in request.args:
   111         elif 'x' in request.args and 'y' in request.args:
   104             png = self.render_tile(request, image)
   112             png = self.render_tile(request, image)
   105 
   113 
   106         else:
   114         else:
   107             raise exceptions.BadRequest("Unknown args")
   115             raise exceptions.BadRequest("Unknown args")
   108 
   116