ó
ÎrOc           @   sR   d  d l  Z  d  d l m Z d  d l m Z m Z m Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   islice(   t   utilt   unitst   formatt   Pointc           B   s,  e  Z d  Z e d d d e j d e j d e j d d ƒ Z e	 j
 d e e	 j ƒ Z d d d d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d d d d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z e d d „ ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   så  
    A geodetic point with latitude, longitude, and altitude.
    
    Latitude and longitude are floating point values in degrees.
    Altitude is a floating point value in kilometers. The reference level
    is never considered and is thus application dependent, so be consistent!
    The default for all values is 0.
    
    Points can be created in a number of ways...
        
    With longitude, latitude, and altitude:
    >>> p1 = Point(41.5, -81, 0)
    >>> p2 = Point(latitude=41.5, longitude=-81)
    
    With a sequence of 0 to 3 values (longitude, latitude, altitude):
    >>> p1 = Point([41.5, -81, 0])
    >>> p2 = Point((41.5, -81))
    
    Copy another `Point` instance:
    >>> p2 = Point(p1)
    >>> p2 == p1
    True
    >>> p2 is p1
    False
    
    Give an object with a 'point' attribute, such as a `Location` instance:
    >>> p = Point(location)
    
    Give a string containing at least latitude and longitude:
    >>> p1 = Point('41.5,-81.0')
    >>> p2 = Point('41.5 N -81.0 W')
    >>> p3 = Point('-41.5 S, 81.0 E, 2.5km')
    >>> p4 = Point('23 26m 22s N 23 27m 30s E 21.0mi')
    >>> p5 = Point('''3 26' 22" N 23 27' 30" E''')
    
    Point values can be accessed by name or by index:
    >>> p = Point(41.5, -81.0, 0)
    >>> p.latitude == p[0]
    True
    >>> p.longitude == p[1]
    True
    >>> p.altitude == p[2]
    True
    
    When unpacking (or iterating), a (latitude, longitude, altitude) tuple is returned
    >>> latitude, longitude, altitude = p
    
    t   FLOATs   \d+(?:\.\d+)?t   DEGREEt   PRIMEt   DOUBLE_PRIMEt   SEPs   \s*[,;\s]\s*sé  
        \s*
        (?P<latitude>
          (?P<latitude_degrees>-?%(FLOAT)s)(?:[%(DEGREE)s ][ ]*
            (?:(?P<latitude_arcminutes>%(FLOAT)s)[%(PRIME)s'm][ ]*)?
            (?:(?P<latitude_arcseconds>%(FLOAT)s)[%(DOUBLE_PRIME)s"s][ ]*)?
            )?(?P<latitude_direction>[NS])?)
        %(SEP)s
        (?P<longitude>
          (?P<longitude_degrees>-?%(FLOAT)s)(?:[%(DEGREE)s\s][ ]*
          (?:(?P<longitude_arcminutes>%(FLOAT)s)[%(PRIME)s'm][ ]*)?
          (?:(?P<longitude_arcseconds>%(FLOAT)s)[%(DOUBLE_PRIME)s"s][ ]*)?
          )?(?P<longitude_direction>[EW])?)(?:
        %(SEP)s
          (?P<altitude>
            (?P<altitude_distance>-?%(FLOAT)s)[ ]*
            (?P<altitude_units>km|m|mi|ft|nm|nmi)))?
        \s*$
    c         C   s€  | d  k o | d  k } | rÅ t | t j ƒ rÅ | } | d  k rF qÅ t | t ƒ rb |  j | ƒ St | t ƒ r~ |  j | ƒ Sy t | ƒ } Wn$ t	 k
 r´ t	 d | f ƒ ‚ qÅ X|  j
 | ƒ Sn  t | pÑ d ƒ } t | ƒ d k rþ | d d d } n  t | p
d ƒ } t | ƒ d k r7| d d d } n  t | pCd ƒ } t t |  ƒ j |  ƒ } | | _ | | _ | | _ | S(   Ns(   Failed to create Point instance from %r.i    iZ   i´   ih  (   t   Nonet
   isinstanceR   t   NUMBER_TYPESR   t
   from_pointt
   basestringt   from_stringt   itert	   TypeErrort   from_sequencet   floatt   abst   supert   __new__t   latitudet	   longitudet   altitude(   t   clsR   R   R   t
   single_argt   argt   seqt   self(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR   Q   s6    			c         C   s   |  j  |  j |  j f | S(   N(   R   R   R   (   R   t   index(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __getitem__u   s    c         C   s>   |  j  |  j |  j g } | | | <| \ |  _  |  _ |  _ d  S(   N(   R   R   R   (   R   R   t   valuet   point(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __setitem__x   s    
c         C   s   t  |  j |  j |  j f ƒ S(   N(   R   R   R   R   (   R   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __iter__}   s    c         C   s   d |  j  |  j |  j f S(   Ns   Point(%r, %r, %r)(   R   R   R   (   R   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __repr__€   s    t    t   mt   sc      	   C   s  d t  j t |  j ƒ d i | d 6| d 6| d 6ƒ|  j d k rH d pK d f } d t  j t |  j ƒ d i | d 6| d 6| d 6ƒ|  j d k rš d	 p d
 f } | | g } | d  k rÎ t |  j ƒ } n  | rt | t	 ƒ sì d } n  | j
 |  j | ƒ ƒ n  d j | ƒ S(   Ns   %s %st   symbolst   degt   arcmint   arcseci    t   Nt   St   Et   Wt   kms   , (   R   t   format_degreesR   R   R   R
   t   boolR   R   R   t   appendt   format_altitudet   join(   R   R   t   deg_chart   min_chart   sec_charR   R   t   coordinates(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR   …   s    00	c         C   sˆ   d |  j  } d |  j } | | g } | d  k rD t |  j ƒ } n  | r{ t | t ƒ sb d } n  | j |  j | ƒ ƒ n  d j	 | ƒ S(   Ns   %sR1   s   , (
   R   R   R
   R3   R   R   R   R4   R5   R6   (   R   R   R   R   R:   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   format_decimal™   s    	R1   c         C   s   t  j |  j | ƒ S(   N(   R   t   distanceR   (   R   t   unit(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR5   §   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __str__ª   s    c         C   s   |  j  d  t  j t  j t  j ƒ S(   N(   R   R
   R   R   R   (   R   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __unicode__­   s    c         C   s   t  |  ƒ t  | ƒ k S(   N(   t   tuple(   R   t   other(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __eq__²   s    c         C   s   t  |  ƒ t  | ƒ k S(   N(   R@   (   R   RA   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   __ne__µ   s    c      
   C   sÆ   | d k  p | j  d ƒ } t | p' d ƒ } t | p9 d ƒ } t | pK d ƒ } | s] | r• t j d | d | ƒ } | rˆ | | 8} q• | | 7} n  | d
 k r¥ | S| d k r¶ | St d	 ƒ ‚ d  S(   Ni    t   -t
   arcminutest
   arcsecondsR-   R/   R.   R0   s+   Invalid direction! Should be one of [NSEW].(   NR-   R/   (   R.   R0   (   t
   startswithR   R   t   degreesR
   t
   ValueError(   R   RH   RE   RF   t	   directiont   negativet   more(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   parse_degrees¸   s    c         C   sp   | d  k	 rh t | ƒ } i d „  d 6d „  d 6d „  d 6d „  d 6d	 „  d
 6d „  d 6} | | | ƒ S| Sd  S(   Nc         S   s   |  S(   N(    (   t   d(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   <lambda>Ò   s    R1   c         S   s   t  j d |  ƒ S(   Nt   meters(   R   t
   kilometers(   RN   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyRO   Ó   s    R'   c         S   s   t  j d |  ƒ S(   Nt   miles(   R   RQ   (   RN   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyRO   Ô   s    t   mic         S   s   t  j d |  ƒ S(   Nt   feet(   R   RQ   (   RN   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyRO   Õ   s    t   ftc         S   s   t  j d |  ƒ S(   Nt   nautical(   R   RQ   (   RN   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyRO   Ö   s    t   nmc         S   s   t  j d |  ƒ S(   NRV   (   R   RQ   (   RN   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyRO   ×   s    t   nmi(   R
   R   (   R   R<   R=   t
   CONVERTERS(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   parse_altitudeÍ   s    




c         C   s×   t  j |  j | ƒ } | rÇ |  j | j d ƒ | j d ƒ | j d ƒ | j d ƒ ƒ } |  j | j d ƒ | j d ƒ | j d ƒ | j d ƒ ƒ } |  j | j d	 ƒ | j d
 ƒ ƒ } |  | | | ƒ St d ƒ ‚ d S(   sè  
        Create and return a Point instance from a string containing latitude
        and longitude, and optionally, altitude.
        
        Latitude and longitude must be in degrees and may be in decimal form
        or indicate arcminutes and arcseconds (labeled with Unicode prime and
        double prime, ASCII quote and double quote or 'm' and 's'). The degree
        symbol is optional and may be included after the decimal places (in
        decimal form) and before the arcminutes and arcseconds otherwise.
        Coordinates given from south and west (indicated by S and W suffixes)
        will be converted to north and east by switching their signs. If no
        (or partial) cardinal directions are given, north and east are the
        assumed directions. Latitude and longitude must be separated by at
        least whitespace, a comma, or a semicolon (each with optional
        surrounding whitespace).
        
        Altitude, if supplied, must be a decimal number with given units.
        The following unit abbrevations (case-insensitive) are supported:
        
            km (kilometers)
            m (meters)
            mi (miles)
            ft (feet)
            nm, nmi (nautical miles)
        
        Some example strings the will work include:
        
            41.5;-81.0
            41.5,-81.0
            41.5 -81.0
            41.5 N -81.0 W
            -41.5 S;81.0 E
            23 26m 22s N 23 27m 30s E
            23 26' 22" N 23 27' 30" E
        
        t   latitude_degreest   latitude_arcminutest   latitude_arcsecondst   latitude_directiont   longitude_degreest   longitude_arcminutest   longitude_arcsecondst   longitude_directiont   altitude_distancet   altitude_unitss<   Failed to create Point instance from string: unknown format.N(   t   ret   matcht   POINT_PATTERNRM   t   groupRZ   RI   (   R   t   stringRf   R   R   R   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR   Ý   s$    &c         C   s   t  t | d ƒ ƒ } |  | Œ  S(   sÍ   
        Create and return a new Point instance from any iterable with 0 to
        3 elements.  The elements, if present, must be latitude, longitude,
        and altitude, respectively.
        
        i   (   R@   R    (   R   R   t   args(    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR     s    c         C   s   |  | j  | j | j ƒ S(   s^   
        Create and return a new Point instance from another Point instance.
        
        (   R   R   R   (   R   R"   (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR   &  s    N(   t   __name__t
   __module__t   __doc__t   dictR   R   R   R   t   UTIL_PATTERNSRe   t   compilet   XRg   R
   R   R    R#   R$   R%   R;   R5   R>   R?   RB   RC   t   classmethodRM   RZ   R   R   R   (    (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyR      s6   0				$								>(	   Re   t	   itertoolsR    t   geopyR   R   R   t   objectR   (    (    (    s*   /var/www/python/geopy-trunk/geopy/point.pyt   <module>   s   