
    \i!                        S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJrJr  S\S\S-  4S jrS\\\
4   S\S-  4S jrS\\\
4   S\S\S\S\4
S jr   S"S\\\
4   S\S-  S\S-  S\S-  S\4
S jjrS\S\4S jrS\\\
4   S\4S jrS r S#S jrS$S\\-  S-  S\\\
4   4S jjr         S%S\S-  S\S-  S\S-  S\S-  S\S\S \S\\-  S-  S\!4S! jjr"g)&z
Based on the logic in the PROJ projsync CLI program

https://github.com/OSGeo/PROJ/blob/9ff543c4ffd86152bc58d0a0164b2ce9ebbb8bec/src/apps/projsync.cpp
    N)datetime)partial)Path)Any)urlretrieve)get_proj_endpoint)BBox)get_data_dirget_user_data_dircoordsreturnc                     [        U 6 u  p[        [        U5      [        U5      [        U5      [        U5      S9$ ! [         a     Of = fSnU  H  n[        U5      nUc  M  Uc  UnM  [        UR                  UR                  5      Ul        [        UR                  UR                  5      Ul        [        UR                  UR                  5      Ul        [        UR                  UR                  5      Ul	        M     U$ )z'
Get the bounding box from coordinates
)westsoutheastnorthN)
zipr	   minmax
ValueError_bbox_from_coordsr   r   r   r   )r   xxxyyy
coord_bbox	coord_setbboxs         N/var/www/html/kml_chatgpt/mouzaenv/lib/python3.13/site-packages/pyproj/sync.pyr   r      s    <SSCCQQ J	 +<J!*//499=JO":#3#3TZZ@J":#3#3TZZ@J!*//499=JO  s   9< 
A	A	geomc                    SU ;  d  SU ;  a  gU S   nU S   S:w  a  [        U5      $ SnSn/ nU HI  n[        U5      nUc  M  UR                  S:X  a  SnOUR                  S:X  a  SnUR                  U5        MK     SnU H  nU(       a3  U(       a,  UR                  S:X  a  SUl        U=R                  S	-  sl        Uc  UnMD  [	        UR                  UR                  5      Ul        [	        UR
                  UR
                  5      Ul        [        UR                  UR                  5      Ul        [        UR                  UR                  5      Ul        M     U$ )
z,
Get the bounding box from geojson geometry
coordinatestypeNMultiPolygonFLT   h  )r   r   r   appendr   r   r   r   )r   r    found_minus_180found_plus_180bboxescoordinate_setr   	grid_bboxs           r   _bbox_from_geomr,   -   s1    D F$$6}%KF|~% --ONF% 0<99"OYY#!Nd & I~$))t2CDIIIII ;IN!)//4::>IO!)//4::>IO ;IN      featurer   spatial_testinclude_world_coveragec                    U R                  S5      nUb  [        U5      nUc  gUR                  UR                  -
  S:  aG  UR                  UR
                  -
  S:  a*  U(       d  g[        S5      * Ul        [        S5      Ul        OJUR                  S:  a:  UR                  S:  a*  U=R                  S-  sl        U=R                  S-  sl        [        X5      " U5      $ g)	z;
Filter by the bounding box. Designed to use with 'filter'
geometryFig     infr$   r#   r%   )getr,   r   r   r   r   floatgetattr)r.   r   r/   r0   r   	geom_bboxs         r   _filter_bboxr9   Q   s     ;;z"D#D)	NNY^^+c1)//1C7)#El]IN"5\IN^^c!dii$&6NNc!NNNc!Nt*955r-   	source_idarea_of_usefilenamec                 >   U R                  S5      nU(       d  gUR                  S5      nUR                  S5      nU(       a  U(       d  gUSL =(       d    X;   nUSL =(       d    X$R                  SS5      ;   nUSL =(       d    X5;   n	U(       a  U(       a  U	(       a  gg)	z9
Filter by the properties. Designed to use with 'filter'

propertiesFnamer:   Nr;    T)r5   )
r.   r:   r;   r<   r>   
p_filenamep_source_idsource_id__matchedarea_of_use__matchedfilename__matcheds
             r   _filter_propertiesrF   k   s     \*J'J..-K["d*Fi.F&$. +rB 3 !D(BH,B27Hr-   	grid_namec                     [        [        5       U 5      R                  5       (       a  g[        5       R	                  [
        R                  5       H#  n[        X5      R                  5       (       d  M#    g   g)zL
Run through all of the PROJ directories to see if the
file already exists.
FT)r   r   existsr
   splitospathsep)rG   data_dirs     r   _is_download_neededrN      sX    
 +2244 N((4$++-- 5 r-   c                 |    U R                  S5      nU(       d  gUR                  S5      nU(       d  g[        U5      $ )zE
Filter grids so only those that need to be downloaded are included.
r>   Fr?   )r5   rN   )r.   r>   r<   s      r   _filter_download_neededrP      s7     \*J~~f%Hx((r-   c                   ^ [         R                  " 5       n[        U S5       m[        U4S jS5       H  nUR	                  U5        M     SSS5        UR                  5       $ ! , (       d  f       UR                  5       $ = f)z/
Return sha256 checksum of file given by path.
rbc                  &   > T R                  S5      $ )Ni   )read)files   r   <lambda>_sha256sum.<locals>.<lambda>   s    $))E"2r-   r-   N)hashlibsha256openiterupdate	hexdigest)
input_filehasherchunkrU   s      @r   
_sha256sumra      sk    
 ^^F	j$	42C8EMM%  9 
  	 
 	 s   )A$$
Bc                 |   U(       a  [        SU  35        [        X! S35      n [        X5        Ub  U[        U5      :w  a  [	        SU 35      eUR                  [        X!5      5         [        R                  " U5        g! [         a     gf = f!  [        R                  " U5        f ! [         a     f f = f= f)z&
Download resource file from PROJ url
zDownloading: z.partNzSHA256 mismatch: )	printr   r   ra   RuntimeErrorreplacerK   removeFileNotFoundError)file_url
short_name	directoryverboserY   tmp_paths         r   _download_resource_filerm      s     hZ()IU34H	H'&Jx,@"@!2:,?@@i45	IIh  			IIh  		sB   AB +B 
BBB;B+*B;+
B85B;7B88B;target_directoryc                 v   U c  [        S5      n [        U S5      nUR                  5       (       aR  [        R                  " 5       [        R
                  " UR                  5       R                  5      -
  R                  S:  a  [        [        5        S3SU S9  [        R                  " UR                  SS95      $ )z>
Returns
-------
dict[str, Any]:
    The PROJ grid data list.
Tzfiles.geojsonr   z/files.geojson)rh   ri   rj   zutf-8)encoding)r   r   rI   r   nowfromtimestampstatst_mtimedaysrm   r   jsonloads	read_text)rn   
local_paths     r   _load_grid_geojsonrz      s     ,T2&8J	(001B1K1KL	LRRUVV)+,N;&&	

 ::j**G*<==r-   include_already_downloadedc           	         [        US9S   nUGb%  UR                  S:  aE  UR                  UR                  :  a+  U=R                  S-  sl        U=R                  S-  sl        OUR                  S:  aE  UR                  UR                  :  a+  U=R                  S-  sl        U=R                  S-  sl        Oa[        UR                  5      S:  aH  [        UR                  5      S:  a/  UR                  UR                  :  a  U=R                  S-  sl        [	        [        [        UUUS9U5      n[	        [        [        U UUS9U5      nU(       a  [        U5      $ [        [	        [        U5      5      $ )a  
Get a list of transform grids that can be downloaded.

Parameters
----------
source_id: str, optional
area_of_use: str, optional
filename: str, optional
bbox: BBox, optional
spatial_test: str, default="intersects"
    Can be "contains" or "intersects".
include_world_coverage: bool, default=True
    If True, it will include grids with a global extent.
include_already_downloaded: bool, default=False
    If True, it will list grids regardless of if they are downloaded.
target_directory: str | Path, optional
    The directory to download the geojson file to.
    Default is the user writable directory.

Returns
-------
list[dict[str, Any]]:
    A list of geojson data of containing information about features
    that can be downloaded.
)rn   featuresr$   r%   r#   )r   r/   r0   )r:   r;   r<   )
rz   r   r   absfilterr   r9   rF   tuplerP   )	r:   r;   r<   r   r/   r0   r{   rn   r}   s	            r   get_transform_grid_listr      s.   F "3CDZPH99s?tyy4994IIIIIIYY$))dii"7IIIIII^c!c$))ns&:tyy499?TIII)'=	 
 #		
 	H "X/:;;r-   )NNN)FN)N)NNNN
intersectsTFN)#__doc__rX   rv   rK   r   	functoolsr   pathlibr   typingr   urllib.requestr   pyproj._syncr   
pyproj.aoir	   pyproj.datadirr
   r   listr   dictstrr,   boolr9   rF   rN   rP   ra   rm   rz   r   r    r-   r   <module>r      s     	     & *  :d td{ 0!$sCx. !TD[ !H#s(^#'7:TX	8 !"	#s(^Tz t Dj	
 
4
3 
4 

)T#s(^ 
) 
)
 <@*>td): >d3PS8n >, !"$#'',*.B<TzB<tB< DjB< +	B<
 B< !B< !%B< Dj4'B< B<r-   