a
    }c$                     @   s   d dl Zd dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZ d dlmZmZ dZG d	d
 d
eZdS )    N)partial)quote	urlencode)GeocoderAuthenticationFailureGeocoderInsufficientPrivilegesGeocoderRateLimitedGeocoderServiceErrorGeocoderUnavailable)DEFAULT_SENTINELGeocoder)Location)join_filterlogger)Bingc                       sv   e Zd ZdZh dZdZdZdeededd fdd
Zd	deddd
dddZ	d	edd
dddZ
dddZ  ZS )r   zGeocoder using the Bing Maps Locations API.

    Documentation at:
        https://msdn.microsoft.com/en-us/library/ff701715.aspx
    >   addressLinecountryRegion
postalCodeadminDistrictlocalityz/REST/v1/Locationsz/REST/v1/Locations/%(point)sNschemetimeoutproxies
user_agentssl_contextadapter_factoryc          	         sN   t  j||||||d || _d}d| j|| jf | _d| j|| jf | _dS )a=  

        :param str api_key: Should be a valid Bing Maps API key
            (https://www.microsoft.com/en-us/maps/create-a-bing-maps-key).

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :param int timeout:
            See :attr:`geopy.geocoders.options.default_timeout`.

        :param dict proxies:
            See :attr:`geopy.geocoders.options.default_proxies`.

        :param str user_agent:
            See :attr:`geopy.geocoders.options.default_user_agent`.

        :type ssl_context: :class:`ssl.SSLContext`
        :param ssl_context:
            See :attr:`geopy.geocoders.options.default_ssl_context`.

        :param callable adapter_factory:
            See :attr:`geopy.geocoders.options.default_adapter_factory`.

            .. versionadded:: 2.0
        r   zdev.virtualearth.netz	%s://%s%sN)super__init__api_keyr   geocode_pathgeocode_apireverse_pathreverse_api)	selfr   r   r   r   r   r   r   domain	__class__ P/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopy/geocoders/bing.pyr   %   s    %zBing.__init__TF)exactly_oneuser_locationr   cultureinclude_neighborhoodinclude_country_codec                   s   t |tjjr0 fdd| D } j|d< n| jd}|r^dt|jt|j	f|d< |rjd|d< |rv||d	< |d
ur||d< |rd|d< d j
t|f}	td jj|	 t j|d}
 j|	|
|dS )a  
        Return a location point by address.

        :param query: The address or query you wish to geocode.

            For a structured query, provide a dictionary whose keys
            are one of: `addressLine`, `locality` (city),
            `adminDistrict` (state), `countryRegion`, or `postalCode`.
        :type query: str or dict

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param user_location: Prioritize results closer to
            this location.
        :type user_location: :class:`geopy.point.Point`

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :param str culture: Affects the language of the response,
            must be a two-letter country code.

        :param bool include_neighborhood: Sets whether to include the
            neighborhood field in the response.

        :param bool include_country_code: Sets whether to include the
            two-letter ISO code of the country in the response (field name
            'countryRegionIso2').

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        c                    s    i | ]\}}| j v r||qS r'   )structured_query_params).0keyvalr#   r'   r(   
<dictcomp>   s   
z Bing.geocode.<locals>.<dictcomp>r0   )queryr0   ,ZuserLocation   Z
maxResultsr+   NZincludeNeighborhoodciso2include?z%s.geocode: %sr)   r   )
isinstancecollectionsabcMappingitemsr   joinstrlatitude	longituder    r   r   debugr&   __name__r   _parse_json_call_geocoder)r#   r4   r)   r*   r   r+   r,   r-   paramsurlcallbackr'   r2   r(   geocodeW   s0    .
zBing.geocode)r)   r   r+   r-   c                C   s   |  |}d| ji}|r ||d< |r,d|d< t|d}d| jt|d t|f}	t	d| j
j|	 t| j|d	}
| j|	|
|d
S )ar  
        Return an address by location point.

        :param query: The coordinates for which you wish to obtain the
            closest human-readable addresses.
        :type query: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :param str culture: Affects the language of the response,
            must be a two-letter country code.

        :param bool include_country_code: Sets whether to include the
            two-letter ISO code of the country in the response (field name
            'countryRegionIso2').

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r0   r+   r7   r8   zutf-8r9   )pointz%s.reverse: %sr:   r;   )Z_coerce_point_to_stringr   r   encoderA   r"   dictr   r   rE   r&   rF   r   rG   rH   )r#   r4   r)   r   r+   r-   rM   rI   Zquoted_pointrJ   rK   r'   r'   r(   reverse   s    #

zBing.reversec                    s   | dd}|dkrp| dd}|dkr2t|n>|dkrDt|n,|dkrVt|n|dkrht|nt||d	 d
 d }|du st|sdS dd  |r |d
 S  fdd|D S dS )zW
        Parse a location name, latitude, and longitude from an JSON response.
        Z
statusCode   ZerrorDetails i  i  i  i  ZresourceSetsr   	resourcesNc                 S   s   d}| d }| dd|}| dd|}| dd|}| dd|}| dd|}td	||g}td
||g}	td	||	|g}
| d d d pd}| d d d pd}|r|rt|}t|}t|
||f| S )z3
            Parse each return object.
            z, 
addressr   rR   r   r   r   r   z,  rM   Zcoordinatesr   Nr6   )getstripr   floatr   )resourceZ
stripcharsaddrrT   ZcitystateZzipcodeZcountryZ
city_stateZplacelocationrC   rD   r'   r'   r(   parse_resource   s     z(Bing._parse_json.<locals>.parse_resourcec                    s   g | ]} |qS r'   r'   )r/   rY   r]   r'   r(   
<listcomp>      z$Bing._parse_json.<locals>.<listcomp>)rV   r   r   r   r	   r   len)r#   docr)   status_codeerrrS   r'   r^   r(   rG      s&    



zBing._parse_json)T)rF   
__module____qualname____doc__r.   r   r!   r
   r   rL   rP   rG   __classcell__r'   r'   r%   r(   r      s0   6Q2r   )collections.abcr=   	functoolsr   urllib.parser   r   Z	geopy.excr   r   r   r   r	   Zgeopy.geocoders.baser
   r   Zgeopy.locationr   Z
geopy.utilr   r   __all__r   r'   r'   r'   r(   <module>   s   