从地球同步投影重新投影图像#

此示例演示了Cartopy将图像实时投影到所需投影中的能力。图像本身从URL中检索并直接加载到内存中,而无需将其中间存储到文件中。它代表来自Meteosat二代上的旋转增强可见光和红外成像仪的预处理数据,该数据已被放入数据原生地球同步坐标系中的图像中,然后通过制图投影到全球米勒地图中。

Traceback (most recent call last):
  File "/pb1/repo/cartopy/examples/scalar_data/geostationary.py", line 61, in <module>
    main()
    ~~~~^^
  File "/pb1/repo/cartopy/examples/scalar_data/geostationary.py", line 54, in main
    img, crs, extent, origin = geos_image()
                               ~~~~~~~~~~^^
  File "/pb1/repo/cartopy/examples/scalar_data/geostationary.py", line 41, in geos_image
    img_handle = BytesIO(urlopen(url).read())
                         ~~~~~~~^^^^^
  File "/usr/lib/python3.13/urllib/request.py", line 189, in urlopen
    return opener.open(url, data, timeout)
           ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/urllib/request.py", line 489, in open
    response = self._open(req, data)
  File "/usr/lib/python3.13/urllib/request.py", line 506, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
                              '_open', req)
  File "/usr/lib/python3.13/urllib/request.py", line 466, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.13/urllib/request.py", line 1367, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        context=self._context)
                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/urllib/request.py", line 1322, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 101] Network is unreachable>

from io import BytesIO
from urllib.request import urlopen

import matplotlib.pyplot as plt

import cartopy.crs as ccrs


def geos_image():
    """
    Return a specific SEVIRI image by retrieving it from a github gist URL.

    Returns
    -------
    img : numpy array
        The pixels of the image in a numpy array.
    img_proj : cartopy CRS
        The rectangular coordinate system of the image.
    img_extent : tuple of floats
        The extent of the image ``(x0, y0, x1, y1)`` referenced in
        the ``img_proj`` coordinate system.
    origin : str
        The origin of the image to be passed through to matplotlib's imshow.

    """
    url = ('https://gist.github.com/pelson/5871263/raw/'
           'EIDA50_201211061300_clip2.png')
    img_handle = BytesIO(urlopen(url).read())
    img = plt.imread(img_handle)
    img_proj = ccrs.Geostationary(satellite_height=35786000)
    img_extent = [-5500000, 5500000, -5500000, 5500000]
    return img, img_proj, img_extent, 'upper'


def main():
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.Miller())
    ax.coastlines()
    ax.set_global()
    print('Retrieving image...')
    img, crs, extent, origin = geos_image()
    print('Projecting and plotting image (this may take a while)...')
    ax.imshow(img, transform=crs, extent=extent, origin=origin, cmap='gray')
    plt.show()


if __name__ == '__main__':
    main()

Total running time of the script: (2 minutes 17.454 seconds)

Gallery generated by Sphinx-Gallery _