
    f!                     .   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
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e         fdZdeee
f         dee         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e         dee         dee         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e
f         fdZ!	 	 	 	 	 	 	 	 d(dee         dee         dee         dee         deded"ed#ee         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Optional)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         E/var/www/html/Alfredo/env/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   r0   r1   r   	geom_bboxs         r   _filter_bboxr:   N   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)r6   )
r/   r;   r<   r=   r?   
p_filenamep_source_idsource_id__matchedarea_of_use__matchedfilename__matcheds
             r   _filter_propertiesrG   h   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)rH   data_dirs     r   _is_download_neededrO      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@   )r6   rO   )r/   r?   r=   s      r   _filter_download_neededrQ      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chunkrV   s      @r   
_sha256sumra      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   ra   RuntimeErrorreplacerL   removeFileNotFoundError)file_url
short_name	directoryverboserY   tmp_paths         r   _download_resource_filerm      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c                    | 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)rh   ri   rj   zutf-8)encoding)r   r   rJ   r   utcnowfromtimestampstatst_mtimedaysrm   r	   jsonloads	read_text)target_directory
local_paths     r   _load_grid_geojsonrz      s     ,T22&88J 
			X3JOO4E4E4NOO	OU
	 	 	 )++;;;&&	
 	
 	
 	

 :j**G*<<===r.   
intersectsTinclude_already_downloadedrx   c                    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 )	aI  
    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: Union[str, Path, None], 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.
    )rx   featuresNr%   r&   r$   )r   r0   r1   )r;   r<   r=   )
rz   r   r   absfilterr   r:   rG   tuplerQ   )	r;   r<   r=   r   r0   r1   r|   rx   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)NNNNr{   TFN)$__doc__rX   ru   rL   r   	functoolsr   pathlibr   typingr   r   urllib.requestr   pyproj._syncr	   
pyproj.aoir
   pyproj.datadirr   r   listr   dictstrr-   boolr:   rG   rO   rQ   ra   rm   rz   r   r    r.   r   <module>r      s   
   				                                   & & & & & & * * * * * *       : : : : : : : :d x~    ,!$sCx. !Xd^ ! ! ! !H#s(^#'7:TX	   8  $!%"	 #s(^} # sm	
 
   4
3 
4 
 
 
 

)T#s(^ 
) 
) 
) 
) 
)
 
 
 <@   *> >c3h > > > >.  $!%"$#'',&*B< B<}B<#B< smB< 4.	B<
 B< !B< !%B< smB< B< B< B< B< B< B<r.   