Shunfeng Zheng commited on
Commit
a6cd249
·
verified ·
1 Parent(s): 17e77ea

Delete 坐标可视.py

Browse files
Files changed (1) hide show
  1. 坐标可视.py +0 -77
坐标可视.py DELETED
@@ -1,77 +0,0 @@
1
- import streamlit as st
2
- import folium
3
- from streamlit_folium import folium_static
4
- import json
5
-
6
-
7
- def extract_coordinates(coords):
8
- """
9
- 只提取前两个数值(经度、纬度),并返回格式化后的坐标列表
10
- 兼容输入可能是 [(lon, lat, ...), (lon, lat, ...)]
11
- """
12
- filtered_coords = []
13
- for coord in coords:
14
- if isinstance(coord, (list, tuple)) and len(coord) >= 2:
15
- filtered_coords.append([float(coord[0]), float(coord[1])]) # 只取前两个数值(经度、纬度)
16
- return filtered_coords
17
-
18
-
19
- def draw_polygon(coords):
20
- """ 在 Streamlit 页面上显示地图,并绘制输入的多边形 """
21
- filtered_coords = extract_coordinates(coords)
22
-
23
- if not filtered_coords:
24
- st.error("无法解析有效的坐标点,请检查输入格式!")
25
- return
26
-
27
- # 计算中心点(取第一个点作为地图中心)
28
- centroid = filtered_coords[0]
29
-
30
- # 创建地图
31
- my_map = folium.Map(location=[centroid[1], centroid[0]], zoom_start=12)
32
-
33
- # 添加多边形
34
- folium.Polygon(
35
- locations=[(lat, lon) for lon, lat in filtered_coords], # Folium 坐标格式 (纬度, 经度)
36
- color="blue",
37
- fill=True,
38
- fill_color="green",
39
- fill_opacity=0.3,
40
- weight=2
41
- ).add_to(my_map)
42
-
43
- # 显示地图
44
- folium_static(my_map)
45
-
46
-
47
- # Streamlit 主函数
48
- def main():
49
- st.title("多边形地图可视化(支持列表和元组格式)")
50
-
51
- # 默认示例(同时包含列表和元组)
52
- default_input = '''[
53
- (151.0214153323324, -33.896092570872, 173.6675068465629),
54
- [151.022519, -33.897120, 172.53421],
55
- (151.023621, -33.898150, 170.49214),
56
- (151.0214153323324, -33.896092570872, 173.6675068465629)
57
- ]'''
58
-
59
- # 用户输入坐标集
60
- coord_input = st.text_area("请输入坐标集(JSON 格式)", default_input)
61
-
62
- # 创建按钮
63
- if st.button("显示地图"):
64
- try:
65
- # 解析用户输入的 JSON 格式坐标
66
- coords = json.loads(coord_input.replace("(", "[").replace(")", "]")) # 兼容 ( ) 格式
67
- if isinstance(coords, list) and all(
68
- isinstance(coord, (list, tuple)) and len(coord) >= 2 for coord in coords):
69
- draw_polygon(coords)
70
- else:
71
- st.error("请输入正确格式的坐标集,例如:[(经度, 纬度, 可选值), ...] 或 [[经度, 纬度, 可选值], ...]")
72
- except json.JSONDecodeError:
73
- st.error("请输入有效的 JSON 格式数据!")
74
-
75
-
76
- if __name__ == "__main__":
77
- main()