目錄
- 例一:使用cartopy内置底圖,繪制流域範圍
- 例二:調用天地圖作為底圖,繪制流域範圍
- 例三:繪制歐洲各國位置
作為python繪制地圖的備忘錄
python調用線上地圖api教程
例一:使用cartopy内置底圖,繪制流域範圍
思路:
- 建立畫布
- 确定參數,如投影,顯示範圍,分辨率等
- 添加底圖,這裡采用内置低分辨率底圖
.stock_img()
- 添加其他要素,如河、湖等,并調整顯示參數
- 添加經緯度等其他資訊
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [65, 110, 25, 45]
res = '10m'
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 添加地形圖
ax.stock_img()
# 添加河湖
ax.add_feature(cfeature.LAKES.with_scale(res), edgecolor=None,color='cyan')
ax.add_feature(cfeature.RIVERS.with_scale(res),lw=1)
for ea in tp.iloc[[0,1,3,5,7,8,9]]['geometry']:
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor='chartreuse', edgecolor='black', lw=0.2,alpha=0.4)
ax.add_feature(feat)
path = '/group1/longjs/TP_shape/Union/'
tp = gpd.read_file(path+'TP_basins.shp') # 本地shape檔案,為流域邊界
# 添加經緯度
ax.set_xticks(np.arange(65, 110, 10), crs=proj)
ax.set_yticks(np.arange(25, 45, 2.5), crs=proj)
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
plt.show();
運作結果
例二:調用天地圖作為底圖,繪制流域範圍
思路:
- 難點在于調用API,網站提供了多個接口,可根據需要調用
- 相較内置的底圖,該方法能夠得到更清晰的底圖
- 後續可考慮調用如谷歌、高德等接口
import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.io.img_tiles as cimgt
# 定義畫布
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [65, 110, 25, 45]
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 調用地圖
ax.add_image(cimgt.MAP_ter_w(),5) # 數字為地圖分層
path = '/group1/longjs/TP_shape/Union/'
tp = gpd.read_file(path+'TP_basins.shp') # 本地shape檔案,為流域邊界
# 依次繪制不同流域
for ea in tp.iloc[[0,1,3,5,7,8,9]]['geometry']:
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor="lime", edgecolor='black', lw=0.2,alpha=0.4)
ax.add_feature(feat)
# 添加細節
lon_lat = ax.gridlines(draw_labels=True, linewidth=1, color='k', alpha=0.2, linestyle='--')
lon_lat.xlabels_top = False
lon_lat.ylabels_right = False
lon_lat.xformatter = LONGITUDE_FORMATTER
lon_lat.yformatter = LATITUDE_FORMATTER
plt.show();
運作結果
例三:繪制歐洲各國位置
思路:
- 内置的國界裡,中國的版圖并不全,後續需要調用線上網站的邊界,或其他途徑下載下傳
- 根據不同國家賦予不同顔色,以示區分
- 疊加地形底圖,讓畫面更美觀
- 根據需要添加海岸線
import cartopy
import cartopy.crs as ccrs
from cartopy.feature import ShapelyFeature
from matplotlib import pyplot as plt
import geopandas as gpd
import cartopy.io.img_tiles as cimgt
# 定義畫布
fig = plt.figure()
proj = ccrs.PlateCarree(central_longitude=0)
extents = [-30, 60, 30, 80]
ax = fig.add_subplot(111, projection=proj)
ax.set_extent(extents, crs=proj)
# 添加底圖
ax.stock_img()
# 添加資料
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = world[(world['continent'] == 'Europe')]
# 依次畫出歐洲不同國家邊界,并給予不同顔色。需要注意,中國的邊界不完整!
for ea in europe['geometry']:
color = ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])]
feat = cartopy.feature.ShapelyFeature([ea], proj, facecolor=color,edgecolor='black', lw=0.2,alpha=0.5)
ax.add_feature(feat)
# ax.add_image(cimgt.MAP_cva_w(),6)
# 添加海岸線
# ax.add_feature(cartopy.feature.COASTLINE)
plt.show();
運作結果