o
    ÔèFh›$  ã                   @   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                       sx   e Zd ZdZh d£ZdZdZdeeded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   z†Geocoder using the Bing Maps Locations API.

    Documentation at:
        https://msdn.microsoft.com/en-us/library/ff701715.aspx
    >   ÚlocalityÚ
postalCodeÚaddressLineÚadminDistrictÚcountryRegionz/REST/v1/Locationsz/REST/v1/Locations/%(point)sNzdev.virtualearth.net)ÚschemeÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryÚdomainc          	         sJ   t ƒ j||||||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

        :param str domain: base api domain

            .. versionadded:: 2.4
        )r   r   r   r   r   r   z	%s://%s%sN)ÚsuperÚ__init__Úapi_keyr   Úgeocode_pathÚgeocode_apiÚreverse_pathÚreverse_api)	Úselfr   r   r   r   r   r   r   r   ©Ú	__class__© úM/var/www/html/loop/nvenv/lib/python3.10/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ƒr‡ fdd„| ¡ D ƒ}ˆ j|d< n|ˆ jdœ}|r'ˆ  |¡|d< |r-d|d< |r3||d< |d	ur;||d
< |rAd|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>r/   )Úqueryr/   ÚuserLocationé   Ú
maxResultsr*   NÚincludeNeighborhoodÚciso2Úincludeú?z%s.geocode: %s©r(   ©r   )Ú
isinstanceÚcollectionsÚabcÚMappingÚitemsr   Ú_coerce_point_to_stringÚjoinr    r   r   Údebugr%   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r#   r3   r(   r)   r   r*   r+   r,   ÚparamsÚurlÚcallbackr&   r1   r'   Úgeocode[   s,   .
ýþ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``.
        r/   r*   r8   r9   zutf-8r:   )Úpointz%s.reverse: %sr;   r<   )rB   r   r   ÚencoderC   r"   Údictr   r   rD   r%   rE   r   rF   rG   )r#   r3   r(   r   r*   r,   rL   rH   Úquoted_pointrI   rJ   r&   r&   r'   Úreverse¦   s   
#
ÿzBing.reversec                    s¶   |  dd¡}|dkr4|  dd¡}|dkrt|ƒ‚|dkr t|ƒ‚|dkr(t|ƒ‚|dkr0t|ƒ‚t|ƒ‚|d	 d
 d }|du sDt|ƒsFdS dd„ ‰ |rRˆ |d
 ƒS ‡ fdd„|D ƒS )zW
        Parse a location name, latitude, and longitude from an JSON response.
        Ú
statusCodeéÈ   ÚerrorDetailsÚ i‘  i“  i­  i÷  Ú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 pRd}| d d d p\d}|ri|rit|ƒ}t|ƒ}t|
||f| ƒS )z3
            Parse each return object.
            z, 
Úaddressr   rT   r   r   r   r   z, ú rL   Úcoordinatesr   Nr5   )ÚgetÚstripr   Úfloatr   )ÚresourceÚ
stripcharsÚaddrrW   ÚcityÚstateÚzipcodeÚcountryÚ
city_stateÚplaceÚlocationÚlatitudeÚ	longituder&   r&   r'   Úparse_resourceî   s    z(Bing._parse_json.<locals>.parse_resourcec                    s   g | ]}ˆ |ƒ‘qS r&   r&   )r.   r]   ©ri   r&   r'   Ú
<listcomp>
  s    z$Bing._parse_json.<locals>.<listcomp>)rZ   r   r   r   r	   r   Úlen)r#   Údocr(   Ústatus_codeÚerrrV   r&   rj   r'   rF   Ø   s&   zBing._parse_json)T)rE   Ú
__module__Ú__qualname__Ú__doc__r-   r   r!   r
   r   rK   rP   rF   Ú__classcell__r&   r&   r$   r'   r      s4    ö:÷Où2r   )Úcollections.abcr>   Ú	functoolsr   Úurllib.parser   r   Ú	geopy.excr   r   r   r   r	   Úgeopy.geocoders.baser
   r   Úgeopy.locationr   Ú
geopy.utilr   r   Ú__all__r   r&   r&   r&   r'   Ú<module>   s    