pngtile/handlers.py
changeset 103 1a6a6957197d
parent 93 581cdb831b32
child 107 9fcf58fb113a
equal deleted inserted replaced
102:a4d1e046ed3e 103:1a6a6957197d
     8 # path to images
     8 # path to images
     9 DATA_ROOT = os.environ.get("PNGTILE_DATA_PATH") or os.path.abspath('data/')
     9 DATA_ROOT = os.environ.get("PNGTILE_DATA_PATH") or os.path.abspath('data/')
    10 
    10 
    11 # only open each image once
    11 # only open each image once
    12 IMAGE_CACHE = {}
    12 IMAGE_CACHE = {}
    13 
       
    14 
    13 
    15 ### Parse request data
    14 ### Parse request data
    16 def get_path (req_path) :
    15 def get_path (req_path) :
    17     """
    16     """
    18         Returns the name and path requested
    17         Returns the name and path requested
    82 
    81 
    83     # viewport
    82     # viewport
    84     return Response(render.img_html(prefix, name, image), content_type="text/html")
    83     return Response(render.img_html(prefix, name, image), content_type="text/html")
    85 
    84 
    86 
    85 
    87 def handle_img_region (req, image) :
    86 def handle_img_region (req, image, cache) :
    88     """
    87     """
    89         Handle request for an image region
    88         Handle request for an image region
    90     """
    89     """
    91 
    90 
    92     # specific image
    91     # specific image
    96     cy = int(req.args['cy'])
    95     cy = int(req.args['cy'])
    97     zoom = int(req.args.get('zl', "0"))
    96     zoom = int(req.args.get('zl', "0"))
    98     
    97     
    99     try :
    98     try :
   100         # yay full render
    99         # yay full render
   101         return Response(render.img_png_region(image, cx, cy, zoom, width, height), content_type="image/png")
   100         return Response(render.img_png_region(image, cx, cy, zoom, width, height, cache), content_type="image/png")
   102 
   101 
   103     except ValueError, ex :
   102     except ValueError, ex :
   104         # too large
   103         # too large
   105         raise exceptions.Forbidden(str(ex))
   104         raise exceptions.Forbidden(str(ex))
   106 
   105 
   107 
   106 
   108 def handle_img_tile (req, image) :
   107 def handle_img_tile (req, image, cache) :
   109     """
   108     """
   110         Handle request for image tile
   109         Handle request for image tile
   111     """
   110     """
   112 
   111 
   113     # tile
   112     # tile
   114     x = int(req.args['x'])
   113     x = int(req.args['x'])
   115     y = int(req.args['y'])
   114     y = int(req.args['y'])
   116     zoom = int(req.args.get('zl', "0"))
   115     zoom = int(req.args.get('zl', "0"))
       
   116 
       
   117     # cache?
   117         
   118         
   118     # yay render
   119     # yay render
   119     return Response(render.img_png_tile(image, x, y, zoom), content_type="image/png")
   120     return Response(render.img_png_tile(image, x, y, zoom, cache), content_type="image/png")
   120 
   121 
   121 ## Dispatch req to handle_img_*
   122 ## Dispatch req to handle_img_*
   122 def handle_img (req, name, path) :
   123 def handle_img (req, name, path, cache) :
   123     """
   124     """
   124         Handle request for an image
   125         Handle request for an image
   125     """
   126     """
   126 
   127 
   127     # get image object
   128     # get image object
   130     # what view?
   131     # what view?
   131     if not req.args :
   132     if not req.args :
   132         return handle_img_viewport(req, image, name)
   133         return handle_img_viewport(req, image, name)
   133 
   134 
   134     elif 'w' in req.args and 'h' in req.args and 'cx' in req.args and 'cy' in req.args :
   135     elif 'w' in req.args and 'h' in req.args and 'cx' in req.args and 'cy' in req.args :
   135         return handle_img_region(req, image)
   136         return handle_img_region(req, image, cache)
   136 
   137 
   137     elif 'x' in req.args and 'y' in req.args :
   138     elif 'x' in req.args and 'y' in req.args :
   138         return handle_img_tile(req, image)
   139         return handle_img_tile(req, image, cache)
   139 
   140 
   140     else :
   141     else :
   141         raise exceptions.BadRequest("Unknown args")
   142         raise exceptions.BadRequest("Unknown args")
   142 
   143 
   143 
   144 
   144 
   145 
   145 ## Dispatch request to handle_*
   146 ## Dispatch request to handle_*
   146 def handle_req (req) :
   147 def handle_req (req, cache) :
   147     """
   148     """
   148         Main request handler
   149         Main request handler
   149     """
   150     """
   150     
   151     
   151     # decode req
   152     # decode req
   164         # invalid file
   165         # invalid file
   165         raise exceptions.BadRequest("Not a PNG file")
   166         raise exceptions.BadRequest("Not a PNG file")
   166     
   167     
   167     else :
   168     else :
   168         # image
   169         # image
   169         return handle_img(req, name, path)
   170         return handle_img(req, name, path, cache)
   170 
   171 
   171 
   172