[9037] | 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:
|
---|
[9038] | 7 | #>>> python manage.py gen_tiles -s 60,6 -e 66,8 -c firetrans -b serve_tile
|
---|
[9037] | 8 | #
|
---|
| 9 | #Note: This command will NOT create tiles if they are empty. In order to do so, change
|
---|
| 10 | #the gheat generate_tile view so that it creates empty tiles for each request given
|
---|
| 11 | #x and y and zoom. This way you wan't have "holes" in the tile overlay if using
|
---|
| 12 | #the static tiles instead of the usual django url/view.
|
---|
| 13 | #
|
---|
| 14 | #Author: Dennis Wagenaar
|
---|
| 15 | #
|
---|
| 16 | #Original Author: Anders Eriksen
|
---|
| 17 | #https://github.com/robertrv/django-gheat/issues/1
|
---|
| 18 | #
|
---|
| 19 | ###############################################################################
|
---|
| 20 |
|
---|
| 21 | from django.core.management.base import BaseCommand
|
---|
| 22 | from optparse import OptionParser, make_option
|
---|
| 23 | from django.core.urlresolvers import reverse
|
---|
| 24 | from django.test.client import Client
|
---|
| 25 | import math
|
---|
| 26 |
|
---|
| 27 | class LatLonToTile:
|
---|
| 28 |
|
---|
[9102] | 29 | def deg2num(self, lat_deg, lon_deg, zoom):
|
---|
| 30 | lat_rad = math.radians(lat_deg)
|
---|
| 31 | n = 2.0 ** zoom
|
---|
| 32 | xtile = int((lon_deg + 180.0) / 360.0 * n)
|
---|
| 33 | ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
|
---|
| 34 | return(xtile, ytile)
|
---|
[9037] | 35 |
|
---|
| 36 |
|
---|
| 37 | class Command(BaseCommand):
|
---|
| 38 |
|
---|
[9102] | 39 | option_list = BaseCommand.option_list + (
|
---|
| 40 | make_option("-b", "--tileview", dest="viewname", default="serve_tile"),
|
---|
| 41 | make_option("-s", "--start", dest="start", default="52.132708,4.437389"),
|
---|
| 42 | make_option("-e", "--end", dest="end", default="52.178837,4.551029"),
|
---|
| 43 | make_option("-c", "--colorscheme", dest="color", default="classic"),
|
---|
| 44 | )
|
---|
[9037] | 45 |
|
---|
[9102] | 46 | def handle(self, *args, **options):
|
---|
| 47 | start = options['start'].split(',')
|
---|
| 48 | end = options['end'].split(',')
|
---|
| 49 | viewname = options['viewname']
|
---|
| 50 | colorscheme = str(options['color'])
|
---|
[9037] | 51 |
|
---|
[9102] | 52 | d2n = LatLonToTile()
|
---|
[9037] | 53 |
|
---|
[9102] | 54 | #initialize browser client
|
---|
| 55 | c = Client()
|
---|
| 56 | from gheat.models import MeetRondje
|
---|
| 57 | # collection = MeetRondje.objects.values('naam')
|
---|
| 58 | # for col in collection:
|
---|
[9105] | 59 | for tz in range(1, 22):
|
---|
[9102] | 60 | #calculate start tiles
|
---|
| 61 | mxmin, mymax = d2n.deg2num(float(start[0]), float(start[1]), tz)
|
---|
[9037] | 62 |
|
---|
[9102] | 63 | #calculate end tiles
|
---|
| 64 | mxmax, mymin = d2n.deg2num(float(end[0]), float(end[1]), tz)
|
---|
[9037] | 65 |
|
---|
[9102] | 66 | #loop through each tile and generate a request to the serve_tile view using Client
|
---|
| 67 | for ty in range(mymin, mymax+1):
|
---|
| 68 | for tx in range(mxmin, mxmax+1):
|
---|
| 69 | tileurl = reverse(viewname, kwargs={'color_scheme': colorscheme, 'zoom':tz, 'x':tx, 'y':ty})
|
---|
| 70 | generate_response = c.get(tileurl)
|
---|
| 71 | #lousy error handling. Should check why status 200 not returned... I.e. wrong colorscheme
|
---|
| 72 | if generate_response.status_code == 200:
|
---|
| 73 | print "Generated tile " + tileurl
|
---|
| 74 | else:
|
---|
| 75 | print "Failed generating tile " + tileurl
|
---|
[9037] | 76 |
|
---|