3
(h                 @   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G dd deZd	S )
    )unicode_literals)atanexplogpisin)GEOSGeometry
LinearRingPointPolygon)GoogleMapException)rangeg     f@c               @   sR   e Zd ZdZ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   st   || _ || _g | _g | _g | _|}xLt|D ]@}| jj|d  | jj|dt   | jj|d  |d9 }q,W dS )z#Initializes the Google Zoom object.g     v@   N)	_tilesize_nzoom_degpp_radpp_npixr   appendr   )selfZnum_zoomZtilesizezi r   G/tmp/pip-install-q3hcpn_q/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   s&   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    
.zGoogleZoom.lonlat_to_pixelc             C   sn   t |dkrtd| 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    
2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$   i  )srid)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|j}| j|j\}}|j}xRt| j	D ]D}| j| j
||j\}}||ksr||krF|dkrtd|d S qFW | 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   sN   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)r   r   )__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   