Permalink
Browse files

Merge branch 'master' of github.com:geocam/geocamTiePoint

  • Loading branch information...
2 parents b3238f7 + b36c6b1 commit 4da0fe609e45d3a944c1ff69266359e027989f6f @jrasky jrasky committed Sep 23, 2012
Showing with 27 additions and 12 deletions.
  1. +18 −7 geocamTiePoint/models.py
  2. +9 −5 geocamTiePoint/views.py
@@ -22,6 +22,7 @@
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
+from django.shortcuts import get_object_or_404
from geocamUtil import anyjson as json
from geocamUtil.models.ExtrasDotField import ExtrasDotField
@@ -126,23 +127,33 @@ def convertImageToRgbaIfNeeded(self, image):
self.imageData.save()
def getImage(self):
- im = PIL.Image.open(self.imageData.image.file)
+ # apparently image.file is not a very good file work-alike,
+ # so let's delegate to StringIO(), which PIL is tested against
+ bits = self.imageData.image.file.read()
+ logging.info('getImage len=%s header=%s'
+ % (len(bits), repr(bits[:10])))
+ fakeFile = StringIO(bits)
+
+ im = PIL.Image.open(fakeFile)
self.convertImageToRgbaIfNeeded(im)
return im
- def getGeneratorCacheKey(self):
- return 'geocamTiePoint.QuadTreeGenerator.%s' % self.id
+ @classmethod
+ def getGeneratorCacheKey(cls, quadTreeId):
+ return 'geocamTiePoint.QuadTreeGenerator.%s' % quadTreeId
- def getGeneratorWithCache(self):
+ @classmethod
+ def getGeneratorWithCache(cls, quadTreeId):
global cachedGeneratorG
cachedGeneratorCopy = cachedGeneratorG
- key = self.getGeneratorCacheKey()
+ key = cls.getGeneratorCacheKey(quadTreeId)
if cachedGeneratorCopy['key'] == key:
logging.debug('getGeneratorWithCache hit %s', key)
result = cachedGeneratorCopy['value']
else:
logging.debug('getGeneratorWithCache miss %s', key)
- result = self.getGenerator()
+ quadTree = get_object_or_404(QuadTree, id=quadTreeId)
+ result = quadTree.getGenerator()
cachedGeneratorG = dict(key=key, value=result)
return result
@@ -159,7 +170,7 @@ def getGenerator(self):
image)
def generateExportZip(self, exportName, metaJson):
- gen = self.getGeneratorWithCache()
+ gen = self.getGeneratorWithCache(self.id)
writer = quadTree.ZipWriter(exportName)
gen.writeQuadTree(writer)
writer.writeData('meta.json', dumps(metaJson))
@@ -129,17 +129,22 @@ def overlayNew(request):
imageData.image.save('dummy.png', ContentFile(pngData), save=False)
imageData.contentType = 'image/png'
else:
- image = PIL.Image.open(imageRef.file)
+ bits = imageRef.file.read()
+ image = PIL.Image.open(StringIO(bits))
if image.mode != 'RGBA':
# add alpha channel to image for better
# transparency handling later
image = image.convert('RGBA')
out = StringIO()
image.save(out, format='png')
- imageData.image.save('dummy.png', ContentFile(out.getvalue()), save=False)
+ convertedBits = out.getvalue()
+ logging.info('converted image to RGBA, output length %s bytes'
+ % len(bits))
+ imageData.image.save('dummy.png', ContentFile(convertedBits),
+ save=False)
imageData.contentType = 'image/png'
else:
- imageData.image = imageRef
+ imageData.image.save('dummy.png', ContentFile(bits), save=False)
imageData.contentType = imageRef.content_type
imageData.save()
@@ -259,8 +264,7 @@ def overlayIdImageFileName(request, key, fileName):
def getTileData(quadTreeId, zoom, x, y):
- qt = get_object_or_404(QuadTree, id=quadTreeId)
- gen = qt.getGeneratorWithCache()
+ gen = QuadTree.getGeneratorWithCache(quadTreeId)
try:
return gen.getTileData(zoom, x, y)
except quadTree.ZoomTooBig:

0 comments on commit 4da0fe6

Please sign in to comment.