- 积分
- 35065
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2019-5-13
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 一大碗年糕 于 2024-11-20 20:25 编辑
2024.11.20更新:
根据hm_others坛友建议可以不用运行python:
1.首先在阿里云选择区域http://datav.aliyun.com/portal/school/atlas/area_selector,下载对应json文件
2.通过这个网站https://mapshaper.org/在线转换成shp等其他格式文件
日常搜一个地区的shp时发现的好文,作者这代码写的太人性化了
以下为代码放入python运行,根据提示输入城市邮政编码即可
'''
@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)
作者:sえりい https://www.bilibili.com/read/cv10961151 出处:bilibili。yysy,阿里这波网站分享数据听不错的哈哈
|
-
|