
    gS0                         d dl Z d dlmZ 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mZ d dlmZ d dlmZ d	Zd
Z G d de      Zy)    N)partial)time)	urlencode)ConfigurationErrorGeocoderAuthenticationFailureGeocoderServiceError)DEFAULT_SENTINELGeocoder_synchronized)Location)logger)ArcGISi  c                        e Zd ZdZdZdZdZdZ	 	 ddddeededdd	d

 fdZ	dedddZ
d ZdedddZd ZeddZed        Z xZS )r   zGeocoder using the ERSI ArcGIS API.

    Documentation at:
        https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm
    i  z/sharing/generateTokenz?/arcgis/rest/services/World/GeocodeServer/findAddressCandidatesz8/arcgis/rest/services/World/GeocodeServer/reverseGeocodeN<   zwww.arcgis.comzgeocode.arcgis.com)
referertoken_lifetimeschemetimeoutproxies
user_agentssl_contextadapter_factoryauth_domaindomainc       
         L   t         |   |||||	|
       |s|s|r+|r|r|st        d      | j                  dk7  rt        d      || _        || _        || _        |j                  d      | _        | j                  d| j                  | j                  | _
        |dz  | _        |j                  d      | _        | j                  d| j                  | j                  | _        | j                  d| j                  | j                  | _        d| _        d| _        y)	a  

        :param str username: ArcGIS username. Required if authenticated
            mode is desired.

        :param str password: ArcGIS password. Required if authenticated
            mode is desired.

        :param str referer: Required if authenticated mode is desired.
            `Referer` HTTP header to send with each request,
            e.g., ``'http://www.example.com'``. This is tied to an issued token,
            so fielding queries for multiple referrers should be handled by
            having multiple ArcGIS geocoder instances.

        :param int token_lifetime: Desired lifetime, in minutes, of an
            ArcGIS-issued token.

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.
            If authenticated mode is in use, it must be ``'https'``.

        :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 auth_domain: Domain where the target ArcGIS auth service
            is hosted. Used only in authenticated mode (i.e. username,
            password and referer are set).

        :param str domain: Domain where the target ArcGIS service
            is hosted.
        )r   r   r   r   r   r   z;Authenticated mode requires username, password, and refererhttpsz-Authenticated mode requires scheme of 'https'/z://r   N)super__init__r   r   usernamepasswordr   stripr   	auth_pathauth_apir   r   geocode_pathapireverse_pathreverse_apitokentoken_expiry)selfr    r!   r   r   r   r   r   r   r   r   r   r   	__class__s                V/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/arcgis.pyr   zArcGIS.__init__!   s$   | 	!#+ 	 	
 x7g(-  {{g%(C  ! &,,S1;;(8(8$..I 	 -r1ll3';;T5F5FG 	  ;;T5F5FG 	
 
     T)exactly_oner   
out_fieldsc                j   |dd}|rd|d<   |*t        |t              r||d<   ndj                  |      |d<   dj                  | j                  t	        |      f      }t        j                  d| j                  j                  |       t        | j                  |	      }| j                  |||
      S )am  
        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 out_fields: A list of output fields to be returned in the
            attributes field of the raw data. This can be either a python
            list/tuple of fields or a comma-separated string. See
            https://developers.arcgis.com/rest/geocode/api-reference/geocoding-service-output.htm
            for a list of supported output fields. If you want to return all
            supported output fields, set ``out_fields="*"``.
        :type out_fields: str or iterable

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        json)
singleLinef   maxLocations	outFields,?z%s.geocode: %sr/   r   )
isinstancestrjoinr&   r   r   debugr,   __name__r   _parse_geocode_authenticated_call_geocoder)r+   queryr/   r   r0   paramsurlcallbacks           r-   geocodezArcGIS.geocode   s    4 !&F3%&F>"!*c*&0{#&)hhz&:{#hh)F"345%t~~'>'>D4..KH00h0PPr.   c           	          d|v rt        t        |d               t        |d         sy g }|d   D ].  }|d   }|j                  t	        |d   |d   |d   f|             0 |r|d   S |S )Nerror
candidateslocationaddressyxr   )r   r=   lenappendr   )r+   responser/   geocodedresourcegeometrys         r-   rA   zArcGIS._parse_geocode   s    h&s8G+<'=>> 8L)* .H
+HOOY'(3-#)G / A;r.   )r/   r   distancec                D   | j                  |d      }t        }|d|d}|||d<   dj                  | j                  t	        |      f      }t        j                  d| j                  j                  |       t        | j                  |      }	| j                  ||	|      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 int distance: Distance from the query location, in meters,
            within which to search. ArcGIS has a default of 100 meters, if not
            specified.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        z%(lon)s,%(lat)sr2   )rK   r4   outSRrU   r9   z%s.reverse: %sr:   r;   )_coerce_point_to_stringDEFAULT_WKIDr>   r(   r   r   r?   r,   r@   r   _parse_reverserB   )
r+   rC   r/   r   rU   rK   wkidrD   rE   rF   s
             r-   reversezArcGIS.reverse   s    2 //7HI&VdC!)F:hh(()F*;<=%t~~'>'>D4..KH00h0PPr.   c                 D   t        |      sy d|v r2|d   d   dk(  r	 d|d   d   d   v ry 	 t        t	        |d               |d   j                  d      r	d	|d   z  }n|d   d
   }t        ||d   d   |d   d   f|d         }|r|S |gS # t        t        f$ r Y rw xY w)NrI   codei  zUnable to finddetailsr   rL   Addressz=%(Address)s, %(City)s, %(Region)s %(Postal)s, %(CountryCode)s	LongLabelrK   rM   rN   )rO   KeyError
IndexErrorr   r=   getr   )r+   rQ   r/   rL   rK   s        r-   rZ   zArcGIS._parse_reverse   s    8}h (C/'8G+<Y+G+JJ# K 's8G+<'=>>I""9-#%-i%89 
 y)+6Gj!#&(<S(ABY

 O:) !*- s   B BBr;   c                      j                   s j                        S  fd} fd fd j                   t        t	                      j
                  kD  r j                  | j                        S  |       S )Nr;   c                      dj                  t        dj                  i      f      } dj                  i}j	                  | t        j                        |      S )N&r)   Referer)
from_tokenr   headers)r>   r   r)   r   _call_geocoderr   )call_urlrk   maybe_reauthenticate_callbackr+   r   rE   s     r-   query_callbackz;ArcGIS._authenticated_call_geocoder.<locals>.query_callback  sb    xxi$**0E&F GHH $,,/G&&5$**M	 '  r.   c                n    d| v r)| d   d   j                   k(  rj                  |      S  |       S )NrI   r^   r   ri   )_TOKEN_EXPIRED_refresh_authentication_token)rQ   ri   parse_callbackquery_retry_callbackr+   r   s     r-   rn   zJArcGIS._authenticated_call_geocoder.<locals>.maybe_reauthenticate_callback  sO    ("G$V,0C0CC==,g* >   "(++r.   c                      dj                  t        dj                  i      f      } dj                  i}j	                  | |      S )Nrg   r)   rh   rj   )r>   r   r)   r   rl   )rm   rk   rt   r+   r   rE   s     r-   ru   zAArcGIS._authenticated_call_geocoder.<locals>.query_retry_callback  sS    xxi$**0E&F GHH $,,/G&&.'7 '  r.   rq   )r    rl   r)   intr   r*   rs   )r+   rE   rt   r   ro   rn   ru   s   ```` @@r-   rB   z#ArcGIS._authenticated_call_geocoder  s     }}&&sNG&LL		,	 ::TVt/@/@!@55DJJ 6   "##r.   c                l    | j                   k7  r        S  j                   j                   j                   j                  dd}dj                   j                  t        |      f      t        j                  d j                  j                          fd} j                  ||      S )Nr2   )r    r!   r   
expirationr4   r9   z$%s._refresh_authentication_token: %sc                     d| vr$t        ddt        j                  |             | d   _        t	        t                     j                  z   _                S )Nr)   z+Missing token in auth request.Request URL: z; response JSON: )r   r2   dumpsr)   rw   r   r   r*   )rQ   callback_successr+   rE   s    r-   cbz0ArcGIS._refresh_authentication_token.<locals>.cb>  s\    h&3<?HAUW  "'*DJ #DFd.A.A AD#%%r.   r;   )r)   r    r!   r   r   r>   r$   r   r   r?   r,   r@   rl   )r+   r|   r   ri   token_request_argumentsr}   rE   s   ``    @r-   rs   z$ArcGIS._refresh_authentication_token+  s    ##%% ||--#
 hhy1H'IJK2NN##S	

	& ""3G"<<r.   )NN)r@   
__module____qualname____doc__rr   r#   r%   r'   r	   r   rG   rA   r\   rZ   rB   r   rs   __classcell__)r,   s   @r-   r   r      s     N(ITLML e!
 $$( ('e!N -1:J%QN& -1:J!QFB /?$$L = =r.   r   )r2   	functoolsr   r   urllib.parser   	geopy.excr   r   r   geopy.geocoders.baser	   r
   r   geopy.locationr   
geopy.utilr   __all__rY   r    r.   r-   <module>r      sB       " 
 K J # 
t=X t=r.   