U
    7h$                     @   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                       s~   e Zd ZdZdddddh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   zGeocoder using the Bing Maps Locations API.

    Documentation at:
        https://msdn.microsoft.com/en-us/library/ff701715.aspx
    addressLinelocalityadminDistrictcountryRegion
postalCodez/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__ N/var/www/formularioweb/env/lib/python3.8/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}|rN ||d< |rZd|d< |rf||d< |d	k	rv||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 kr||qS r&   )structured_query_params).0keyvalr#   r&   r'   
<dictcomp>   s   
 z Bing.geocode.<locals>.<dictcomp>r/   )queryr/   ZuserLocation   Z
maxResultsr*   NZ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*   r5   r6   zutf-8r7   )pointz%s.reverse: %sr8   r9   )r?   r   r   encoder@   r"   dictr   r   rA   r%   rB   r   rC   rD   )r#   r3   r(   r   r*   r,   rI   rE   Zquoted_pointrF   rG   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k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   rN   r   r   r   r   z,  rI   Zcoordinatesr   Nr4   )getstripr   floatr   )resourceZ
stripcharsaddrrP   ZcitystateZzipcodeZcountryZ
city_stateZplacelocationZlatitudeZ	longituder&   r&   r'   parse_resource   s     z(Bing._parse_json.<locals>.parse_resourcec                    s   g | ]} |qS r&   r&   )r.   rU   rY   r&   r'   
<listcomp>
  s     z$Bing._parse_json.<locals>.<listcomp>)rR   r   r   r   r	   r   len)r#   docr(   status_codeerrrO   r&   rZ   r'   rC      s&    



zBing._parse_json)T)rB   
__module____qualname____doc__r-   r   r!   r
   r   rH   rL   rC   __classcell__r&   r&   r$   r'   r      s<   :O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   