
    F1i$                        d Z ddlZddlZddlmZ ddlZddlmZ ddlmZ ddl	Z
ddlZddlmZ d Zdefd	Zed
k(  rI ej&                  d      Zej+                  dd       ej-                         Z eej0                         yy)uZ  
Extracts only road features (highways, streets, etc.) from an
OpenStreetMap .pbf file downloaded from Geofabrik.

✅ Features:
 - Automatic folder creation pe    except MemoryError as e:
        print(f"❌ Memory error during extraction: {e}")
        print("🔄 Attempting regional processing...")
        roads = extract_roads_by_regions(filename, location_dir, location)
        if roads is None:
            print("💡 Regional processing failed. Try processing a smaller country/region")
            sys.exit(1)ion
 - Saves standardized GeoPackage to: output/{LOCATION}/{location}_roads.gpkg
    N)Path)tqdm)OSM)get_geofabrik_urlc                    ddl }ddlm} t        d       t	        t        |             }|j                         }t        d|        |\  }}}	}
|	|z
  dz  }|
|z
  dz  }g }t        d      D ]  }t        d      D ]  }	 |||z  z   }||dz   |z  z   }|||z  z   }||dz   |z  z   }||||f}t        d|dz    d	|dz    d
|        |j                  ddiddd|g d      }|B|j                  s6t        dt        |      dd|dz    d	|dz           |j                  |       nt        d|dz    d	|dz           |j                            |rPt        d       t        j                  t!        j"                  |d            }t        dt        |      dd       |S y# t        $ r%}t        d|dz    d	|dz    d|        Y d}~Pd}~ww xY w)z9Extract roads by processing geographic regions separatelyr   N)boxu8   �️ Processing large dataset by geographic regions...u   📍 Dataset bounds:       u   🔍 Processing region -z/16: highwayTkeepF	r   namelaneswidthsurfacematerialbridgetunnellayer)custom_filterfilter_type
keep_nodeskeep_relationsbounding_boxextra_attributesu     ✅ Found ,z roads in region u     ⚪ No roads in region u     ❌ Error in region : u   � Combining all regions...)ignore_indexu   ✅ Total extracted: z roads from all regions)gcshapely.geometryr   printr   str
get_boundsrangeget_data_by_custom_criteriaemptylenappendcollect	ExceptiongpdGeoDataFramepdconcat)filenamelocation_dirlocationr    r   osmboundsmin_lonmin_latmax_lonmax_latlon_steplat_step	all_roadsijregion_min_lonregion_max_lonregion_min_latregion_max_latregion_bboxregion_roadsecombined_roadss                           '/data2/heatmap/scripts/extract_roads.pyextract_roads_by_regionsrG      s8   $	
DE
c(m
C ^^F	!&
*+ *0&GWgw '!Q&H'!Q&HI1XqA#!(1x<!7!(AEX+=!=!(1x<!7!(AEX+=!=-~~~^/!uAacU%}MN  #>>#,d"3 &$#(!,&  ? 
   +L4F4FL\):1(==NqQRseSTUVWXUXTYZ[$$\25acU!AaC5AB 

A  N ,-))"))ID*QR%c.&9!%<<STU  .qse1QqSEA3?@s   >CF!!	G*G

Gr2   c           
      
   | j                         } t        j                         }|dz  | z  }|j                  dd       ||  dz  }|dz  }	 t	        |       }t        d|        	 |j                         s t        d	| j                          d
       t        j                  dd      5 }|j                          t        |j                  j                  dd            }d}	t!        |d      5 }
t#        |ddd|  d      5 }|j%                  |	      D ]-  }|
j'                  |       |j)                  t+        |             / 	 d d d        d d d        d d d        |j-                         j.                  dk  rt        d      t        d|        nt        d|        t        d       d }	 t3        t5        |            }t        d       |j7                  ddidddg d       }||j8                  rt        d!      t        d"t+        |      d#d$| j                          d%       t        d&|j;                  d'      j=                         d(z  d)d*       t        d+t?        |j@                                	 |jG                  d01      }t        d2       	 ||  d4z  }|jI                  |d5d67       t        d8|        d}	 |r9|j                         r)|jK                          t        d:|jL                          nt        d;| d<       t        d?       t        d@|        t        dA|jL                          t        dB|jL                          t        dCt+        |      d#       t        dD       t        |jO                                y # t        $ r.}t        d|        t        j                  d       Y d }~jd }~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t0        $ r.}t        d|        t        j                  d       Y d }~d }~ww xY w# tB        $ rS}t        d,|        t        d-       tE        |||       }| t        d.       t        j                  d       Y d }~d }~wt0        $ r.}t        d/|        t        j                  d       Y d }~Cd }~ww xY w# t0        $ r}t        d3|        Y d }~Jd }~ww xY w# t0        $ r.}t        d9|        t        j                  d       Y d }~Yd }~ww xY w# t0        $ r}t        d=| d>|        Y d }~4d }~ww xY w)ENoutputT)parentsexist_okz.osm.pbfz	data.gpkgu   🔗 Geofabrik URL: u   ❌ r
   u    ⬇️ Downloading OSM data for z...
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   ✅ Found existing file: u   ❌ Download failed: u   
🚧 Extracting roads...u   🔍 Reading OSM metadata...r   r   Fr   )r   r   r   r   r   uD   No road data extracted — check the .osm.pbf file or location name.u   ✅ Extracted r   z roads from z data.u   📊 Memory usage: )deepi   z.1fz MBu   📋 Columns available: u$   ❌ Memory error during extraction: u$   � Attempting chunked extraction...uF   �💡 Try processing a smaller region or use a machine with more RAMu   ❌ Road extraction failed: i  )epsgu&   🌐 Reprojected to WGS84 (EPSG:4326).u   ⚠️ Reprojection failed: z_roads.gpkgroadsGPKG)r   driveru   💾 Saved extracted roads to: u   ❌ Saving failed: u!   🧹 Deleted processed PBF file: u+   📦 Kept PBF files (DELETE_DOWNLOADED_PBF=)u   ⚠️ Could not delete r   u   
✅ Process complete!u   📁 Folder: u   📥 Input: u   💾 Output: u   🛣️ Roads extracted: z<------------------------------------------------------------)(lowerr   cwdmkdirr   r"   
ValueErrorsysexitexists
capitalizerequestsgetraise_for_statusintheadersopenr   iter_contentwriteupdater(   statst_sizer+   r   r#   r&   r'   memory_usagesumlistcolumnsMemoryErrorextract_roads_chunkedto_crsto_fileunlinkr   head)r2   base_dirr1   r0   output_pathurlrD   r
total_size
block_sizefbarchunkrY   r3   DELETE_DOWNLOADED_PBFs                   rF   extract_roadsr   f   s   ~~HxxzHh&1Ltd3
(33H,K)$SE*+ 4X5H5H5J4K5QRc$<""$ /?!CD
!
(D)Q$#!%$:X.1 !":!F

3u:. "G1) =  }}&&3 !_``-hZ89-hZ89 

&'E+#h-  	,- //$d+ 
 0 
& =EKKcdds5z!nL9L9L9N8OvVW#E$6$6D$6$A$E$E$G'$QRU#VVYZ[(emm)<(=>? 2$'67"z%==k@/}=> ": X__%6OO5hmm_EF?@U?VVWXY 

#$	M,
()	L
()	M+**+
,-	%c%j^
45	(O	%**,C  QCj1 1)) =<*  %aS)*X  4QC8945%hhG=Z[HHQK ,QC01  2,QC0112  #A3'(  :(
"QC899:s   N (AP  .AO32O&AO
O&O3AP  5CP: S "*S3 A
T- 	O(#OOO#O&&O0	+O33O=8P   	P7	#P22P7:	SARS#SS	S0S++S03	T*<#T%%T*-	U6UU__main__z2Download and extract OSM road data for a location.)descriptionz/Location name (e.g., alabama, egypt, thailand).)help)__doc__argparsera   pathlibr   re   r   pyrosmr   	geopandasr,   pandasr.   scripts.geofabrik_registryr   rG   r#   r   __name__ArgumentParserparseradd_argument
parse_argsargsr2        rF   <module>r      s     
       8
BNNC Nb z$X$$HF 
)Z[D$--  r   