a
    }c1                     @   s   d dl mZ d dl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mZ d dlmZ dZG d	d
 d
eZdS )    )partial)	urlencode)GeocoderAuthenticationFailureGeocoderInsufficientPrivilegesGeocoderQueryErrorGeocoderQuotaExceededGeocoderServiceError)DEFAULT_SENTINELGeocoder)Location)ensure_pytz_is_installedfrom_fixed_gmt_offsetfrom_timezone_name)logger)GeoNamesc                       s   e Zd ZdZdZdZdZdZeededdd fd	d

Z	dedddddZ
deddddddZdd Zdd ZedddZdd Zdd Zdd Z  ZS ) r   zGeoNames geocoder.

    Documentation at:
        http://www.geonames.org/export/geonames-search.html

    Reverse geocoding documentation at:
        http://www.geonames.org/export/web-services.html#findNearbyPlaceName
    z/searchJSONz/findNearbyPlaceNameJSONz/findNearbyJSONz/timezoneJSONNhttp)timeoutproxies
user_agentssl_contextadapter_factoryschemec          	         sv   t  j||||||d || _d}d| j|| jf | _d| j|| jf | _d| j|| jf | _	d| j|| j
f | _dS )ax  

        :param str username: GeoNames username, required. Sign up here:
            http://www.geonames.org/login

        :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`. Note that
            at the time of writing GeoNames doesn't support `https`, so
            the default scheme is `http`. The value of
            :attr:`geopy.geocoders.options.default_scheme` is not respected.
            This parameter is present to make it possible to switch to
            `https` once GeoNames adds support for it.
        )r   r   r   r   r   r   zapi.geonames.orgz	%s://%s%sN)super__init__usernamer   geocode_pathapireverse_pathapi_reversereverse_nearby_pathapi_reverse_nearbytimezone_pathapi_timezone)	selfr   r   r   r   r   r   r   domain	__class__ T/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopy/geocoders/geonames.pyr   &   s$    *zGeoNames.__init__T)exactly_oner   countrycountry_biasc          
      C   s   d|fd| j fg}|r$|d|f |s,g }t|tr<|g}|D ]}|d|f q@|rb|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.

        :param country: Limit records to the specified countries.
            Two letter country code ISO-3166 (e.g. ``FR``). Might be
            a single string or a list of strings.
        :type country: str or list

        :param str country_bias: Records from the country_bias are listed first.
            Two letter country code ISO-3166.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        qr   ZcountryBiasr*   )ZmaxRows   ?z%s.geocode: %sr)   r   )r   append
isinstancestrjoinr   r   r   debugr&   __name__r   _parse_json_call_geocoder)
r#   queryr)   r   r*   r+   paramsZcountry_itemurlcallbackr'   r'   r(   geocodeh   s"    "

zGeoNames.geocodefindNearbyPlaceName)r)   r   feature_codelangfind_nearby_typec                C   s   z|  |d\}}W n ty2   tdY n0 |dkrn|rHtd| j|||d}	d| jt|	f}
nF|dkr|rtd| j|||d	}	d| jt|	f}
nt	d
| 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 feature_code: A GeoNames feature code

        :param str lang: language of the returned ``name`` element (the pseudo
            language code 'local' will return it in local language)
            Full list of supported languages can be found here:
            https://www.geonames.org/countries/

        :param str find_nearby_type: A flag to switch between different
            GeoNames API endpoints. The default value is ``findNearbyPlaceName``
            which returns the closest populated place. Another currently
            implemented option is ``findNearby`` which returns
            the closest toponym for the lat/lng query.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        ,"Must be a coordinate pair or Pointr>   zMfind_nearby_type=findNearbyPlaceName doesn't support the `feature_code` param)latlngr@   r.   Z
findNearbyz<find_nearby_type=findNearby doesn't support the `lang` param)rD   rE   r?   z/`%s` find_nearby_type is not supported by geopyz%s.reverse: %sr/   r0   )_coerce_point_to_stringsplit
ValueError&_reverse_find_nearby_place_name_paramsr4   r   r   _reverse_find_nearby_paramsr    r   r   r5   r&   r6   r   r7   r8   )r#   r9   r)   r   r?   r@   rA   rD   rE   r:   r;   r<   r'   r'   r(   reverse   s@    ,zGeoNames.reversec                 C   s   ||| j d}|r||d< |S )NrD   rE   r   ZfeatureCoder   )r#   rD   rE   r?   r:   r'   r'   r(   rJ      s    z$GeoNames._reverse_find_nearby_paramsc                 C   s   ||| j d}|r||d< |S )NrL   r@   rM   )r#   rD   rE   r@   r:   r'   r'   r(   rI      s    z/GeoNames._reverse_find_nearby_place_name_paramsr0   c                C   s   t   z| |d\}}W n ty8   tdY n0 ||| jd}d| jt|f}t	d| j
j| | j|| j|dS )aO  
        Find the timezone for a point in `query`.

        GeoNames always returns a timezone: if the point being queried
        doesn't have an assigned Olson timezone id, a ``pytz.FixedOffset``
        timezone is used to produce the :class:`geopy.timezone.Timezone`.

        :param query: The coordinates for which you want a timezone.
        :type query: :class:`geopy.point.Point`, list or tuple of (latitude,
            longitude), or string as "%(latitude)s, %(longitude)s"

        :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: :class:`geopy.timezone.Timezone`.
        rB   rC   rL   r.   z%s.reverse_timezone: %sr0   )r   rF   rG   rH   r   r4   r"   r   r   r5   r&   r6   r8   _parse_json_timezone)r#   r9   r   rD   rE   r:   r;   r'   r'   r(   reverse_timezone  s    zGeoNames.reverse_timezonec                 C   s\   | d}|rX|d }|d }|dr0t||dkr@t||dv rPt|t|d S )Nstatusvaluemessagezuser account not enabled to use
   )         )get
startswithr   r   r   r   )r#   bodyerrcoderR   r'   r'   r(   _raise_for_error)  s    

zGeoNames._raise_for_errorc                 C   s@   |  | |d}|d u r0|d }t||dS t||dS d S )NZ
timezoneIdZ	rawOffset)raw)r\   rW   r   r   )r#   responseZtimezone_idZ
raw_offsetr'   r'   r(   rN   7  s    

zGeoNames._parse_json_timezonec                    sP   | dg }| | t|s"dS dd  |r: |d S  fdd|D S dS )z+
        Parse JSON response body.
        ZgeonamesNc                 S   s   |  dd}|  dd}|r2|r2t|}t|}ndS |  d}|  dd}|  dd}ddd	 |||fD }t|||f| S )
z,
            Parse each record.
            rD   NrE   nameZ
adminName1ZcountryNamez, c                 S   s   g | ]}|r|qS r'   r'   ).0xr'   r'   r(   
<listcomp>_      z<GeoNames._parse_json.<locals>.parse_code.<locals>.<listcomp>)rW   floatr4   r   )placeZlatitudeZ	longitudeZ	placenamestater*   locationr'   r'   r(   
parse_codeN  s    

z(GeoNames._parse_json.<locals>.parse_coder   c                    s   g | ]} |qS r'   r'   )r`   re   rh   r'   r(   rb   g  rc   z(GeoNames._parse_json.<locals>.<listcomp>)rW   r\   len)r#   docr)   Zplacesr'   ri   r(   r7   E  s    
zGeoNames._parse_json)r6   
__module____qualname____doc__r   r   r   r!   r	   r   r=   rK   rJ   rI   rO   r\   rN   r7   __classcell__r'   r'   r%   r(   r      s:   	F;Q

%r   N)	functoolsr   urllib.parser   Z	geopy.excr   r   r   r   r   Zgeopy.geocoders.baser	   r
   Zgeopy.locationr   Zgeopy.timezoner   r   r   Z
geopy.utilr   __all__r   r'   r'   r'   r(   <module>   s   