博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Basemap系列教程:使用shapefiles绘制地图
阅读量:3959 次
发布时间:2019-05-24

本文共 3903 字,大约阅读时间需要 13 分钟。

译注:绘制地图时因为一些原因可能需要使用shapefile添加地图信息,比如很多软件中的关于中国的地图信息都不是很准确,当要明确标注中国边界信息时就会出问题。这次就介绍一下如何利用shp文件添加地图信息。

基本用法

首先用一种最简单的方式绘制 shapefile

from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43.,             resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques')plt.show()

在这里插入图片描述

  • 第一个参数为 shapefile 文件名,而且不应含有扩展名,因为Basemap假设给定名的 shapefile 文件,其 shp,sbf, shx文件均存在
  • 第二个参数为之后 Basemap实例要从shapefile文件中获取的信息名称
    当然这些存在一些约束:

注意:

  • 文件必须是 EPSG:4326 或 经纬度坐标。如果不是,可以使用 ogr2ogr 进行转换
  • 元素必须只有2维
  • 此例只有在元素是 polygons 或 polylines 时有效

如图所示,结果仅是 polygons(polylines)的边界。如果要填充 polygons,可看后面的 填充 polygons 部分。

读取点数据

绘制点要复杂一些。首先,要读取 shapefile,然后使用 scatter, plot 或 matplotlib 函数进行绘制。

from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43.,             resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques')lightning_info = map.readshapefile('../sample_files/lightnings', 'lightnings')print lightning_infofor info, lightning in zip(map.lightnings_info, map.lightnings):    if float(info['amplitude']) < 0:        marker = '_'    else:        marker = '+'    map.plot(lightning[0], lightning[1], marker=marker, color='m', markersize=8, markeredgewidth=2)plt.show()

例子显示了一次雷暴过程中Catalonia 发生的闪电位置。

在这里插入图片描述

  • 第二个参数名为 lightnings ,而且是 Basemap 实例映射,因此可以使用 map.lightning 获取shapefile文件中的几何元素,map.lightning_info 获取元素fields
  • shapefile 方法返回元素序列,几何类型代码 及 边界范围
  • 17行表示如何迭代所有元素
    – zip 将每一个 geometry 和对应的 field value 联结到一起
    – 当迭代字典时,使用 for 循环可以迭代每一个元素
  • 此例中, 域名 amplitude 可用于判断闪电是正还是负,从而确定符号
  • 使用 plot 绘制点时,使用 marker属性改变符号

多边形信息

此例展示了如何使用 shapefile 属性选择一些 geometries

from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43.,             resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques', drawbounds = False)for info, shape in zip(map.comarques_info, map.comarques):    if info['nombre'] == 'Selva':        x, y = zip(*shape)         map.plot(x, y, marker=None,color='m')plt.show()

在这里插入图片描述

  • 如果要迭代所有元素,使用上例中的 zip
  • 使用 nombre 域名进行过滤,这里也仅选择值为 Selva
  • 要绘制线的话, x 和 y必须为单独的数组,但 geometry 通常是成对的点。如何实现分离可以看这里:
  • 使用 plot 方法绘制,去除 marker 即可获得一条线

填充多边形

基本的绘制并不会填充多边形,下面介绍以下如何绘制填充多边形:

from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltfrom matplotlib.patches import Polygonfrom matplotlib.collections import PatchCollectionfrom matplotlib.patches import PathPatchimport numpy as npfig     = plt.figure()ax      = fig.add_subplot(111)map = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43.,             resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques', drawbounds = False)patches   = []for info, shape in zip(map.comarques_info, map.comarques):    if info['nombre'] == 'Selva':        patches.append( Polygon(np.array(shape), True) )        ax.add_collection(PatchCollection(patches, facecolor= 'm', edgecolor='k', linewidths=1., zorder=2))plt.show()

在这里插入图片描述

  • matplotlib 使用一个名为 PatchCollection 的类用于设置填充多边形
  • 此例中,形状为 Polygon。要创建它的话,坐标必须为 numpy 数组。第二个参数设置多边形为闭合。

  1. http://shapelib.maptools.org/shp_api.html

    http://stackoverflow.com/questions/15968762/shapefile-and-matplotlib-plot-polygon-collection-of-shapefile-coordinates

转载地址:http://wfmzi.baihongyu.com/

你可能感兴趣的文章
Source Insight 经典教程
查看>>
快速打开菜单附件中的工具
查看>>
Windows系统进程间通信
查看>>
linux exec的用法
查看>>
C语言中如何使用宏
查看>>
Http与RPC通信协议的比较
查看>>
Source Insight的对齐问题
查看>>
ubuntu设置开机默认进入字符界面方法
查看>>
chrome 快捷键
查看>>
Linux下buffer和cache的区别
查看>>
程序员不应该再犯的五大编程错误
查看>>
utf8中文编码范围
查看>>
oracle中文(utf8)按拼音排序的简单解决方案
查看>>
[转载][转帖]Hibernate与Sleep的区别
查看>>
Linux系统的默认编码设置
查看>>
Linux系统调用
查看>>
Linux 信号signal处理机制
查看>>
Linux 信号signal处理函数
查看>>
perror简介
查看>>
signal( SIGINT, SigIntHandler )
查看>>