naohiro701 commited on
Commit
5b0fd14
·
verified ·
1 Parent(s): e22abef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import osmnx as ox
2
+ import matplotlib.pyplot as plt
3
+ import geopandas as gpd
4
+ import pandas as pd
5
+ import random
6
+ import io
7
+ from PIL import Image
8
+ import streamlit as st
9
+
10
+ def adjust_color_hex(hex_color, variation=20):
11
+ """
12
+ 元の16進数カラーコードに対してランダムな変化を加えた新しいカラーコードを生成します。
13
+ Parameters:
14
+ - hex_color (str): 元の16進数カラーコード(例:'#A3BE8C')
15
+ - variation (int): 各RGB成分に加える変化の最大値(0-255)
16
+ Returns:
17
+ - str: 新しい16進数カラーコード
18
+ """
19
+ hex_color = hex_color.lstrip('#')
20
+ r = int(hex_color[0:2], 16)
21
+ g = int(hex_color[2:4], 16)
22
+ b = int(hex_color[4:6], 16)
23
+
24
+ r = min(255, max(0, r + random.randint(-variation, variation)))
25
+ g = min(255, max(0, g + random.randint(-variation, variation)))
26
+ b = min(255, max(0, b + random.randint(-variation, variation)))
27
+
28
+ new_hex_color = '#{:02X}{:02X}{:02X}'.format(r, g, b)
29
+ return new_hex_color
30
+
31
+ def create_artistic_map(lat, lon, distance):
32
+ """
33
+ 指定した緯度経度と距離に基づいて、色鮮やかなアート風の地図画像を作成します。
34
+ Parameters:
35
+ - lat (float): 中心の緯度
36
+ - lon (float): 中心の経度
37
+ - distance (float): 地図の範囲(メートル単位)
38
+ Returns:
39
+ - PIL.Image.Image: 生成された地図画像
40
+ """
41
+ point = (lat, lon)
42
+
43
+ G = ox.graph_from_point(point, dist=distance, network_type='all')
44
+ nodes, edges = ox.graph_to_gdfs(G)
45
+ buildings = ox.features_from_point(point, tags={'building': True}, dist=distance)
46
+ water = ox.features_from_point(point, tags={'natural': 'water'}, dist=distance)
47
+
48
+ greenery_list = []
49
+ greenery_tags = [
50
+ {'leisure': 'park'},
51
+ {'landuse': 'grass'},
52
+ {'landuse': 'forest'},
53
+ {'natural': 'wood'}
54
+ ]
55
+ for tags in greenery_tags:
56
+ greenery_part = ox.features_from_point(point, tags=tags, dist=distance)
57
+ if not greenery_part.empty:
58
+ greenery_list.append(greenery_part)
59
+
60
+ if greenery_list:
61
+ greenery = pd.concat(greenery_list)
62
+ else:
63
+ greenery = gpd.GeoDataFrame()
64
+
65
+ fig, ax = plt.subplots(figsize=(20, 20))
66
+ bg_color = adjust_color_hex('#2E3440', variation=10)
67
+ fig.patch.set_facecolor(bg_color)
68
+ ax.set_facecolor(bg_color)
69
+
70
+ if not greenery.empty:
71
+ greenery_color = adjust_color_hex('#A3BE8C')
72
+ greenery.plot(ax=ax, facecolor=greenery_color, edgecolor='none')
73
+
74
+ if not water.empty:
75
+ water_color = adjust_color_hex('#81A1C1')
76
+ water.plot(ax=ax, facecolor=water_color, edgecolor='none')
77
+
78
+ if not buildings.empty:
79
+ building_color = adjust_color_hex('#D08770')
80
+ buildings.plot(ax=ax, facecolor=building_color, edgecolor='none')
81
+
82
+ if not edges.empty and 'highway' in edges.columns:
83
+ major_roads = edges[edges['highway'].isin(['motorway', 'trunk', 'primary'])]
84
+ if not major_roads.empty:
85
+ major_road_color = adjust_color_hex('#88C0D0')
86
+ major_roads.plot(ax=ax, linewidth=2, edgecolor=major_road_color)
87
+
88
+ medium_roads = edges[edges['highway'].isin(['secondary', 'tertiary'])]
89
+ if not medium_roads.empty:
90
+ medium_road_color = adjust_color_hex('#5E81AC')
91
+ medium_roads.plot(ax=ax, linewidth=1.5, edgecolor=medium_road_color)
92
+
93
+ minor_roads = edges[~edges['highway'].isin(['motorway', 'trunk', 'primary', 'secondary', 'tertiary'])]
94
+ if not minor_roads.empty:
95
+ minor_road_color = adjust_color_hex('#4C566A')
96
+ minor_roads.plot(ax=ax, linewidth=1, edgecolor=minor_road_color)
97
+
98
+ ax.axis('off')
99
+
100
+ buf = io.BytesIO()
101
+ plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=300)
102
+ plt.close()
103
+ buf.seek(0)
104
+ img = Image.open(buf)
105
+ return img
106
+
107
+ # Streamlit app setup
108
+ st.title("Artistic Map Generator")
109
+
110
+ st.write("Generate an artistic map based on the specified latitude, longitude, and distance.")
111
+
112
+ # User inputs
113
+ lat = st.number_input("Latitude", value=35.6895)
114
+ lon = st.number_input("Longitude", value=139.6917)
115
+ distance = st.number_input("Distance (meters)", value=1000)
116
+
117
+ if st.button("Generate Map"):
118
+ with st.spinner("Generating map..."):
119
+ img = create_artistic_map(lat, lon, distance)
120
+ st.image(img, caption="Generated Artistic Map", use_column_width=True)