使用 gma 绘制汉魏洛阳城
背景
汉魏洛阳城始建于公元前1040年(有争议),前身为周洛邑成周城,于唐朝初年(约627年)废弃,存世约1600余年。相对位置如下:
环境和数据准备
安装 gma:
pip install gma
本文基于:gma 2.1.4,Python 3.12
本文用到的矢量数据为:
链接: https://pan.baidu.com/s/11son9jl0ZlV5xebmFJPOoA?pwd=pshx
提取码: pshx
绘图目标
代码
import matplotlib.pyplot as plt
from gma.map import inres, plot
from gma import io## 0.准备数据
## 0.1打开数据
### 0.1.1打开数据库(城)
hanweiLY = io.Open("汉魏洛阳城.gdb")
#### 打开矢量(城)
Qiao = hanweiLY.GetLayer('桥')
Chengmen = hanweiLY.GetLayer('城门')
Shi = hanweiLY.GetLayer('市')
Lifang = hanweiLY.GetLayer('里坊')
Neicheng = hanweiLY.GetLayer('内城')
Chengqiang = hanweiLY.GetLayer('城墙')
### 0.1.2打开矢量(古水系)
GuShuixi = io.ReadVector("其他要素.gdb", LYIDOrName = '古水系')## 0.2定义绘图范围
Extent = [112.544, 34.660, 112.700, 34.764]## 1.绘图准备
## 1.1创建子图
fig, ax = plt.subplots(1, 1, dpi=600)## 1.2定义地图框
MapF = plot.MapFrame(Axes = ax, BaseMapProj = Lifang.SpatRef, Extent = Extent)## 1.3添加数据
### 1.3.1古洛水
L0 = MapF.AddLayer(GuShuixi, FaceColor='#B0E0E6', LineWidth = 0)### 1.3.2市
L1 = MapF.AddLayer(Shi, FaceColor='#87CEFA', LineWidth = 0.1)
L1.AddLabel(FieldName = '市', Font='KaiTi', FontSize=3.5)
### 1.3.3里坊
L2 = MapF.AddLayer(Lifang, FaceColor='#dcd2fa', LineWidth = 0.1)
L2.AddLabel(FieldName = '里坊', Font='KaiTi', FontSize=3)
### 1.3.4内城、城墙和太微城
L3= MapF.AddLayer(Neicheng, FaceColor='#FFCBB4', LineWidth = 0.1)
L3.AddLabel(FieldName = '名称', Font='KaiTi', FontSize=2.5)
L4 = MapF.AddLayer(Chengqiang, LineColor = 'green', LineWidth = 0.6, Zorder=6)
### 1.3.5城门
L6 = MapF.AddLayer(Chengmen, PointSize = 5, FaceColor = 'green')
locM = [(-30, -30), (0, -30), (-20, -30), (30, -30), (80, 0), (80, 0), (80, 0),(0, 30), (0, 30), (-80, 0), (-80, 0), (-80, 0), (-80, 0), (0, 0), (0, 0),(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (-80, 0), (-80, 0),(-80, 0), (80, 0), (80, 0), (-60, -30), (60, -30), (-80, 0)] ## 城门名称标注位置的偏移量
L6.AddLabel(FieldName = '城门', Font='KaiTi', FontSize=5, Offset=locM)
### 1.3.6桥
L7 = MapF.AddLayer(Qiao, LineColor = '#008B00', LineWidth = 1, Zorder=6)
L7.AddLabel(FieldName = '桥', Font='KaiTi', FontSize=5, Offset=[(-60, 0)])
### 1.3.7其他标注
MapF.Axes.text(10300, 3662200, f'南 外 郭 城', fontdict = {'family':'SimSun', 'size': 6})
MapF.Axes.text(10300, 3667700, f'北 外 郭 城', fontdict = {'family':'SimSun', 'size': 6})
MapF.Axes.text(6600, 3665600, f'西 外 郭 城', fontdict = {'family':'SimSun', 'size': 6})
MapF.Axes.text(13900, 3664600, f'东 外 郭 城', fontdict = {'family':'SimSun', 'size': 6})## 2.保存绘制结果
fig.savefig("汉魏洛阳.jpg", bbox_inches = 'tight')