爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 6827|回复: 13

[其他] 好帖分享:省、市、县、区shp 文件自动生成(python)

[复制链接]

新浪微博达人勋

发表于 2021-11-30 00:22:59 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 一大碗年糕 于 2021-11-30 00:25 编辑

日常搜一个地区的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,阿里这波网站分享数据听不错的哈哈



2021-11-30 00-23-54 的屏幕截图.png
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2021-11-30 09:10:30 | 显示全部楼层
{:eb513:}{:eb513:}
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-11-30 10:48:41 | 显示全部楼层
{:5_213:}{:5_213:}{:5_213:}{:5_213:}
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-11-30 17:51:03 | 显示全部楼层
相当好用,谢谢了
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-11-30 18:50:59 | 显示全部楼层
牛啊牛啊,非常感谢,先存着
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-12-3 08:00:12 | 显示全部楼层
感谢分享!!!
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2021-12-3 09:09:59 | 显示全部楼层
666,多谢楼主分享
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2021-12-21 10:47:33 | 显示全部楼层
66666666666
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-1-20 16:40:08 | 显示全部楼层
多谢楼主分享,牛些牛
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

发表于 2022-3-10 09:18:38 | 显示全部楼层
本帖最后由 hm_others 于 2022-3-10 09:22 编辑

https://mapshaper.org/ 这里可以将楼主的json格式转换为shp
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表