数据来源:http://datav.aliyun.com/tools/atlas/
目前提供两种格式数据下载:GeoJSON,svg
1.若仅需要一两个geojson,可直接从网站上下载即可;
2.若需要全量的,则可使用python爬取,主要代码如下
github地址: https://github.com/sky-Aimee/geoJson-cn
# -*- coding: UTF-8 -*-
import json
import urllib.request
import urllib.parse
import ssl
import os
# 将不包含子区域的geojson写入文件中
def write_json_to_file(item, text, level):
if not os.path.isdir("data/" + level):
os.mkdir("data/" + level)
with open("data/" + level + "/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
# 将包含子区域的geojson写入文件中
def write_full_json_to_file(item, text, level):
if not os.path.isdir("data/" + level + "_full"):
os.mkdir("data/" + level + "_full")
with open("data/" + level + "_full/" + item + ".json", "w", encoding='utf-8') as json_file:
json.dump(text, json_file, ensure_ascii=False)
# 若没有对应文件夹则创建对应文件夹
if not os.path.isdir("data"):
os.mkdir("data")
if not os.path.isdir("data/country_full"):
os.mkdir("data/country_full")
if not os.path.isdir("data/country"):
os.mkdir("data/country")
if not os.path.isdir("data/province_full"):
os.mkdir("data/province_full")
if not os.path.isdir("data/province"):
os.mkdir("data/province")
if not os.path.isdir("data/city_full"):
os.mkdir("data/city_full")
if not os.path.isdir("data/city"):
os.mkdir("data/city")
if not os.path.isdir("data/district"):
os.mkdir("data/district")
context = ssl._create_unverified_context()
url = "https://geo.datav.aliyun.com/areas_v2/bound/infos.json"
with urllib.request.urlopen(url, context=context) as response:
html = json.loads(response.read().decode("UTF-8"))
with open("location.json", "w", encoding='utf-8') as json_file:
json.dump(html, json_file, ensure_ascii=False)
for item in list(html.keys()):
level = html[item]['level']
# 将不包含子区域的geojson读取并写入文件中
try:
with urllib.request.urlopen(
"https://geo.datav.aliyun.com/areas_v2/bound/" + item + ".json",
context=context,
) as res:
text = json.loads(res.read().decode("UTF-8"))
write_json_to_file(item, text, level)
except Exception as e:
print(item, '报错', e)
# 将包含子区域的geojson读取并写入文件中,中国geojson中街道没有子区域,故判断了level != "district"
try:
if level != "district":
with urllib.request.urlopen(
"https://geo.datav.aliyun.com/areas_v2/bound/" + item + "_full.json",
context=context,
) as res:
text = json.loads(res.read().decode("UTF-8"))
write_full_json_to_file(item, text, level)
except Exception as e:
print(item, '报错', e)
print("写入完成")
执行python之后得文件,对应文件夹解释:
- 全国地图-包含子区域: data/country_full
- 全国地图-外轮廓: data/country
- 全国各省地图-包含子区域:data/province_full
- 全国各省地图-外轮廓:data/province
- 全国各市地图-包含子区域:data/city_full
- 全国各市地图-外轮廓:data/city
- 全国各区县地图-外轮廓:data/county
- 全国省市区县所对应行政区划代码以及中心点的坐标:location.json (方便用于echarts上显示某个点的位置)可直接用于echarts地图的显示
若是业务当中不对省市区县层级做要求,可将数据分为 包含子区域,不包含子区域的两个文件夹
若此geojson不够全面,可参考国家地理信息公共服务平台: https://www.tianditu.gov.cn/