o
    4c                     @   sl   d dl mZ d dlmZ d dlmZmZ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)GeocoderAuthenticationFailureGeocoderQuotaExceededGeocoderServiceError)DEFAULT_SENTINELGeocoder)Location)logger)GeocodeFarmc                       st   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Zded	ddZ	dd Z
dd Zdd Z  ZS )r   zGeocoder using the GeocodeFarm API.

    Documentation at:
        https://www.geocode.farm/geocoding/free-api-documentation/
    z/v3/json/forward/z/v3/json/reverse/N)timeoutproxies
user_agentssl_contextadapter_factoryschemec          	         sN   t  j||||||d || _d}d| j|| jf | _d| j|| jf | _dS )a(  

        :param str api_key: (optional) The API key required by GeocodeFarm
            to perform geocoding requests.

        :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 scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.
        )r   r   r   r   r   r   zwww.geocode.farmz	%s://%s%sN)super__init__api_keyr   geocode_pathapireverse_pathreverse_api)	selfr   r   r   r   r   r   r   domain	__class__ Q/var/www/html/gps/gps/lib/python3.10/site-packages/geopy/geocoders/geocodefarm.pyr      s   %zGeocodeFarm.__init__T)exactly_oner   c                C   s\   d|i}| j r| j |d< d| j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.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        addrkey?z%s.geocode: %sr   r   )r   joinr   r   r
   debugr   __name__r   _parse_json_call_geocoder)r   queryr   r   paramsurlcallbackr   r   r   geocodeP   s   
zGeocodeFarm.geocodec          	      C   s   z|  |d\}}W n ty   tdw ||d}| jr%| j|d< d| j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. GeocodeFarm's API will always return at most one
            result.

        :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``.
        ,z"Must be a coordinate pair or Point)latlonr!   r"   z%s.reverse: %sr#   r$   )_coerce_point_to_stringsplit
ValueErrorr   r%   r   r   r
   r&   r   r'   r   r(   r)   )	r   r*   r   r   r0   r1   r+   r,   r-   r   r   r   reversek   s   
zGeocodeFarm.reversec           	      C   s   g }| dD ]K}| di }| di }| dd }| dd }| dd }|d u r1| dd }|d u r;| dd }|rG|rGt|}t|}|t|||f| q|S )	NRESULTSCOORDINATESADDRESSlatitude	longitudeaddress_returnedaddressformatted_address)getfloatappendr	   )	r   resultsplacesresultcoordinatesr<   r9   r:   	placenamer   r   r   _parse_code   s    zGeocodeFarm._parse_codec                 C   sT   |d u rd S |d }|  | d|di ddv rd S | |}|r(|d S |S )Ngeocoding_results
NO_RESULTSSTATUSstatus r   )_check_for_api_errorsr>   rF   )r   
api_resultr   rG   rB   r   r   r   r(      s   

zGeocodeFarm._parse_jsonc                 C   s`   | di }d| ddv rdS | dddk}|s.| d}ttd}| |t}||dS )	zc
        Raise any exceptions if there were problems reported
        in the api response.
        rI   rH   rJ   rK   NSUCCESSaccess)API_KEY_INVALIDOVER_QUERY_LIMIT)r>   r   r   r   )r   rG   status_resultapi_call_successaccess_erroraccess_error_to_exceptionexception_clsr   r   r   rL      s   
z!GeocodeFarm._check_for_api_errors)N)r'   
__module____qualname____doc__r   r   r   r   r.   r5   rF   r(   rL   __classcell__r   r   r   r   r      s$    6$r   N)	functoolsr   urllib.parser   	geopy.excr   r   r   geopy.geocoders.baser   r   geopy.locationr	   
geopy.utilr
   __all__r   r   r   r   r   <module>   s    