使用python下载地图边界坐标
省市县级
#! /usr/bin/env python
# encoding: utf-8
import os
import requests
import json
def downloadOne(imgurl,path):
try:
r = requests.get(imgurl,timeout=30,verify=True)
r.raise_for_status()
#使用with语句可以不用自己手动关闭已经打开的文件流
with open(path,"wb") as f: #开始写文件,wb代表写二进制文件
f.write(r.content)
print('下载'+path+'完成')
except Exception:
print('')
def mkPath(path,name):
floder = os.path.join(path,name)
if not os.path.exists(floder):
os.mkdir(floder)
return floder
def digui(path,full_url):
print(path,full_url)
r = requests.get(full_url)
ret = json.loads(r.text)
p_list = ret['features']
size = len(p_list)
print(size)
for dic in p_list:
p_adcode = dic['properties']['adcode']
p_name = dic['properties']['name']
print(p_adcode,p_name)
url = "https://***/areas_v3/bound/{}.json".format(str(p_adcode))
p_path = mkPath(path,p_name)
file_p = os.path.join(p_path,p_name+'.json')
downloadOne(url,file_p)
try:
url = "https://***/areas_v3/bound/{}_full.json".format(str(p_adcode))
file_full_p = os.path.join(path,p_name+'_full.json')
downloadOne(url,file_full_p)
digui(p_path,url)
except:
print(p_name,'是个直辖市')
def downloadGeos():
url = 'https://***/areas_v3/bound/100000.json'
name = '中国'
path = mkPath(r'D:\\bigemap',name)
file_p = os.path.join(path,name+'.json')
downloadOne(url,file_p)
full_url = 'https://***/areas_v3/bound/100000_full.json'
file_full_p = os.path.join(path,name+'_full.json')
downloadOne(full_url,file_full_p)
digui(path,full_url)
downloadGeos()