程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

下载地图包,并基于python的pyshp库读取.shp数据来获取中国省界的经纬度数据

发布于2019-08-22 16:47     阅读(239)     评论(0)     点赞(21)     收藏(1)


目前画地图的软件都能很方便的调用省界数据,但是如果想要单独对省界做一些个性化设置,如设置宽度、样式、颜色什么的,就需要将省界数据单独拿出来进行设置了。

软件在画地图时,描述的边界都是一些列经纬度格点的集合。目前常用的存储边界信息的文件格式为.shp格式(虽然我也不太明白为什么要用这种稀罕的格式)。
接下来详细介绍如何通过python获取省界数据

  1. 下载边界数据
    可以参考如下链接:
    https://blog.csdn.net/weixin_36677127/article/details/83314583
    https://gadm.org/download_country_v3.html 这个链接进去后,
    如下:
    在这里插入图片描述
    点击Shapefile,
    在这里插入图片描述
    下载完成,解压缩,文件信息如下:

在这里插入图片描述
从这个文件夹中可以看到尾号为0 1 2 3 的文件,应该是分别表示 国界、省界、市界和县(区)界;
虽然相同尾号的文件有5个,但是最为重要的还是.shp文件。该文件包含了重要的地理边界信息。

  1. 读取.shp文件

现在读取的.shp文件的库有很多,
https://mp.weixin.qq.com/s/aAucKybX_FE8aImja_M8VQ 可以参考这个链接
这里使用 pyshp库进行处理
可以参考这个链接:https://blog.csdn.net/GISuuser/article/details/81664223

import shapefile
import numpy as np
from mpl_toolkits.basemap import Basemap

file=shapefile.Reader('gadm36_CHN_shp/gadm36_CHN_1.shp')  #读取省界.shp文件
shapes=file.shapes()   #获取point 
records=file.records() #获取省名称
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

打开shapes文件:
在这里插入图片描述
打开第一个shapes的第一个Shape文件,可以看到地理信息保存在points里面,每一个点是一个经纬度二元组。
在这里插入图片描述
打开records文件,可以看到一些具体省名称信息:
records=file.records()
records

在这里插入图片描述

pro_points=[]  #建立省边界列表
pro_names=[] #建立省名称列表

for i in range(len(shapes)):
    points=shapes[i].points #h获取经纬度数据    
    pro_name=file.records()[i][3]  #获取省名称
    lon =[]  
    lat  =[] 
    #将每个tuple的lon和lat组合起来
    [lon.append(points[i][0]) for i in range(len(points))]  
    [lat.append(points[i][1]) for i in range(len(points))]   
 
    lon=np.array(lon).reshape(-1,1)
    lat=np.array(lat).reshape(-1,1)
    loc=np.concatenate((lon,lat),axis=1)
    
    pro_points.append(loc)
    pro_names.append(pro_name)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

形成的pro_names和pro_points如下:
在这里插入图片描述
现在将

lat_min,lat_max=37,55
lon_min,lon_max=115,135

fig = plt.figure(figsize=(18,10))
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])

m=Basemap(projection='cyl',llcrnrlat=lat_min,llcrnrlon=lon_min,
       urcrnrlat=lat_max,urcrnrlon=lon_max,resolution='l',ax=ax1)
       
m.drawcoastlines()
m.drawcountries()

name_list=['Liaoning','Jilin','Beijing','Hebei','Heilongjiang']  #画出这几个省的边界

for name,point in zip(pro_names,pro_points):
    
    if name in name_list:
        
        lon=point[:,0]
        lat=point[:,1]
        
        plt.scatter(lon,lat,marker='.',c='k',s=0.5)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

  1. 将省界数据保存下来
    为了方便后面调用,可以考虑所有数据保存为.hdf文件,调用h5py库就好了
import h5py

file_name='province_boundary_lon_lat.hdf'  #保存的目标文件名和路径
f=h5py.File(file_name,mode='w')  #创建一个hdf文件

for pro_name,points in zip(pro_names,pro_points):
   #每一个省份创建一个群组 
    a=f.create_group(pro_name) 
    a['longitude']=points[:,0]
    a['latitude']=points[:,1]
 
f.close() #写入完成后,记得关掉。 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述
创建完成以后,由相关hdf软件打开以后,结果如上图

attention !!!
台湾是中国领土不可分割的一部分!
还是在那个网址
在这里插入图片描述
一样的下载方式。后面获取台湾边界的经纬度格点的方式和上述一样,这里就不详细介绍了。

由于CSDN似乎不方便上传文件,因此我把刚创建的.hdf文件放在了气象家园里面,
下载链接如下:http://bbs.06climate.com/forum.php?mod=viewthread&tid=91308,
不过下载这个需要金币。如果没有的话,可以私戳我,我免费发给你。



所属网站分类: 技术文章 > 博客

作者:j878

链接:https://www.pythonheidong.com/blog/article/53021/0cdde87dbf676587f83b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)