Skip to content

Instantly share code, notes, and snippets.

@rburhum
Created January 17, 2013 20:15
Show Gist options
  • Save rburhum/4559323 to your computer and use it in GitHub Desktop.
Save rburhum/4559323 to your computer and use it in GitHub Desktop.
Add tilestache to Django
...
url(r'^v1/tiles/(?P<tile_user>([^/]+))/(?P<tile_layer>([^/]+))/(?P<tile_zoom>(\d+))/(?P<tile_column>(\d+))/(?P<tile_row>(\d+))\.(?P<tile_format>([a-z]+))$', TileManager.as_view(), name='tile_manager'),
...
from django.http import HttpResponse, HttpResponseServerError
from ModestMaps.Core import Coordinate
from TileStache import getTile, parseConfigfile
from TileStache.Core import KnownUnknown
from django.contrib.auth.models import User
from django.views.generic import View
from django.contrib.sites.models import Site
#include my custom permission mixin see Django docs on several ways to do this
from utils import APIPermissionMixin
class TileManager(APIPermissionMixin, View):
"""
Returns tiles
"""
def get(self, request, *args, **kwargs):
try:
#the mixin already checked that the layers and users exist
user = User.objects.get(username__exact=kwargs['tile_user'])
config = parseConfigfile(user.get_profile().tile_config_file)
layer = config.layers[kwargs['tile_layer']]
coord = Coordinate(int(kwargs['tile_row']), int(kwargs['tile_column']), int(kwargs['tile_zoom']))
tile_mimetype, tile_content = getTile(layer, coord, kwargs['tile_format'], ignore_cached=False)
return HttpResponse(mimetype=tile_mimetype, content=tile_content)
except KnownUnknown, e:
return HttpResponseServerError(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment