o
    4c                      @   st   d dl mZ d dlmZmZ 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)quote	urlencode)AdapterHTTPError)GeocoderQuotaExceeded)DEFAULT_SENTINELGeocoderLocation)logger)TomTomc                       s   e Zd ZdZdZdZdeededdd fdd
Zd	edd
ddddZd	eddddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd  Z  ZS )!r   zuTomTom geocoder.

    Documentation at:
        https://developer.tomtom.com/search-api/search-api-documentation
    z /search/2/geocode/%(query)s.jsonz*/search/2/reverseGeocode/%(position)s.jsonNzapi.tomtom.com)schemetimeoutproxies
user_agentssl_contextadapter_factorydomainc          	         sJ   t  j||||||d || _d| j|| jf | _d| j|| jf | _dS )a7  
        :param str api_key: TomTom API 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: Domain where the target TomTom service
            is hosted.
        )r   r   r   r   r   r   z	%s://%s%sN)super__init__api_keyr   geocode_pathapireverse_pathapi_reverse)	selfr   r   r   r   r   r   r   r   	__class__ L/var/www/html/gps/gps/lib/python3.10/site-packages/geopy/geocoders/tomtom.pyr      s   'zTomTom.__init__TF)exactly_oner   limit	typeaheadlanguagec                C   s   |  |}| ||d< |rtt||d< |rd|d< |r"||d< t|d}d| jt|d t	|f}	t
d| jj|	 t| j|d	}
| j|	|
|d
S )a=  
        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: Maximum amount of results to return from the service.
            Unless exactly_one is set to False, limit will always be 1.

        :param bool typeahead: If the "typeahead" flag is set, the query
            will be interpreted as a partial input and the search will
            enter predictive mode.

        :param str language: Language in which search results should be
            returned. When data in specified language is not
            available for a specific field, default language is used.
            List of supported languages (case-insensitive):
            https://developer.tomtom.com/online-search/online-search-documentation/supported-languages

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r"   r!   1r#   utf-8?)queryz%s.geocode: %sr    r   )_geocode_params_boolean_valuestrintr   encodejoinr   dictr   r   debugr   __name__r   _parse_json_call_geocoder)r   r'   r    r   r!   r"   r#   paramsquoted_queryurlcallbackr   r   r   geocodeJ   s   
'zTomTom.geocode)r    r   r#   c          
      C   s|   |  |}| |}|r||d< t|d}d| jt|d t|f}t	d| j
j| t| j|d}	| j||	|dS )a  
        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 language: Language in which search results should be
            returned. When data in specified language is not
            available for a specific field, default language is used.
            List of supported languages (case-insensitive):
            https://developer.tomtom.com/online-search/online-search-documentation/supported-languages

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r#   r%   r&   )positionz%s.reverse: %sr(   r)   )_coerce_point_to_string_reverse_paramsr   r.   r/   r   r0   r   r   r1   r   r2   r   _parse_reverse_jsonr4   )
r   r'   r    r   r#   r:   r5   quoted_positionr7   r8   r   r   r   reverse   s   
!
zTomTom.reversec                 C   s   |rdS dS )Ntruefalser   )r   
bool_valuer   r   r   r+      s   zTomTom._boolean_valuec                 C   
   d| j iS Nkeyr   )r   formatted_queryr   r   r   r*         zTomTom._geocode_paramsc                 C   rC   rD   rF   )r   r:   r   r   r   r<      rH   zTomTom._reverse_paramsc                    <   |r|d sd S |r  |d d S  fdd|d D S )Nresultsr   c                       g | ]}  |qS r   _parse_search_result.0resultr   r   r   
<listcomp>       z&TomTom._parse_json.<locals>.<listcomp>rL   r   	resourcesr    r   rQ   r   r3         
zTomTom._parse_jsonc                 C   s0   |d d }|d d }t |d d ||f|S )Nr:   latlonaddressfreeformAddressr	   r   rP   latitude	longituder   r   r   rM      s
   zTomTom._parse_search_resultc                    rI   )N	addressesr   c                    rK   r   _parse_reverse_resultrN   rQ   r   r   rR      rS   z.TomTom._parse_reverse_json.<locals>.<listcomp>r_   rT   r   rQ   r   r=      rV   zTomTom._parse_reverse_jsonc                 C   s*   |d  d\}}t|d d ||f|S )Nr:   ,rY   rZ   )splitr
   r[   r   r   r   r`      s   zTomTom._parse_reverse_resultc                 C   sL   t |tsd S |jd u s|jd u rd S |jdkr"d|jv r$td|d S d S )Ni  zDeveloper Over Qps)
isinstancer   status_codetextr   )r   errorr   r   r   _geocoder_exception_handler   s   

z"TomTom._geocoder_exception_handler)r2   
__module____qualname____doc__r   r   r   r   r9   r?   r+   r*   r<   r3   rM   r=   r`   rg   __classcell__r   r   r   r   r      s<    7=.

r   N)	functoolsr   urllib.parser   r   geopy.adaptersr   	geopy.excr   geopy.geocoders.baser   r   geopy.locationr
   
geopy.utilr   __all__r   r   r   r   r   <module>   s    