o
    4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
    >   locality
postalCodeaddressLineadminDistrictcountryRegionz/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__ J/var/www/html/gps/gps/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t|jt|j	f|d< |r5d|d< |r;||d	< |d
urC||d< |rId|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   ,userLocation   
maxResultsr+   N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+   r:   r;   zutf-8r<   )pointz%s.reverse: %sr=   r>   )_coerce_point_to_stringr   r   encoderD   r"   dictr   r   rH   r&   rI   r   rJ   rK   )r#   r4   r)   r   r+   r-   rP   rL   quoted_pointrM   rN   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   rY   r   r   r   r   z,  rP   coordinatesr   Nr7   )getstripr   floatr   )resource
stripcharsaddrr\   citystatezipcodecountry
city_stateplacelocationrF   rG   r'   r'   r(   parse_resource   s    z(Bing._parse_json.<locals>.parse_resourcec                    s   g | ]} |qS r'   r'   )r/   rb   rl   r'   r(   
<listcomp>  s    z$Bing._parse_json.<locals>.<listcomp>)r_   r   r   r   r	   r   len)r#   docr)   status_codeerrr[   r'   rm   r(   rJ      s&   zBing._parse_json)T)rI   
__module____qualname____doc__r.   r   r!   r
   r   rO   rU   rJ   __classcell__r'   r'   r%   r(   r      s2    6Q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    