a
    î}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                       sr   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   z‚Geocoder 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__© úW/var/www/html/django/DPS/env/lib/python3.9/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y2   tdƒ‚Y n0 ||dœ}| jrN| 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$   )Z_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 ]–}|  di ¡}|  di ¡}|  dd ¡}|  dd ¡}|  dd ¡}|d u rb|  dd ¡}|d u rv|  dd ¡}|rŽ|rŽt|ƒ}t|ƒ}| t|||f|ƒ¡ q|S )	NZRESULTSZCOORDINATESZADDRESSÚlatitudeÚ	longitudeZaddress_returnedÚaddressZformatted_address)ÚgetÚfloatÚappendr	   )	r   ÚresultsÚplacesÚresultZcoordinatesr7   r5   r6   Z	placenamer   r   r   Ú_parse_code   s     zGeocodeFarm._parse_codec                 C   sX   |d u rd S |d }|   |¡ d| di ¡ dd¡v r:d S |  |¡}|rP|d S |S d S )NÚgeocoding_resultsÚ
NO_RESULTSÚSTATUSÚstatusÚ r   )Ú_check_for_api_errorsr8   r>   )r   Z
api_resultr   r?   r<   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.
        rA   r@   rB   rC   NÚSUCCESSÚaccess)ZAPI_KEY_INVALIDZOVER_QUERY_LIMIT)r8   r   r   r   )r   r?   Zstatus_resultZapi_call_successZaccess_errorZaccess_error_to_exceptionZexception_clsr   r   r   rD   ±   s    
þÿz!GeocodeFarm._check_for_api_errors)N)r'   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r.   r4   r>   r(   rD   Ú__classcell__r   r   r   r   r      s"    þ÷6$r   N)Ú	functoolsr   Úurllib.parser   Z	geopy.excr   r   r   Zgeopy.geocoders.baser   r   Zgeopy.locationr	   Z
geopy.utilr
   Ú__all__r   r   r   r   r   Ú<module>   s   