Spaces:
Runtime error
Runtime error
File size: 7,062 Bytes
d4bea00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
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. ')
|