
    rbi!                        d Z ddlZddlZddlZddlmZ ddlmZ ddlmZ ddl	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ededz  fdZdeee
f         dedz  fdZdeee
f         dedededef
dZ	 	 	 d%deee
f         dedz  dedz  dedz  def
dZdedefdZdeee
f         defdZd Z	 d&dZd'deez  dz  deee
f         fd Z 	 	 	 	 	 	 	 	 d(dedz  dedz  dedz  dedz  deded#edeez  dz  de!fd$Z"dS ))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                    	 t          |  \  }}t          t          |          t          |          t          |          t          |                    S # t          $ r Y nw xY wd}| D ]}t          |          }|||}t          |j        |j                  |_        t          |j        |j                  |_        t          |j        |j                  |_        t          |j	        |j	                  |_	        |S )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         [/var/www/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/pyproj/sync.pyr   r      s    <SSSCCQQQQ   J 
> 
>	 ++<JJ!*/49==JO":#3TZ@@J":#3TZ@@J!*/49==JOOs   AA 
A"!A"geomc                 V   d| vsd| vrdS | d         }| d         dk    rt          |          S d}d}g }|D ]D}t          |          }||j        dk    rd}n|j        dk    rd}|                    |           Ed}|D ]}|r$|r"|j        dk    rd|_        |xj        d	z  c_        ||}-t	          |j        |j                  |_        t	          |j        |j                  |_        t          |j        |j                  |_        t          |j        |j                  |_        |S )
z4
    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,   -   sb    D  F$$6$6t}%KF|~%% ---ONF%   00<9"OOY#!NdI 
< 
< 	~ 	$)t2C2CDIIIIIII ;;IN!)/4:>>IO!)/4:>>IO ;;INN    featurer   spatial_testinclude_world_coveragec                    |                      d          }|t          |          }|dS |j        |j        z
  dk    rA|j        |j        z
  dk    r.|sdS t          d           |_        t          d          |_        n6|j        dk    r+|j        dk     r |xj        d	z  c_        |xj        d	z  c_         t          ||          |          S dS )
zC
    Filter by the bounding box. Designed to use with 'filter'
    geometryNFig     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))	5NY^+c11)/1C77) u#Ell]IN"5\\INN^c!!di$&6&6NNc!NNNNc!NN*wt\**95555r-   	source_idarea_of_usefilenamec                     |                      d          }|sdS |                     d          }|                     d          }|r|sdS |du p||v }|du p||                     dd          v }|du p||v }	|r|r|	rdS dS )	zA
    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 u''J..--K [ u"d*Fi;.F&$. +rB B 3 !D(BH
,B 2 7H t5r-   	grid_namec                    t          t                      |                                           rdS t                                          t
          j                  D ]'}t          ||                                           r dS (dS )zX
    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      s    
 ++2244 u NN((44  )$$++-- 	55	4r-   c                     |                      d          }|sdS |                     d          }|sdS t          |          S )zM
    Filter grids so only those that need to be downloaded are included.
    r>   Fr?   )r5   rN   )r.   r>   r<   s      r   _filter_download_neededrP      sN     \**J u~~f%%H ux(((r-   c                     t          j                    }t          | d          5 t          fdd          D ]}|                    |           	 ddd           n# 1 swxY w Y   |                                S )z7
    Return sha256 checksum of file given by path.
    rbc                  .                          d          S )Ni   )read)files   r   <lambda>z_sha256sum.<locals>.<lambda>   s    $))E"2"2 r-   r-   N)hashlibsha256openiterupdate	hexdigest)
input_filehasherchunkrU   s      @r   
_sha256sumr`      s    
 ^F	j$		 !42222C88 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,AA"%A"Fc                    |rt          d|             t          || d          }	 t          | |           |%|t          |          k    rt	          d|           |                    t          ||                     	 t          j        |           dS # t          $ r Y dS w xY w# 	 t          j        |           w # t          $ r Y w w xY wxY w)z.
    Download resource file from PROJ url
    zDownloading: z.partNzSHA256 mismatch: )	printr   r   r`   RuntimeErrorreplacerK   removeFileNotFoundError)file_url
short_name	directoryverboserX   tmp_paths         r   _download_resource_filerl      s     *(h(()))I*33344H	Hh'''&Jx,@,@"@"@?:??@@@i44555	Ih  	 	 	DD		Ih  	 	 	D	sB   AB+ B 
B('B(+C-CC
CCCCtarget_directoryc                    | t          d          } t          | d          }|                                rHt          j                    t          j        |                                j                  z
  j        dk    r!t          t                       dd|            t          j        |                    d                    S )	zR
    Returns
    -------
    dict[str, Any]:
        The PROJ grid data list.
    NTzfiles.geojsonr   z/files.geojson)rg   rh   ri   zutf-8)encoding)r   r   rI   r   nowfromtimestampstatst_mtimedaysrl   r   jsonloads	read_text)rm   
local_paths     r   _load_grid_geojsonry      s     ,T22&88J 
	(01B1B1KLL	LRUVVV)++;;;&&	
 	
 	
 	

 :j**G*<<===r-   
intersectsTinclude_already_downloadedc                    t          |          d         }||j        dk    r1|j        |j        k    r!|xj        dz  c_        |xj        dz  c_        n|j        dk     r1|j        |j        k    r!|xj        dz  c_        |xj        dz  c_        nPt          |j                  dk     r8t          |j                  dk     r |j        |j        k     r|xj        dz  c_        t	          t          t          |||          |          }t	          t          t          | ||          |          }|rt          |          S t          t	          t          |                    S )	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.
    )rm   featuresNr$   r%   r#   )r   r/   r0   )r:   r;   r<   )
ry   r   r   absfilterr   r9   rF   tuplerP   )	r:   r;   r<   r   r/   r0   r{   rm   r}   s	            r   get_transform_grid_listr      sq   F "3CDDDZPH9s??ty4944IIIIIIIIIY$)di"7"7IIIIIIIII^^c!!c$)nns&:&:ty49?T?TIIII)'=	   
 
 #		
 	
 	
 	 H " X/::;;;r-   )NNN)FN)N)NNNNrz   TFN)#__doc__rW   ru   rK   r   	functoolsr   pathlibr   typingr   urllib.requestr   pyproj._syncr   
pyproj.aoir	   pyproj.datadirr
   r   listr   dictstrr,   boolr9   rF   rN   rP   r`   rl   ry   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< B<TzB<tB< DjB< +	B<
 B< !B< !%B< Dj4'B< B< B< B< B< B< B<r-   