python通过批量经纬度生成标记地图生成
通过读取excel文件中经纬度数据生成定位图
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType
import pandas as pd
def mapa():
city = 'xx区县'
g = Geo(init_opts=opts.InitOpts(width='1850px', height='850px'))
g.add_schema(maptype=city)
# 定义坐标对应的名称,添加到坐标库中 add_coordinate(name, lng, lat)
# 导入excel表举例
df = pd.read_excel("学校机构经纬度表.xlsx")
df.head()
# 导入自定义的地点经纬度
for i in range(len(df)):
if isinstance(df.iloc[i]['经度'], float):
g.add_coordinate(df.iloc[i]['机构名称'], df.iloc[i]['经度'], df.iloc[i]['纬度'])
print(df.iloc[i]['机构名称'], df.iloc[i]['经度'], df.iloc[i]['纬度'])
# data_pair= [(df.iloc[i]['机构名称'], int(df.iloc[i]['学校类型编号']))]
data_pair = [(df.iloc[i]['机构名称'], int(df.iloc[i]['学校类型编号'])) for i in range(len(df)) if
isinstance(df.iloc[i]['经度'], float)]
# 定义数据对,
# 将数据添加到地图上
g.add('学校地图', data_pair, type_=GeoType.EFFECT_SCATTER, symbol_size=3)
# 设置样式
g.set_series_opts(label_opts=opts.LabelOpts(is_show=True, formatter='{b}'))
# 自定义分段 color 可以用取色器取色
pieces = [
{'min': 110, 'max': 111, 'label': '幼儿园', 'color': '#FF0000'}, # 红色
{'min': 210, 'max': 211, 'label': '小学', 'color': '#FF8C00'}, # 橙色
{'min': 217, 'max': 218, 'label': '小学教学点', 'color': '#FF8C66'}, # 橙色
{'min': 310, 'max': 311, 'label': '初级中学', 'color': '#00CED1'}, # 绿色
{'min': 312, 'max': 313, 'label': '九年一贯制', 'color': '#55CED1'}, # 绿色
{'min': 340, 'max': 341, 'label': '完全中学', 'color': '#99CED2'}, # 绿色
{'min': 342, 'max': 343, 'label': '高级中学', 'color': '#22CED6'}, # 绿色
{'min': 363, 'max': 364, 'label': '职业高中', 'color': '#2200FF'},
{'min': 512, 'max': 514, 'label': '特殊教育', 'color': '#0066FF'},
{'min': 410, 'max': 415, 'label': '高等学校', 'color': '#6600FF'} # 蓝色
]
# is_piecewise 是否自定义分段, 变为true 才能生效
g.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_piecewise=True, pieces=pieces),
title_opts=opts.TitleOpts(title="{}-学校分布".format(city)), )
# g = test_geo()
# 渲染成html, 可用浏览器直接打开
g.render_notebook()
g.render("a.html")
mapa()