Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import pandas as pd
|
|
6 |
import io
|
7 |
from PIL import Image
|
8 |
import numpy as np
|
|
|
9 |
|
10 |
def meters_to_degrees(meters, lat):
|
11 |
# 緯度1度あたりの距離は常に約111.32km
|
@@ -20,8 +21,22 @@ def create_artistic_map(
|
|
20 |
lat, lon, distance, dpi, width, height, bg_color, greenery_color, water_color,
|
21 |
building_colors, major_road_color, medium_road_color, minor_road_color,
|
22 |
display_greenery, display_water, display_buildings,
|
23 |
-
display_major_roads, display_medium_roads, display_minor_roads
|
|
|
24 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# 中心点の設定
|
26 |
point = (lat, lon)
|
27 |
|
@@ -35,6 +50,15 @@ def create_artistic_map(
|
|
35 |
fig.patch.set_facecolor(bg_color)
|
36 |
ax.set_facecolor(bg_color)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# 緑地の描画
|
39 |
if display_greenery:
|
40 |
# 緑地データ取得
|
@@ -57,6 +81,15 @@ def create_artistic_map(
|
|
57 |
if greenery_list:
|
58 |
greenery = pd.concat(greenery_list)
|
59 |
greenery.plot(ax=ax, facecolor=greenery_color, edgecolor='none')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# 水域の描画
|
62 |
if display_water:
|
@@ -76,6 +109,15 @@ def create_artistic_map(
|
|
76 |
if water_list:
|
77 |
water = pd.concat(water_list)
|
78 |
water.plot(ax=ax, facecolor=water_color, edgecolor='none')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# 建物の描画
|
81 |
if display_buildings:
|
@@ -87,6 +129,15 @@ def create_artistic_map(
|
|
87 |
building_subset = buildings[buildings['building'] == building_type]
|
88 |
building_color = building_colors[i % len(building_colors)]
|
89 |
building_subset.plot(ax=ax, facecolor=building_color, edgecolor='none')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
# 道路の描画
|
92 |
if display_major_roads or display_medium_roads or display_minor_roads:
|
@@ -99,31 +150,51 @@ def create_artistic_map(
|
|
99 |
major_roads = edges[edges['highway'].isin(['motorway', 'trunk', 'primary'])]
|
100 |
if not major_roads.empty:
|
101 |
major_roads.plot(ax=ax, linewidth=2, edgecolor=major_road_color)
|
102 |
-
|
103 |
# 中程度の道路の描画
|
104 |
if display_medium_roads:
|
105 |
medium_roads = edges[edges['highway'].isin(['secondary', 'tertiary'])]
|
106 |
if not medium_roads.empty:
|
107 |
medium_roads.plot(ax=ax, linewidth=1.5, edgecolor=medium_road_color)
|
108 |
-
|
109 |
# 小規模な道路の描画
|
110 |
if display_minor_roads:
|
111 |
minor_roads = edges[~edges['highway'].isin(['motorway', 'trunk', 'primary', 'secondary', 'tertiary'])]
|
112 |
if not minor_roads.empty:
|
113 |
minor_roads.plot(ax=ax, linewidth=1, edgecolor=minor_road_color)
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# 軸をオフに設定
|
116 |
ax.axis('off')
|
117 |
-
|
118 |
# 表示範囲を設定
|
119 |
ax.set_xlim([lon - lon_deg, lon + lon_deg])
|
120 |
ax.set_ylim([lat - lat_deg, lat + lat_deg])
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
# バッファに保存して、PIL画像として返す
|
123 |
buf = io.BytesIO()
|
124 |
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=dpi)
|
125 |
plt.close()
|
126 |
buf.seek(0)
|
|
|
|
|
|
|
|
|
|
|
127 |
return Image.open(buf)
|
128 |
|
129 |
# Streamlit UI
|
@@ -203,12 +274,15 @@ else:
|
|
203 |
|
204 |
# マップ生成ボタン
|
205 |
if st.button("マップを生成"):
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
|
6 |
import io
|
7 |
from PIL import Image
|
8 |
import numpy as np
|
9 |
+
import time
|
10 |
|
11 |
def meters_to_degrees(meters, lat):
|
12 |
# 緯度1度あたりの距離は常に約111.32km
|
|
|
21 |
lat, lon, distance, dpi, width, height, bg_color, greenery_color, water_color,
|
22 |
building_colors, major_road_color, medium_road_color, minor_road_color,
|
23 |
display_greenery, display_water, display_buildings,
|
24 |
+
display_major_roads, display_medium_roads, display_minor_roads,
|
25 |
+
progress_bar, time_placeholder
|
26 |
):
|
27 |
+
start_time = time.time()
|
28 |
+
current_step = 0
|
29 |
+
total_steps = 2 # 基本のステップ数
|
30 |
+
|
31 |
+
if display_greenery:
|
32 |
+
total_steps += 1
|
33 |
+
if display_water:
|
34 |
+
total_steps += 1
|
35 |
+
if display_buildings:
|
36 |
+
total_steps += 1
|
37 |
+
if display_major_roads or display_medium_roads or display_minor_roads:
|
38 |
+
total_steps += 1
|
39 |
+
|
40 |
# 中心点の設定
|
41 |
point = (lat, lon)
|
42 |
|
|
|
50 |
fig.patch.set_facecolor(bg_color)
|
51 |
ax.set_facecolor(bg_color)
|
52 |
|
53 |
+
# 進捗を更新
|
54 |
+
current_step += 1
|
55 |
+
elapsed_time = time.time() - start_time
|
56 |
+
estimated_total_time = (elapsed_time / current_step) * total_steps
|
57 |
+
remaining_time = estimated_total_time - elapsed_time
|
58 |
+
progress_percent = int((current_step / total_steps) * 100)
|
59 |
+
progress_bar.progress(progress_percent)
|
60 |
+
time_placeholder.text(f"残り時間: 約{int(remaining_time // 60)}分{int(remaining_time % 60)}秒")
|
61 |
+
|
62 |
# 緑地の描画
|
63 |
if display_greenery:
|
64 |
# 緑地データ取得
|
|
|
81 |
if greenery_list:
|
82 |
greenery = pd.concat(greenery_list)
|
83 |
greenery.plot(ax=ax, facecolor=greenery_color, edgecolor='none')
|
84 |
+
|
85 |
+
# 進捗を更新
|
86 |
+
current_step += 1
|
87 |
+
elapsed_time = time.time() - start_time
|
88 |
+
estimated_total_time = (elapsed_time / current_step) * total_steps
|
89 |
+
remaining_time = estimated_total_time - elapsed_time
|
90 |
+
progress_percent = int((current_step / total_steps) * 100)
|
91 |
+
progress_bar.progress(progress_percent)
|
92 |
+
time_placeholder.text(f"残り時間: 約{int(remaining_time // 60)}分{int(remaining_time % 60)}秒")
|
93 |
|
94 |
# 水域の描画
|
95 |
if display_water:
|
|
|
109 |
if water_list:
|
110 |
water = pd.concat(water_list)
|
111 |
water.plot(ax=ax, facecolor=water_color, edgecolor='none')
|
112 |
+
|
113 |
+
# 進捗を更新
|
114 |
+
current_step += 1
|
115 |
+
elapsed_time = time.time() - start_time
|
116 |
+
estimated_total_time = (elapsed_time / current_step) * total_steps
|
117 |
+
remaining_time = estimated_total_time - elapsed_time
|
118 |
+
progress_percent = int((current_step / total_steps) * 100)
|
119 |
+
progress_bar.progress(progress_percent)
|
120 |
+
time_placeholder.text(f"残り時間: 約{int(remaining_time // 60)}分{int(remaining_time % 60)}秒")
|
121 |
|
122 |
# 建物の描画
|
123 |
if display_buildings:
|
|
|
129 |
building_subset = buildings[buildings['building'] == building_type]
|
130 |
building_color = building_colors[i % len(building_colors)]
|
131 |
building_subset.plot(ax=ax, facecolor=building_color, edgecolor='none')
|
132 |
+
|
133 |
+
# 進捗を更新
|
134 |
+
current_step += 1
|
135 |
+
elapsed_time = time.time() - start_time
|
136 |
+
estimated_total_time = (elapsed_time / current_step) * total_steps
|
137 |
+
remaining_time = estimated_total_time - elapsed_time
|
138 |
+
progress_percent = int((current_step / total_steps) * 100)
|
139 |
+
progress_bar.progress(progress_percent)
|
140 |
+
time_placeholder.text(f"残り時間: 約{int(remaining_time // 60)}分{int(remaining_time % 60)}秒")
|
141 |
|
142 |
# 道路の描画
|
143 |
if display_major_roads or display_medium_roads or display_minor_roads:
|
|
|
150 |
major_roads = edges[edges['highway'].isin(['motorway', 'trunk', 'primary'])]
|
151 |
if not major_roads.empty:
|
152 |
major_roads.plot(ax=ax, linewidth=2, edgecolor=major_road_color)
|
153 |
+
|
154 |
# 中程度の道路の描画
|
155 |
if display_medium_roads:
|
156 |
medium_roads = edges[edges['highway'].isin(['secondary', 'tertiary'])]
|
157 |
if not medium_roads.empty:
|
158 |
medium_roads.plot(ax=ax, linewidth=1.5, edgecolor=medium_road_color)
|
159 |
+
|
160 |
# 小規模な道路の描画
|
161 |
if display_minor_roads:
|
162 |
minor_roads = edges[~edges['highway'].isin(['motorway', 'trunk', 'primary', 'secondary', 'tertiary'])]
|
163 |
if not minor_roads.empty:
|
164 |
minor_roads.plot(ax=ax, linewidth=1, edgecolor=minor_road_color)
|
165 |
+
|
166 |
+
# 進捗を更新
|
167 |
+
current_step += 1
|
168 |
+
elapsed_time = time.time() - start_time
|
169 |
+
estimated_total_time = (elapsed_time / current_step) * total_steps
|
170 |
+
remaining_time = estimated_total_time - elapsed_time
|
171 |
+
progress_percent = int((current_step / total_steps) * 100)
|
172 |
+
progress_bar.progress(progress_percent)
|
173 |
+
time_placeholder.text(f"残り時間: 約{int(remaining_time // 60)}分{int(remaining_time % 60)}秒")
|
174 |
+
|
175 |
# 軸をオフに設定
|
176 |
ax.axis('off')
|
177 |
+
|
178 |
# 表示範囲を設定
|
179 |
ax.set_xlim([lon - lon_deg, lon + lon_deg])
|
180 |
ax.set_ylim([lat - lat_deg, lat + lat_deg])
|
181 |
+
|
182 |
+
# 最終処理の進捗を更新
|
183 |
+
current_step += 1
|
184 |
+
progress_percent = int((current_step / total_steps) * 100)
|
185 |
+
progress_bar.progress(progress_percent)
|
186 |
+
time_placeholder.text("最終処理中...")
|
187 |
+
|
188 |
# バッファに保存して、PIL画像として返す
|
189 |
buf = io.BytesIO()
|
190 |
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=dpi)
|
191 |
plt.close()
|
192 |
buf.seek(0)
|
193 |
+
|
194 |
+
# 進捗を100%に更新
|
195 |
+
progress_bar.progress(100)
|
196 |
+
time_placeholder.text("完了しました!")
|
197 |
+
|
198 |
return Image.open(buf)
|
199 |
|
200 |
# Streamlit UI
|
|
|
274 |
|
275 |
# マップ生成ボタン
|
276 |
if st.button("マップを生成"):
|
277 |
+
# 進捗バーと残り時間のプレースホルダーを初期化
|
278 |
+
progress_bar = st.progress(0)
|
279 |
+
time_placeholder = st.empty()
|
280 |
+
map_image = create_artistic_map(
|
281 |
+
lat, lon, distance*1000, dpi, width, height,
|
282 |
+
bg_color, greenery_color, water_color, building_colors,
|
283 |
+
major_road_color, medium_road_color, minor_road_color,
|
284 |
+
display_greenery, display_water, display_buildings,
|
285 |
+
display_major_roads, display_medium_roads, display_minor_roads,
|
286 |
+
progress_bar, time_placeholder
|
287 |
+
)
|
288 |
+
st.image(map_image, caption="生成されたアートマップ", use_column_width=True)
|