具体代码
from pyecharts.charts import Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import urllib.request, urllib.parse, urllib.error
import json
import hashlib
import numpy as np
import pandas as pd
MyAK = 'sIWgaZ4ypVeGYGwnMPfUDB1FHV0dLgtC'
MySK = 'GucWw7tIwOzHvlwWyloQHNLqzc8iAb2U'
lat=0.0
lng=0.0
#处理得到url
def get_url(name):
#GET请求 http://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation
queryStr = '/geocoding/v3/?address={}&output=json&ak={}'.format(name,MyAK)
# 对queryStr进行转码,safe内的保留字符不转换
encodedStr = urllib.parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
# 在最后追加sk
rawStr = encodedStr + MySK
# 计算sn
sn = (hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf8")).hexdigest())
#由于URL里面含有中文,所以需要用parse.quote进行处理,然后返回最终可调用的url
url = urllib.parse.quote("http://api.map.baidu.com" + queryStr + "&sn=" + sn, safe="/:=&?#+!$,;'@()*[]")
#print('URL:', url)
return url
#得到json数据
def get_json(url):
# 从API读取数据
req = urllib.request.urlopen(url)
res = req.read().decode()
# 解析数据
try:
# 将 JSON 对象转换为 Python 字典
json_data = json.loads(res)
except:
json_data = "地址未知"
if not json_data or 'status' not in json_data or json_data['status'] != 0:
print('json数据获取失败',json_data['status'])
'''else:
#输出Json数据
print(json.dumps(json_data, indent=4, ensure_ascii=False))'''
return json_data
# 获取经纬度坐标
def get_lnglat(json_data):
#Python中定义函数时,若想在函数内部对函数外的变量进行操作,就需要在函数内部声明其为global
global lat,lng
lat = json_data["result"]["location"]["lat"]
lng = json_data["result"]["location"]["lng"]
#print('纬度', lat, '经度', lng)
def get_url2(lat,lng):
#GET请求 http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194
queryStr = '/reverse_geocoding/v3/?ak={}&output=json&coordtype=wgs84ll&location={},{} '.format(MyAK,str(lat),str(lng))
# 对queryStr进行转码,safe内的保留字符不转换
encodedStr = urllib.parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
# 在最后追加sk
rawStr = encodedStr + MySK
# 计算sn
sn = (hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf8")).hexdigest())
#由于URL里面含有中文,所以需要用parse.quote进行处理,然后返回最终可调用的url
url = urllib.parse.quote("http://api.map.baidu.com" + queryStr + "&sn=" + sn, safe="/:=&?#+!$,;'@()*[]")
#print('URL:', url)
return url
# 解析Json数据
def get_province(json_data):
if json_data != "地址未知":
province=json_data["result"]["addressComponent"]["province"]
city=json_data["result"]["addressComponent"]["city"]
district=json_data["result"]["addressComponent"]["district"]
# adcode=json_data["result"]["addressComponent"]["adcode"]
# print("地理:",province+city+district)
result = province
else:
result = json_data
return result
# print("行政代码:"+adcode)
def get_city(json_data):
if json_data != "地址未知":
province=json_data["result"]["addressComponent"]["province"]
city=json_data["result"]["addressComponent"]["city"]
district=json_data["result"]["addressComponent"]["district"]
# adcode=json_data["result"]["addressComponent"]["adcode"]
# print("地理:",province+city+district)
result = city
else:
result = json_data
return result
# print("行政代码:"+adcode)
def get_district(json_data):
if json_data != "地址未知":
province=json_data["result"]["addressComponent"]["province"]
city=json_data["result"]["addressComponent"]["city"]
district=json_data["result"]["addressComponent"]["district"]
# adcode=json_data["result"]["addressComponent"]["adcode"]
# print("地理:",province+city+district)
result = district
else:
result = json_data
return result
# print("行政代码:"+adcode)
# 读取csv文件相关数据
df = pd.read_csv("D:/12140/Desktops/111/222/test007.csv", encoding="utf-8") # 编码默认UTF-8,若乱码自行更改
province_data = df[['联系单位地址']]
gdp_data = df[['ID_x']]
province_data_array = np.array(province_data.stack()) # 首先将pandas读取的数据转化为array
province_map_data = province_data_array.tolist() # 然后转化为list形式
gdp_data_array = np.array(gdp_data.stack())
gdp_map_data = gdp_data_array.tolist()
ppp_list = []
ccc_list = []
ddd_list = []
# 得到各个地址所对应的省市
for i in range(0, len(province_map_data)):
uu = get_url(province_map_data[i])
json_data = get_json(uu)
if json_data['status'] != 1:
lnglat = get_lnglat(json_data)
uu2 = get_url2(lat, lng)
json_data = get_json(uu2)
province = get_province(json_data)
city = get_city(json_data)
district = get_district(json_data)
ppp_list.append(province)
ccc_list.append(city)
ddd_list.append(district)
df['省份'] = ppp_list
df['城市'] = ccc_list
df['区县'] = ddd_list
new_ppp_list = [x[:-1] for x in ppp_list]
new_ccc_list = [x[:-1] for x in ccc_list]
new_ddd_list = [x[:-1] for x in ddd_list]
df.to_csv("D:/12140/Desktops/111/222/test007.csv", index=False)
# 读取数据,进行地图下钻
data = pd.read_csv('D:/12140/Desktops/111/222/test007.csv')
province = list(data["省份"])
vc = data['省份'].value_counts()
vvv =list(vc)
u = data['省份'].unique()
list_province_gdp = [list(z) for z in zip(u, vvv)]
city = list(data['城市'])
vc = data['城市'].value_counts()
vvv =list(vc)
u = data['城市'].unique()
list_city_gdp = [list(z) for z in zip(u, vvv)]
district = list(data['区县'])
vc = data['区县'].value_counts()
vvv =list(vc)
u = data['区县'].unique()
list_district_gdp = [list(z) for z in zip(u, vvv)]
print(list_province_gdp)
print(list_city_gdp)
print(list_district_gdp)
# 生成全国地图
c = (
Map(init_opts=opts.InitOpts(width="1000px", height="600px"))
.set_global_opts(
title_opts=opts.TitleOpts(title="全国地图"),
visualmap_opts=opts.VisualMapOpts(
type_="scatter"
)
)
.add("GDP", list_province_gdp, maptype="china")
.render("Modern地图1.html")
)
# 对应的省市地图的生成
for i in new_ppp_list:
province_city = (
Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE))
.add("",
list_city_gdp,
i)
.set_global_opts(
title_opts=opts.TitleOpts(title=i + "地图"),
visualmap_opts=opts.VisualMapOpts(
min_=1,
max_=17,
is_piecewise=True
)
)
.render(path=i + "1地图.html")
)
# 对应的区县地图的生成
for i in new_ccc_list:
city_qu = (
Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE))
.add("",
list_district_gdp,
i)
.set_global_opts(
title_opts=opts.TitleOpts(title=i + "地图"),
visualmap_opts=opts.VisualMapOpts(
min_=1,
max_=17,
is_piecewise=True
)
)
.render(path=i + "1地图.html")
)