source: src/django_gheat/gheat/management/commands/generate_tiles.py@ 9029

Last change on this file since 9029 was 9029, checked in by dennisw, 14 years ago

generate_tiles.py - Generate dus de tiles (voor command met opties zie bestand), maar slaat ze nog niet (goed) op

File size: 3.1 KB
Line 
1###############################################################################
2#Management command to generate tiles using gheat.
3#
4#Must be given a bounding box by using options -s and -e (start and end)
5#
6#Example usage:
7#>>> python manage.py generate_tiles -s 60,6 -e 66,8 -z 10 -c firetrans -v serve_tile
8#
9#Requires the globalmaptiles.py file. Get it from
10#http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/globalmaptiles.py
11#
12#Note: This command will NOT create tiles if they are empty. In order to do so, change
13#the gheat generate_tile view so that it creates empty tiles for each request given
14#x and y and zoom. This way you wan't have "holes" in the tile overlay if using
15#the static tiles instead of the usual django url/view.
16#
17#Author: Anders Eriksen
18###############################################################################
19
20
21
22from django.core.management.base import BaseCommand
23from optparse import OptionParser, make_option
24from django.core.urlresolvers import reverse
25from django.test.client import Client
26from gheat.globalmaptiles import *
27
28
29class Command(BaseCommand):
30
31 option_list = BaseCommand.option_list + (
32 make_option("-b", "--tileview", dest="viewname", default="serve_tile"),
33 make_option("-z", "--zoom", dest="zoomlevel", default=1),
34 make_option("-s", "--start", dest="start", default="60,6"),
35 make_option("-e", "--end", dest="end", default="62,6"),
36 make_option("-c", "--colorscheme", dest="color", default="fire"),
37 )
38
39 def handle(self, *args, **options):
40
41 profile = 'mercator'
42 zoomlevel = None
43 lat, lon, latmax, lonmax, baseurl = None, None, None, None, None
44 boundingbox = False
45
46 start = options['start'].split(',')
47 end = options['end'].split(',')
48 viewname = options['viewname']
49 colorscheme = str(options['color'])
50
51 #create bounding box from start latlon and end latlon
52 boundingbox = (float(start[0]), float(start[1]), float(end[0]), float(end[1]))
53
54 tz = int(options['zoomlevel'])
55 mercator = GlobalMercator()
56
57 #calculate start tiles
58 mx, my = mercator.LatLonToMeters(float(start[0]), float(start[1]))
59 tminx, tminy = mercator.MetersToTile(mx, my, tz)
60
61 #calculate end tiles
62 mx, my = mercator.LatLonToMeters(float(end[0]), float(end[1]))
63 tmaxx, tmaxy = mercator.MetersToTile(mx, my, tz)
64
65 #initialize browser client
66 c = Client()
67
68 #loop through each tile and generate a request to the serve_tile view using Client
69 for ty in range(tminy, tmaxy+1):
70 for tx in range(tminx, tmaxx+1):
71
72 tileurl = reverse(viewname, kwargs={'color_scheme': colorscheme, 'zoom':tz, 'x':tx, 'y':ty})
73
74 generate_response = c.get(tileurl)
75
76 #lousy error handling. Should check why status 200 not returned... I.e. wrong colorscheme
77 if generate_response.status_code == 200:
78 print "Generated tile " + tileurl
79 else:
80 print "Failed generating tile " + tileurl
81
Note: See TracBrowser for help on using the repository browser.