世界级地图数据处理 及 联动效果
https://www.naturalearthdata.com/downloads/10m-cultural-vectors/ 首先
从这个地方下载数据 这里面的香港、澳门、台湾是需要处理的
第一步 现将数据转为geojson
import geopandas as gpd
# 读取 Shapefile
shapefile = 'ne_10m_admin_0_map_units.shp'
data = gpd.read_file(shapefile)
# 转换为 GeoJSON
geojson_path = 'output_path.geojson'
data.to_file(geojson_path, driver='GeoJSON')
第二部 读取json数据 (也可以合并一起)转换为世界地图
import json
# 加载 GeoJSON 文件
with open('output_path.geojson', 'r') as file:
geojson_data = json.load(file)
# 提取所有的 'properties' 数据
properties_list = [feature['properties'] for feature in geojson_data['features']]
geometry_list = [feature['geometry'] for feature in geojson_data['features']]
extracted_data = {}
features=[]
tests=['Europe', 'Seven seas (open ocean)', 'Oceania', 'North America', 'Antarctica', 'Asia', 'Africa', 'South America']
paths=r'./result/'
for shuju in geojson_data['features']:
features.append({
"type": "Feature",
"id": shuju['properties']['ADM0_ISO'],
"properties":{
"name": shuju['properties']['ADMIN'],
"name_cn": shuju['properties']['NAME_ZH'],
"TYPE": shuju['properties']['TYPE'],
"CONTINENT": shuju['properties']['CONTINENT'],
"iso-a3":shuju['properties']['ADM0_ISO'],
"LABEL_X":shuju['properties']['LABEL_X'],
"LABEL_Y":shuju['properties']['LABEL_Y'],
},
"geometry":shuju['geometry']
})
lists={
'title':"world",
'id':'ALL',
'features':features
}
# 将提取的数据转换为 JSON 格式
extracted_json = json.dumps(lists, ensure_ascii=False, indent=4)
# 将 JSON 数据保存到文件中
with open('world.json', 'w') as outfile:
outfile.write(extracted_json)
第三步 读取数据 分别转换为 各个州地图(地址:https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_1_states_provinces.zip)
import json
# 加载 GeoJSON 文件
with open('output_path.geojson', 'r') as file:
geojson_data = json.load(file)
# 提取所有的 'properties' 数据
properties_list = [feature['properties'] for feature in geojson_data['features']]
geometry_list = [feature['geometry'] for feature in geojson_data['features']]
extracted_data = {}
features={}
tests=['Europe', 'Seven seas (open ocean)', 'Oceania', 'North America', 'Antarctica', 'Asia', 'Africa', 'South America']
paths=r'./result/'
for shuju in geojson_data['features']:
continents = shuju['properties']['CONTINENT']
if continents not in extracted_data:
extracted_data[continents]=[]
if continents not in features:
features[continents]=[]
features[continents].append({
"type": "Feature",
"id": "AU.NT",
"properties":{
"name": shuju['properties']['ADMIN'],
"name_cn": shuju['properties']['NAME_ZH'],
"TYPE": shuju['properties']['TYPE'],
"CONTINENT": shuju['properties']['CONTINENT'],
"iso-a3":shuju['properties']['ADM0_A3_CN'],
"LABEL_X":shuju['properties']['LABEL_X'],
"LABEL_Y":shuju['properties']['LABEL_Y'],
},
"geometry":shuju['geometry']
})
extracted_data[continents].append({
'title':shuju['properties']['CONTINENT'],
'id':shuju['properties']['ADM0_A3_CN']
})
for key,values in features.items():
lists={}
lists={
'title':extracted_data[key][0]['title'],
'id':extracted_data[key][0]['id'],
'features':values
}
# 将提取的数据转换为 JSON 格式
extracted_json = json.dumps(lists, ensure_ascii=False, indent=4)
# 将 JSON 数据保存到文件中
with open(paths+key+'.json', 'w') as outfile:
outfile.write(extracted_json)
以上数据为 世界-洲的地图数据 包含中文
以下为各个国家的洲/省份 地图(建议中国地方 还是从阿里云拿 到时候替换下)
import json
# 加载 GeoJSON 文件
with open('output_path.geojson', 'r') as file:
geojson_data = json.load(file)
# 提取所有的 'properties' 数据
properties_list = [feature['properties'] for feature in geojson_data['features']]
geometry_list = [feature['geometry'] for feature in geojson_data['features']]
extracted_data = {}
features={}
paths=r'./result/'
for shuju in geojson_data['features']:
continents=shuju['properties']['admin']
if continents not in extracted_data:
extracted_data[continents]=[]
if continents not in features:
features[continents]=[]
features[continents].append({
"type": "Feature",
"id":shuju['properties']['iso_3166_2'],
"properties":{
"name": shuju['properties']['name_en'],
"name_cn": shuju['properties']['name_zh'],
"admin": shuju['properties']['admin'],
"adm0_a3":shuju['properties']['adm0_a3'],
"iso_3166_2":shuju['properties']['iso_3166_2'],
"type_en":shuju['properties']['type_en'],
},
"geometry":shuju['geometry']
})
extracted_data[continents].append({
'title':shuju['properties']['admin'],
'id':shuju['properties']['adm0_a3']
})
for key,values in features.items():
lists={}
lists={
'title':extracted_data[key][0]['title'],
'id':extracted_data[key][0]['id'],
'features':values
}
# 将提取的数据转换为 JSON 格式
extracted_json = json.dumps(lists, ensure_ascii=False, indent=4)
# 将 JSON 数据保存到文件中
with open(paths+key+'.json', 'w') as outfile:
outfile.write(extracted_json)
现在数据可以直接用于 ecahrts的地图
比如 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="
https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js
"></script>
</head>
<body>
<div id="main" style="width: 100%;height: 900px;">sdsd</div>
</body>
<script type="text/javascript">
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
echarts.registerMap('HK',"这里直接把json数据拷贝过来 就可以看到效果");
myChart.setOption(
(option = {
title: {
text: 'Population Density of Hong Kong (2011)',
subtext: 'Data from Wikipedia',
sublink:
'http://zh.wikipedia.org/wiki/%E9%A6%99%E6%B8%AF%E8%A1%8C%E6%94%BF%E5%8D%80%E5%8A%83#cite_note-12'
},
tooltip: {
trigger: 'item',
formatter: '{b}<br/>{c} (p / km2)'
},
toolbox: {
show: true,
orient: 'vertical',
left: 'right',
top: 'center',
feature: {
dataView: { readOnly: false },
restore: {},
saveAsImage: {}
}
},
series: [
{
name: '香港18区人口密度',
type: 'map',
map: 'HK',
label: {
show: true
}
}
]
})
);
</script>
</html>
将数据处理为洲-国家-省级 联动效果 mysql数据
目录结构 看代码
$config=array('Europe'=>'EU','Oceania'=>'OC','Africa'=>'AF','Asia'=>'AS','North America.json'=>'NA','South America.json'=>'SA');
$names=[
'AF' => '非洲',
'AS' => '亚洲',
'EU' => '欧洲',
'OC' => '大洋洲',
'NA' => '北美洲',
'SA' => '南美洲',
];
foreach($config as $keys=>$vals) {
//获取洲的数据
$one=[];
$one['cn_anme']=$names[$config[$keys]];
$one['en_anme']=$keys;
$one['code_name']=$config[$keys];
$one['dic_type']=1;
$one['pid']=0;
$pid_code1=$config[$keys];
$one_id=$this->db->insert('country_areas',$one);
$one_id=$this->db->insert_id();
//根据洲加载国家数据
$zhou_path=APPPATH.'data/maps/洲/'.$keys.'.json';
var_dump($zhou_path);
$zhou_json=json_decode(file_get_contents($zhou_path),true)['features'];
$zhou_properties=array_column($zhou_json,'properties');
foreach($zhou_properties as $key1=>$vals1) {
//插入国家信息
$two=[];
$pid_code2=$vals1['iso-a3'];
$two['cn_anme']=$vals1['name_cn'];;
$two['en_anme']=$vals1['name'];;
$two['code_name']=$pid_code2;
$two['dic_type']=2;
$two['pid']=$one_id;
$two['pid_code']=$pid_code1;
$two_id=$this->db->insert('country_areas',$two);
$two_id=$this->db->insert_id();
//获取洲的数据
$gj_path=APPPATH.'data/maps/国家/'.$vals1['name'].'.json';
$gj_json=json_decode(file_get_contents($gj_path),true)['features'];
$gj_properties=array_column($gj_json,'properties');
foreach($gj_properties as $key2=>$vals2) {
$three=[];
$three['en_anme']=$vals2['name'];
$three['cn_anme']=$vals2['name_cn'];
$three['code_name']=$vals2['iso_3166_2'];
$three['dic_type']=3;
$three['pid']=$two_id;
$three['pid_code']=$pid_code2;
$three_id=$this->db->insert('country_areas',$three);
$three_id=$this->db->insert_id();
}
}
}