î
ªÍ X  ã               @   s¨   d  d l  m Z d  d l m Z m Z m Z m Z m Z d  d l m	 Z	 m
 Z
 m Z m Z d  d l m Z d  d l m Z e d Z d e Z Gd d „  d e ƒ Z d	 S)
é    )Úunicode_literals)ÚatanÚexpÚlogÚpiÚsin)ÚGEOSGeometryÚ
LinearRingÚPointÚPolygon)ÚGoogleMapException)Úrangeg     €f@c               @   s|   e  Z d  Z d Z d d d d „ Z d d „  Z d d	 „  Z d
 d „  Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d S)Ú
GoogleZooma  
    GoogleZoom is a utility for performing operations related to the zoom
    levels on Google Maps.

    This class is inspired by the OpenStreetMap Mapnik tile generation routine
    `generate_tiles.py`, and the article "How Big Is the World" (Hack #16) in
    "Google Maps Hacks" by Rich Gibson and Schuyler Erle.

    `generate_tiles.py` may be found at:
      http://trac.openstreetmap.org/browser/applications/rendering/mapnik/generate_tiles.py

    "Google Maps Hacks" may be found at http://safari.oreilly.com/0596101619
    é   é   c             C   s˜   | |  _  | |  _ g  |  _ g  |  _ g  |  _ | } x^ t | ƒ D]P } |  j j | d ƒ |  j j | d t ƒ |  j j | d ƒ | d 9} q@ Wd S)z#Initializes the Google Zoom object.g     €v@é   N)Ú	_tilesizeÚ_nzoomÚ_degppÚ_radppÚ_npixr   Úappendr   )ÚselfZnum_zoomZtilesizeÚzÚi© r   úQ/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/maps/google/zoom.pyÚ__init__   s    					zGoogleZoom.__init__c             C   s   |  j  S)z"Returns the number of zoom levels.)r   )r   r   r   r   Ú__len__7   s    zGoogleZoom.__len__c             C   s7   t  | t ƒ r! | j \ } } n | \ } } | | f S)z:Unpacks longitude, latitude from GEOS Points and 2-tuples.)Ú
isinstancer
   Zcoords)r   ÚlonlatÚlonÚlatr   r   r   Úget_lon_lat;   s    zGoogleZoom.get_lon_latc       	      C   sž   |  j  | ƒ \ } } |  j | } t | | |  j | ƒ } t t t t | ƒ d ƒ d ƒ } t | d t d | d | ƒ d |  j	 | ƒ } | | f S)zHConverts a longitude, latitude coordinate pair for the given zoom level.g§èH.ÿï?g      à?é   g      ð?g§èH.ÿï¿g      ð¿)
r#   r   Úroundr   ÚminÚmaxr   ÚDTORr   r   )	r   r    Úzoomr!   r"   ÚnpixZpx_xZfacZpx_yr   r   r   Úlonlat_to_pixelC   s    "5zGoogleZoom.lonlat_to_pixelc             C   sŠ   t  | ƒ d k r! t d ƒ ‚ n  |  j | } | d | |  j | } t d t t | d | d |  j | ƒ ƒ d t } | | f S)zGConverts a pixel to a longitude, latitude pair at the given zoom level.r   z+Pixel should be a sequence of two elements.r   r$   g      ð?g      à?g      ð¿)	ÚlenÚ	TypeErrorr   r   ÚRTODr   r   r   r   )r   Úpxr)   r*   r!   r"   r   r   r   Úpixel_to_lonlatZ   s    9zGoogleZoom.pixel_to_lonlatc             C   s­   |  j  d } |  j | | ƒ } |  j | d | | d | f | ƒ } |  j | d | | d | f | ƒ } t t | | d | d f | | d | d f | ƒ d d ƒS)zó
        Returns a Polygon  corresponding to the region represented by a fictional
        Google Tile for the given longitude/latitude pair and zoom level. This
        tile is used to determine the size of a tile at the given point.
        r   r   r$   Úsridiæ  )r   r+   r0   r   r	   )r   r    r)   Údeltar/   ÚllÚurr   r   r   Útilek   s
    ((zGoogleZoom.tilec       	      C   sÜ   t  | t ƒ s | j d k r. t d ƒ ‚ n  | j } |  j | j ƒ \ } } | j } xv t |  j	 ƒ D]e } |  j |  j
 | | ƒ j ƒ \ } } | | k sª | | k rh | d k rÅ t d ƒ ‚ n  | d Sqh W|  j	 d S)z6Returns the optimal Zoom level for the given geometry.iæ  z8get_zoom() expects a GEOS Geometry with an SRID of 4326.r   z>Geometry width and height should not exceed that of the Earth.r$   )r   r   r1   r-   ZenvelopeÚget_width_heightÚextentZcentroidr   r   r5   r   )	r   ZgeomÚenvZenv_wZenv_hÚcenterr   Ztile_wZtile_hr   r   r   Úget_zoom€   s    		$zGoogleZoom.get_zoomc             C   sk   t  | d d … ƒ } t  | d | d ƒ } t  | d d … ƒ } | j | ƒ } | j | ƒ } | | f S)zD
        Returns the width and height for the given extent.
        Nr   r   é   )r
   Zdistance)r   r7   r3   Úulr4   ÚheightÚwidthr   r   r   r6   š   s    zGoogleZoom.get_width_heightN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r+   r0   r5   r:   r6   r   r   r   r   r      s   r   N)Ú
__future__r   Úmathr   r   r   r   r   Zdjango.contrib.gis.geosr   r	   r
   r   Z#django.contrib.gis.maps.google.gmapr   Zdjango.utils.six.movesr   r(   r.   Úobjectr   r   r   r   r   Ú<module>   s   ("

