~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to django/contrib/gis/maps/google/zoom.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.contrib.gis.geos import GEOSGeometry, LinearRing, Polygon, Point
2
2
from django.contrib.gis.maps.google.gmap import GoogleMapException
 
3
from django.utils.six.moves import xrange
3
4
from math import pi, sin, log, exp, atan
4
5
 
5
6
# Constants used for degree to radian conversion, and vice-versa.
20
21
 
21
22
    "Google Maps Hacks" may be found at http://safari.oreilly.com/0596101619
22
23
    """
23
 
    
 
24
 
24
25
    def __init__(self, num_zoom=19, tilesize=256):
25
26
        "Initializes the Google Zoom object."
26
27
        # Google's tilesize is 256x256, square tiles are assumed.
27
28
        self._tilesize = tilesize
28
 
        
 
29
 
29
30
        # The number of zoom levels
30
31
        self._nzoom = num_zoom
31
32
 
32
 
        # Initializing arrays to hold the parameters for each one of the 
 
33
        # Initializing arrays to hold the parameters for each one of the
33
34
        # zoom levels.
34
35
        self._degpp = [] # Degrees per pixel
35
36
        self._radpp = [] # Radians per pixel
36
37
        self._npix  = [] # 1/2 the number of pixels for a tile at the given zoom level
37
 
        
 
38
 
38
39
        # Incrementing through the zoom levels and populating the parameter arrays.
39
40
        z = tilesize # The number of pixels per zoom level.
40
41
        for i in xrange(num_zoom):
70
71
        # with with the number of degrees/pixel at the given zoom level.
71
72
        px_x = round(npix + (lon * self._degpp[zoom]))
72
73
 
73
 
        # Creating the factor, and ensuring that 1 or -1 is not passed in as the 
 
74
        # Creating the factor, and ensuring that 1 or -1 is not passed in as the
74
75
        # base to the logarithm.  Here's why:
75
 
        #  if fac = -1, we'll get log(0) which is undefined; 
 
76
        #  if fac = -1, we'll get log(0) which is undefined;
76
77
        #  if fac =  1, our logarithm base will be divided by 0, also undefined.
77
78
        fac = min(max(sin(DTOR * lat), -0.9999), 0.9999)
78
79
 
98
99
 
99
100
        # Returning the longitude, latitude coordinate pair.
100
101
        return (lon, lat)
101
 
    
 
102
 
102
103
    def tile(self, lonlat, zoom):
103
104
        """
104
105
        Returns a Polygon  corresponding to the region represented by a fictional
119
120
 
120
121
        # Constructing the Polygon, representing the tile and returning.
121
122
        return Polygon(LinearRing(ll, (ll[0], ur[1]), ur, (ur[0], ll[1]), ll), srid=4326)
122
 
        
 
123
 
123
124
    def get_zoom(self, geom):
124
125
        "Returns the optimal Zoom level for the given geometry."
125
126
        # Checking the input type.
139
140
            # When we span more than one tile, this is an approximately good
140
141
            # zoom level.
141
142
            if (env_w > tile_w) or (env_h > tile_h):
142
 
                if z == 0: 
 
143
                if z == 0:
143
144
                    raise GoogleMapException('Geometry width and height should not exceed that of the Earth.')
144
145
                return z-1
145
 
        
 
146
 
146
147
        # Otherwise, we've zoomed in to the max.
147
148
        return self._nzoom-1
148
149