o
    ÔèFh´  ã                   @   sX   d dl 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G dd„ deƒZd	S )
é    )Úpartial)Ú	urlencode)ÚDEFAULT_SENTINELÚGeocoder©ÚLocation)Úlogger)ÚMapQuestc                       sh   e Zd ZdZdZdZdeededddœ‡ fdd„
Zdd
d„Zd	edddœdd„Z	d	edœdd„Z
‡  ZS )r	   a¨  Geocoder using the MapQuest API based on Licensed data.

    Documentation at:
        https://developer.mapquest.com/documentation/geocoding-api/

    MapQuest provides two Geocoding APIs:

    - :class:`geopy.geocoders.OpenMapQuest` Nominatim-alike API
      which is based on Open data from OpenStreetMap.
    - :class:`geopy.geocoders.MapQuest` (this class) MapQuest's own API
      which is based on Licensed data.
    z/geocoding/v1/addressz/geocoding/v1/reverseNzwww.mapquestapi.com)ÚschemeÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryÚdomainc          	         sZ   t ƒ j||||||d� || _| d¡| _d| j| j| jf | _d| j| j| jf | _	dS )aÈ  
        :param str api_key: The API key required by Mapquest to perform
            geocoding requests. API keys are managed through MapQuest's "Manage Keys"
            page (https://developer.mapquest.com/user/me/apps).

        :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 for mapquest
        )r
   r   r   r   r   r   ú/z	%s://%s%sN)
ÚsuperÚ__init__Úapi_keyÚstripr   r
   Úgeocode_pathÚgeocode_apiÚreverse_pathÚreverse_api)	Úselfr   r
   r   r   r   r   r   r   ©Ú	__class__© úQ/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/mapquest.pyr      s   (ú	ÿÿzMapQuest.__init__Tc                    sR   |d d d }|g krdS dd„ ‰‡fdd„‰ |r ˆ |d ƒS ‡ fd	d
„|D ƒS )z7Returns location, (latitude, longitude) from json feed.Úresultsr   Ú	locationsNc                    s$   g d¢}‡ fdd„|D ƒ}d  |¡S )N)ÚstreetÚ
adminArea6Ú
adminArea5Ú
adminArea4Ú
adminArea3Ú
adminArea2Ú
adminArea1Ú
postalCodec                    s   g | ]}ˆ   |¡rˆ | ‘qS r   )Úget)Ú.0Úk©Úfeaturer   r   Ú
<listcomp>j   s    z@MapQuest._parse_json.<locals>.parse_location.<locals>.<listcomp>z, )Újoin)r-   Ú	addr_keysÚlocationr   r,   r   Úparse_location^   s   
z,MapQuest._parse_json.<locals>.parse_locationc                    s0   ˆ | ƒ}| d d }| d d }t |||f| ƒS )NÚlatLngÚlngÚlatr   )r-   r1   Ú	longitudeÚlatitude)r2   r   r   Úparse_featurem   s   z+MapQuest._parse_json.<locals>.parse_featurec                    s   g | ]}ˆ |ƒ‘qS r   r   )r*   r-   )r8   r   r   r.   v   s    z(MapQuest._parse_json.<locals>.<listcomp>r   )r   ÚjsonÚexactly_oneÚfeaturesr   )r8   r2   r   Ú_parse_jsonW   s   zMapQuest._parse_json)r:   r   ÚlimitÚboundsc          	      C   sŠ   i }| j |d< ||d< |dur||d< |rd|d< |r#|  |d¡|d< d | jt|ƒf¡}t d	| jj|¡ t	| j
|d
�}| j|||d�S )af  
        Return a location point by address.

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

        :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 int limit: Limit the maximum number of items in the
            response. This will be reset to one if ``exactly_one`` is True.

        :param bounds: The bounding box of the viewport within which
            to bias geocode results more prominently.
            Example: ``[Point(22, 180), Point(-22, -180)]``.
        :type bounds: list or tuple of 2 items of :class:`geopy.point.Point` or
            ``(latitude, longitude)`` or ``"%(latitude)s, %(longitude)s"``.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        Úkeyr1   NÚ
maxResultsé   z#%(lat2)s,%(lon1)s,%(lat1)s,%(lon2)sÚboundingBoxú?z%s.geocode: %s©r:   ©r   )r   Ú_format_bounding_boxr/   r   r   r   Údebugr   Ú__name__r   r<   Ú_call_geocoder)	r   Úqueryr:   r   r=   r>   ÚparamsÚurlÚcallbackr   r   r   Úgeocodex   s   "
ÿzMapQuest.geocode)r:   r   c                C   sf   i }| j |d< |  |d¡}||d< d | jt|ƒf¡}t d| jj|¡ t	| j
|d�}| j|||d�S )aL  
        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.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r?   z%(lat)s,%(lon)sr1   rC   z%s.reverse: %srD   rE   )r   Ú_coerce_point_to_stringr/   r   r   r   rG   r   rH   r   r<   rI   )r   rJ   r:   r   rK   ÚpointrL   rM   r   r   r   Úreverse¯   s   
zMapQuest.reverse)T)rH   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r<   rN   rQ   Ú__classcell__r   r   r   r   r	      s*    ö
;%ù;ûr	   N)Ú	functoolsr   Úurllib.parser   Úgeopy.geocoders.baser   r   Úgeopy.locationr   Ú
geopy.utilr   Ú__all__r	   r   r   r   r   Ú<module>   s    