
    gB                         d dl Z d dlmc mZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ dZ G d	 d
e      Zy)    N)partial)	urlencode)GeocoderQueryError)DEFAULT_SENTINELGeocoder)Location)logger)	IGNFrancec                        e Zd ZdZdZdZ	 ddddddeededd
 fdZdd	d
ddeddZdd	ddeddZ		 	 	 ddZ
ddZd ZddZ xZS )r
   zGeocoder using the IGN France GeoCoder OpenLS API.

    Documentation at:
        https://geoservices.ign.fr/services-web-essentiels
    aK  <?xml version="1.0" encoding="UTF-8"?>
    <XLS version="1.2"
        xmlns="http://www.opengis.net/xls"
        xmlns:gml="http://www.opengis.net/gml"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.opengis.net/xls
        http://schemas.opengis.net/ols/1.2/olsAll.xsd">
        <RequestHeader srsName="epsg:4326"/>
        <Request methodName="{method_name}"
                 maximumResponses="{maximum_responses}"
                 requestID=""
                 version="1.2">
            {sub_request}
        </Request>
    </XLS>z/essentiels/geoportail/olsNz
wxs.ign.fr)
usernamepasswordrefererdomainschemetimeoutproxies
user_agentssl_contextadapter_factoryc       
             t         |   ||||	|
|       |s|s|s|rt        j                  dt        d       |j                  d      | _        | j                  }| j                  d| j                  || _	        y)a=  

        :param str api_key: Not used.

            .. deprecated:: 2.3
                IGNFrance geocoding methods no longer accept or require
                authentication, see `<https://geoservices.ign.fr/actualites/2021-10-04-evolution-des-modalites-dacces-aux-services-web>`_.
                This parameter is scheduled for removal in geopy 3.0.

        :param str username: Not used.

            .. deprecated:: 2.3
                See the `api_key` deprecation note.

        :param str password: Not used.

            .. deprecated:: 2.3
                See the `api_key` deprecation note.

        :param str referer: Not used.

            .. deprecated:: 2.3
                See the `api_key` deprecation note.

        :param str domain: Currently it is ``'wxs.ign.fr'``, can
            be changed for testing purposes for developer API
            e.g ``'gpp3-wxs.ign.fr'`` at the moment.

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

        :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
        )r   r   r   r   r   r   zIGNFrance no longer accepts or requires authentication, so api_key, username, password and referer are not used anymore. These arguments should be removed. In geopy 3 these options will be removed, causing an error instead of this warning.   )
stacklevel/z://N)
super__init__warningswarnDeprecationWarningstripr   api_pathr   api)selfapi_keyr   r   r   r   r   r   r   r   r   r   r    	__class__s                Y/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/ignfrance.pyr   zIGNFrance.__init__'   s    @ 	!#+ 	 	
 h(gMM4
 # ll3'=="&++t{{HE    StreetAddress   FT)
query_typemaximum_responsesis_freeform	filteringexactly_oner   c                   |dvrt        d      |dk(  r't        |j                               dk7  rt        d      d}| j                  j	                  d||      }	|rd	}nd
}|d}|	j	                  ||||      }
d|
i}dj                  | j                  t        |      f      }t        j                  d| j                  j                  |       t        | j                  ||      }| j                  |||      S )a>  
        Return a location point by address.

        :param str query: The query string to be geocoded.

        :param str query_type: The type to provide for geocoding. It can be
            `PositionOfInterest`, `StreetAddress` or `CadastralParcel`.
            `StreetAddress` is the default choice if none provided.

        :param int maximum_responses: The maximum number of responses
            to ask to the API in the query body.

        :param str is_freeform: Set if return is structured with
            freeform structure or a more structured returned.
            By default, value is False.

        :param str filtering: Provide string that help setting geocoder
            filter. It contains an XML string. See examples in documentation
            and ignfrance.py file in directory tests.

        :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``.

        )PositionOfInterestr'   CadastralParcelzYou did not provided a query_type the
            webservice can consume. It should be PositionOfInterest,
            'StreetAddress or CadastralParcelr0      zfYou must send a string of fourteen
                characters long to match the cadastre required codea*  
                <GeocodeRequest returnFreeForm="{is_freeform}">
                    <Address countryCode="{query_type}">
                        <freeFormAddress>{query}</freeFormAddress>
                        {filtering}
                    </Address>
                </GeocodeRequest>
        LocationUtilityServicemethod_namesub_requestr*   truefalse )r+   queryr)   r,   xls?z%s.geocode: %s)r+   r-   r   )r   lenr   xml_requestformatjoinr!   r   r	   debugr$   __name__r   
_parse_xml_request_raw_content)r"   r9   r)   r*   r+   r,   r-   r   r5   r>   request_stringparamsurlcallbacks                 r%   geocodezIGNFrance.geocode   s:   Z  1 1 % &1 2 2
 **s5;;=/AR/G$ &G H H &&--0#/ . 
  K!K I %++#!	 , 
 >
 hh)F"345%t~~'>'>DOO+
 ((h(HHr&   )r'   r8   )reverse_geocode_preferencer*   r,   r-   r   c                   d}| j                   j                  d||      }|D ]  }	|	dvst        d       | j                  |d      }
dj	                  d |D              }|j                  ||
||	      }d
j	                  | j
                  t        d|i      f      }t        j                  d| j                  j                  |       t        | j                  |dd      }| j                  |||      S )aU  
        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 list reverse_geocode_preference: Enable to set expected results
            type. It can be `StreetAddress` or `PositionOfInterest`.
            Default is set to `StreetAddress`.

        :param int maximum_responses: The maximum number of responses
            to ask to the API in the query body.

        :param str filtering: Provide string that help setting geocoder
            filter. It contains an XML string. See examples in documentation
            and ignfrance.py file in directory tests.

        :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``.

        a@  
            <ReverseGeocodeRequest>
                {reverse_geocode_preference}
                <Position>
                  <gml:Point>
                    <gml:pos>{query}</gml:pos>
                  </gml:Point>
                  {filtering}
                </Position>
            </ReverseGeocodeRequest>
        ReverseGeocodeRequestr3   )r'   r/   z[`reverse_geocode_preference` must contain one or more of: StreetAddress, PositionOfInterestz%(lat)s %(lon)s
c              3   &   K   | ]	  }d |z    yw)z7<ReverseGeocodePreference>%s</ReverseGeocodePreference>N ).0prefs     r%   	<genexpr>z$IGNFrance.reverse.<locals>.<genexpr>)  s#      /
 *  FL)s   )r*   r9   rJ   r,   r;   r:   z%s.reverse: %sTr7   )r-   
is_reverser+   r<   )r>   r?   r   _coerce_point_to_stringr@   r!   r   r	   rA   r$   rB   r   rC   rD   )r"   r9   rJ   r*   r,   r-   r   r5   r>   rQ   pointrE   rG   rH   s                 r%   reversezIGNFrance.reverse   s    T
 &&--/#/ . 
 /DBB(H  / ,,U4EF%)YY /
 */
 &
" %++/'A	 , 
 hh)UN,C"DEF%t~~'>'>DOO#	
 ((h(HHr&   c                 .   t        j                  |j                  d            }d } ||d        ||d        ||d       | j                  ||      }|sy|r| j	                  |d   |	      S |D cg c]  }| j	                  ||	       c}S c c}w )
ze
        Returns location, (latitude, longitude) from XML feed
        and transform to json
        zutf-8c                     d|z  }t        |      }| j                         D ]2  }|j                  j                  |      s|j                  |d |_        4 y)z*Remove namespace in the document in place.z{%s}N)r=   itertag
startswith)doc	namespacensnslelems        r%   remove_namespacez.IGNFrance._parse_xml.<locals>.remove_namespaceN  sH    )#Bb'C
88&&r*#xx~DH #r&   zhttp://www.opengis.net/gmlzhttp://www.opengis.net/xlszhttp://www.opengis.net/xlsext)rS   Nr   )r+   )ET
fromstringencode_xml_to_json_places_parse_place)	r"   pagerS   r+   r-   treera   placesplaces	            r%   rC   zIGNFrance._parse_xmlA  s     }}T[[12	. 	;<;<>? ))$:)F$$VAYK$HH  &	  &e !! + "   &	  s   5Bc                 &   |sdnd}|j                  d|z         }g }d}|D ]l  }i }|j                  d      |d<   |j                  d      |d<   |j                  d	      |d
<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d<   |j                  |j                  d            |d <   |j                  |j                  d!            |d"<   |j                  |j                  d#            |d$<   |j                  |j                  d%            |d&<   |j                  d'      |d(<   |j                  d)      |d*<   i }	d+ }
 |
|j                  d,      d-      |	d-<    |
|j                  d,      d.      |	d/<    |
|j                  d0      d1      |	d2<    |
|j                  d3      d4      |	d5<   t        |j	                               D ]  \  }}||j
                  |	|<   d6|	|<    |	d   r>|	d   j                  d7      \  }}|j                         |	d8<   |j                         |	d9<   n
d6x|	d8<   |	d9<   |	j                  dd6       |j                  |	       o |S ):zT
        Transform the xml ElementTree due to XML webservice return to json
        GeocodedAddressReverseGeocodedLocationz.//z.//Address/Place[@type="{}"]z./Point/posposz.//Address/StreetAddress/Streetstreetz.//Address/freeFormAddressfreeformaddressMunicipalitymunicipalityNumeronumeroFeuillefeuilleSectionsectionDepartementdepartementCommuneAbsorbeecommune_absorbeeCommunecommuneINSEEinseeQualitequalite
Territoire
territoireIDidID_TRid_trBboxbboxNaturenaturez.//Address/PostalCodepostal_codez.//ExtendedGeocodeMatchCodeextended_geocode_match_codec                 B    | | j                   j                  |d      S dS )z
                Helper to select by attribute and if not attribute,
                value set to empty string
                N)attribget)selectorkeys     r%   testContentAttribz8IGNFrance._xml_to_json_places.<locals>.testContentAttrib  s3     )  ** 4 044r&   z.//GeocodeMatchCodeaccuracy	matchType
match_typez!.//Address/StreetAddress/Buildingnumberbuildingz.//SearchCentreDistancevaluesearch_centre_distanceN latlng)
findallfindr?   rY   itemstextsplitr   popappend)r"   rh   rS   select_multiadressesri   sel_pladrelrj   r   r   r   r   r   s                  r%   re   zIGNFrance._xml_to_json_placesi  sc     * 	 << 45/CB/BuI88$EFBxL$'HH-I$JB !!$&--*G!HB~88FMM($;<BxLHHV]]9%=>ByMHHV]]9%=>ByM #})E FB}%(XXfmm<M.N%OB!"HHV]]9%=>ByM((6==#9:BwKHHV]]9%=>ByM"xxl(CDB|xxd 34BtH((6==#9:BwK&--"78BvJ88FMM($;<BxL #)@ AB}03-1B,- E4 !2./!=E* #4./#>E, !2<=x!IE* /@23W/>E*+ #288:.
U$!&E#J!%E#J	 / U| <--c2S"yy{e"yy{e.22euU| IIeT"MM% A D r&   c                ,    | j                  |||d      S )z6
        Send the request to get raw content.
        F)r   is_json)_call_geocoder)r"   rG   rH   r   s       r%   rD   zIGNFrance._request_raw_content  s(     ""	 # 
 	
r&   c                    |dk(  r|j                  d      }n|j                  d      r|j                  d      }nw|j                  dd      d|j                  dd      }|j                  d      r|j                  dd      d	|}|j                  d
      r|j                  d
d      d|}t        ||j                  d      |j                  d      f|      S )zP
        Get the location, lat, lng and place from a single json place.
        r6   rp   rt   ro   r   r8   r   r~   z, r   r   r   )r   r   )r"   rj   r+   locations       r%   rf   zIGNFrance._parse_place  s    
 & yy!23H yy" 99X.
 IImR0IIi, 99X&		(B/  H 99Z(		*b1  H
 599U#3UYYu5E"FNNr&   )N)FFT)F)rB   
__module____qualname____doc__r>   r    r   r   rI   rV   rC   re   rD   rf   __classcell__)r$   s   @r%   r
   r
      s    K  ,H VF $$( VFx ' $dIT (; $ZI| $$#	&PQf	
Or&   r
   )r   xml.etree.ElementTreeetreeElementTreerb   	functoolsr   urllib.parser   	geopy.excr   geopy.geocoders.baser   r   geopy.locationr   
geopy.utilr	   __all__r
   rO   r&   r%   <module>r      s5     " "  " ( ; # 
VO VOr&   