如何用Shapefile或Geopandas绘制带掩膜的南极洲区域
南极洲掩膜处理难题:如何避免多余线条并完美适配地图边界?
最近我在绘制南极洲周边的数据,核心需求是对大陆进行掩膜,但不想把冰架包含进去。最开始用basemap的map.fillcontinents()方法,虽然能快速掩膜大陆,但它把冰架也算成大陆范围,完全不符合我的要求。
后来我尝试了网上找到的geopandas方案,功能倒是实现了,但南极洲多边形的首尾位置出现了一条多余的海岸线,效果如下:
import numpy as np from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection import geopandas as gpd import shapely from descartes import PolygonPatch lats = np.arange(-90,-59,1) lons = np.arange(0,361,1) X, Y = np.meshgrid(lons, lats) data = np.random.rand(len(lats),len(lons)) world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) fig=plt.figure(dpi=150) ax = fig.add_subplot(111) m = Basemap(projection='spstere',boundinglat=-60,lon_0=180,resolution='i',round=True) xi, yi = m(X,Y) cf = m.contourf(xi,yi,data) patches = [] selection = world[world.name == 'Antarctica'] for poly in selection.geometry: if poly.geom_type == 'Polygon': mpoly = shapely.ops.transform(m, poly) patches.append(PolygonPatch(mpoly)) elif poly.geom_type == 'MultiPolygon': for subpoly in poly: mpoly = shapely.ops.transform(m, poly) patches.append(PolygonPatch(mpoly)) else: print(poly, 'blah') ax.add_collection(PatchCollection(patches, match_original=True,color='w',edgecolor='k'))

我用Natural Earth Data免费下载的land shapefile测试时,也出现了同样的多余线条。后来我在QGIS里编辑这个shapefile,试图去掉南极洲的边界,但又不知道怎么对shapefile的内部区域进行掩膜。我还试过把上述geopandas代码里的linewidth设为0,再叠加自己创建的shapefile,结果两者的边界完全匹配不上:

补充一下:后来我参考了Thomas Khün的过往回答,结合自己编辑后的shapefile,终于实现了效果不错的南极洲大陆掩膜,但新问题来了——海岸线超出了地图的圆形边缘:

我用的编辑后shapefile是去掉了多余线条的Natural Earth Data 50m land shapefile,原文件是Natural Earth Data提供的50m land shapefile。
想请教各位,有没有办法用shapefile实现精准掩膜,或者用geopandas的时候避免出现那条多余的线条?
内容的提问来源于stack exchange,提问作者lanadaquenada




