OttoYu commited on
Commit
6593e5f
·
verified ·
1 Parent(s): 8f4b7d8

Upload 4 files

Browse files
6_Heat island.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import folium
3
+ import json
4
+ import plotly.express as px
5
+ import pandas as pd
6
+ from streamlit_folium import st_folium
7
+ import plotly.graph_objs as go
8
+
9
+ st.set_page_config(layout="wide", page_title="Heat Island Effect Analysis")
10
+
11
+ def load_geojson(filepath):
12
+ with open(filepath, 'r', encoding='utf-8') as f:
13
+ return json.load(f)
14
+
15
+ def plot_geojson(feature_group, geojson_data, property_name, colormap):
16
+ folium.GeoJson(
17
+ geojson_data,
18
+ style_function=lambda feature: {
19
+ 'fillColor': colormap(feature['properties'][property_name]),
20
+ 'color': 'black',
21
+ 'weight': 1,
22
+ 'fillOpacity': 0.7,
23
+ },
24
+ popup=folium.GeoJsonPopup(fields=['NAME_EN', property_name], aliases=['District:', 'Value:']),
25
+ ).add_to(feature_group)
26
+
27
+ def compute_difference_geojson(geojson_2013, geojson_2023):
28
+ difference_geojson = {"type": "FeatureCollection", "features": []}
29
+
30
+ name_to_hot_nights_2013 = {
31
+ feature['properties']['NAME_EN']: feature['properties']['Hot_Nights']
32
+ for feature in geojson_2013['features']
33
+ }
34
+
35
+ for feature in geojson_2023['features']:
36
+ name_en = feature['properties']['NAME_EN']
37
+ hot_nights_2013 = name_to_hot_nights_2013.get(name_en, 0)
38
+ hot_nights_2023 = feature['properties']['Hot_Nights']
39
+ difference = hot_nights_2023 - hot_nights_2013
40
+
41
+ feature['properties']['Difference'] = difference
42
+ difference_geojson['features'].append(feature)
43
+
44
+ return difference_geojson
45
+
46
+ def geojson_to_dataframe(geojson_data, year):
47
+ features = geojson_data['features']
48
+ data = {
49
+ 'District': [feature['properties']['NAME_EN'] for feature in features],
50
+ 'Hot_Nights': [feature['properties']['Hot_Nights'] for feature in features],
51
+ 'Year': [year] * len(features) # Add year column
52
+ }
53
+ return pd.DataFrame(data)
54
+
55
+ geojson_2013 = load_geojson('ref/2013_hot.geojson')
56
+ geojson_2023 = load_geojson('ref/2023_hot.geojson')
57
+
58
+ hot_nights_2013 = [feature['properties']['Hot_Nights'] for feature in geojson_2013['features']]
59
+ hot_nights_2023 = [feature['properties']['Hot_Nights'] for feature in geojson_2023['features']]
60
+ all_hot_nights = hot_nights_2013 + hot_nights_2023
61
+
62
+ colormap = folium.LinearColormap(
63
+ colors=['white', 'orange', 'red'],
64
+ vmin=min(all_hot_nights),
65
+ vmax=max(all_hot_nights),
66
+ caption='Hot Nights'
67
+ )
68
+
69
+ difference_geojson = compute_difference_geojson(geojson_2013, geojson_2023)
70
+
71
+ diff_colormap = folium.LinearColormap(
72
+ colors=['blue', 'lightblue', 'white', 'pink', 'red'],
73
+ index=[-50, -10, 0, 10, 50],
74
+ vmin=-50,
75
+ vmax=50,
76
+ caption='Change in Hot Nights'
77
+ )
78
+
79
+ m = folium.Map(location=[22.35994791346238, 114.15924623933743], zoom_start=11, tiles='CartoDB positron')
80
+
81
+ feature_group_2013 = folium.FeatureGroup(name='2013 Hot Nights', show=False)
82
+ feature_group_2023 = folium.FeatureGroup(name='2023 Hot Nights', show=False)
83
+ feature_group_diff = folium.FeatureGroup(name='Change in Hot Nights', show=True)
84
+
85
+ plot_geojson(feature_group_2013, geojson_2013, 'Hot_Nights', colormap)
86
+ plot_geojson(feature_group_2023, geojson_2023, 'Hot_Nights', colormap)
87
+ plot_geojson(feature_group_diff, difference_geojson, 'Difference', diff_colormap)
88
+
89
+ feature_group_2013.add_to(m)
90
+ feature_group_2023.add_to(m)
91
+ feature_group_diff.add_to(m)
92
+
93
+ layer_control = folium.LayerControl().add_to(m)
94
+
95
+ colormap.add_to(m)
96
+ diff_colormap.add_to(m)
97
+
98
+ df_2013 = geojson_to_dataframe(geojson_2013, '2013')
99
+ df_2023 = geojson_to_dataframe(geojson_2023, '2023')
100
+
101
+ combined_df = pd.concat([df_2013, df_2023])
102
+
103
+ def plot_combined_box_plot(df):
104
+ fig = px.box(
105
+ df,
106
+ x='Year',
107
+ y='Hot_Nights',
108
+ title='Hot Nights (2013 vs 2023)',
109
+ labels={'Hot_Nights': 'Number of Hot Nights', 'Year': 'Year'},
110
+ color='Year'
111
+ )
112
+ fig.update_layout(
113
+ yaxis_title='Number of Hot Nights',
114
+ boxmode='group'
115
+ )
116
+ return fig
117
+
118
+ data_table = pd.read_csv('ref/final_summary_with_available_stations.csv')
119
+
120
+ stations = data_table['station_name'].unique()
121
+
122
+ col1, col2, col3 = st.columns([1.35, 2, 1.1])
123
+
124
+ with col1:
125
+ st.subheader('Heat Island Effect')
126
+ st.caption(
127
+ 'The "heat island effect" refers to the temperature difference between urban and rural areas, particularly at night.')
128
+ st.caption(
129
+ '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.')
130
+
131
+ selected_station = st.selectbox('Select a Station:', options=stations)
132
+
133
+ filtered_data_table = data_table[data_table['station_name'] == selected_station]
134
+
135
+ fig = go.Figure()
136
+
137
+ fig.add_trace(go.Scatter(
138
+ x=filtered_data_table['month'],
139
+ y=filtered_data_table['13day_temp'],
140
+ mode='lines+markers',
141
+ name='2013 Day Temp',
142
+ line=dict(color='blue')
143
+ ))
144
+ fig.add_trace(go.Scatter(
145
+ x=filtered_data_table['month'],
146
+ y=filtered_data_table['13night_temp'],
147
+ mode='lines+markers',
148
+ name='2013 Night Temp',
149
+ line=dict(color='blue', dash='dash')
150
+ ))
151
+ fig.add_trace(go.Scatter(
152
+ x=filtered_data_table['month'],
153
+ y=filtered_data_table['23day_temp'],
154
+ mode='lines+markers',
155
+ name='2023 Day Temp',
156
+ line=dict(color='red')
157
+ ))
158
+ fig.add_trace(go.Scatter(
159
+ x=filtered_data_table['month'],
160
+ y=filtered_data_table['23night_temp'],
161
+ mode='lines+markers',
162
+ name='2023 Night Temp',
163
+ line=dict(color='red', dash='dash')
164
+ ))
165
+
166
+ fig.update_layout(
167
+ title=f'Temperature Comparison',
168
+ xaxis_title='Month',
169
+ yaxis_title='Temperature (°C)',
170
+ legend_title='Legend',
171
+ height =300
172
+ )
173
+
174
+ st.plotly_chart(fig, height=180)
175
+
176
+ with col2:
177
+ st_folium(m, width=450, height=650)
178
+
179
+ with col3:
180
+ st.caption(
181
+ 'From data from the CO-WIN network, there has been a significant increase in the number of hot nights in Hong Kong. "Hot nights" refers to nights where the temperature remains above 28 degrees. Within the period from 2013 to 2023, 9 districts in Hong Kong have experienced an increase in the frequency of hot nights, the most significant are those in the urban.')
182
+
183
+ st.plotly_chart(plot_combined_box_plot(combined_df), use_container_width=True ,height=380)
ref/2013_hot.geojson ADDED
The diff for this file is too large to render. See raw diff
 
ref/2023_hot.geojson ADDED
The diff for this file is too large to render. See raw diff
 
ref/final_summary_with_available_stations.csv ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ station_id,month,13day_temp,13night_temp,13temp_variation,23day_temp,23night_temp,23temp_variation,diff,station_name,latitude,longitude
2
+ 6003,5,26.905058365758755,25.414201183431956,1.4908571823268026,26.974954627949185,25.56833333333333,1.4066212946158494,-0.084235888,Po Leung Kuk Wu Chung College,22.41633979,114.2271951
3
+ 6003,6,29.566290018832397,28.18888888888889,1.3774011299435038,29.41703703703704,27.83277777777777,1.5842592592592624,0.206858129,Po Leung Kuk Wu Chung College,22.41633979,114.2271951
4
+ 6003,7,29.260877513711154,27.87722222222222,1.3836552914889353,30.663799283154123,28.529032258064515,2.1347670250896087,0.751111734,Po Leung Kuk Wu Chung College,22.41633979,114.2271951
5
+ 6003,8,29.896616541353385,28.42303370786517,1.4735828334882155,29.977419354838712,28.288709677419355,1.6887096774193573,0.215126844,Po Leung Kuk Wu Chung College,22.41633979,114.2271951
6
+ 6003,9,28.985852713178296,27.5,1.4858527131782964,28.74611111111111,27.45555555555556,1.290555555555553,-0.195297158,Po Leung Kuk Wu Chung College,22.41633979,114.2271951
7
+ 6028,5,25.13046594982079,24.08333333333333,1.047132616487456,25.51049504950495,24.39135802469136,1.1191370248135932,0.072004408,Ho Koon Education Center,22.38384169,114.1079789
8
+ 6028,6,27.449444444444445,26.39888888888889,1.0505555555555546,28.04143126177025,26.93793103448276,1.103500227287487,0.052944672,Ho Koon Education Center,22.38384169,114.1079789
9
+ 6028,7,27.065326633165828,25.87121212121212,1.1941145119537069,29.01344902386117,27.480666666666668,1.532782357194506,0.338667845,Ho Koon Education Center,22.38384169,114.1079789
10
+ 6028,8,27.81200716845878,26.69301075268817,1.118996416,28.500208768267225,27.14451612903225,1.3556926392349702,0.236696223,Ho Koon Education Center,22.38384169,114.1079789
11
+ 6028,9,27.149414519906323,25.708695652173915,1.4407188677324072,27.71090226,26.379310344827587,1.331591910811511,-0.109126957,Ho Koon Education Center,22.38384169,114.1079789
12
+ 6029,5,26.976164874551973,26.053763440860216,0.922401434,26.10799180327869,25.320987654320987,0.7870041489577027,-0.135397285,Lingnan Hang Yee Memorial Secondary School,22.26343722,114.2511955
13
+ 6029,6,29.96703703703704,28.76555555555556,1.2014814814814798,28.93746397694524,27.85,1.0874639769452408,-0.114017505,Lingnan Hang Yee Memorial Secondary School,22.26343722,114.2511955
14
+ 6029,7,29.87078853046595,28.58279569892473,1.2879928315412208,30.448816568047334,29.060185185185187,1.388631382862151,0.100638551,Lingnan Hang Yee Memorial Secondary School,22.26343722,114.2511955
15
+ 6029,8,30.543726235741445,29.39080459770115,1.1529216380402971,29.953016453382084,28.59722222222222,1.3557942311598623,0.202872593,Lingnan Hang Yee Memorial Secondary School,22.26343722,114.2511955
16
+ 6029,9,29.637874659400545,28.3456,1.2922746594005474,28.380185185185184,27.486666666666668,0.8935185185185155,-0.398756141,Lingnan Hang Yee Memorial Secondary School,22.26343722,114.2511955
17
+ 6062,5,25.889068100358426,24.15537634408602,1.7336917562724032,26.86244444444445,24.95694444444445,1.9055,0.171808244,Scout Association of Hong Kong (Tung Tsz Scout Centre),22.47311,114.19801
18
+ 6062,6,28.438333333333336,26.46222222222222,1.9761111111111165,28.849244060475165,26.626666666666665,2.222577394,0.246466283,Scout Association of Hong Kong (Tung Tsz Scout Centre),22.47311,114.19801
19
+ 6062,7,28.248387096774195,26.16989247311828,2.0784946236559136,29.86229508196721,27.023076923076925,2.8392181588902865,0.760723535,Scout Association of Hong Kong (Tung Tsz Scout Centre),22.47311,114.19801
20
+ 6062,8,28.69616858237548,26.79768786127168,1.8984807211038015,29.41398467,26.97241379310345,2.441570881226056,0.54309016,Scout Association of Hong Kong (Tung Tsz Scout Centre),22.47311,114.19801
21
+ 6062,9,28.48260869565217,26.199029126213592,2.2835795694385794,28.313894736842105,26.46993464052288,1.843960096319229,-0.439619473,Scout Association of Hong Kong (Tung Tsz Scout Centre),22.47311,114.19801