source: src/django_gheat/gheat/management/commands/gen_tiles.py@ 9037

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

gen_tiles.py - aangepaste versie van generate_tiles.py, werkt nu wel, geen globalmaptiles.py meer nodig

File size: 2.8 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 -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: Dennis Wagenaar
18#
19#Original Author: Anders Eriksen
20#https://github.com/robertrv/django-gheat/issues/1
21#
22###############################################################################
23
24from django.core.management.base import BaseCommand
25from optparse import OptionParser, make_option
26from django.core.urlresolvers import reverse
27from django.test.client import Client
28import math
29
30class LatLonToTile:
31
32 def deg2num(self, lat_deg, lon_deg, zoom):
33 lat_rad = math.radians(lat_deg)
34 n = 2.0 ** zoom
35 xtile = int((lon_deg + 180.0) / 360.0 * n)
36 ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
37 return(xtile, ytile)
38
39
40class Command(BaseCommand):
41
42 option_list = BaseCommand.option_list + (
43 make_option("-b", "--tileview", dest="viewname", default="serve_tile"),
44 make_option("-s", "--start", dest="start", default="52.132708,4.437389"),
45 make_option("-e", "--end", dest="end", default="52.178837,4.551029"),
46 make_option("-c", "--colorscheme", dest="color", default="classic"),
47 )
48
49 def handle(self, *args, **options):
50
51 start = options['start'].split(',')
52 end = options['end'].split(',')
53 viewname = options['viewname']
54 colorscheme = str(options['color'])
55
56 d2n = LatLonToTile()
57
58 #initialize browser client
59 c = Client()
60
61 for tz in range(1, 17):
62 #calculate start tiles
63 mxmin, mymax = d2n.deg2num(float(start[0]), float(start[1]), tz)
64
65 #calculate end tiles
66 mxmax, mymin = d2n.deg2num(float(end[0]), float(end[1]), tz)
67
68 #loop through each tile and generate a request to the serve_tile view using Client
69 for ty in range(mymin, mymax+1):
70 for tx in range(mxmin, mxmax+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.