
    y3i                     r   S r SSKrSSKJr  \R                  R                  S\" \" \5      R                  R                  5      5        SSK	J
r
  SSKJr  SSKrSS\4S jjr\S:X  aP  \" \R$                  5      S	:  a"  \" S
5        \" S5        \R(                  " S5        \R$                  S   r\" \5        gg)z
Wrapper for extract_roads that forces chunked extraction for large countries.
Use this for Germany, France, UK, etc. to avoid OOM kills.
    N)Path)extract_roads_by_regions)OSMlocationc                    U R                  5       n [        R                  " 5       nUS-  U -  nUR                  SSS9  X0 S3-  nUR	                  5       (       d/  [        SU 35        [        S5        [        R                  " S5        UR                  5       R                  S-  n[        S	US
 S35        [        SU SU S35        [        XCU 5      nUb  UR                  (       a!  [        S5        [        R                  " S5        [        S[        U5      S S35         UR                  SS9n[        S5         X0 S3-  nUR                  USSS9  [        SU 35        [        S[        U5      S 35        [        S5        g! [         a  n[        SU 35         SnANhSnAff = f! [         a.  n[        SU 35        [        R                  " S5         SnAgSnAff = f) z=Extract roads using chunked processing - safe for large filesoutputT)parentsexist_okz.osm.pbfu   ❌ PBF file not found: z7   Run: python -m scripts.download_pbf {location} first   i   u   📊 PBF file size: z.1fz MBu'   🗺️  Using chunked extraction with xz grid to avoid OOMNu   ❌ No roads extractedu   ✅ Extracted ,z total roadsi  )epsgu%   🌐 Reprojected to WGS84 (EPSG:4326)u   ⚠️  Reprojection failed: z_roads.gpkgroadsGPKG)layerdriveru   💾 Saved to: u   🛣️  Total roads: u   
✅ Extraction complete!u   ❌ Save failed: )lowerr   cwdmkdirexistsprintsysexitstatst_sizer   emptylento_crs	Exceptionto_file)	r   	grid_sizebase_dirlocation_dirfilenamefile_size_mbr   eoutput_paths	            7/data2/heatmap/scripts/extract_roads_chunked_wrapper.pyextract_roads_chunked_safer)      s   ~~HxxzHh&1Ltd3
(33H??(
34GH==?**i8L	 c 2#
67	3I;a	{J\
]^ %XXFE}&'	N3u:a.
563$'56
"z%==k@}-.&s5z!n56*+  3-aS1223  !!%&s1   #F >A	F- 
F*F%%F*-
G%7$G  G%__main__   zAUsage: python -m scripts.extract_roads_chunked_wrapper <location>z@Example: python -m scripts.extract_roads_chunked_wrapper germanyr   )   )__doc__r   pathlibr   pathinsertstr__file__parentscripts.extract_roadsr   pyrosmr   	geopandasgpdr)   __name__r   argvr   r   r        r(   <module>r<      s   
   3tH~,,334 5 :  + +\ z
388}qQRPQxx{Hx( r;   