在自定义投影上绘制自然地球特征

时间:2013-10-01 15:09:02

标签: matplotlib cartopy

我正在尝试制作一些海冰数据图。数据在EASE-North网格中传送,示例文件(HDF4)可以在以下位置下载:

ftp://n4ftl01u.ecs.nasa.gov/SAN/OTHR/NISE.004/2013.09.30/

我为EASE-Grid创建了一个自定义投影类,它似乎正在工作(海岸线与数据很好地对齐)。

当我尝试添加自然地球功能时,它会返回一个空的Matplotlib图。

import gdal
import cartopy

# projection class
class EASE_North(cartopy.crs.Projection):

    def __init__(self):

        # see: http://www.spatialreference.org/ref/epsg/3408/
        proj4_params = {'proj': 'laea',
            'lat_0': 90.,
            'lon_0': 0,
            'x_0': 0,
            'y_0': 0,
            'a': 6371228,
            'b': 6371228,
            'units': 'm',
            'no_defs': ''}

        super(EASE_North, self).__init__(proj4_params)

    @property
    def boundary(self):
        coords = ((self.x_limits[0], self.y_limits[0]),(self.x_limits[1], self.y_limits[0]),
                  (self.x_limits[1], self.y_limits[1]),(self.x_limits[0], self.y_limits[1]),
                  (self.x_limits[0], self.y_limits[0]))

        return cartopy.crs.sgeom.Polygon(coords).exterior

    @property
    def threshold(self):
        return 1e5

    @property
    def x_limits(self):
        return (-9000000, 9000000)

    @property
    def y_limits(self):
        return (-9000000, 9000000)


# read the data
ds = gdal.Open('D:/NISE_SSMISF17_20130930.HDFEOS')

# this loads the layers for both hemispheres
data = np.array([gdal.Open(name, gdal.GA_ReadOnly).ReadAsArray()
                 for name, descr in ds.GetSubDatasets() if 'Extent' in name])

ds = None

# mask anything other then sea ice
sea_ice_concentration = np.ma.masked_where((data < 1) | (data > 100), data, 0)

# plot
lim = 3000000

fig, ax = plt.subplots(figsize=(8,8),subplot_kw={'projection': EASE_North(), 'xlim': [-lim,lim], 'ylim': [-lim,lim]})

land = cartopy.feature.NaturalEarthFeature(
            category='physical',
            name='land',
            scale='50m',
            facecolor='#dddddd',
            edgecolor='none')

#ax.add_feature(land)
ax.coastlines()

# from the metadata in the HDF
extent = [-9036842.762500, 9036842.762500, -9036842.762500, 9036842.762500]

ax.imshow(sea_ice_concentration[0,:,:], cmap=plt.cm.Blues, vmin=1,vmax=100,
          interpolation='none', origin='upper', extent=extent, transform=EASE_North())

上面的脚本运行正常并产生以下结果:

enter image description here

但是当我取消注释ax.add_feature(land)它失败而没有任何错误时,只返回空图。我错过了一些明显的东西吗?

这是IPython笔记本: http://nbviewer.ipython.org/6779935

My Cartopy版本是Christoph Gohlke网站的0.9版本(谢谢!)。

编辑:

尝试保存图形会引发异常:

fig.savefig(r'D:\test.png')

C:\Python27\Lib\site-packages\shapely\speedups\_speedups.pyd in shapely.speedups._speedups.geos_linearring_from_py (shapely/speedups/_speedups.c:2270)()

ValueError: A LinearRing must have at least 3 coordinate tuples

检查'land'cartopy.feature显示没有问题,所有多边形都通过.isvalid(),所有环(ext en int)都是4个或更多元组。所以输入形状似乎不是问题(并且在PlateCaree()中工作正常)。

也许有些戒指(比如南半球)在转变为EASE_North之后变得'腐败'?

EDIT2:

当我删除内置网元功能并加载相同的shapefile(但是剪切了40N以下的任何内容)时,它可以工作。所以这似乎是某种重投问题。

for state in shpreader.Reader(r'D:\ne_50m_land_clipped.shp').geometries():
    ax.add_geometries([state], cartopy.crs.PlateCarree(),facecolor='#cccccc', edgecolor='#cccccc')

enter image description here

1 个答案:

答案 0 :(得分:1)

我已经说过这是一个错误。我猜测add_feature更新matplotlib viewLim,结果是图片放大到一个很小的区域(除非你缩小很多,否则它会显示为白色)。

从我的头脑中,我认为matplotlib中的基本行为已得到改善,但是cartopy尚未使用新的viewLim计算。与此同时,我建议手动设置地图的范围:

ax.set_extent(extent, transform=EASE_North())

HTH

相关问题