a
    }cL                     @   s   d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dZdd	d
ddddZdd ZdddZdd ZG dd dZG dd deZeZG dd deZeZeZdS )a{  
Geopy can calculate geodesic distance between two points using the
`geodesic distance
<https://en.wikipedia.org/wiki/Geodesics_on_an_ellipsoid>`_ or the
`great-circle distance
<https://en.wikipedia.org/wiki/Great-circle_distance>`_,
with a default of the geodesic distance available as the function
``geopy.distance.distance``.

Great-circle distance (:class:`.great_circle`) uses a spherical model of
the earth, using the mean earth radius as defined by the International
Union of Geodesy and Geophysics, (2\ *a* + *b*)/3 = 6371.0087714150598
kilometers approx 6371.009 km (for WGS-84), resulting in an error of up
to about 0.5%. The radius value is stored in
:const:`distance.EARTH_RADIUS`, so it can be customized (it should
always be in kilometers, however).

The geodesic distance is the shortest distance on the surface of an
ellipsoidal model of the earth.  The default algorithm uses the method
is given by `Karney (2013)
<https://doi.org/10.1007%2Fs00190-012-0578-z>`_ (:class:`.geodesic`);
this is accurate to round-off and always converges.

``geopy.distance.distance`` currently uses :class:`.geodesic`.

There are multiple popular ellipsoidal models,
and which one will be the most accurate depends on where your points are
located on the earth.  The default is the WGS-84 ellipsoid, which is the
most globally accurate.  geopy includes a few other models in the
:const:`distance.ELLIPSOIDS` dictionary::

                  model             major (km)   minor (km)     flattening
    ELLIPSOIDS = {'WGS-84':        (6378.137,    6356.7523142,  1 / 298.257223563),
                  'GRS-80':        (6378.137,    6356.7523141,  1 / 298.257222101),
                  'Airy (1830)':   (6377.563396, 6356.256909,   1 / 299.3249646),
                  'Intl 1924':     (6378.388,    6356.911946,   1 / 297.0),
                  'Clarke (1880)': (6378.249145, 6356.51486955, 1 / 293.465),
                  'GRS-67':        (6378.1600,   6356.774719,   1 / 298.25),
                  }

Here are examples of ``distance.distance`` usage, taking pair
of :code:`(lat, lon)` tuples::

    >>> from geopy import distance
    >>> newport_ri = (41.49008, -71.312796)
    >>> cleveland_oh = (41.499498, -81.695391)
    >>> print(distance.distance(newport_ri, cleveland_oh).miles)
    538.39044536

    >>> wellington = (-41.32, 174.81)
    >>> salamanca = (40.96, -5.50)
    >>> print(distance.distance(wellington, salamanca).km)
    19959.6792674

Using :class:`.great_circle` distance::

    >>> print(distance.great_circle(newport_ri, cleveland_oh).miles)
    536.997990696

You can change the ellipsoid model used by the geodesic formulas like so::

    >>> ne, cl = newport_ri, cleveland_oh
    >>> print(distance.geodesic(ne, cl, ellipsoid='GRS-80').miles)

The above model name will automatically be retrieved from the
:const:`distance.ELLIPSOIDS` dictionary. Alternatively, you can specify
the model values directly::

    >>> distance.geodesic(ne, cl, ellipsoid=(6377., 6356., 1 / 297.)).miles

Distances support simple arithmetic, making it easy to do things like
calculate the length of a path::

    >>> from geopy import Nominatim
    >>> d = distance.distance
    >>> g = Nominatim(user_agent="specify_your_app_name_here")
    >>> _, wa = g.geocode('Washington, DC')
    >>> _, pa = g.geocode('Palo Alto, CA')
    >>> print((d(ne, cl) + d(cl, wa) + d(wa, pa)).miles)
    3277.30439191


.. _distance_altitudes:

Currently all algorithms assume that altitudes of the points are either
zero (as in the examples above) or equal, and are relatively small.
Thus altitudes never affect the resulting distances::

    >>> from geopy import distance
    >>> newport_ri = (41.49008, -71.312796)
    >>> cleveland_oh = (41.499498, -81.695391)
    >>> print(distance.distance(newport_ri, cleveland_oh).km)
    866.4554329098687
    >>> newport_ri = (41.49008, -71.312796, 100)
    >>> cleveland_oh = (41.499498, -81.695391, 100)
    >>> print(distance.distance(newport_ri, cleveland_oh).km)
    866.4554329098687

If you need to calculate distances with elevation, then for short
distances the `Euclidean distance
<https://en.wikipedia.org/wiki/Euclidean_distance>`_ formula might give
a suitable approximation::

    >>> import math
    >>> from geopy import distance
    >>> p1 = (43.668613, 40.258916, 0.976)
    >>> p2 = (43.658852, 40.250839, 1.475)
    >>> flat_distance = distance.distance(p1[:2], p2[:2]).km
    >>> print(flat_distance)
    1.265133525952866
    >>> euclidian_distance = math.sqrt(flat_distance**2 + (p2[2] - p1[2])**2)
    >>> print(euclidian_distance)
    1.359986705262199

An attempt to calculate distances between points with different altitudes
would result in a :class:`ValueError` exception.

    )asinatan2cossinsqrt)Geodesic)unitsutilPointradiansgM@)獗n#@gQթԸ@g(Zwk?)r   g'Ը@g!6Zwk?)gb:@gAԸ@gq߇E^k?)gSc@gKuԸ@g#+k?)gz?@g}΃Ը@g԰Ǖ,k?)g\(@g SԸ@gܜwk?)WGS-84zGRS-80zAiry (1830)z	Intl 1924zClarke (1880)zGRS-67c                 C   s   | |k| |k  S N abr   r   J/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopy/distance.pycmp   s    r   c                 C   s   t || |S )a  
    ``geopy.distance.distance`` accepts coordinates in ``(y, x)``/``(lat, lon)``
    order, while some other libraries and systems might use
    ``(x, y)``/``(lon, lat)``.

    This function provides a convenient way to convert coordinates of the
    ``(x, y)``/``(lon, lat)`` format to a :class:`geopy.point.Point` instance.

    Example::

        >>> from geopy.distance import lonlat, distance
        >>> newport_ri_xy = (-71.312796, 41.49008)
        >>> cleveland_oh_xy = (-81.695391, 41.499498)
        >>> print(distance(lonlat(*newport_ri_xy), lonlat(*cleveland_oh_xy)).miles)
        538.3904453677203

    :param x: longitude
    :param y: latitude
    :param z: (optional) altitude
    :return: Point(latitude, longitude, altitude)
    r
   )xyzr   r   r   lonlat   s    r   c                 C   s    t | j|j dkrtdd S )Ngư>zMCalculating distance between points with different altitudes is not supported)absZaltitude
ValueErrorr   r   r   r   _ensure_same_altitude   s    r   c                   @   s:  e Zd ZdZ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d Zdd Zdd ZdCddZ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+d, Zd-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Z ed;d< Z!ed=d> Z"ed?d@ Z#edAdB Z$dS )DDistanceaD  
    Base class for other distance algorithms. Represents a distance.

    Can be used for units conversion::

        >>> from geopy.distance import Distance
        >>> Distance(miles=10).km
        16.09344

    Distance instances have all *distance* properties from :mod:`geopy.units`,
    e.g.: ``km``, ``m``, ``meters``, ``miles`` and so on.

    Distance instances are immutable.

    They support comparison::

        >>> from geopy.distance import Distance
        >>> Distance(kilometers=2) == Distance(meters=2000)
        True
        >>> Distance(kilometers=2) > Distance(miles=1)
        True

    String representation::

        >>> from geopy.distance import Distance
        >>> repr(Distance(kilometers=2))
        'Distance(2.0)'
        >>> str(Distance(kilometers=2))
        '2.0 km'
        >>> repr(Distance(miles=2))
        'Distance(3.218688)'
        >>> str(Distance(miles=2))
        '3.218688 km'

    Arithmetics::

        >>> from geopy.distance import Distance
        >>> -Distance(miles=2)
        Distance(-3.218688)
        >>> Distance(miles=2) + Distance(kilometers=1)
        Distance(4.218688)
        >>> Distance(miles=2) - Distance(kilometers=1)
        Distance(2.218688)
        >>> Distance(kilometers=6) * 5
        Distance(30.0)
        >>> Distance(kilometers=6) / 5
        Distance(1.2)
    c                 O   st   | dd}t|dkr&||d 7 }n0t|dkrVt|D ]\}}|| ||7 }q<|tjf i |7 }|| _dS )a  
        There are 3 ways to create a distance:

        - From kilometers::

            >>> from geopy.distance import Distance
            >>> Distance(1.42)
            Distance(1.42)

        - From units::

            >>> from geopy.distance import Distance
            >>> Distance(kilometers=1.42)
            Distance(1.42)
            >>> Distance(miles=1)
            Distance(1.609344)

        - From points (for non-abstract distances only),
          calculated as a sum of distances between all points::

            >>> from geopy.distance import geodesic
            >>> geodesic((40, 160), (40.1, 160.1))
            Distance(14.003702498106215)
            >>> geodesic((40, 160), (40.1, 160.1), (40.2, 160.2))
            Distance(27.999954644813478)
        
kilometersr      N)poplenr	   pairwisemeasurer   r   _Distance__kilometers)selfargskwargsr   r   r   r   r   r   __init__   s    zDistance.__init__c                 C   s(   t |tr| | j|j S tdd S )Nz7Distance instance must be added with Distance instance.)
isinstancer   	__class__r   	TypeErrorr&   otherr   r   r   __add__  s
    
zDistance.__add__c                 C   s   |  | j S r   )r+   r   r&   r   r   r   __neg__!  s    zDistance.__neg__c                 C   s
   | |  S r   r   r-   r   r   r   __sub__$  s    zDistance.__sub__c                 C   s(   t |trtdn| | j| S d S Nz5Distance instance must be multiplicated with numbers.r*   r   r,   r+   r   r-   r   r   r   __mul__'  s
    
zDistance.__mul__c                 C   s(   t |trtdn| || j S d S r3   r4   r-   r   r   r   __rmul__/  s
    
zDistance.__rmul__c                 C   s*   t |tr| j|j S | | j| S d S r   r*   r   r   r+   r-   r   r   r   __truediv__7  s    
zDistance.__truediv__c                 C   s*   t |tr| j|j S | | j| S d S r   r7   r-   r   r   r   __floordiv__=  s    
zDistance.__floordiv__c                 C   s   |  t| jS r   )r+   r   r   r0   r   r   r   __abs__C  s    zDistance.__abs__c                 C   s
   t | jS r   )boolr   r0   r   r   r   __bool__F  s    zDistance.__bool__c                 C   s   t dd S )NDistance is an abstract classNotImplementedError)r&   r   r   r   r   r   r$   I  s    zDistance.measureNc                 C   s   t ddS )a`  
        Calculate destination point using a starting point, bearing
        and a distance. This method works for non-abstract distances only.

        Example: a point 10 miles east from ``(34, 148)``::

            >>> import geopy.distance
            >>> geopy.distance.distance(miles=10).destination((34, 148), bearing=90)
            Point(33.99987666492774, 148.17419994321995, 0.0)

        :param point: Starting point.
        :type point: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.

        :param float bearing: Bearing in degrees: 0 -- North, 90 -- East,
            180 -- South, 270 or -90 -- West.

        :param distance: Distance, can be used to override
            this instance::

                >>> from geopy.distance import distance, Distance
                >>> distance(miles=10).destination((34, 148), bearing=90, distance=Distance(100))
                Point(33.995238229104764, 149.08238904409637, 0.0)

        :type distance: :class:`.Distance`

        :rtype: :class:`geopy.point.Point`
        r=   Nr>   )r&   pointbearingdistancer   r   r   destinationN  s    zDistance.destinationc                 C   s
   d| j  S )NzDistance(%s)r   r0   r   r   r   __repr__n  s    zDistance.__repr__c                 C   s
   d| j  S )Nz%s kmr%   r0   r   r   r   __str__q  s    zDistance.__str__c                 C   s(   t |trt| j|jS t| j|S d S r   )r*   r   r   r   r-   r   r   r   __cmp__t  s    
zDistance.__cmp__c                 C   s
   t | jS r   )hashr   r0   r   r   r   __hash__z  s    zDistance.__hash__c                 C   s   |  |dkS Nr   rH   r-   r   r   r   __eq__}  s    zDistance.__eq__c                 C   s   |  |dkS rK   rL   r-   r   r   r   __ne__  s    zDistance.__ne__c                 C   s   |  |dkS rK   rL   r-   r   r   r   __gt__  s    zDistance.__gt__c                 C   s   |  |dk S rK   rL   r-   r   r   r   __lt__  s    zDistance.__lt__c                 C   s   |  |dkS rK   rL   r-   r   r   r   __ge__  s    zDistance.__ge__c                 C   s   |  |dkS rK   rL   r-   r   r   r   __le__  s    zDistance.__le__c                 C   s   t j| jdS NrD   )r   feetr   r0   r   r   r   rT     s    zDistance.feetc                 C   s   | j S r   )rT   r0   r   r   r   ft  s    zDistance.ftc                 C   s   | j S r   rF   r0   r   r   r   r     s    zDistance.kilometersc                 C   s   | j S r   rD   r0   r   r   r   km  s    zDistance.kmc                 C   s   | j S r   )metersr0   r   r   r   m  s    z
Distance.mc                 C   s   t j| jdS rS   )r   rW   r   r0   r   r   r   rW     s    zDistance.metersc                 C   s   | j S r   )milesr0   r   r   r   mi  s    zDistance.mic                 C   s   t j| jdS rS   )r   rY   r   r0   r   r   r   rY     s    zDistance.milesc                 C   s   t j| jdS rS   )r   nauticalr   r0   r   r   r   r[     s    zDistance.nauticalc                 C   s   | j S r   )r[   r0   r   r   r   nm  s    zDistance.nm)N)%__name__
__module____qualname____doc__r)   r/   r1   r2   r5   r6   r8   r9   r:   r<   r$   rC   rE   rG   rH   rJ   rM   rN   rO   rP   rQ   rR   propertyrT   rU   r   rV   rX   rW   rZ   rY   r[   r\   r   r   r   r   r      sV   1)
 








r   c                       s2   e Zd ZdZ fddZdd Zd	ddZ  ZS )
great_circlea3  
    Use spherical geometry to calculate the surface distance between
    points.

    Set which radius of the earth to use by specifying a ``radius`` keyword
    argument. It must be in kilometers. The default is to use the module
    constant `EARTH_RADIUS`, which uses the average great-circle radius.

    Example::

        >>> from geopy.distance import great_circle
        >>> newport_ri = (41.49008, -71.312796)
        >>> cleveland_oh = (41.499498, -81.695391)
        >>> print(great_circle(newport_ri, cleveland_oh).miles)
        536.997990696

    c                    s$   | dt| _t j|i | d S )NZradius)r!   EARTH_RADIUSRADIUSsuperr)   )r&   r'   r(   r+   r   r   r)     s    zgreat_circle.__init__c                 C   s   t |t | }}t|| t|jdt|jd }}t|jdt|jd }}t|t| }}t|t| }	}
|| }t|t| }}tt|
| d ||	 ||
 |  d  ||	 ||
 |  }| j	| S )Ndegrees   )
r   r   r   latitude	longituder   r   r   r   rd   )r&   r   r   lat1lng1lat2lng2Zsin_lat1Zcos_lat1Zsin_lat2Zcos_lat2Z	delta_lngZcos_delta_lngZsin_delta_lngdr   r   r   r$     s$    

zgreat_circle.measureNc           	      C   s   t |}tj|jd}tj|jd}tj|d}|d u r<| }t|trL|j}t|| j	 }t
t|t| t|t| t|  }|tt|t| t| t|t|t|   }t tj|dtj|dS )Nrg   r   )r   r   r   rj   rk   r*   r   r   floatrd   r   r   r   r   rh   )	r&   r@   rA   rB   rl   rm   Zd_div_rrn   ro   r   r   r   rC     s&    
zgreat_circle.destination)N)r]   r^   r_   r`   r)   r$   rC   __classcell__r   r   rf   r   rb     s   rb   c                       s:   e Zd ZdZ fddZdd Zdd Zdd	d
Z  ZS )geodesica  
    Calculate the geodesic distance between points.

    Set which ellipsoidal model of the earth to use by specifying an
    ``ellipsoid`` keyword argument. The default is 'WGS-84', which is the
    most globally accurate model.  If ``ellipsoid`` is a string, it is
    looked up in the `ELLIPSOIDS` dictionary to obtain the major and minor
    semiaxes and the flattening. Otherwise, it should be a tuple with those
    values.  See the comments above the `ELLIPSOIDS` dictionary for
    more information.

    Example::

        >>> from geopy.distance import geodesic
        >>> newport_ri = (41.49008, -71.312796)
        >>> cleveland_oh = (41.499498, -81.695391)
        >>> print(geodesic(newport_ri, cleveland_oh).miles)
        538.390445368

    c                    sF   d | _ d | _d | _| |dd | j\}}}t j|i | d S )N	ellipsoidr   )ellipsoid_key	ELLIPSOIDgeodset_ellipsoidr!   re   r)   )r&   r'   r(   majorminorfrf   r   r   r)     s    zgeodesic.__init__c                 C   sL   t |tr<zt| | _|| _W qH ty8   tdY qH0 n|| _d | _d S )Nz0Invalid ellipsoid. See geopy.distance.ELLIPSOIDS)r*   str
ELLIPSOIDSrv   ru   KeyError	Exception)r&   rt   r   r   r   rx     s    


zgeodesic.set_ellipsoidc                 C   s   t |t | }}t|| |j|j }}|j|j }}t| jtrh| jj| jd krh| jj	| jd kst| jd | jd | _| j
||||tjd }|S )Nr   ri   s12)r   r   rj   rk   r*   rw   r   r   rv   r{   ZInverseZDISTANCE)r&   r   r   rl   lon1rn   lon2r   r   r   r   r$   +  s     
zgeodesic.measureNc                 C   s   t |}|j}|j}|}|d u r$| }t|tr4|j}t| jtrd| jj| j	d krd| jj
| j	d ks|t| j	d | j	d | _| j||||tjtjB }t |d |d S )Nr   ri   rn   r   )r   rj   rk   r*   r   r   rw   r   r   rv   r{   ZDirectZLATITUDEZ	LONGITUDE)r&   r@   rA   rB   rl   r   Zazi1rr   r   r   rC   ;  s$    

zgeodesic.destination)N)	r]   r^   r_   r`   r)   rx   r$   rC   rr   r   r   rf   r   rs      s
   rs   N)r   )r`   mathr   r   r   r   r   Zgeographiclib.geodesicr   Zgeopyr   r	   Zgeopy.pointr   Zgeopy.unitsr   rc   r}   r   r   r   r   rb   ZGreatCircleDistancers   ZGeodesicDistancerB   r   r   r   r   <module>   s.   v
 {EQ