以北京市为例,流程如下: 输入:北京和北京市的邮政编码 输入:输出路径 输出:北京市的shp文件
如果有高德地图API KEY即可实现全自动
如果没有的话只能手动获取目标城市的邮政编码或城市代码
手动获取:
1.打开http://datav.aliyun.com/tools/atlas/

2.输入目标城市:北京

获取北京邮政编码为110000
自动获取:
def get_district_code(city, api_key):
url = district_url.format(city=city, api_key=api_key)
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
result = json.loads(response.text)
return result["districts"][0]["adcode"]
输入城市:北京
输入存储路径:E:
运行结果:


'''
@author: ricardo_sakura
@date: 2021.4.21
@function: 生成想要的城市shp文件
'''
import geopandas
import requests
import json
import os
district_url = 'https://restapi.amap.com/v3/config/district?keywords={city}&key={api_key}'
geo_json_url = 'https://geo.datav.aliyun.com/areas/bound/{city_code}_full.json'
api_key = None # 配置高德地图API KEY
path = None
def get_district_code(city, api_key):
url = district_url.format(city=city, api_key=api_key)
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
result = json.loads(response.text)
return result["districts"][0]["adcode"]
def download_geojson(city, city_code):
file_path = os.path.join(path, city + '.json')
if os.path.exists(file_path):
print('Reading from local files...')
with open(file_path, 'r') as f:
result = json.load(f)
else:
print('Downloading from website...')
url = geo_json_url.format(city_code=city_code)
response = requests.get(url)
result = json.loads(response.text)
with open(file_path, 'w') as f:
json.dump(result, f, indent=4)
return result
def generate_shape(city):
file_name = os.path.join(path, city + '.json')
shp_file_path = os.path.join(path, city + '.shp')
try:
data = geopandas.read_file(file_name)
localPath = str(shp_file_path)
data.to_file(localPath, driver='ESRI Shapefile', encoding='gbk')
print(f"{city}shp文件生成成功")
print(f"文件存储在:{os.path.join(path,city+'.shp')}")
except Exception as e:
print(e)
if __name__ == '__main__':
city = input('输入城市名称:')
if api_key is None:
city_code = input('输入城市编码:')
else:
city_code = get_district_code(city,api_key)
path = input('输入存储路径:')
download_geojson(city,city_code)
generate_shape(city)