Cartopy画地图第七天(python画浮雕地图和比例尺)
本文利用了python、cartopy进行了浮雕地图的绘制,同时还画了比例尺。
先上图为敬,一些图例符号不对请不要介意,随便表示的
第一、下载浮雕地图
想画浮雕地图的朋友们,第一步首先是要下载到浮雕地图文件,网站是Natural Earth,网址是:https://www.naturalearthdata.com/downloads/
只有1:10和1:50的有浮雕地图,就是第三个选项“栅格”(别问为啥我是中文的,问我就告诉你我是浏览器翻译的。)
点进去之后找自己需要的下载就好了,反正我的电脑1:10里面最大的那个地图画不了,报错显示地图太大,其他都没问题,大家自己按实际情况下载就好了。
下载完成以后是一个压缩包,解压得到里面的xxxxx.tif文件就好了,放到代码目录下就能使用。
第二、画浮雕地图
其实没啥难度,就是告诉大家cartopy是可以画浮雕地图的
#地图四周经纬度
west = 110
east = 125
south = 20
north = 30
#解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']
#画布设置,投影设置
fig = plt.figure(figsize=(16,9.6))
ax = fig.add_subplot(111,projection = ccrs.PlateCarree())
#边界设置
img_extent = [west, east, south, north]
ax.set_extent(img_extent,crs = ccrs.PlateCarree())
#浮雕地图导入
fname = 'HYP_LR_SR_OB_DR.tif'
ax.imshow(plt.imread(fname), origin='upper', transform=ccrs.PlateCarree(),extent=[-180, 180, -90, 90])
有些朋友到这里就OK了。
第三、地图美画
经纬度网格设置
#设置经纬度网格和标签,透明度改成1,就会出现网格 alpha=1
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='k', alpha=1,linestyle='--')#坐标轴设置
gl.xformatter = LONGITUDE_FORMATTER ##坐标刻度转换为经纬度样式
gl.yformatter = LATITUDE_FORMATTER
#设置经纬度的显示,例如经度从70到135,间隔5显示
gl.xlocator = mticker.FixedLocator(np.arange(70,135,5))
gl.ylocator = mticker.FixedLocator(np.arange(15,55,5))
画比例尺和黑白边框
def drow_the_scale(y,x,text,length = 1.5,lw = 5):
#画比例尺函数
# y代表比例尺所在纬度
# x代表比例尺开始的经度
# text代表比例尺最后刻度值
# length代表比例尺的长度,单位为多少个经度
# lw代表比例尺的宽度
step = length/5#计算步长,画五格
#画黑白线五条
plt.hlines(y=y,xmin=x,xmax=x + step,colors="black", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step,xmax=x + step*2,colors="white", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*2,xmax=x + step*3,colors="black", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*3,xmax=x + step*4,colors="white", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*4,xmax=x + step*5,colors="black", ls="-", lw=lw)
#画长刻度两个
plt.vlines(x = x, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + length, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)
#画段刻度四个
plt.vlines(x = x + step, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*2, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*3, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*4, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
#写字,0,500,km
plt.text(x,y - (lw/100) *7,'0',horizontalalignment = 'center')
plt.text(x + length,y - (lw/100) *7,text,horizontalalignment = 'center')
plt.text(x + length/2,y + (lw/100)*2,'km',horizontalalignment = 'center')
def drowscale(extent,scale_y,scale_x,scale_text,step = 5,lw = 10,scale_length = 1.5,scale_lw = 5):
# 画地图黑白边框和比例尺
# extent:表示四周经纬度[west, east, south, north]
# scale_y,scale_x,scale_text:代表比例尺的位置,纬度,经度,刻度值
# step:表示步长,一格代表几个经纬度
# lw:代表边框宽度
# scale_length:代表比例尺长度(单位为经度例如1.5个经度)
# scale_lw:代表比例尺宽度
for y in [extent[2],extent[3]] :#画上下两边框
xmin = extent[0]
while (xmin < extent[1]):
plt.hlines(y=y,xmin=xmin,xmax=xmin+step,colors="white", ls="-", lw=lw)
xmin = xmin+step*2
xmin = extent[0]+step
while (xmin < extent[1]):
plt.hlines(y=y,xmin=xmin,xmax=xmin+step,colors="black", ls="-", lw=lw)
xmin = xmin+step*2
for x in [extent[0],extent[1]] :#画左右两边狂
ymin = extent[2]
while (ymin < extent[3]):
plt.vlines(x = x, ymin = ymin, ymax = ymin+step, colors="black", ls="-", lw=lw)
ymin = ymin+step*2
ymin = extent[2]+step
while (ymin < extent[3]):
plt.vlines(x = x, ymin = ymin, ymax = ymin+step, colors="white", ls="-", lw=lw)
ymin = ymin+step*2
drow_the_scale(scale_y,scale_x,scale_text)#画比例尺
这里写了两个函数,一个套一个,其实就是在地图四周画黑白的线,然后在图片某个位置画黑白的线组成了一个比例尺,可以说有些取巧,以及相当的不规范,默认了1度代表111公里。
主要是作者实在是找不到cartopy画比例尺的函数是什么,basemap倒是有,但是不想用。如果哪位朋友知道的,请留言告诉我一下,感谢。
画散点信息
随便画了一些散点信息,给朋友们使用,总共五个函数,都是批量画散点的,给个经纬度列表就好了。上面的点是我随便标的。
def drowpentagram(ax,points,c = 'red',s=300):
#画五角星 ax,points必带,c颜色,s大小
for point in points:
ax.scatter(point[0], point[1], marker = '*',c = c,s=s,transform=ccrs.PlateCarree())
plt.text(point[0],point[1]+0.3,point[2],horizontalalignment = 'center')
def drowcircle(ax,points,edgecolors = 'red',linewidths = 2,s=300):
#画圈 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 'o',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowfork(ax,points,c = 'blue',linewidths = 2,s=300):
#画叉 ax,points必带,c颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 'x',c = c,linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowbox(ax,points,edgecolors = 'blue',linewidths = 2,s=300):
#画方 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 's',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowtriangle(ax,points,edgecolors = 'blue',linewidths = 2,s=300):
#画三角 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = '^',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
画图例
给出了一个图例函数,都是封装好的
def drowlegend(ax,x,y,text,step = 0.5):
#画图例
# x为图例左上角经度;
# y为图例左上角纬度;
# text为文字描述;
# step为间距
drowcircle(ax,((x,y),))#画红圈
plt.text(x+step*1.5,y ,text[0],horizontalalignment = 'center')
drowcircle(ax,((x,y - step),),edgecolors = 'green')#画绿圈
plt.text(x+step*1.5,y- step ,text[1],horizontalalignment = 'center')
drowfork(ax,((x,y - step*2),))#画叉
plt.text(x+step*1.5,y- step*2 ,text[2],horizontalalignment = 'center')
drowbox(ax,((x,y - step*3),))#画方
plt.text(x+step*1.5,y- step*3 ,text[3],horizontalalignment = 'center')
drowtriangle(ax,((x,y - step*4),))#画三角
plt.text(x+step*1.5,y- step*4 ,text[4],horizontalalignment = 'center')
第四、全部代码
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
import matplotlib.ticker as mticker
def drowpentagram(ax,points,c = 'red',s=300):
#画五角星 ax,points必带,c颜色,s大小
for point in points:
ax.scatter(point[0], point[1], marker = '*',c = c,s=s,transform=ccrs.PlateCarree())
plt.text(point[0],point[1]+0.3,point[2],horizontalalignment = 'center')
def drowcircle(ax,points,edgecolors = 'red',linewidths = 2,s=300):
#画圈 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 'o',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowfork(ax,points,c = 'blue',linewidths = 2,s=300):
#画叉 ax,points必带,c颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 'x',c = c,linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowbox(ax,points,edgecolors = 'blue',linewidths = 2,s=300):
#画方 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = 's',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drowtriangle(ax,points,edgecolors = 'blue',linewidths = 2,s=300):
#画三角 ax,points必带,edgecolors颜色,s大小,linewidths线宽
for point in points:
ax.scatter(point[0], point[1], marker = '^',edgecolors = edgecolors,c = 'none',linewidths = linewidths,s=s,transform=ccrs.PlateCarree())
def drow_the_scale(y,x,text,length = 1.5,lw = 5):
#画比例尺函数
# y代表比例尺所在纬度
# x代表比例尺开始的经度
# text代表比例尺最后刻度值
# length代表比例尺的长度,单位为多少个经度
# lw代表比例尺的宽度
step = length/5#计算步长,画五格
#画黑白线五条
plt.hlines(y=y,xmin=x,xmax=x + step,colors="black", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step,xmax=x + step*2,colors="white", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*2,xmax=x + step*3,colors="black", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*3,xmax=x + step*4,colors="white", ls="-", lw=lw)
plt.hlines(y=y,xmin=x + step*4,xmax=x + step*5,colors="black", ls="-", lw=lw)
#画长刻度两个
plt.vlines(x = x, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + length, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)
#画段刻度四个
plt.vlines(x = x + step, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*2, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*3, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
plt.vlines(x = x + step*4, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)
#写字,0,500,km
plt.text(x,y - (lw/100) *7,'0',horizontalalignment = 'center')
plt.text(x + length,y - (lw/100) *7,text,horizontalalignment = 'center')
plt.text(x + length/2,y + (lw/100)*2,'km',horizontalalignment = 'center')
def drowscale(extent,scale_y,scale_x,scale_text,step = 5,lw = 10,scale_length = 1.5,scale_lw = 5):
# 画地图黑白边框和比例尺
# extent:表示四周经纬度[west, east, south, north]
# scale_y,scale_x,scale_text:代表比例尺的位置,纬度,经度,刻度值
# step:表示步长,一格代表几个经纬度
# lw:代表边框宽度
# scale_length:代表比例尺长度(单位为经度例如1.5个经度)
# scale_lw:代表比例尺宽度
for y in [extent[2],extent[3]] :#画上下两边框
xmin = extent[0]
while (xmin < extent[1]):
plt.hlines(y=y,xmin=xmin,xmax=xmin+step,colors="white", ls="-", lw=lw)
xmin = xmin+step*2
xmin = extent[0]+step
while (xmin < extent[1]):
plt.hlines(y=y,xmin=xmin,xmax=xmin+step,colors="black", ls="-", lw=lw)
xmin = xmin+step*2
for x in [extent[0],extent[1]] :#画左右两边狂
ymin = extent[2]
while (ymin < extent[3]):
plt.vlines(x = x, ymin = ymin, ymax = ymin+step, colors="black", ls="-", lw=lw)
ymin = ymin+step*2
ymin = extent[2]+step
while (ymin < extent[3]):
plt.vlines(x = x, ymin = ymin, ymax = ymin+step, colors="white", ls="-", lw=lw)
ymin = ymin+step*2
drow_the_scale(scale_y,scale_x,scale_text)#画比例尺
def drowlegend(ax,x,y,text,step = 0.5):
#画图例
# x为图例左上角经度;
# y为图例左上角纬度;
# text为文字描述;
# step为间距
drowcircle(ax,((x,y),))#画红圈
plt.text(x+step*1.5,y ,text[0],horizontalalignment = 'center')
drowcircle(ax,((x,y - step),),edgecolors = 'green')#画绿圈
plt.text(x+step*1.5,y- step ,text[1],horizontalalignment = 'center')
drowfork(ax,((x,y - step*2),))#画叉
plt.text(x+step*1.5,y- step*2 ,text[2],horizontalalignment = 'center')
drowbox(ax,((x,y - step*3),))#画方
plt.text(x+step*1.5,y- step*3 ,text[3],horizontalalignment = 'center')
drowtriangle(ax,((x,y - step*4),))#画三角
plt.text(x+step*1.5,y- step*4 ,text[4],horizontalalignment = 'center')
pass
#主函数
if __name__ == '__main__':
points_pentagram = [(113.3, 23.23,'Guangzhou')]#五角星坐标和名称
points_circle_red = [(116.1, 26.7),(117.1, 27.7)]#红圈坐标
points_circle_green = [(115.1, 25.7),(118.1, 28.7)]#绿圈坐标
points_fork = [(115.1, 26.7),(118.1, 29.7)]#叉坐标
points_box = [(117.1, 26.7),(115.1, 27.7)]#方坐标
points_triangle = [(118.1, 26.7),(114.1, 27.7)]#三角坐标
#图例的文字描述
text = ['大风','冰雹','龙卷','闪电','强降水']
#地图四周经纬度
west = 110
east = 125
south = 20
north = 30
#解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['SimHei']
#画布设置,投影设置
fig = plt.figure(figsize=(16,9.6))
ax = fig.add_subplot(111,projection = ccrs.PlateCarree())
#边界设置
img_extent = [west, east, south, north]
ax.set_extent(img_extent,crs = ccrs.PlateCarree())
#浮雕地图导入
fname = 'HYP_LR_SR_OB_DR.tif'
ax.imshow(plt.imread(fname), origin='upper', transform=ccrs.PlateCarree(),extent=[-180, 180, -90, 90])
#设置经纬度网格和标签,透明度改成1,就会出现网格 alpha=1
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='k', alpha=0,linestyle='--')#坐标轴设置
gl.xformatter = LONGITUDE_FORMATTER ##坐标刻度转换为经纬度样式
gl.yformatter = LATITUDE_FORMATTER
#设置经纬度的显示,例如经度从70到135,间隔5显示
gl.xlocator = mticker.FixedLocator(np.arange(70,135,5))
gl.ylocator = mticker.FixedLocator(np.arange(15,55,5))
drowscale(img_extent,21,123.1,'500')#画比例尺
drowpentagram(ax,points_pentagram)#画五角星
drowcircle(ax,points_circle_red)#画红圈
drowcircle(ax,points_circle_green,edgecolors = 'green')#画绿圈
drowfork(ax,points_fork)#画叉
drowbox(ax,points_box)#画方
drowtriangle(ax,points_triangle)#画三角
drowlegend(ax,123.1,24,text,step = 0.5)#画图例
plt.show()
第五、求助
有朋友知道cartopy有自带的比例尺函数吗,翻了一下文档没找到,会的朋友留言帮忙一下,作者画的比例尺徒有其表,深究起来很不严谨。