1. 程式人生 > 實用技巧 >(二)Cartopy中新增地理要素(海岸線、陸地、河流等)

(二)Cartopy中新增地理要素(海岸線、陸地、河流等)

1、繪製海岸線、陸地、河流等要素

  我們在進行地圖繪製時,除所屬顯示地圖資料外,在地圖空白區域,往往需要新增一些地理要素,如海岸線、陸地、河流等。一方面可以彌補空白區域造成的視覺上的不美觀,另一方面也更符合製圖規範。

  使用Cartopy進行繪製地理要素程式碼如下:

import matplotlib.pyplot as plt###引入庫包
import cartopy.crs as ccrs

import cartopy.feature as cfeature
proj = ccrs.PlateCarree(central_longitude=130)##
fig = plt.figure(figsize=(3, 2), dpi=550)  #
建立頁面 ax = fig.subplots(1, 1, subplot_kw={'projection': proj}) # 建立子圖 ax.add_feature(cfeature.LAND)####新增陸地###### ax.add_feature(cfeature.COASTLINE,lw=0.25)#####新增海岸線######### ax.add_feature(cfeature.RIVERS,lw=0.25)#####新增河流###### ax.add_feature(cfeature.LAKES)######新增湖泊##### # ax.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)####此方法用來繪製國外地圖尚可,如繪製我國地圖是不正確的,會丟失了藏南、臺灣等領土############
ax.add_feature(cfeature.OCEAN)######新增海洋######## plt.show()

  其中,線狀要素可以使用關鍵字lw設定線寬,linestyle設定線型。線狀和麵狀要素可以使用color關鍵字設定顏色,其他的關鍵字引數可查閱matplotlib的pyplot模組。

2、新增經緯度資訊

  當前地圖沒有新增經緯度資訊,使用Cartopy增加經緯度資訊程式碼如下所示:

import matplotlib.ticker as mticker
import numpy as np
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
dlon, dlat 
= 60, 30 xticks = np.arange(0, 360.1, dlon) yticks = np.arange(-90, 90.1, dlat) gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=1, linestyle=':', color='k', alpha=0.8) gl.xlocator = mticker.FixedLocator(xticks) gl.ylocator = mticker.FixedLocator(yticks) ax.set_xticks(xticks, crs=ccrs.PlateCarree()) ax.set_yticks(yticks, crs=ccrs.PlateCarree()) ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True)) ax.yaxis.set_major_formatter(LatitudeFormatter()) fig_fname = "全球地圖.png" plt.savefig(fig_fname, dpi=500, bbox_inches='tight')

3、使用更高解析度地理要素

  當繪製區域性範圍地圖時,使用預設的新增地理要素,所繪製圖像會出現失真現象,這是由於預設的地理要素解析度不夠導致的,預設的解析度為110m,在繪製時可以設定要素解析度,程式碼如下:

ax.add_feature(cfeature.LAND.with_scale('10m'))####新增陸地######
ax.add_feature(cfeature.COASTLINE.with_scale('10m'),lw=0.25)#####新增海岸線#########
ax.add_feature(cfeature.RIVERS.with_scale('10m'),lw=0.25)#####新增河流######
ax.add_feature(cfeature.LAKES.with_scale('10m'))######新增湖泊#####
# ax.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)####不推薦,我國丟失了藏南、臺灣等領土############
ax.add_feature(cfeature.OCEAN.with_scale('10m'))######新增海洋########

  注意:使用這種方式繪圖第一次繪圖速度很慢,原因是所需資料需要聯網下載,第二次後速度會快很多。

4、繪製地球陰影浮雕圖

  Cartopy提供了地球陰影浮雕圖的繪製,可使用此圖作為空白地圖的背景圖,程式碼如下:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
plt.show()

5、繪製點、線、文字

   使用Cartopy繪製在地圖上繪製點、線、文字的程式碼如下:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),#繪製曲線
         )


plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='gray', linestyle='--',
         transform=ccrs.PlateCarree(),#繪製直線
         )

plt.text(ny_lon - 3, ny_lat - 12, 'New York',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
         horizontalalignment='left',
         transform=ccrs.Geodetic())

plt.show()

6、新增中國國界線

  在實際應用過程中,很多時候需要在地圖上繪製中國國界線,Cartopy中提供的cfeature.BORDERS無法滿足要求,會丟失了藏南、臺灣等領土;因此需要讀取中國的shapefile檔案,增加國界線。程式碼如下:

import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
proj = ccrs.PlateCarree(central_longitude=130)##
fig = plt.figure(figsize=(6, 4), )  # 建立頁面
ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 建立子圖
ax.set_extent([60,139,5,54])
ax.add_feature(cfeature.LAND.with_scale('10m'))####新增陸地######
ax.add_feature(cfeature.COASTLINE.with_scale('10m'),lw=0.25)#####新增海岸線#########
ax.add_feature(cfeature.RIVERS.with_scale('10m'),lw=0.25)#####新增河流######
ax.add_feature(cfeature.LAKES.with_scale('10m'))######新增湖泊#####
# ax.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)####不推薦,我國丟失了藏南、臺灣等領土############
ax.add_feature(cfeature.OCEAN.with_scale('10m'))######新增海洋########

reader = Reader(r"china_country.shp") #國界
china_country= cfeature.ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), edgecolor='black', facecolor='none') 
ax.add_feature(
china_country, linewidth=0.7) # 新增國界線
plt.show()

參考

微信公眾號:雲臺書史:https://mp.weixin.qq.com/s/tY5Qjo1kIG4cCut3a6Lnzw

Cartopy官網地址:https://scitools.org.uk/cartopy/docs/latest/matplotlib/intro.html