2017-10-20 16:21:25 +02:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseNotModified
|
2017-10-10 14:39:11 +02:00
|
|
|
from shapely.geometry import box
|
|
|
|
|
2017-10-20 16:21:25 +02:00
|
|
|
from c3nav.mapdata.models import Level, MapUpdate, Source
|
2017-10-19 17:20:55 +02:00
|
|
|
from c3nav.mapdata.render.svg import SVGRenderer
|
2017-10-10 14:39:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
def tile(request, level, zoom, x, y, format):
|
|
|
|
zoom = int(zoom)
|
|
|
|
if not (0 <= zoom <= 10):
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
bounds = Source.max_bounds()
|
|
|
|
|
|
|
|
x, y = int(x), int(y)
|
|
|
|
size = 256/2**zoom
|
|
|
|
minx = size * x
|
2017-10-10 19:31:51 +02:00
|
|
|
miny = size * (-y-1)
|
2017-10-10 14:39:11 +02:00
|
|
|
maxx = minx + size
|
|
|
|
maxy = miny + size
|
|
|
|
|
|
|
|
if not box(bounds[0][1], bounds[0][0], bounds[1][1], bounds[1][0]).intersects(box(minx, miny, maxx, maxy)):
|
|
|
|
raise Http404
|
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
renderer = SVGRenderer(level, miny, minx, maxy, maxx, scale=2**zoom, user=request.user)
|
|
|
|
|
2017-10-20 16:21:25 +02:00
|
|
|
update_cache_key = MapUpdate.cache_key()
|
|
|
|
access_cache_key = renderer.access_cache_key
|
|
|
|
etag = update_cache_key+'_'+access_cache_key
|
|
|
|
|
|
|
|
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
|
|
|
|
if if_none_match == etag:
|
|
|
|
return HttpResponseNotModified()
|
|
|
|
|
2017-10-19 13:47:03 +02:00
|
|
|
try:
|
2017-10-19 17:20:55 +02:00
|
|
|
renderer.check_level()
|
2017-10-19 13:47:03 +02:00
|
|
|
except Level.DoesNotExist:
|
|
|
|
raise Http404
|
2017-10-10 14:39:11 +02:00
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
svg = renderer.render()
|
|
|
|
|
2017-10-10 14:39:11 +02:00
|
|
|
if format == 'svg':
|
2017-10-17 12:24:38 +02:00
|
|
|
response = HttpResponse(svg.get_xml(), 'image/svg+xml')
|
2017-10-10 14:39:11 +02:00
|
|
|
elif format == 'png':
|
2017-10-17 14:34:09 +02:00
|
|
|
response = HttpResponse(content_type='image/png')
|
|
|
|
svg.get_png(f=response)
|
2017-10-10 14:39:11 +02:00
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
|
2017-10-20 16:21:25 +02:00
|
|
|
response['ETag'] = etag
|
|
|
|
response['Cache-Control'] = 'no-cache'
|
|
|
|
|
2017-10-10 14:39:11 +02:00
|
|
|
return response
|