利用Python获取全国GeoJSON数据并预览转换成shp格式文件

GeoPandas

GeoPandas是一个开源项目,Pandas是Python的一个结构化数据分析的利器,GeoPandas扩展了pandas使用的数据类型,允许对几何类型进行空间操作,其DataFrame结构相当于GIS数据中的一张属性表,使得可以直接操作矢量数据属性表,其目标是使得在python中操作地理数据更方便。下面是具体步骤:

安装GeoPandas库

利用Anaconda3集成环境,所以就直接在cmd命令窗口执行conda install -c conda-forge geopandas。这句话会下载geopandas依赖的一些库,安装时间较长耐心等待就好。 ( 可以从清华大学镜像下载Miniconda。地址是 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/),其他方式都比较复杂一点,网上也很全面,可以自行百度下载,不在细说。

GeoJSON基础数据获取
# -*- coding: utf-8 -*-

"""
@File    : JsonCrawlerLocal.py
@notice  : 将数据GeoJSON文件保存到本地
"""
# 数据下载地址
import json
import requests
import io


# 获取所有数据json文件


def download_Json(url, name):
    print("-----------正在下载json文件 %s" % (url))
    try:
        # 将响应信息进行json格式化
        response = requests.get(url)
        versionInfo = response.text
        versionInfoPython = json.loads(versionInfo)

        # print(versionInfo)
        path = "./data/" + str(name) + ".json"
        # 将json格式化的数据保存
        with open(path, 'w', encoding='utf-8') as f1:
            f1.write(json.dumps(versionInfoPython, indent=4))
        print("下载成功,文件保存位置:" + path)
    except Exception as ex:
        print("--------下载出错----")
        pass


# 数据下载地址
# http://datav.aliyun.com/tools/atlas/#&lat=33.521903996156105&lng=104.29849999999999&zoom=4
# 地名:中国|adcode:100000
# https://geo.datav.aliyun.com/areas/bound/100000.json
# 地名:河南省|adcode:410000
# https://geo.datav.aliyun.com/areas/bound/410000.json
# 地名:河南省+子区域|adcode:410000
# https://geo.datav.aliyun.com/areas/bound/410000_full.json
# 地名:江苏省|adcode:320000
# https://geo.datav.aliyun.com/areas/bound/320000.json
# 地名:山东省|adcode:370000
# 地名:山东省|adcode:340000
# 地名:北京市ad|code:110000

# 获取对应数据的json文件
url = 'https://geo.datav.aliyun.com/areas/bound/320500.json'#输入json地址
download_Json(url, "苏州")
#第一个参数是json文件的地址,第二个参数是文件保存的名称,
GeoJSON数据展示
# -*- coding: utf-8 -*-

"""
@File    : MapDisplay.py
@notice  : 展示geojson生成的数据,并进行投影
"""

import geopandas
from shapely import geometry
import matplotlib.pyplot as plt

#保存在本地的geoJson数据
# data1 = geopandas.read_file('./data/河南.json')
data2 = geopandas.read_file('./data/江苏.json')
# data3 = geopandas.read_file('./data/山东.json')
# data4 = geopandas.read_file('./data/安徽.json')

fig, ax = plt.subplots()
# data1.plot(ax=ax, color="#FDECD2",alpha=1)#透明样式alpha=0.8
data2.plot(ax=ax, color="#FADCE8",alpha=0.8)
# data3.plot(ax=ax, color="#DFE2F3",alpha=0.9)
# data4.plot(ax=ax, color="#E0ECDF",alpha=0.7)
# 绘制bbox框示意,进行重点标记(可以进行注释)
ax = geopandas.GeoSeries([geometry.box(minx=115,  #红框经度(小)
                                      maxx=118,  # 红框经度(大)
                                      miny=34,  #红框纬度(小)
                                      maxy=36)##红框纬度(大)
                        .boundary]).plot(ax=ax, color='red')
plt.savefig("./images/MapDisplayMoreProvince.png")#保存图片到项目images路径下
plt.show()
保存 GeoJSON数据为shapefile到本地
# -*- coding: utf-8 -*-

"""
@File    : MapDisplay.py
@notice  : 将json生成的数据保存到本地
"""

import geopandas
import matplotlib.pyplot as plt


def saveShapefile(file_path, output_shapefile_name):
    try:
        data = geopandas.read_file('./data/' + str(file_path) + '.json')
        ax = data.plot()
        plt.show()  # 显示生成的地图
        localPath = 'output/' + str(output_shapefile_name)#用于存放生成的文件
        data.to_file(localPath, driver='ESRI Shapefile', encoding='utf-8')
        print("--保存成功,文件存放位置:"+localPath)
    except Exception as ex:
        print("--------JSON文件不存在,请检查后重试!----")
        pass

#第一个参数是输入爬取GeoJSON的名称,
# 第二个参数是输出shapfile的名称(默认投影为wgs1984)
saveShapefile('苏州', '苏州市')

小tips:
1.pip升级安装 python -m pip install --upgrade pip -i https://pypi.douban.com/simple
2.python3.7安装geopandas库需要同时安装gdal文件和Fiona文件,参考文章:
如果是下载的文件,需要指定具体路径,如pip install C:\Users\Run\Downloads\pyHook-1.5.1-cp37-cp37m-win_amd64.whl
https://blog.csdn.net/micrasoft007/article/details/112652700
https://blog.csdn.net/weixin_38917807/article/details/81675233
3.为python安装matplotlib模块:
matplotlib是python中强大的画图模块。
首先确保已经安装python,然后用pip来安装matplotlib模块。
进入到cmd窗口下,执行python -m pip install -U pip setuptools进行升级。
接着键入python -m pip install matplotlib进行自动的安装,系统会自动下载安装包。
安装完成后,可以用python -m pip list查看本机的安装的所有模块,确保matplotlib已经安装成功。
参考文章:https://www.cnblogs.com/-1307/p/6529269.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章