import streamlit as st import folium import json import plotly.express as px import pandas as pd from streamlit_folium import st_folium import plotly.graph_objs as go st.set_page_config(layout="wide", page_title="Heat Island Effect Analysis") def load_geojson(filepath): with open(filepath, 'r', encoding='utf-8') as f: return json.load(f) def plot_geojson(feature_group, geojson_data, property_name, colormap): folium.GeoJson( geojson_data, style_function=lambda feature: { 'fillColor': colormap(feature['properties'][property_name]), 'color': 'black', 'weight': 1, 'fillOpacity': 0.7, }, popup=folium.GeoJsonPopup(fields=['NAME_EN', property_name], aliases=['District:', 'Value:']), ).add_to(feature_group) def compute_difference_geojson(geojson_2013, geojson_2023): difference_geojson = {"type": "FeatureCollection", "features": []} name_to_hot_nights_2013 = { feature['properties']['NAME_EN']: feature['properties']['Hot_nights'] for feature in geojson_2013['features'] } for feature in geojson_2023['features']: name_en = feature['properties']['NAME_EN'] hot_nights_2013 = name_to_hot_nights_2013.get(name_en, 0) hot_nights_2023 = feature['properties']['Hot_nights'] difference = hot_nights_2023 - hot_nights_2013 feature['properties']['Difference'] = difference difference_geojson['features'].append(feature) return difference_geojson def geojson_to_dataframe(geojson_data, year): features = geojson_data['features'] data = { 'District': [feature['properties']['NAME_EN'] for feature in features], 'Hot_nights': [feature['properties']['Hot_nights'] for feature in features], 'Year': [year] * len(features) # Add year column } return pd.DataFrame(data) geojson_2013 = load_geojson('ref/2013_hot.geojson') geojson_2023 = load_geojson('ref/2023_hot.geojson') hot_nights_2013 = [feature['properties']['Hot_nights'] for feature in geojson_2013['features']] hot_nights_2023 = [feature['properties']['Hot_nights'] for feature in geojson_2023['features']] all_hot_nights = hot_nights_2013 + hot_nights_2023 colormap = folium.LinearColormap( colors=['white', 'orange', 'red'], vmin=min(all_hot_nights), vmax=max(all_hot_nights), caption='Hot Nights' ) difference_geojson = compute_difference_geojson(geojson_2013, geojson_2023) diff_colormap = folium.LinearColormap( colors=['blue', 'lightblue', 'white', 'yellow', 'red'], index=[-20, -5, 0, 10, 50], vmin=-20, vmax=50, caption='Change in Hot Nights' ) m = folium.Map(location=[22.35994791346238, 114.15924623933743], zoom_start=11, tiles='https://landsd.azure-api.net/dev/osm/xyz/basemap/gs/WGS84/tile/{z}/{x}/{y}.png?key=f4d3e21d4fc14954a1d5930d4dde3809', attr="Map information from Lands Department", control_scale=True) folium.TileLayer( tiles='https://mapapi.geodata.gov.hk/gs/api/v1.0.0/xyz/label/hk/en/wgs84/{z}/{x}/{y}.png', attr="Map information from Lands Department", overlay=True, name="Labels" ).add_to(m) feature_group_2013 = folium.FeatureGroup(name='2013 Hot Nights', show=False) feature_group_2023 = folium.FeatureGroup(name='2023 Hot Nights', show=False) feature_group_diff = folium.FeatureGroup(name='Change in Hot Nights', show=True) plot_geojson(feature_group_2013, geojson_2013, 'Hot_nights', colormap) plot_geojson(feature_group_2023, geojson_2023, 'Hot_nights', colormap) plot_geojson(feature_group_diff, difference_geojson, 'Difference', diff_colormap) feature_group_2013.add_to(m) feature_group_2023.add_to(m) feature_group_diff.add_to(m) folium.LayerControl().add_to(m) colormap.add_to(m) diff_colormap.add_to(m) colormap.add_to(m) diff_colormap.add_to(m) df_2013 = geojson_to_dataframe(geojson_2013, '2013') df_2023 = geojson_to_dataframe(geojson_2023, '2023') combined_df = pd.concat([df_2013, df_2023]) def plot_combined_box_plot(df): fig = px.box( df, x='Year', y='Hot_nights', title='Hot Nights (2013 vs 2023)', labels={'Hot_nights': 'Number of Hot Nights', 'Year': 'Year'}, color='Year' ) fig.update_layout( yaxis_title='Number of Hot Nights', boxmode='group' ) return fig data_table = pd.read_csv('ref/final_summary_with_available_stations.csv') stations = data_table['station_name'].unique() col1, col2, col3 = st.columns([1.35, 2, 1.1]) with col1: st.subheader('Heat Island Effect') st.caption( 'The "heat island effect" refers to the temperature difference between urban and rural areas, particularly at night.') st.caption( 'This phenomenon is a result of the urbanization and development processes. During the day, the urban environment (such as cement pavement) absorbs and stores more heat from solar insolation compared to rural areas (vegetation). This heat is then slowly released in the evening and nighttime, leading to higher temperatures in the urban areas.') selected_station = st.selectbox('Select a Station:', options=stations) filtered_data_table = data_table[data_table['station_name'] == selected_station] fig = go.Figure() fig.add_trace(go.Scatter( x=filtered_data_table['month'], y=filtered_data_table['13day_temp'], mode='lines+markers', name='2013 Day Temp', line=dict(color='blue') )) fig.add_trace(go.Scatter( x=filtered_data_table['month'], y=filtered_data_table['13night_temp'], mode='lines+markers', name='2013 Night Temp', line=dict(color='blue', dash='dash') )) fig.add_trace(go.Scatter( x=filtered_data_table['month'], y=filtered_data_table['23day_temp'], mode='lines+markers', name='2023 Day Temp', line=dict(color='red') )) fig.add_trace(go.Scatter( x=filtered_data_table['month'], y=filtered_data_table['23night_temp'], mode='lines+markers', name='2023 Night Temp', line=dict(color='red', dash='dash') )) fig.update_layout( title=f'Temperature Comparison', xaxis_title='Month', yaxis_title='Temperature (°C)', legend_title='Legend', height =300 ) st.plotly_chart(fig, height=180) with col2: st_folium(m, use_container_width=True , height=650) with col3: st.plotly_chart(plot_combined_box_plot(combined_df), height=250, use_container_width=True) st.caption( '"Hot nights" refers to nights with temperatures above 28 degrees. From the data of the Co-WIN network and AWS (Automatic Weather Stations), there has been a significant increase in the number of hot nights in Hong Kong from 2013 to 2023. All districts in Hong Kong experienced a big increase in the frequency of hot days and nights. Most significant increases are in the developed and closely packed urban environments, such as Tsim Sha Tsui, Kowloon City, and Eastern districts. ')