import streamlit as st import geopandas as gpd import pydeck as pdk import pandas as pd from branca import colormap as cm import pathlib import os import requests import json from shapely.geometry import shape import matplotlib.pyplot as plt import matplotlib.colors as mcolors import io import base64 import numpy as np st.set_page_config(layout="wide") # Data source prefectures = { "北海道": "Hokkaido", "青森県": "Aomori", "岩手県": "Iwate", "宮城県": "Miyagi", "秋田県": "Akita", "山形県": "Yamagata", "福島県": "Fukushima", "茨城県": "Ibaraki", "栃木県": "Tochigi", "群馬県": "Gunma", "埼玉県": "Saitama", "千葉県": "Chiba", "東京都": "Tokyo", "神奈川県": "Kanagawa", "新潟県": "Niigata", "富山県": "Toyama", "石川県": "Ishikawa", "福井県": "Fukui", "山梨県": "Yamanashi", "長野県": "Nagano", "岐阜県": "Gifu", "静岡県": "Shizuoka", "愛知県": "Aichi", "三重県": "Mie", "滋賀県": "Shiga", "京都府": "Kyoto", "大阪府": "Osaka", "兵庫県": "Hyogo", "奈良県": "Nara", "和歌山県": "Wakayama", "鳥取県": "Tottori", "島根県": "Shimane", "岡山県": "Okayama", "広島県": "Hiroshima", "山口県": "Yamaguchi", "徳島県": "Tokushima", "香川県": "Kagawa", "愛媛県": "Ehime", "高知県": "Kochi", "福岡県": "Fukuoka", "佐賀県": "Saga", "長崎県": "Nagasaki", "熊本県": "Kumamoto", "大分県": "Oita", "宮崎県": "Miyazaki", "鹿児島県": "Kagoshima", "沖縄県": "Okinawa" } data_links = {pref_jp: f"https://raw.githubusercontent.com/kunifujiwara/data/master/frac_veg/FRAC_VEG_{pref_en}.geojson" for pref_jp, pref_en in prefectures.items()} @st.cache_data def get_geom_data(prefecture): response = requests.get(data_links[prefecture]) if response.status_code == 200: geojson_data = json.loads(response.content) gdf = gpd.GeoDataFrame.from_features(geojson_data['features']) return gdf else: st.error(f"Failed to fetch data for {prefecture}. Status code: {response.status_code}") return None def create_discontinuous_colormap(n_colors): cmap = plt.get_cmap('Greens') colors = [cmap(i / (n_colors - 1)) for i in range(n_colors)] return mcolors.ListedColormap(colors) def create_colormap_legend(vmin, vmax, cmap, n_colors): fig, ax = plt.subplots(figsize=(6, 0.8)) fig.subplots_adjust(bottom=0.5) bounds = np.linspace(vmin, vmax, n_colors + 1) norm = mcolors.BoundaryNorm(bounds, cmap.N) cbar = fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation='horizontal', spacing='proportional', boundaries=bounds, format='%.0f') cbar.set_ticks(bounds) # ax.set_title("緑被率 %", fontsize=10, pad=10) buf = io.BytesIO() plt.savefig(buf, format='png', dpi=300, bbox_inches='tight') plt.close(fig) return base64.b64encode(buf.getvalue()).decode() def calculate_zoom_level(bbox): lon_range = bbox[2] - bbox[0] lat_range = bbox[3] - bbox[1] max_range = max(lon_range, lat_range) zoom = int(np.log2(360 / max_range)) - 1 return min(max(1, zoom), 20) # Clamp zoom between 1 and 20 def app(): st.title("日本全国緑被率マップ (町丁目別)") prefecture = st.selectbox("都道府県", list(prefectures.keys())) gdf = get_geom_data(prefecture) if gdf is None: st.error("Failed to load data. Please try again later.") return # Convert FRAC_VEG to percentage gdf['FRAC_VEG_PERCENT'] = gdf['FRAC_VEG'] * 100 # City filter cities = sorted(gdf['CITY_NAME'].unique().tolist()) selected_cities = st.multiselect("市区町村", cities, default=[]) # Filter GeoDataFrame based on selected cities if selected_cities: gdf_filtered = gdf[gdf['CITY_NAME'].isin(selected_cities)] else: gdf_filtered = gdf selected_attribute = "FRAC_VEG_PERCENT" # Custom CSS to create a box around the color scale controls st.markdown(""" """, unsafe_allow_html=True) # Color scale controls in an expander with st.expander("カラースケール設定", expanded=True): # st.markdown('