U
    ;i                     @   s|   d Z ddlZddlmZ ddlmZ ddlZedddZ	e
dkrxeejd	k rjed
 ed ed e	ejd  dS )z
Extracts building features from a state's OpenStreetMap .pbf file.

This script complements extract_state_roads.py by extracting building polygons
with attributes like building type, height, and building:levels for heatmap analysis.
    N)Path)OSM)statec                 C   s  |   } t }|d |  }||  d }| s~t|d}|r^|d }td|j  n td|  td t	d ||  d	 }td
|j d zt
t|}|jddidddddddddddgd}|d ks|jrtd td W d S tdt|dd|   d tdt|j  d|jkrX|d  d}td |  d|jkr~|d  }	td!|	  W n< tk
r }
 ztd"|
  t	d W 5 d }
~
X Y nX z|jd#d$}td% W n2 tk
r }
 ztd&|
  W 5 d }
~
X Y nX z"|j|d'd(d) td*|  W n< tk
rh }
 ztd+|
  t	d W 5 d }
~
X Y nX td, td-|  td.|j  td/|j  td0t|d td1 td2 td3 td4 d S )5NZOSM_State_Filesz.osm.pbfz*.pbfr   u   📦 Using PBF file: u   ❌ No PBF file found in z=   Run extract_state_roads.py first to download the OSM data.   z_buildings.gpkgu    
🏢 Extracting buildings from z...ZbuildingTZkeepFZheightzbuilding:levelszaddr:housenumberzaddr:streetZamenityZshopZoffice)Zcustom_filterZfilter_typeZ
keep_nodesZkeep_relationsZextra_attributesu/   ⚠️ No building data found in this OSM file.z9   This state may not have detailed building data in OSM.u   ✅ Extracted ,z buildings from z data.u   📋 Columns available: 
   u   🏘️ Top building types:
u   📏 Height stats:
u    ❌ Building extraction failed: i  )Zepsgu&   🌐 Reprojected to WGS84 (EPSG:4326).u   ⚠️ Reprojection failed: 	buildingsZGPKG)ZlayerZ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listZglobprintnamesysexitr   strZget_data_by_custom_criteriaemptylen
capitalizecolumnsZvalue_countsheadZdescribe	ExceptionZto_crsZto_file)r   Zbase_dirZ	state_dirZpbf_fileZ	pbf_filesZoutput_pathZosmr   Zbuilding_typesZheight_statse r   1/data2/heatmap/scripts/extract_state_buildings.pyextract_state_buildings   s    
  r   __main__   z=Usage: python -m scripts.extract_state_buildings <STATE_NAME>z7Example: python -m scripts.extract_state_buildings ohior   )__doc__r   pathlibr   Zpyrosmr   Z	geopandasZgpdr   r   __name__r   argvr   r   r   r   r   r   <module>   s   b
