U
    OÙi„  ã                   @   s‚   d Z ddlZddlZddlmZ dejd< ddlZedœdd„Z	e
d	kr~eejƒd
k rpedƒ edƒ e d¡ e	ejd ƒ dS )a+  
Loads extracted buildings GeoPackage and reprojects to meter-based CRS for analysis.

This script complements load_gpkg.py by handling buildings data specifically.
It reprojects buildings from WGS84 to a meter-based coordinate system (EPSG:5070)
suitable for spatial analysis and grid aggregation.
é    N)ÚPathÚ0Z
USE_PYGEOS)Ústatec           	   
   C   sô  |   ¡ } t ¡ }|d |  }|| › d }|| › d }| ¡ s`td|› ƒ tdƒ t d¡ td|j› dƒ z t 	t
|ƒ¡}td	t|ƒd
›dƒ td|j› ƒ |jd krÂtdƒ d|_|j ¡ dkrîtdƒ |jdd}tdƒ ntdƒ |j|ddd td|j› ƒ tdƒ |jD ]8}|dkr$||  ¡  ¡ }td|› d|d
›dƒ q$tdƒ t| ¡ ƒ W n< tk
r° } ztd|› ƒ t d¡ W 5 d }~X Y nX tdƒ td |› ƒ td!|j› ƒ td"|j› ƒ td#ƒ d S )$NZOSM_State_Filesz_buildings.gpkgz_buildings_projected.gpkgu   âŒ Buildings file not found: z=   Run extract_state_buildings.py first to extract buildings.é   u!   ðŸ“¦ Loading buildings data from z...u   âœ… Loaded ú,z
 buildingsu   ðŸ“‹ Current CRS: u/   âš ï¸ No CRS found, assuming WGS84 (EPSG:4326)z	EPSG:4326iÎ  u=   ðŸ”„ Reprojecting to EPSG:5070 (Albers Equal Area for USA)...)Zepsgu   âœ… Reprojection completeu   âœ… Already in EPSG:5070Zbuildings_projectedZGPKG)ZlayerZdriveru#   ðŸ’¾ Saved projected buildings to: u$   
ðŸ“‹ Building attributes available:Zgeometryz  z: z non-null valuesu   
ðŸ” Sample buildings data:u   âŒ Processing failed: u#   
âœ… Buildings processing complete!u   ðŸ“ Folder: u   ðŸ“¥ Input: u   ðŸ’¾ Output: uL   ðŸ”„ Next: Run visualize_all_attribute_grid.py to generate building heatmaps)Úlowerr   ÚcwdÚexistsÚprintÚsysÚexitÚnameÚgpdZ	read_fileÚstrÚlenZcrsZto_epsgZto_crsZto_fileÚcolumnsZnotnaÚsumÚheadÚ	Exception)	r   Zbase_dirZ	state_dirZ
input_pathZoutput_pathZ	buildingsZcolZnon_null_countÚe© r   ú(/data2/heatmap/scripts/load_buildings.pyÚload_buildings_gpkg   sN    




r   Ú__main__é   z4Usage: python -m scripts.load_buildings <STATE_NAME>z.Example: python -m scripts.load_buildings ohior   )Ú__doc__r   ÚosÚpathlibr   ÚenvironZ	geopandasr   r   r   Ú__name__r   Úargvr
   r   r   r   r   r   Ú<module>   s   
:
