o
    ÔèFh™7  ã                   @   s†   d dl Zd dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlmZ d dlmZ dZd	Zd
ddde
fZG dd„ deƒZdS )é    N)Úpartial)Ú	urlencode)ÚConfigurationErrorÚGeocoderQueryError)Ú_DEFAULT_USER_AGENTÚDEFAULT_SENTINELÚGeocoder)ÚLocation)Úlogger)Ú	Nominatimznominatim.openstreetmap.orgzmy-applicationzmy_app/1zmy_user_agent/1.0Úspecify_your_app_name_herec                       s–   e Zd ZdZh d£ZdZdZeeeddeddœ‡ fdd„
Z	d	d
„ Z
dedddddddddddœdd„Zdedddddœdd„Zdd„ Zdd„ Z‡  ZS )r   aÉ  Nominatim geocoder for OpenStreetMap data.

    Documentation at:
        https://nominatim.org/release-docs/develop/api/Overview/

    .. attention::
       Using Nominatim with the default `user_agent` is strongly discouraged,
       as it violates Nominatim's Usage Policy
       https://operations.osmfoundation.org/policies/nominatim/
       and may possibly cause 403 and 429 HTTP errors. Please make sure
       to specify a custom `user_agent` with
       ``Nominatim(user_agent="my-application")`` or by
       overriding the default `user_agent`:
       ``geopy.geocoders.options.default_user_agent = "my-application"``.
       An exception will be thrown if a custom `user_agent` is not specified.
    >   ÚcityÚstateÚcountyÚstreetÚcountryÚ
postalcodez/searchz/reverseN)ÚtimeoutÚproxiesÚdomainÚschemeÚ
user_agentÚssl_contextÚadapter_factoryc                   s~   t ƒ j||||||d� | d¡| _| jtkr'| jd tv r'td| jd  ƒ‚d| j| j| j	f | _
d| j| j| jf | _dS )a  

        :param int timeout:
            See :attr:`geopy.geocoders.options.default_timeout`.

        :param dict proxies:
            See :attr:`geopy.geocoders.options.default_proxies`.

        :param str domain: Domain where the target Nominatim service
            is hosted.

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :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
        )r   r   r   r   r   r   ú/z
User-Agenta˜  Using Nominatim with default or sample `user_agent` "%s" is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"`.z	%s://%s%sN)ÚsuperÚ__init__Ústripr   Ú_DEFAULT_NOMINATIM_DOMAINÚheadersÚ_REJECTED_USER_AGENTSr   r   Úgeocode_pathÚapiÚreverse_pathÚreverse_api)Úselfr   r   r   r   r   r   r   ©Ú	__class__© úR/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/nominatim.pyr   5   s$   'ú	
øÿzNominatim.__init__c                 C   s   d  |t|ƒf¡S )aV  
        Construct geocoding request url.
        The method can be overridden in Nominatim-based geocoders in order
        to extend URL parameters.

        :param str base_api: Geocoding function base address - self.api
            or self.reverse_api.

        :param dict params: Geocoding params.

        :return: string URL.
        ú?)Újoinr   )r%   Úbase_apiÚparamsr(   r(   r)   Ú_construct_urlx   s   zNominatim._construct_urlTF)Úexactly_oner   ÚlimitÚaddressdetailsÚlanguageÚgeometryÚ	extratagsÚcountry_codesÚviewboxÚboundedÚfeaturetypeÚnamedetailsc                   s–  t |tjjƒr‡ fdd„| ¡ D ƒ}nd|i}| ddi¡ |r%d|d< n|dur9t|ƒ}|dk r5td	ƒ‚||d< |
rCˆ  |
d
¡|d< |rId|d< |	sMg }	t |	t	ƒrU|	g}	|	r^d 
|	¡|d< |rdd|d< |rjd|d< |rp||d< |rvd|d< |dur¦| ¡ }|dkr‡d|d< n|dkr�d|d< n|dkr™d|d< n|dkr¢d|d< ntdƒ‚|r¬||d< ˆ  ˆ j|¡}t dˆ jj|¡ tˆ j|d�}ˆ j|||d �S )!aõ  
        Return a location point by address.

        :param query: The address, query or a structured query
            you wish to geocode.

            For a structured query, provide a dictionary whose keys
            are one of: `street`, `city`, `county`, `state`, `country`, or
            `postalcode`. For more information, see Nominatim's
            documentation for `structured requests`:

                https://nominatim.org/release-docs/develop/api/Search

        :type query: dict or str

        :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 Nominatim.
            Unless exactly_one is set to False, limit will always be 1.

        :param bool addressdetails: If you want in *Location.raw* to include
            address details such as house_number, city_district, postcode, etc
            (in a structured form) set it to True

        :param str language: Preferred language in which to return results.
            Either uses standard
            `RFC2616 <http://www.ietf.org/rfc/rfc2616.txt>`_
            accept-language string or a simple comma-separated
            list of language codes.

        :param str geometry: If present, specifies whether the geocoding
            service should return the result's geometry in `wkt`, `svg`,
            `kml`, or `geojson` formats. This is available via the
            `raw` attribute on the returned :class:`geopy.location.Location`
            object.

        :param bool extratags: Include additional information in the result if available,
            e.g. wikipedia link, opening hours.

        :param country_codes: Limit search results
            to a specific country (or a list of countries).
            A country_code should be the ISO 3166-1alpha2 code,
            e.g. ``gb`` for the United Kingdom, ``de`` for Germany, etc.

        :type country_codes: str or list

        :type viewbox: list or tuple of 2 items of :class:`geopy.point.Point` or
            ``(latitude, longitude)`` or ``"%(latitude)s, %(longitude)s"``.

        :param viewbox: Prefer this area to find search results. By default this is
            treated as a hint, if you want to restrict results to this area,
            specify ``bounded=True`` as well.
            Example: ``[Point(22, 180), Point(-22, -180)]``.

        :param bool bounded: Restrict the results to only items contained
            within the bounding ``viewbox``.

        :param str featuretype: If present, restrict results to certain type of features.
            Allowed values: `country`, `state`, `city`, `settlement`.

        :param bool namedetails: If you want in *Location.raw* to include
            namedetails, set it to True. This will be a list of alternative names,
            including language variants, etc.

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

        c                    s    i | ]\}}|ˆ j v r||“qS r(   )Ústructured_query_params)Ú.0ÚkeyÚval©r%   r(   r)   Ú
<dictcomp>ä   s
    
ýz%Nominatim.geocode.<locals>.<dictcomp>ÚqÚformatÚjsoné   r0   NzLimit cannot be less than 1z#%(lon1)s,%(lat1)s,%(lon2)s,%(lat2)sr6   r7   ú,Úcountrycodesr1   r9   úaccept-languageTr4   ÚwktÚpolygon_textÚsvgÚpolygon_svgÚkmlÚpolygon_kmlÚgeojsonÚpolygon_geojsonz@Invalid geometry format. Must be one of: wkt, svg, kml, geojson.ÚfeatureTypez%s.geocode: %s©r/   ©r   )Ú
isinstanceÚcollectionsÚabcÚMappingÚitemsÚupdateÚintÚ
ValueErrorÚ_format_bounding_boxÚstrr+   Úlowerr   r.   r"   r
   Údebugr'   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r%   Úqueryr/   r   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r-   ÚurlÚcallbackr(   r>   r)   Úgeocode‡   sl   \
ýÿ
ÿ




ÿzNominatim.geocode)r/   r   r2   r1   Úzoomr9   c                C   s²   z|   |¡ d¡\}}	W n ty   tdƒ‚w ||	ddœ}
|r$||
d< |r(dnd|
d< |d	ur4||
d
< |r:d|
d< |  | j|
¡}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: Preferred language in which to return results.
            Either uses standard
            `RFC2616 <http://www.ietf.org/rfc/rfc2616.txt>`_
            accept-language string or a simple comma-separated
            list of language codes.

        :param bool addressdetails: Whether or not to include address details,
            such as city, county, state, etc. in *Location.raw*

        :param int zoom: Level of detail required for the address,
            an integer in range from 0 (country level) to 18 (building level),
            default is 18.

        :param bool namedetails: If you want in *Location.raw* to include
            namedetails, set it to True. This will be a list of alternative names,
            including language variants, etc.

            .. versionadded:: 2.3

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

        rD   z"Must be a coordinate pair or PointrB   )ÚlatÚlonrA   rF   rC   r   r1   Nre   r9   z%s.reverse: %srP   rQ   )Ú_coerce_point_to_stringÚsplitrY   r.   r$   r
   r]   r'   r^   r   r_   r`   )r%   ra   r/   r   r2   r1   re   r9   rf   rg   r-   rb   rc   r(   r(   r)   Úreverse+  s(   2ÿýzNominatim.reversec                 C   sT   |  dd ¡}|  dd ¡}|  dd ¡}|d ur"|d ur"t|ƒ}t|ƒ}t|||f|ƒS )Nrf   rg   Údisplay_name)ÚgetÚfloatr	   )r%   ÚplaceÚlatitudeÚ	longitudeÚ	placenamer(   r(   r)   Ú_parse_codev  s   zNominatim._parse_codec                    sr   |sd S t |tjjƒrd|v r|d dkrd S t|d ƒ‚t |tjjƒs'|g}|r0ˆ  |d ¡S ‡ fdd„|D ƒS )NÚerrorzUnable to geocoder   c                    s   g | ]}ˆ   |¡‘qS r(   )rr   )r;   rn   r>   r(   r)   Ú
<listcomp>�  s    z)Nominatim._parse_json.<locals>.<listcomp>)rR   rS   rT   rU   r   ÚSequencerr   )r%   Úplacesr/   r(   r>   r)   r_   €  s   zNominatim._parse_json)r^   Ú
__module__Ú__qualname__Ú__doc__r:   r!   r#   r   r   r   r.   rd   rj   rr   r_   Ú__classcell__r(   r(   r&   r)   r      sJ    	÷Cñ )÷K
r   )Úcollections.abcrS   Ú	functoolsr   Úurllib.parser   Ú	geopy.excr   r   Úgeopy.geocoders.baser   r   r   Úgeopy.locationr	   Ú
geopy.utilr
   Ú__all__r   r    r   r(   r(   r(   r)   Ú<module>   s     ú
