pyecharts地理信息可视化(3) -- 绘制动态轨迹

坐标点的采集

打开高德地图的地铁网页可以轻松得到北京地铁数据的接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
import json
import pprint

url = 'http://map.amap.com/service/subway?_1615466846985&srhdata=1100_drw_beijing.json'
response = requests.get(url)
result = json.loads(response.text)
stations = []
for i in result['l']:
station = []
for a in i['st']:
station.append([float(b) for b in a['sl'].split(',')])
stations.append(station)
pprint.pprint(stations)

坐标转换

在高德地图中获得的坐标点集合是使用的是GCJ-02坐标系,而下文可视化中会调用百度地图的接口,也就是需要在BD-09坐标系中进行可视化,所以需要做一次坐标转换。

1
2
3
4
5
6
7
8
9
10
11
12
13
pi = 3.1415926535897932384 
r_pi = pi * 3000.0/180.0

def gcj02_bd09(lon_gcj02, lat_gcj02):
b = math.sqrt(lon_gcj02 * lon_gcj02 + lat_gcj02 * lat_gcj02) + 0.00002 * math.sin(lat_gcj02 * r_pi)
o = math.atan2(lat_gcj02 , lon_gcj02) + 0.000003 * math.cos(lon_gcj02 * r_pi)
lon_bd09 = b * math.cos(o) + 0.0065
lat_bd09 = b * math.sin(o) + 0.006
return [lon_bd09, lat_bd09]

bd09_stations = []
for station in stations:
bd09_stations.append([gcj02_bd09(*point) for point in station])

绘制动态轨迹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
map_b = (
BMap(init_opts = opts.InitOpts(width = '800px', height = '600px'))
.add_schema(
baidu_ak = '********', # 百度地图开放平台appkey
center = [116.403963, 39.915119], # 当前视角的中心点(天安门)
zoom = 10, # 当前视角的缩放比例
is_roam = True, # 开启鼠标缩放和平移漫游
)
.add(
series_name = '',
type_ = ChartType.LINES, # 设置Geo图类型
data_pair = bd09_stations, # 数据项
is_polyline = True, # 是否是多段线
linestyle_opts = opts.LineStyleOpts(color = 'blue', opacity = 0.5, width = 1), # 线样式配置项
)
.add_control_panel(
maptype_control_opts = opts.BMapTypeControlOpts(type_ = BMapType.MAPTYPE_CONTROL_DROPDOWN), # 切换地图类型的控件
scale_control_opts = opts.BMapScaleControlOpts(), # 比例尺控件
overview_map_opts = opts.BMapOverviewMapControlOpts(is_open = True), # 添加缩略地图
navigation_control_opts = opts.BMapNavigationControlOpts() # 地图的平移缩放控件
)
)

map_b.render(path = 'subway_beijing.html')

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import json
import pprint
import math
from pyecharts.charts import BMap
from pyecharts import options as opts
from pyecharts.globals import BMapType, ChartType

#========================================================
# 获取数据
#========================================================

url = 'http://map.amap.com/service/subway?_1615466846985&srhdata=1100_drw_beijing.json'
response = requests.get(url)
result = json.loads(response.text)
stations = []
for i in result['l']:
station = []
for a in i['st']:
station.append([float(b) for b in a['sl'].split(',')])
stations.append(station)
pprint.pprint(stations)

#========================================================
# 坐标转换
#========================================================

pi = 3.1415926535897932384
r_pi = pi * 3000.0/180.0

def gcj02_bd09(lon_gcj02, lat_gcj02):
b = math.sqrt(lon_gcj02 * lon_gcj02 + lat_gcj02 * lat_gcj02) + 0.00002 * math.sin(lat_gcj02 * r_pi)
o = math.atan2(lat_gcj02 , lon_gcj02) + 0.000003 * math.cos(lon_gcj02 * r_pi)
lon_bd09 = b * math.cos(o) + 0.0065
lat_bd09 = b * math.sin(o) + 0.006
return [lon_bd09, lat_bd09]

bd09_stations = []
for station in stations:
bd09_stations.append([gcj02_bd09(*point) for point in station])

#========================================================
# 地图绘制
#========================================================

map_b = (
BMap(init_opts = opts.InitOpts(width = '800px', height = '600px'))
.add_schema(
baidu_ak = '********', # 百度地图开放平台appkey
center = [116.403963, 39.915119], # 当前视角的中心点(天安门)
zoom = 10, # 当前视角的缩放比例
is_roam = True, # 开启鼠标缩放和平移漫游
)
.add(
series_name = '',
type_ = ChartType.LINES, # 设置Geo图类型
data_pair = bd09_stations, # 数据项
is_polyline = True, # 是否是多段线
linestyle_opts = opts.LineStyleOpts(color = 'blue', opacity = 0.5, width = 1), # 线样式配置项
)
.add_control_panel(
maptype_control_opts = opts.BMapTypeControlOpts(type_ = BMapType.MAPTYPE_CONTROL_DROPDOWN), # 切换地图类型的控件
scale_control_opts = opts.BMapScaleControlOpts(), # 比例尺控件
overview_map_opts = opts.BMapOverviewMapControlOpts(is_open = True), # 添加缩略地图
navigation_control_opts = opts.BMapNavigationControlOpts() # 地图的平移缩放控件
)
)

map_b.render(path = 'subway_beijing.html')
0%