I'm working on a Django tile solution to serve thousands of images. So far I have been able to set a Mapscript server with tilecache. Everything seems ok but when loading into Leaflet I got some kind of shift.

import os
from django.conf import settings
from django.http import Http404
from django.contrib.gis.gdal import SpatialReference

from TileCache.Service import Service
from TileCache.Layer import Tile
import TileCache.Layers.MapServer as MS
import TileCache.Layers.WMS as WMS
from TileCache.Caches.Disk import Disk

basedir = r"/tmp/somecache"

def get_eb_layer(name):
    mapfile = r"/dir/mapfile.map"
    svc = Service(
        {"raster": MS.MapServer("basic", mapfile, layers="raster", debug=False),}
    return svc.layers[name]

def render_tile(name, z, x, y, extension='png', source_srs=None, dest_srs=None, bbox=None, scales=None, units=None):

    directory = basedir + '/' + str(name) + '/' + str(z) + '/' + str(x)

    if check_directory(directory):
        tile_path = directory + '/' + str(y) + ".png"
        if not check_for_tile(tile_path):
            layer = get_eb_layer(name)
            layer.source_srs = 'EPSG:4326'
            layer.dest_srs = 'EPSG:4326'
            tile = Tile(layer, x, y, z)
            img = layer.renderTile(tile)
            save_tile(tile_path, img)
            return img
            img = open(tile_path)
            return img
        print "Unable to create directory"

def check_directory(directory):
    if not os.path.exists(directory):
    return True

def check_for_tile(tile_path):
    if not os.path.isfile(tile_path):
        return False
        return True

def save_tile(tile_path, img):
    with open(tile_path, "wb") as f:

Here is the image:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.