Skip to content

Instantly share code, notes, and snippets.

@lxfly2000
Created January 4, 2025 05:32
Show Gist options
  • Save lxfly2000/000dac18be37ed07a579e6749e0f553b to your computer and use it in GitHub Desktop.
Save lxfly2000/000dac18be37ed07a579e6749e0f553b to your computer and use it in GitHub Desktop.
下载阿里云DataV.GeoAtlas的地图数据(到县级)
# python 3.11
import os
import json
from urllib import request
from urllib.error import HTTPError
def save_json(str,path):
print("保存至\""+path+".json\"")
open(path+".json","wb").write(str)
def download_json(id,path):
print("下载 [%d]%s ..."%(id,path))
if id%100==0:
try:
downloaded_str=request.urlopen("https://geo.datav.aliyun.com/areas_v3/bound/%d_full.json"%(id)).read()
if id%10000!=0:
save_json(downloaded_str,path)
else:
if not os.path.exists(path):
os.mkdir(path)
gj=json.loads(downloaded_str)
for f in gj["features"]:
#遍历ID和名称
#如果ID中间两位不是0
eachId=f["properties"]["adcode"]
eachName=f["properties"]["name"]
if eachName=="":
print("跳过空名称数据:adcode="+eachId)
else:
download_json(eachId,path+"/"+eachName)
except HTTPError as e:#没有更详细的数据
print("下载失败:",e.reason)
downloaded_str=request.urlopen("https://geo.datav.aliyun.com/areas_v3/bound/%d.json"%(id)).read()
save_json(downloaded_str,path)
else:#已到县级
downloaded_str=request.urlopen("https://geo.datav.aliyun.com/areas_v3/bound/%d.json"%(id)).read()
save_json(downloaded_str,path)
download_json(100000,".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment