【python地理信息绘制入门】cartopy学习

cartopy简介

  • cartopy是一个用于绘制地图投影和地理数据可视化的 Python 库。
  • 它是建立在 matplotlib 的基础上,专门用于制作地图和地理数据的图表。
  • cartopy 的目标是使地图制作变得更加简单和直观,同时提供了一些强大的功能来处理地理数据和投影。

cartopy绘制中国行政地图(cartopy的版本为0.20.0)

  • 省界显示
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader

fig = plt.figure(figsize=(12,8))
# 投影方式
crs = ccrs.PlateCarree()
ax = fig.add_subplot(111, projection=crs)
# 显示范围
extents = [70, 140, 0, 55]
ax.set_extent(extents, crs)

filepath = '.\bou2_4p.shp'
file_nineline = ".\九段线.shp"
reader = shpreader.Reader(filepath)
reader_nineline = shpreader.Reader(file_nineline)
geoms = reader.geometries()
geoms_nineline = reader_nineline.geometries()
# 绘制陆地和九段线
ax.add_geometries(geoms, crs, lw=0.5, fc='none')
ax.add_geometries(geoms_nineline, crs, lw=0.5, fc='none')
reader.close()
reader_nineline.close()
plt.show()

在这里插入图片描述

  • 市级行政单位显示
# 在以上代码中添加市图层即可
city_files = shpreader.Reader(r'.\市.shp')
ax.add_geometries(city_files.geometries(), crs, lw=0.5, fc='none')
reader.close()

在这里插入图片描述

  • 县级行政单位显示
# 在以上代码中添加市图层即可
county_files = shpreader.Reader(r'.\县.shp')
ax.add_geometries(county_file.geometries(), crs, lw=0.5, fc='none')
reader.close()

在这里插入图片描述

cartopy绘制中国行政地图,单一省细分画出市区

  • 有时候我们仅仅关注一个省内细分的市区,而不关注其他省份,那么需要将单一省内的市边界画出
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader

extents = [70, 140, 0, 55]
crs = ccrs.PlateCarree()
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111, projection=crs)
ax.set_extent(extents, crs)

city_reader = shpreader.Reader(r'.\市.shp')
reader = shpreader.Reader('.\bou2_4p.shp')
reader_nineline = shpreader.Reader(".\九段线.shp")
geoms = reader.geometries()
geoms_nineline = reader_nineline.geometries()

# 河南省市区
for record, geos in zip(city_reader.records(),city_reader.geometries()):
    if record.attributes['省'] == '河南省':
        ax.add_geometries([geos], crs, edgecolor='r', facecolor='none', lw=1)
records = city_reader.records()
# 中国地图
ax.add_geometries(geoms, crs, lw=0.5, fc='none')
ax.add_geometries(geoms_nineline, crs, lw=0.5, fc='none')

reader.close()
reader_nineline.close()
city_reader.close()

plt.show()

在这里插入图片描述

cartopy库的一些问题

  • cartopy依赖于shapefile库读取文件,但是在初始化中只设置了一个参数,即文件的名称,没有不定参数的设定。这使得文档编码格式不是UTF-8类型的文件,读取出现乱码,无法使用关键字参数筛选数据。如图所示:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import shapefile
filepath = '.\bou2_4p.shp'
x = shpreader.Reader(filepath)
for i in x.records():
    print(i.attributes)

shapefile读取bou2_4p.shp文件,NAME中文显示乱码

  • shapefile源码中只设定了filename参数,没有不定参数的设定,导致无法选择encoding参数。
    在这里插入图片描述
  • 因此,在上述画图示例中,如果想使用关键字将“河南省”边界改变比较困难,因此,查看源码,使用以下方法进行修改,完成上述功能。
import matplotlib.pyplot as plt
impor
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值