
    I0i                         d Z ddlZddlZddlmZ ddlmZ ddlZde	fdZ
edk(  rI ej                  d	      Zej                  dd
       ej                         Z e
ej"                         yy)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                    | j                         } t        j                         }|dz  | z  }||  dz  }|j                         sht	        |j                  d            }|r|d   }t        d|j                          n.t        d|        t        d       t        j                  d       ||  d	z  }t        d
|j                   d       	 t        t        |            }|j                  ddidddg d      }||j                  rt        d       t        d       y t        dt        |      dd| j                          d       t        dt	        |j                                 d|j                   v r0|d   j#                         j%                  d      }t        d|        d|j                   v r!|d   j'                         }	t        d|	        	 j+                  d      }t        d        	 j-                  |d"d#$       t        d%|        t        d'       t        d(|        t        d)|j                          t        d*|j                          t        d+t              d       t        d,       t        d-       t        d.       t        d/       y # t(        $ r.}
t        d|
        t        j                  d       Y d }
~
d }
~
ww xY w# t(        $ r}
t        d!|
        Y d }
~
d }
~
ww xY w# t(        $ r.}
t        d&|
        t        j                  d       Y d }
~
d }
~
ww xY w)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              scripts/extract_buildings.pyextract_buildingsr:      s&   ~~HxxzHh&1L 
(33H??**734	 |H)(--9:-l^<=KMHHQKH:_!==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J) "B8J) K# 9"L )	K 2#KK #	L,L  L	L?#L::L?__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$X$$RF 
)Z[Ddmm$ rK   