pyecharts中显示浙江省所有县市区地图

前言

上一篇《pyecharts中自定义geojson来创建地图》介绍了怎么通过自定义geojson来生成我们自定义的地图,这一篇我们来看看怎么通过自定义geojson生成全省各县市区的地图。

PS: 如果使用pyecharts自带的库全省的地图最小显示到市级别,无法显示县区;如果遇到的特殊需求,显然使用默认pyecharts库的结果是无法满足的。这就需要我们重写pyecharts的库文件来实现我们的需求。

步骤

1、首先需要创建省以及各市的geojson数据文件,我们这里以浙江省以为例,首先创建zhejiang_geojson.json文件来存放浙江省中所有的市信息,然后在依次创建浙江省杭州市下的区县信息hangzhou_geojson.json,同样的操作创建其他市的geojson信息。这里省略。如果不知道怎么创建geojson数据,可以参考上一篇《pyecharts中自定义geojson来创建地图》

创建全省、市 geojson

接着创建一份zhejiang_cities_districts_geojson.json,将全省geojson(zhejiang_geojson.json)以及各市geojson(hangzhou_geojson.json、huzhou_geojson.json……)拷贝到zhejiang_cities_districts_geojson.json文件中,需要注意json格式要正确

2、编写python代码创建自定义地图

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import random

from pyecharts.charts import Map
from pyecharts import options as opts


def values(rge, start: int = 20, end: int = 150) -> list:
ar = [random.randint(start, end) for _ in range(rge)]
print(ar)
return ar


hangzhou = ["滨江区", "淳安县", "富阳区", "拱墅区", "建德市", "临安区", "临平区", "钱塘区", "上城区", "桐庐县", "西湖区", "萧山区", "临平区", "余杭区"]
ningbo = ["海曙区", "江北区", "北仑区", "镇海区", "鄞州区", "奉化区", "象山县", "宁海县", "余姚市", "慈溪市"]
wenzhou = ["鹿城区", "龙湾区", "瓯海区", "洞头区", "永嘉县", "平阳县", "苍南县", "文成县", "泰顺县", "瑞安市", "乐清市", "龙港市"]
jiaxing = ["南湖区", "秀洲区", "嘉善县", "海盐县", "海宁市", "平湖市", "桐乡市"]
huzhou = ["吴兴区", "南浔区", "德清县", "长兴县", "安吉县"]
shaoxing = ["越城区", "柯桥区", "上虞区", "新昌县", "诸暨市", "嵊州市"]
jinhua = ["婺城区", "武义县", "浦江县", "磐安县", "兰溪市", "义乌市", "东阳市", "永康市", "金东区"]
quzhou = ["柯城区", "衢江区", "常山县", "开化县", "龙游县", "江山市"]
zhoushan = ["定海区", "普陀区", "岱山县", "嵊泗县"]
taizhou = ["椒江区", "黄岩区", "路桥区", "三门县", "天台县", "仙居县", "温岭市", "临海市", "玉环市"]
lishui = ["莲都区", "青田县", "缙云县", "遂昌县", "松阳县", "云和县", "庆元县", "景宁畲族自治县", "龙泉市"]

# 浙江省-市 映射表
zhejiang_dic = {
'hangzhou': "杭州市",
'ningbo': "宁波市",
'wenzhou': "温州市",
'jiaxing': "嘉兴市",
'huzhou': "湖州市",
'shaoxing': "绍兴市",
'jinhua': "金华市",
'quzhou': "衢州市",
'zhoushan': "舟山市",
'taizhou': "台州市",
'lishui': "丽水市"
}

# 市-区县 映射表
zhejiang_city_dic = {
'hangzhou': hangzhou,
'ningbo': ningbo,
'wenzhou': wenzhou,
'jiaxing': jiaxing,
'huzhou': huzhou,
'shaoxing': shaoxing,
'jinhua': jinhua,
'quzhou': quzhou,
'zhoushan': zhoushan,
'taizhou': taizhou,
'lishui': lishui
}

# 区县-数据映射 导入数据时全部将数据放在这个变量中
dic_data = {}

if __name__ == '__main__':

print("正在导出浙江全省城市地图...")
html_path = "./html/map_zhejiang_all.html"
if os.path.exists(html_path):
os.remove(html_path)
# 统计全省区县数量,并合并全省区县
with open("./geojson/zhejiang_cities_districts_geojson.json", "r", encoding="utf-8") as file:
stream = file.read()

cities_dic = []
cities_dic_data = []
idx = 0
for city in zhejiang_city_dic.values():
for dic in city.values():
cities_dic.append(dic)
cities_dic_data.append(random.randint(1, 100))
cities_dic.append(zhejiang_city_dic.keys()[idx])
cities_dic_data.append(random.randint(1, 100))
idx += 1

map0 = Map(init_opts=opts.InitOpts(width='900px', height='800px')) \
.add_js_funcs("echarts.registerMap('浙江省',{});".format(stream)) \
.add("商家A", [list(z) for z in zip(cities_dic, cities_dic_data)],
"浙江", label_opts=opts.LabelOpts(font_size=10)) \
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-浙江省地图"), visualmap_opts=opts.VisualMapOpts(max_=100, is_piecewise=True)
)
map0.render(html_path)
print("浙江全省城市地图导出完成,文件路径:{}".format(html_path))

3、运行

运行之后会在html文件夹下生成一个map_zhejiang_all.html文件,打开文件看结果

全省各市县区地图

完整工程代码

工程代码在github 需要的可以自提。

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信