o
    4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__ N/var/www/html/gps/gps/lib/python3.10/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 |r1|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   countryBiasr*   )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+   params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y   tdw |dkr5|r"td| j|||d}	d| jt|	f}
n#|dkrR|r?t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 PointrA   zMfind_nearby_type=findNearbyPlaceName doesn't support the `feature_code` param)latlngrC   r0   
findNearbyz<find_nearby_type=findNearby doesn't support the `lang` param)rG   rH   rB   z/`%s` find_nearby_type is not supported by geopyz%s.reverse: %sr1   r2   )_coerce_point_to_stringsplit
ValueError&_reverse_find_nearby_place_name_paramsr6   r   r   _reverse_find_nearby_paramsr    r   r   r7   r&   r8   r   r9   r:   )r#   r;   r)   r   rB   rC   rD   rG   rH   r<   r>   r?   r'   r'   r(   reverse   sB   ,zGeoNames.reversec                 C      ||| j d}|r||d< |S )NrG   rH   r   featureCoder   )r#   rG   rH   rB   r<   r'   r'   r(   rN         z$GeoNames._reverse_find_nearby_paramsc                 C   rP   )NrQ   rC   rS   )r#   rG   rH   rC   r<   r'   r'   r(   rM      rT   z/GeoNames._reverse_find_nearby_place_name_paramsr2   c                C   s|   t   z| |d\}}W n ty   tdw ||| 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`.
        rE   rF   rQ   r0   z%s.reverse_timezone: %sr2   )r   rJ   rK   rL   r   r6   r"   r   r   r7   r&   r8   r:   _parse_json_timezone)r#   r;   r   rG   rH   r<   r>   r'   r'   r(   reverse_timezone  s   zGeoNames.reverse_timezonec                 C   s\   | d}|r,|d }|d }|drt||dkr t||dv r(t|t|d S )Nstatusvaluemessagezuser account not enabled to use
   )         )get
startswithr   r   r   r   )r#   bodyerrcoderY   r'   r'   r(   _raise_for_error)  s   

zGeoNames._raise_for_errorc                 C   s<   |  | |d}|d u r|d }t||dS t||dS )N
timezoneId	rawOffset)raw)rc   r^   r   r   )r#   responsetimezone_id
raw_offsetr'   r'   r(   rU   7  s   

zGeoNames._parse_json_timezonec                    sL   | dg }| | t|sdS dd  |r |d S  fdd|D S )z+
        Parse JSON response body.
        geonamesNc                 S   s   |  dd}|  dd}|r|rt|}t|}ndS |  d}|  dd}|  dd}ddd	 |||fD }t|||f| S )
z,
            Parse each record.
            rG   NrH   name
adminName1countryNamez, c                 S   s   g | ]}|r|qS r'   r'   ).0xr'   r'   r(   
<listcomp>_      z<GeoNames._parse_json.<locals>.parse_code.<locals>.<listcomp>)r^   floatr6   r   )placelatitude	longitude	placenamestater*   locationr'   r'   r(   
parse_codeN  s   

z(GeoNames._parse_json.<locals>.parse_coder   c                    s   g | ]} |qS r'   r'   )rn   rs   ry   r'   r(   rp   g  rq   z(GeoNames._parse_json.<locals>.<listcomp>)r^   rc   len)r#   docr)   placesr'   rz   r(   r9   E  s   
zGeoNames._parse_json)r8   
__module____qualname____doc__r   r   r   r!   r	   r   r@   rO   rN   rM   rV   rc   rU   r9   __classcell__r'   r'   r%   r(   r      s<    	F;Q

%r   N)	functoolsr   urllib.parser   	geopy.excr   r   r   r   r   geopy.geocoders.baser	   r
   geopy.locationr   geopy.timezoner   r   r   
geopy.utilr   __all__r   r'   r'   r'   r(   <module>   s    