
     h!                        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"y)&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         g/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/pyproj/sync.pyr   r      s    <SSSCCQQ J 
>	 +<J!*//499=JO":#3#3TZZ@J":#3#3TZZ@J!*//499=JO
> s   =A   	AAgeomc                    d| vsd| vry| d   }| d   dk7  rt        |      S d}d}g }|D ]D  }t        |      }||j                  dk(  rd}n|j                  dk(  rd}|j                  |       F d}|D ]  }|r-|r+|j                  dk(  rd|_        |xj                  d	z  c_        ||}7t	        |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,   -   s;    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                    | j                  d      }|t        |      }|y|j                  |j                  z
  dkD  rA|j                  |j
                  z
  dkD  r%|syt        d       |_        t        d      |_        nH|j                  dkD  r9|j                  dk  r*|xj                  dz  c_        |xj                  dz  c_         t        ||      |      S y)	zC
    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*wt\*955r-   	source_idarea_of_usefilenamec                     | j                  d      }|sy|j                  d      }|j                  d      }|r|sy|du xs ||v }|du xs ||j                  dd      v }|du xs ||v }	|r|r|	ryy)	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'J..-K["d*Fi;.F&$. +rB 3 !D(BH
,B27Hr-   	grid_namec                     t        t               |       j                         ryt               j	                  t
        j                        D ]  }t        ||       j                         s y y)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      sY    
 +224 N((4 )$++- r-   c                 h    | j                  d      }|sy|j                  d      }|sy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      s7     \*J~~f%Hx((r-   c                     t        j                         }t        | d      5 t        fdd      D ]  }|j	                  |        	 ddd       |j                         S # 1 sw Y   |j                         S xY w)z7
    Return sha256 checksum of file given by path.
    rbc                  &     j                  d      S )Ni   )read)files   r   <lambda>z_sha256sum.<locals>.<lambda>   s    $))E"2 r-   r-   N)hashlibsha256openiterupdate	hexdigest)
input_filehasherchunkrU   s      @r   
_sha256sumr`      sr    
 ^^F	j$	 !42C8 	!EMM% 	!! 	! s   &A!!A9c                 l   |rt        d|         t        || d      }	 t        | |       ||t        |      k7  rt	        d|       |j                  t        ||             	 t        j                  |       y# t        $ r Y y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     hZ()I*U34H	Hh'&Jx,@"@!2:,?@@i45	IIh  			IIh  		sB   AB 'A= =	B	B	B3B$#B3$	B0-B3/B00B3target_directoryc                 n   | t        d      } t        | d      }|j                         rOt        j                         t        j
                  |j                         j                        z
  j                  dkD  rt        t                dd|        t        j                  |j                  d            S )zR
    Returns
    -------
    dict[str, Any]:
        The PROJ grid data list.
    Tz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&8J	(001B1K1KL	LRRUVV)+,N;&&	

 ::j**G*<==r-   include_already_downloadedc                    t        |      d   }|!|j                  dkD  rD|j                  |j                  kD  r+|xj                  dz  c_        |xj                  dz  c_        n|j                  dk  rD|j                  |j                  kD  r+|xj                  dz  c_        |xj                  dz  c_        n^t        |j                        dk  rFt        |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   featuresr$   r%   r#   )r   r/   r0   )r:   r;   r<   )
ry   r   r   absfilterr   r9   rF   tuplerP   )	r:   r;   r<   r   r/   r0   rz   rm   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__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<TzB<tB< DjB< +	B<
 B< !B< !%B< Dj4'B< B<r-   