source: subversion/sites/other/tilesAtHome_tahngo/browse/MapOf.py @ 9853

Last change on this file since 9853 was 9486, checked in by spaetz, 11 years ago

fix MapOf? vertical offset. All done now.

File size: 2.5 KB
Line 
1from django.http import HttpResponse
2from math import *
3import StringIO
4from PIL import Image
5from tah.tah_intern.Tile import Tile
6from django import forms
7from django.views.decorators.cache import cache_control
8
9def latlon2relativeXY(lat,lon):
10  x = (lon + 180) / 360
11  y = (1 - log(tan(radians(lat)) + 1/cos(radians(lat))) / pi) / 2
12  return(x,y)
13
14def latlon2xy(lat,lon,z):
15  n = pow(2,z)
16  x,y = latlon2relativeXY(lat,lon)
17  return(n*x, n*y)
18
19@cache_control(max_age=864000) #cache for 10 days
20def export_MapOf(request):
21  x_range = []
22  y_range= []
23  form = MapOfForm(request.GET)
24  if form.is_valid():
25    form = form.cleaned_data
26    x,y = latlon2xy(form['lat'],form['long'],form['z'])
27    x_range.append(int(x - form['w'] / 512.0))  # minimum x tile
28    x_range.append(int(x + form['w'] / 512.0))  # maximum x tile
29    y_range.append(int(y - form['h'] / 512.0))  # minimum y tile
30    y_range.append(int(y + form['h'] / 512.0))  # maximum y tile
31    pngfile = StringIO.StringIO()
32    im = Image.new('RGBA', (256*(x_range[1]-x_range[0]+1),256*(y_range[1]-y_range[0]+1)))
33
34    for i in range(x_range[0], x_range[1]+1):
35      for j in range(y_range[0], y_range[1]+1):
36        image =  Image.open(StringIO.StringIO(Tile(None,form['z'],i,j).serve_tile('tile')))
37        im.paste(image,(256*(i-x_range[0]),256*(j-y_range[0])))
38    marg_w = int(256*(x-x_range[0]-form['w']/512.0))
39    marg_h  = int(256*(y-y_range[0]-form['h']/512.0))
40    im = im.crop((marg_w,marg_h,marg_w+form['w'],marg_h+form['h']))
41    #return HttpResponse(str(marg_w)+" "+str(marg_h)+" " +str(x) + str(x_range) + str(y)+str(y_range))
42    im.save(pngfile,form['format'])
43    pngfile.seek(0)
44    response = HttpResponse(pngfile.read(), mimetype= 'image/'+str(form['format']))
45    response['Content-Disposition'] = 'attachment; filename=map.'+form['format']
46    return response
47  else: return HttpResponse("<form ACTION=\"\" METHOD=\"GET\">%s<input type=\"submit\"></form>"%(form.as_p()))
48
49
50 
51class MapOfForm(forms.Form):
52    lat = forms.FloatField()
53    long = forms.FloatField()
54    z = forms.IntegerField(initial=12)
55    w = forms.IntegerField(initial=1024)
56    h = forms.IntegerField(initial=1024)
57    format = forms.ChoiceField(choices=[('png','png'),('jpeg','jpeg')])
58
59    def clean_h(self):
60      h = self.cleaned_data['h']
61      if h > 2000:
62          raise forms.ValidationError('Image height needs to be <= 2000 pixels.')
63      else: return h
64
65    def clean_w(self):
66      w = self.cleaned_data['w']
67      if w > 2000:
68          raise forms.ValidationError('Image width needs to be <= 2000 pixels.')
69      else: return w
Note: See TracBrowser for help on using the repository browser.