首页 > 解决方案 > 使用 cartopy.io.img_tiles 穿越日期变更线

问题描述

我试图弄清楚如何使用 Cartopy 和 img_tiles 的地形生成跨越日期线的地图。这是我到目前为止所拥有的:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.io.img_tiles as cimgt
import shapely.geometry as sgeom

my_dpi = 96
plt.figure(figsize=(1530/my_dpi, 900/my_dpi), dpi=my_dpi, frameon=False)
plt.subplots_adjust(left=0.0, right=1.0, top=1.0, bottom=0)
ax = plt.axes(projection=ccrs.Mercator(central_longitude=180))
terrain = cimgt.Stamen('terrain-background')
ax.add_image(terrain, 4)     
states = cfeature.NaturalEarthFeature('cultural', 'admin_1_states_provinces', '10m', edgecolor='darkblue',facecolor='none')
ax.add_feature(states, linewidth = 0.1, linestyle='-')

# draw box
box = sgeom.box(minx=69, maxx=210, miny=-57, maxy=13.5)
ax.add_geometries([box], ccrs.PlateCarree(), facecolor='coral', 
                   edgecolor='black', alpha=0.5)
# Set extent
ax.set_extent(oceania_coords,  crs=ccrs.PlateCarree())

plt.show()

当我在要放大的区域周围画一个框时,它看起来是正确的。

在此处输入图像描述

当我尝试在此范围内设置 ax.set_extent 时,似乎正确设置了所有 cfeatures 但与 img_tiles 功能搞砸了。

在此处输入图像描述

有没有办法解决这个问题?谢谢您的帮助!

标签: pythoncartopy

解决方案


我有一个对我来说足够好的解决方案,通过关闭两个具有适当比率和边界的子图。接缝上有一个小工件,但我主要在这个框架中切割海洋,所以我没问题。当我将俄罗斯放在框架中时,它就更加明显了。

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.io.img_tiles as cimgt
import shapely.geometry as sgeom
import matplotlib.gridspec as gridspec
my_dpi=96
f = plt.figure(figsize=(1530/my_dpi, 900/my_dpi), dpi=my_dpi, frameon=False)
spec = gridspec.GridSpec(ncols=2, nrows=1,width_ratios=[111,30])
plt.subplots_adjust(left=0.0, right=1.0, top=1.0, bottom=0)

ax1 = f.add_subplot(spec[0],projection=ccrs.Mercator(central_longitude=180))
terrain = cimgt.Stamen('terrain-background')
ax1.add_image(terrain, 3)
states = cfeature.NaturalEarthFeature('cultural', 'admin_1_states_provinces', '10m', edgecolor='darkblue',facecolor='none')
ax1.add_feature(states, linewidth = 0.1, linestyle='-')
ax1.set_extent([69, 180, -57, 13.5],  crs=ccrs.PlateCarree())
plt.gca().outline_patch.set_visible(False)

ax2 = f.add_subplot(spec[1],projection=ccrs.Mercator(central_longitude=180))
ax2.add_image(terrain, 3)
ax2.add_feature(states, linewidth = 0.1, linestyle='-')
ax2.set_extent([-180,-150, -57, 13.5],  crs=ccrs.PlateCarree())
plt.gca().outline_patch.set_visible(False)

plt.subplots_adjust(wspace=0)

在此处输入图像描述


推荐阅读