ó
ÎrOc           @   s(  d  d l  m Z d  d l m Z y d  d l Z WnE e k
 rw y d  d l Z Wqx e k
 rs d  d l m Z qx Xn Xd  d l Z d  d l	 m
 Z
 d  d l m Z m Z m Z d  d l m Z m Z m Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t	   urlencode(   t   urlopenN(   t
   simplejson(   t
   ExpatError(   t   Geocodert   GeocoderErrort   GeocoderResultError(   t   Pointt   Locationt   utilt   Googlec           B   sn   e  Z d  Z d
 d d
 d d
 d „ Z e d „  ƒ Z e d „ Z e d „ Z	 e d „ Z
 e d „ Z d	 „  Z RS(   s#   Geocoder using the Google Maps API.s   maps.google.coms   %sc         C   sÉ   | d
 k r0 d d l m } | d d t ƒ n  | d
 k r\ d d l m } | d t ƒ n  | |  _ | |  _ | |  _ | r¼ | d k r˜ t d	 ƒ ‚ qÅ | d k r° d |  _ qÅ | |  _ n	 d |  _ d
 S(   sv  Initialize a customized Google geocoder with location-specific
        address information and your Google Maps API key.

        ``api_key`` should be a valid Google Maps API key. Required as per Google Geocoding API
        V2 docs, but the API works without a key in practice.

        ``domain`` should be the localized Google Maps domain to connect to. The default
        is 'maps.google.com', but if you're geocoding address in the UK (for
        example), you may want to set it to 'maps.google.co.uk' to properly bias results.

        ``resource`` is DEPRECATED and ignored -- the parameter remains for compatibility
        purposes.  The supported 'maps/geo' API is used regardless of this parameter.

        ``format_string`` is a string containing '%s' where the string to
        geocode should be interpolated before querying the geocoder.
        For example: '%s, Mountain View, CA'. The default is just '%s'.
        
        ``output_format`` (DEPRECATED) can be 'json', 'xml', or 'kml' and will
        control the output format of Google's response. The default is 'json'. 'kml' is
        an alias for 'xml'.
        iÿÿÿÿ(   t   warnsN   geopy.geocoders.google.GoogleGeocoder: The `resource` parameter is deprecated sB   and now ignored. The Google-supported "maps/geo" API will be used.sS   geopy.geocoders.google.GoogleGeocoder: The `output_format` parameter is deprecated.t   jsont   xmlt   kmls>   if defined, `output_format` must be one of: "json","xml","kml"N(   s   jsons   xmlR   (	   t   Nonet   warningsR   t   DeprecationWarningt   api_keyt   domaint   format_stringt
   ValueErrort   output_format(   t   selfR   R   t   resourceR   R   R   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt   __init__   s"    			c         C   s   |  j  j d ƒ } d | S(   Nt   /s   http://%s/maps/geo?%%s(   R   t   strip(   R   R   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt   urlC   s    c         C   s`   i |  j  | d 6|  j j ƒ  d 6} |  j r= |  j | d <n  |  j t | ƒ } |  j | | ƒ S(   Nt   qt   outputt   key(   R   R   t   lowerR   R   R    t   geocode_url(   R   t   stringt   exactly_onet   paramsR   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt   geocodeH   s    	c         C   sC   t  j j d | ƒ t | ƒ } t |  d |  j ƒ } | | | ƒ S(   Ns   Fetching %s...t   parse_(   R	   t   loggert   debugR   t   getattrR   (   R   R   R#   t   paget   dispatch(    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyR!   S   s    c   	         s7  t  | t ƒ s! t j | ƒ } n  y t j j j | ƒ } Wn t k
 rY g  } d } n X| j
 d ƒ } t | ƒ d k rÂ | d k	 rÂ | j
 d ƒ } t t j | d d ƒ ƒ } ˆ  j | ƒ n  | ró t | ƒ d k ró t d t | ƒ ƒ ‚ n  ‡  f d †  } | r| | d ƒ Sg  | D] } | | ƒ ^ qSd S(	   sM   Parse a location name, latitude, and longitude from an XML response.
        t	   Placemarki    t   Statust   codei   s.   Didn't find exactly one placemark! (Found %d.)c   	         sË   t  j |  d d g ƒ p d  } |  j d ƒ } | r= | d p@ d  } t  j | d ƒ pX d  } | r– g  | j d ƒ d  D] } t | ƒ ^ qu \ } } n% d  } } ˆ  j | ƒ \ } \ } } | | | f f S(   Nt   addresst   nameR   i    t   coordinatest   ,i   (   R	   t   get_first_textR   t   getElementsByTagNamet   splitt   floatR%   (	   t   placet   locationt   pointst   pointt   coordst   ft	   longitudet   latitudet   _(   R   (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt   parse_placeq   s    5
N(   t
   isinstancet
   basestringR	   t   decode_pageR   t   domt   minidomt   parseStringR   R   R4   t   lent   intR3   t   check_status_codeR   (	   R   R*   R#   t   doct   placest   statust   status_codeR@   R7   (    (   R   s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt	   parse_xmlZ   s&    
c   	      C   sð   t  | t ƒ s! t j | ƒ } n  t j | ƒ } | j d g  ƒ } t | ƒ d k r | j d g  ƒ } | d } |  j | ƒ d  S| r² t | ƒ d k r² t
 d t | ƒ ƒ ‚ n  d „  } | rÏ | | d ƒ Sg  | D] } | | ƒ ^ qÖ Sd  S(   NR,   i    R-   R.   i   s.   Didn't find exactly one placemark! (Found %d.)c         S   s7   |  j  d ƒ } |  d d d  \ } } | | | f f S(   NR/   R   R1   i   (   t   get(   R7   R8   R=   R>   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyR@   “   s    (   RA   RB   R	   RC   R   t   loadsRO   RG   RI   R   R   (	   R   R*   R#   RJ   RK   RL   RM   R@   R7   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt
   parse_json‚   s     
	c         C   sÁ   | d k r t  d ƒ ‚ n¢ | d k r6 t  d ƒ ‚ n‡ | d k rQ t d ƒ ‚ nl | d k rl t d ƒ ‚ nQ | d	 k r‡ t d
 ƒ ‚ n6 | d k r¢ t d ƒ ‚ n | d k r½ t d ƒ ‚ n  d  S(   Ni  s(   Bad request (Server returned status 400)iô  s)   Unkown error (Server returned status 500)iY  s   An empty lookup was performediZ  sŸ   No corresponding geographic location could be found for the specified location, possibly because the address is relatively new, or because it may be incorrect.i[  sX   The geocode for the given location could be returned due to legal or contractual reasonsib  sR   The api_key is either invalid or does not match the domain for which it was given.il  s†   The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.(   R   t   GQueryErrort   GBadKeyErrort   GTooManyQueriesError(   R   RM   (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyRI      s    N(   t   __name__t
   __module__t   __doc__R   R   t   propertyR   t   TrueR%   R!   RN   RQ   RI   (    (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyR
      s   .(RS   c           B   s   e  Z RS(    (   RU   RV   (    (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyRS   ­   s   RR   c           B   s   e  Z RS(    (   RU   RV   (    (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyRR   °   s   RT   c           B   s   e  Z RS(    (   RU   RV   (    (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyRT   ³   s   (   t   urllibR    t   urllib2R   R   t   ImportErrorR   t   django.utilsR   t   xml.parsers.expatR   t   geopy.geocoders.baseR   R   R   t   geopyR   R   R	   R
   RS   RR   RT   (    (    (    s5   /var/www/python/geopy-trunk/geopy/geocoders/google.pyt   <module>   s    œ