
    I0i                         S r SSKrSSKrSSKJr  SSKJr  SSKrS\	4S jr
\S:X  aC  \R                  " SS	9r\R                  SS
S9  \R                  5       r\
" \R"                  5        gg)z
Extracts building features from an OpenStreetMap .pbf file.

This script complements extract_roads.py by extracting building polygons
with attributes like building type, height, and building:levels for heatmap analysis.
    N)Path)OSMlocationc                    U R                  5       n [        R                  " 5       nUS-  U -  nX  S3-  nUR                  5       (       dn  [	        UR                  S5      5      nU(       a  US   n[        SUR                   35        O/[        SU 35        [        S5        [        R                  " S5        X  S	3-  n[        S
UR                   S35         [        [        U5      5      nUR                  SS0SSS/ SQS9nUb  UR                  (       a  [        S5        [        S5        g [        S[        U5      S SU R                  5        S35        [        S[	        UR                   5       35        SUR                   ;   a0  US   R#                  5       R%                  S5      n[        SU 35        SUR                   ;   a!  US   R'                  5       n	[        SU	 35         WR+                  SS9n[        S 5         WR-                  US"S#S$9  [        S%U 35        [        S'5        [        S(U 35        [        S)UR                   35        [        S*UR                   35        [        S+[        W5      S 35        [        S,5        [        S-5        [        S.5        [        S/5        g ! [(         a.  n
[        SU
 35        [        R                  " S5         S n
A
NS n
A
ff = f! [(         a  n
[        S!U
 35         S n
A
GNS n
A
ff = f! [(         a/  n
[        S&U
 35        [        R                  " S5         S n
A
GN#S n
A
ff = f)0Noutputz.osm.pbfz*.pbfr   u   📦 Using PBF file: u   ❌ No PBF file found in z7   Run extract_roads.py first to download the OSM data.   z_buildings.gpkgu    
🏢 Extracting buildings from z...buildingTkeepF)r	   heightzbuilding:levelszaddr:housenumberzaddr:streetamenityshopoffice)custom_filterfilter_type
keep_nodeskeep_relationsextra_attributesu/   ⚠️ No building data found in this OSM file.z<   This location may not have detailed building data in OSM.u   ✅ Extracted ,z buildings from z data.u   📋 Columns available: 
   u   🏘️ Top building types:
r   u   📏 Height stats:
u    ❌ Building extraction failed: i  )epsgu&   🌐 Reprojected to WGS84 (EPSG:4326).u   ⚠️ Reprojection failed: 	buildingsGPKG)layerdriveru#   💾 Saved extracted buildings to: u   ❌ Saving failed: u"   
✅ Building extraction complete!u   📁 Folder: u   📥 Input: u   💾 Output: u   🏢 Buildings extracted: z<------------------------------------------------------------zNext steps:z91. Run load_buildings.py to reproject buildings to meterszD2. Run visualize_all_attribute_grid.py to generate building heatmaps)lowerr   cwdexistslistglobprintnamesysexitr   strget_data_by_custom_criteriaemptylen
capitalizecolumnsvalue_countsheaddescribe	Exceptionto_crsto_file)r   base_dirlocation_dirpbf_file	pbf_filesoutput_pathosmr   building_typesheight_statses              +/data2/heatmap/scripts/extract_buildings.pyextract_buildingsr:      s%   ~~HxxzHh&1L 
(33H??**734	 |H)(--9:-l^<=KMHHQK:_!==K
 
-hmm_C
@A'#h- 33%t, 	 4 
	$ 	CDPQs9~a00@ATATAV@WW]^_(i.?.?)@(ABC ***&z2??AFFrJN1.1ABCy((($X.779L(782$$$$/	67+[H3K=AB 

/0	M,
()	L
()	M+**+
,-	&s9~a&8
9:	(O	-	
EF	
PQE  045  2,QC0112  #A3'(sP   AJ4 /B<J4 ,K/ L 4
K,>$K''K,/
L9LL
M$M		M__main__z<Extract OSM buildings for a location using a local PBF file.)descriptionz/Location name (e.g., alabama, egypt, thailand).)help)__doc__argparser"   pathlibr   pyrosmr   	geopandasgpdr$   r:   __name__ArgumentParserparseradd_argument
parse_argsargsr        r9   <module>rL      s~     
   _R _RD z$$RF 
)Z[Ddmm$ rK   