Changeset 9184
- Timestamp:
- May 12, 2011, 12:05:59 PM (14 years ago)
- Location:
- src/django_gheat
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/models.py
r9179 r9184 1 from django.db import models2 1 from gheat import managers 3 2 import datetime 3 import binascii 4 import hashlib 5 6 from django.core import validators 7 from django.db import models 8 from django.dispatch import receiver 9 from django.utils.encoding import smart_unicode 10 from django.utils.translation import ugettext_lazy as _ 11 12 class BinaryField(models.Field): 13 MAGIC = '###MAGIC###' 14 description = _('Binary object using base64 (up to %(max_length)s)') 15 16 __metaclass__ = models.SubfieldBase 17 18 def __init__(self, *args, **kwargs): 19 # base64 roughly max the binary size with with 4 times 20 kwargs['max_length'] = kwargs['max_length'] * 4 + len(self.MAGIC) 21 super(BinaryField, self).__init__(*args, **kwargs) 22 self.validators.append(validators.MaxLengthValidator(self.max_length)) 23 24 def to_python(self,value): 25 if value.startswith(self.MAGIC): 26 return binascii.a2b_base64(value[len(self.MAGIC):]) 27 else: 28 return value 29 30 def get_db_prep_value(self, value, connection, prepared=False): 31 target = self.MAGIC + binascii.b2a_base64(value) 32 if len(target) > self.max_length: 33 raise ValueError(len(target)) 34 return target 35 36 def get_prep_lookup(self, lookup_type, value): 37 raise TypeError('Lookup type %r not supported.' % lookup_type) 38 39 def get_internal_type(self): 40 return 'TextField' 41 42 class TileCache(models.Model): 43 key = models.CharField(max_length=34,unique=True) 44 data = BinaryField(max_length=10000) 45 creation_time = models.DateTimeField(auto_now_add=True) 46 47 def __unicode__(self): 48 return self.key 49 50 @staticmethod 51 def make_key(source): 52 return hashlib.md5(source).hexdigest() 53 4 54 5 55 class Accespoint(models.Model): -
src/django_gheat/website/tile.py
r9174 r9184 12 12 import sys 13 13 import tempfile 14 import time 14 15 15 16 # Rending with PIL and computation with numpy has proven to be to slow to be … … 22 23 pass 23 24 24 logging.basicConfig(level=logging. WARNING)25 logging.basicConfig(level=logging.DEBUG) 25 26 log = logging.getLogger('tile') 26 27 … … 44 45 f.seek(0) 45 46 fh.write(f.read()) 47 48 def get_image(self,format='png'): 49 f = tempfile.NamedTemporaryFile(suffix=format) 50 pygame.image.save(self.surf,f.name) 51 f.seek(0) 52 return f.read() 46 53 47 54 … … 222 229 xcoord = int(dif(nw_deg.lon,meting.longitude) / (resolution_deg.lon)) 223 230 ycoord = int(dif(nw_deg.lat,meting.latitude) / (resolution_deg.lat)) 224 log.info(meting.accespoint.ssid, meting.latitude, meting.longitude, xcoord, ycoord)225 231 226 232 # TODO: Please note that this 'logic' technically does apply to WiFi signals, … … 238 244 signal_normalized = MAX_RANGE - (MAX_SIGNAL - meting.signaal) 239 245 im.add_circle((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal) 246 #im.add_point((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal) 240 247 241 248 log.info("BoundingBox NW: %s" % nw_deg) … … 253 260 # Create your views here. 254 261 def serve_tile(request,zoom,x,y): 255 filter = {} 256 colour = (255,0,0) 257 for key, value in request.GET.iteritems(): 258 if key == 'colour': 259 colour = tuple(map(int,value.split(','))) 260 else: 261 filter[key] = value 262 im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour) 262 hash_key = TileCache.make_key("%s %s %s %s" % (zoom, x ,y, request.GET.urlencode())) 263 try: 264 d = TileCache.objects.get(key=hash_key) 265 data = d.data 266 except TileCache.DoesNotExist: 267 #log.setLevel(logging.DEBUG) 268 filter = {} 269 colour = (255,0,0) 270 for key, value in request.GET.iteritems(): 271 if key == 'colour': 272 colour = tuple(map(int,value.split(','))) 273 else: 274 filter[key] = value 275 now = time.time() 276 im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour) 277 log.info("Processing time: %s" % (time.time() - now)) 278 data = im.get_image('png') 279 r = TileCache.objects.create(key=hash_key,data=data) 280 263 281 response = HttpResponse(mimetype="image/png") 264 im.write(response,'png')282 response.write(data) 265 283 return response 284
Note:
See TracChangeset
for help on using the changeset viewer.