gma 空间绘图实战(1):绘制多个子图,连接并展示局部放大区域

安装 gma:pip install gma

本文基于:gma 2.0.3,Python 3.10

本文用到的矢量数据为:CTAmap 1.12。来源于 https://www.shengshixian.com/ 。(感谢锐多宝)

绘图目标

参考代码

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
from gma.map import plot, inres
from gma import io, crs

步骤一:读取 CTAmap_1.12 矢量数据

## 读取数据
L0 = io.ReadVector('2023年_CTAmap_1.12版/国界/国家矢量.shp')
L1 = io.ReadVector('2023年_CTAmap_1.12版/2023年省级/2023年省级.shp')
L2 = io.ReadVector('2023年_CTAmap_1.12版/九段线/九段线.shp')
L3 = io.ReadVector('2023年_CTAmap_1.12版/2023年地级/2023年地级.shp')

L1 属性表如下:

L3 属性表如下:

步骤二:定义绘图投影

1.定义一个适用于全国的 阿尔伯思等积圆锥投影
# 定义一个阿尔伯思等积圆锥投影方法(中央经线:东经105°)
ProjM1 = crs.ProjMethod.AlbersConicEqualArea(CentralLongitude = 105)   
ProjCS1 = crs.ProjCS(ProjMethod = ProjM1) # 创建一个投影坐标系(阿尔伯思等积圆锥投影)
2.定义一个适用于河南的 阿尔伯思等积圆锥投影
# 定义一个以 河南为中心 的阿尔伯思等积圆锥投影坐标系
##(中央经线:东经113°, 标准纬线1:北纬31.5°, 标准纬线2:北纬34.5°)
ProjM2 = crs.ProjMethod.AlbersConicEqualArea(CentralLongitude = 113, 
											 StandardParallels1 = 31.5, 
											 StandardParallels2 = 34.5)   
ProjCS2 = crs.ProjCS(ProjMethod = ProjM2)

步骤三:筛选并制作河南省数据

1.筛选河南省数据
### 河南省边界(筛选 L1 “省”字段中值为“河南省”的部分)
Henan = L1.SelectWhere('"省" = "河南省"')
### 河南省地级市(筛选L3 “省级”字段中值为“河南省”的部分)
HNDJ = L3.SelectWhere('"省级" = "河南省"')
2.制作河南省外包范围
## 创建一个河南的外包框,向外扩展 20%
HenanPCS = Henan.Reproject(ProjCS1)
XMin, YMin, XMax, YMax = HenanPCS.Boundary
XPTP, YPTP = XMax - XMin, YMax - YMin
HenanBod = [[XMin - XPTP * 0.2, YMax + YPTP * 0.2], 
            [XMin - XPTP * 0.2, YMin - YPTP * 0.2],
            [XMax + XPTP * 0.2, YMin - YPTP * 0.2], 
            [XMax + XPTP * 0.2, YMax + YPTP * 0.2]]
PlotAreaF = io.CreateFeatureFromPoints(HenanBod, Projection = ProjCS1)
## 在 2.0.4 之后的版本中,请用 io.CreateFeature 替代。

步骤四:绘图

Fig = plt.figure(figsize = (12, 6), dpi = 600)
1.绘制 子图1
### 子图 1
Axes1 = plt.subplot2grid((1, 2), (0, 0), colspan = 1, rowspan = 1)
# 在子图 Axes1 上绘制,绘图坐标系为 ProjCS1,绘图范围(四至边界)为 [82°E, 0°, 126°E, 55°N](重投影到底图坐标系后范围可能发生变化)。
MapF1 = plot.MapFrame(Axes = Axes1, BaseMapProj = ProjCS1, Extent = [82, 0, 126, 55])  

## 1.0 底图(内置的国家、海洋、湖泊、河流)
MapB0 = MapF1.AddLayer(inres.WorldLayer.Country, FaceColor = 'none', LineColor = 'black', LineWidth = 0.3, Zorder = 1)
MapB1 = MapF1.AddLayer(inres.WorldLayer.Ocean, FaceColor = '#BEE8FF', LineWidth = 0, Zorder = 1)
MapB2 = MapF1.AddLayer(inres.WorldLayer.Lake, FaceColor = '#BEE8FF', LineWidth = 0, Zorder = 1)
MapB3 = MapF1.AddLayer(inres.WorldLayer.River, LineColor = '#BEE8FF', LineWidth = 0.3, Zorder = 1)

## 1.4 国界。Zorder 用于调整图层顺序(避免图层遮盖);
#### Labels,FieldName 用于为图层添加标签,服务于标注(AddLabel)、图例(AddLegend)功能
MapL4 = MapF1.AddLayer(L0, FaceColor = 'none', LineColor = '#4B0082', LineWidth = 0.5, Zorder = 2, Labels = '国界(陆地)')
## 1.5 省界
MapL5 = MapF1.AddLayer(L1, FaceColor = '#F8F8FF', LineColor = 'lightgray', LineWidth = 0.3, Zorder = 0, Labels = '省级行政区界')
## 1.6 九段线
MapL6 = MapF1.AddLayer(L2, LineWidth = 0.5, LineColor = '#4B0082', Zorder = 2, Labels = '国界(海洋)/九段线')
## 1.7 河南省
MapL7 = MapF1.AddLayer(Henan, LineWidth = 0, Zorder = 2, FieldName = '省')
Label7 = MapL7.AddLabel(Font = 'SimSun', FontSize = 7)

## 1.8 河南外包框
MapL8 = MapF1.AddFeature(PlotAreaF, FaceColor = 'none', LineColor = '#FFA500', LineWidth = 0.8)

## 1.n 整饰要素
### 1.n.1 图例
Legend1 = MapF1.AddLegend(LegendName = '图例',        # 图例名称
                         LOC = (0.1, 0.15),           # 图例位置(0.1:X轴宽度的0.1倍,0.15:Y轴高度的0.15倍),可为负数
                         LabelFont = 'SimSun',        # 标签字体(宋体)
                         LabelFontSize = 6,           # 标签字体大小(6号)
                         TitleFont = 'SimSun',        # 图例标题字体(宋体) 
                         TitleAlignment = 'left',     # 图例标题对齐方式(左对齐) 
                         PlotID = [4, 5, 6])          # 对 MapF1 第 4(MapL4), 5(MapL5), 6(MapL6) 个图层绘制图例
### 1.n.2 指北针
Compass1 = MapF1.AddCompass(LOC = (0.12, 0.85),      # 指北针位置,同图例参数中的 LOC
                           Width = 0.06,             # 指北针的相对宽度,X 轴宽度的 0.06 倍
                           Style = 'GMACompass1')    # 指北针样式,目前有 GMACompass1-4 四种样式
### 1.n.3 比例尺
ScaleBar1 = MapF1.AddScaleBar(LOC = (0.04, 0.05))    # 比例尺位置,同图例参数中的 LOC
### 1.n.4 经纬网
GridLines1 = MapF1.AddGridLines()
### 1.n.5 设置地图框边框
Frame1 = MapF1.SetFrame(LabelFontSize = 8,           # 边框字体大小(8号),主要为经纬度显示信息
                        FrameWidth = 0.8)            # 边框字宽度0.8
2.绘制 子图2
Axes2 = plt.subplot2grid((1, 2), (0, 1), colspan = 1, rowspan = 1)
MapF2 = plot.MapFrame(Axes = Axes2, BaseMapProj = ProjCS2, Extent = PlotAreaF.Reproject('WGS84').Boundary)

## 1.1 地级行政区
MapL1 = MapF2.AddLayer(L3, FaceColor = 'none', LineColor = 'lightgray', LineWidth = 0.15)
## 1.2 中国省界
MapL2 = MapF2.AddLayer(L1, FaceColor = 'none', LineColor = 'black', LineWidth = 0.5)
## 1.3 河南市界
MapL3 = MapF2.AddLayer(HNDJ, LineColor = 'gray', LineWidth = 0.4)
Label3 = MapL3.AddLabel(FieldName = '地名', Font = 'Microsoft YaHei', FontSize = 6.5)

## 其他地图整饰要素
Compass2 = MapF2.AddCompass(LOC = (0.08, 0.85), Width = 0.05, Style = 'GMACompass2')
ScaleBar2 = MapF2.AddScaleBar(LOC = (0.04, 0.05))
GridLines2 = MapF2.AddGridLines(LONRange = (100, 120, 2), LATRange = (30, 50, 2))
Frame2 = MapF2.SetFrame(LabelFontSize = 8, FrameWidth = 0.8)
3.连接两个子图
P1 = PlotAreaF.Points2D
Point11 = P1[3] # 左上
Point12 = P1[2] # 左下

P2 = MapF2.FrameFeature.ToLayer().Envelope().GetFeature().Points2D
Point21 = P2[3] # 右上
Point22 = P2[0] # 右下

Con1 = ConnectionPatch(xyA = Point11, xyB = Point21, coordsA = "data", coordsB = "data",
                       axesA = Axes1, axesB = Axes2, color = "#FFA500")
Con2 = ConnectionPatch(xyA = Point12, xyB = Point22, coordsA = "data", coordsB = "data",
                       axesA = Axes1, axesB = Axes2, color = "#FFA500")

Fig.add_artist(Con1)
Fig.add_artist(Con2)

plt.subplots_adjust(left = None, bottom = None, right = None, top = None, wspace = 0.06)

步骤五:调整子图间距并保存结果

plt.subplots_adjust(left = None, bottom = None, right = None, top = None, wspace = 0.06)
plt.savefig('Fig.9.jpg', bbox_inches = 'tight')

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛的地理研学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值