
    c0i              	           S r SSKrSSKrSSKJr  SSKrSSKJr  SSKJrJ	r	  SS\
S\S\S	\4S
 jjrS r\S:X  a  \" 5         gg)z
Downloads OSM PBF files from Geofabrik if they don't already exist.

Usage:
    python -m scripts.download_pbf ohio
    python -m scripts.download_pbf egypt india thailand
    python -m scripts.download_pbf --list
    N)Path)tqdm)get_geofabrik_urllist_available_locationslocation
output_dirforcereturnc           
      X   U R                  5       n  [        U 5      nUc  [        R                  " 5       nUS-  U -  nUR                  SSS9  X S3-  nUR                  5       (       a9  U(       d2  UR                  5       R                  S-  n[        S	U S
US S35        U$ [        SU R                  5        S35        [        SW 35         [        R                  " USSS9 nUR                  5         [!        UR"                  R                  SS5      5      n	Sn
[%        US5       n['        U	SSSU  S3S9 nUR)                  U
S9 H.  nUR+                  U5        UR-                  [/        U5      5        M0     SSS5        SSS5        SSS5        UR                  5       R                  S:  a  [        S5      eUR                  5       R                  S-  n[        SU S
US S35        U$ ! [         a/  n[        SU 35        [        R
                  " S5         SnAGNSnAff = f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       N= f! [        R0                  R2                   aS  n[        SU 35        UR                  5       (       a  UR5                  5         [        R
                  " S5         SnAgSnAf[6         aS  n[        SU 35        UR                  5       (       a  UR5                  5         [        R
                  " S5         SnAgSnAff = f)a  
Download a PBF file from Geofabrik if it doesn't exist.

Args:
    location: Location name (e.g., 'ohio', 'egypt')
    output_dir: Directory to save the file (default: output/{location}/)
    force: Force re-download even if file exists
    
Returns:
    Path to the downloaded file
u   ❌    NoutputT)parentsexist_okz.osm.pbf   u   ✅ File already exists:  (.1f MB)u!   ⬇️  Downloading OSM data for z...u
   🔗 URL: x   )streamtimeoutzcontent-lengthr   i    wbBi   )totalunit
unit_scaleunit_divisordesc)
chunk_sizei  u<   Downloaded file seems too small — possible download error.u   ✅ Download complete: u   ❌ Download failed: u   ❌ Error: )lowerr   
ValueErrorprintsysexitr   cwdmkdirexistsstatst_size
capitalizerequestsgetraise_for_statusintheadersopenr   iter_contentwriteupdatelen
exceptionsRequestExceptionunlink	Exception)r   r   r	   urlebase_dirfilename	file_sizer
total_size
block_sizefbarchunks                 &/data2/heatmap/scripts/download_pbf.pydownload_pbfrD      s    ~~H) 88:(83
TD1j11H MMO++{;	)(2i_DIJ 
-h.A.A.C-DC
HI	Jse
"\\#dC8A QYY]]+;Q?@JJh%D ! z*- ^^z^BEGGENJJs5z* C-% 9" ==?""Z/[\\MMO++{;	'zIc?$GH[  QCj8- -%% 980 // %aS)*??OO A3 ??OO	s   G. I $AI(H;9AH*;H;IA"I .
H'8$H""H'*
H84H;;
I		I
II L);A	K		L)A	L$$L)c            
         [         R                  " S[         R                  SS9n U R                  SSSS9  U R                  SS	S
S9  U R                  SS	SS9  U R                  S[        SS9  U R                  5       nUR                  (       a  [        5         g UR                  (       d1  U R                  5         [        S5        [        R                  " S5        [        S[        UR                  5       S35        / n/ nUR                   H?  n [        UUR                  UR                   S9nUR#                  XE45        [        5         MA     [        S5        [        S[        U5       S[        UR                  5       35        U(       aI  [        S5        U H8  u  pEUR'                  5       R(                  S-  n[        SU SU SUS  S!35        M:     U(       a.  [        S"[        U5       35        U H  n[        SU 35        M     [        S5        g ! [$         a/  n[        SU SU S35        UR#                  U5         S nAGM@  S nAff = f)#Nz%Download OSM PBF files from Geofabrika  
Examples:
  Download single location:
    python -m scripts.download_pbf ohio
    
  Download multiple locations:
    python -m scripts.download_pbf ohio kentucky new-york
    python -m scripts.download_pbf egypt india thailand
    
  Force re-download:
    python -m scripts.download_pbf ohio --force
    
  List available locations:
    python -m scripts.download_pbf --list
        )descriptionformatter_classepilog	locations*z5Location names to download (e.g., ohio, egypt, india))nargshelpz--list
store_truez,List all available locations in the registry)actionrL   z--forcez%Force re-download even if file existsz--output-dirz5Custom output directory (default: output/{location}/))typerL   u5   
❌ Error: Please provide at least one location namer   u   
📥 Downloading z location(s)...
)r   r	   u   ❌ Failed to download z: 
z<============================================================u   ✅ Successfully downloaded: /u   
📁 Downloaded files:r   u     • r   r   r   u   
❌ Failed: )argparseArgumentParserRawDescriptionHelpFormatteradd_argumentr   
parse_argslistr   rI   
print_helpr!   r"   r#   r3   rD   r   r	   appendr7   r'   r(   )parserargs
downloadedfailedr   filepathr9   r<   s           rC   mainr_   `   sD   $$; <<F( D   ;   4   D   D yy " >>FG 
DNN 344E
FGJFNN
	$#??jjH
 x23G # 
(O	)#j/):!C<O;P
QR()",H //;?IF8*Bxj9S/FG #- s6{m,-HF8*%&  
(O'  	$+H:Rs"=>MM(##	$s   ;H
I##II__main__)NF)__doc__rR   r"   pathlibr   r*   r   scripts.geofabrik_registryr   r   strboolrD   r_   __name__     rC   <module>ri      sa     
    RI3 ID I IQU IX\~ zF rh   