Ignore:
Timestamp:
Aug 23, 2011, 8:38:09 PM (13 years ago)
Author:
rick
Message:
  • Caches are now fully-file based as memcached was not working well with large entries (like images).
  • OSM django proxy is way to slow, rewrite to use apache balanced proxy members.
  • Use HTTPD mod_cache to cache heavy image generation work.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/wlheatmap/tile.py

    r9398 r9549  
    99from django.views.decorators.cache import cache_page
    1010from gheat.models import *
     11import os
    1112import pygame
    1213import sys
     
    3637    self.surf = new_surf
    3738
    38   def write(self, fh,format='png'):
    39     # XXX: How to get a PNG stream directly to the output
    40     f = tempfile.NamedTemporaryFile(suffix=format)
    41     pygame.image.save(self.surf,f.name)
    42     f.seek(0)
    43     fh.write(f.read())
     39  def save_and_get_image(self,filename):
     40    """ Save the file to the location and return the file """
     41    basedir = os.path.dirname(filename)
     42    if not os.path.isdir(basedir):
     43      os.makedirs(basedir)
     44    pygame.image.save(self.surf,filename)
     45    return open(filename,'r').read()
    4446
    4547  def get_image(self,format='png'):
     
    4850    f.seek(0)
    4951    return f.read()
     52
    5053
    5154
     
    199202    'longitude__gte' : nw_deg.lon
    200203  })
    201   # TODO: This is currently hard-coded to display _all_ metingen
    202   metingen = Meting.objects.select_related().filter(**filter)
     204  # Limit such that high level zooms does not get the whole database
     205  metingen = Meting.objects.filter(**filter)[:1000].values_list('latitude', 'longitude', 'signaal')
    203206
    204207  # XXX: Signal is not normalized in the database making it unknown when a
     
    219222    return max(x,y) - min(x,y)
    220223 
    221   for meting in metingen:
    222     lat_min = min(lat_min, meting.latitude)
    223     lat_max = max(lat_max, meting.latitude)
    224     lon_min = min(lon_min, meting.longitude)
    225     lon_max = max(lon_max, meting.longitude)
    226     xcoord = int(dif(nw_deg.lon,meting.longitude) / (resolution_deg.lon))
    227     ycoord = int(dif(nw_deg.lat,meting.latitude) / (resolution_deg.lat))
     224  for (latitude, longitude, signaal) in metingen:
     225    lat_min = min(lat_min, latitude)
     226    lat_max = max(lat_max, latitude)
     227    lon_min = min(lon_min, longitude)
     228    lon_max = max(lon_max, longitude)
     229    xcoord = int(dif(nw_deg.lon,longitude) / (resolution_deg.lon))
     230    ycoord = int(dif(nw_deg.lat,latitude) / (resolution_deg.lat))
    228231
    229232    # TODO: Please note that this 'logic' technically does apply to WiFi signals,
     
    239242    # with examples like this:
    240243    # http://stackoverflow.com/questions/3104781/inverse-distance-weighted-idw-interpolation-with-python
    241     signal_normalized = MAX_RANGE - (MAX_SIGNAL - meting.signaal)
    242     im.add_circle((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal)
     244    signal_normalized = MAX_RANGE - (MAX_SIGNAL - signaal)
     245    im.add_circle((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - signaal)
    243246    #im.add_point((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal)
    244247 
     
    246249  return im
    247250
    248 
    249 # Create your views here.
    250 @cache_page(60 * 60 * 24, key_prefix="wlheatmap_tile")
    251 def serve_tile(request,zoom,x,y):
     251def pre_process_tile(request,zoom,x,y):
    252252  filter = {}
    253253  colour = (255,0,0)
     
    259259  now = time.time()
    260260  im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour)
     261  return im
     262
     263# Create your views here.
     264@cache_page(60 * 60 * 24, cache="tile_cache")
     265def serve_tile(request,zoom,x,y):
     266  im = pre_process_tile(request,zoom,x,y)
    261267  data = im.get_image('png')
    262 
    263   response = HttpResponse(mimetype="image/png")
    264   response.write(data)
    265   return response
    266 
     268  return HttpResponse(data,mimetype="image/png")
     269
     270def fixed_wl_only(request,zoom,x,y):
     271  """ Pre-render and save attempt """
     272  im = pre_process_tile(request,zoom,x,y)
     273  data = im.save_and_get_image('/usr/local/var/django/tile/fixed/wl-only/%s/%s,%s.png' % (zoom, x, y))
     274  return HttpResponse(data,mimetype="image/png")
Note: See TracChangeset for help on using the changeset viewer.