用Cartopy在地图上显示图像时的投影问题

Jul*_*ang 5 python matplotlib cartopy

我有一些我想用Cartopy显示的卫星图像数据.我已成功按照此处详述的图像示例进行操作.导致此代码:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

fig = plt.figure(figsize=(12, 12))
img_extent = (-77, -59, 9, 26)

ax = plt.axes(projection=ccrs.PlateCarree())
# image data coming from server, code not shown
ax.imshow(img, origin='upper', extent=img_extent)
ax.set_xmargin(0.05)
ax.set_ymargin(0.10)

# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7)
ax.text(-117, 33, 'San Diego')

ax.coastlines()
ax.gridlines()

plt.show() 
Run Code Online (Sandbox Code Playgroud)

此代码生成以下图像 投射错误

我的问题是卫星图像数据不在PlateCarree投影中,而是墨卡托投影.

但是当我得到轴对象时

ax = plt.axes(projection=ccrs.Mercator())
Run Code Online (Sandbox Code Playgroud)

我失去了海岸线.

没有海岸线

我在这里看到了这个问题.但

ax.set_global()
Run Code Online (Sandbox Code Playgroud)

得到这张图片的结果:

我的数据在哪里

数据不存在,圣地亚哥位置错误.纬度/经度范围也发生了变化.我究竟做错了什么?

发布讨论更新

主要问题是我没有使用该transform_points方法在目标投影中正确指定图像范围.我还必须具体说明imshow菲尔建议的方法中的坐标参考系统.这是正确的代码:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

proj = ccrs.Mercator()
fig = plt.figure(figsize=(12, 12))
extents = proj.transform_points(ccrs.Geodetic(),
                                np.array([-77, -59]),
                                np.array([9, 26]))

img_extents = (extents[0][0], extents[1][0], extents[0][6], extents[1][7] ) 

ax = plt.axes(projection=proj)
# image data coming from server, code not shown
ax.imshow(img, origin='upper', extent=img_extents,transform=proj)

ax.set_xmargin(0.05)
ax.set_ymargin(0.10)

# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())

ax.coastlines()
ax.gridlines()

plt.show() 
Run Code Online (Sandbox Code Playgroud)

导致这个正确地理投影的卫星图像:

正确的形象

pel*_*son 5

理想情况下,在使用 cartopy 绘图时(通过 transform 关键字),尝试始终具体说明数据所在的坐标参考系。这意味着您只需在脚本中切换投影,数据就会自动放置在正确的位置。

因此,在您的情况下,plt.imshow应该有一个transform=ccrs.Mercator()关键字参数(您可能需要一个更具体的参数化墨卡托实例)。如果您的范围采用大地测量(纬度和经度),您将必须将边界框转换为墨卡托坐标,但除此之外,其他一切都应该按预期工作。

注意:我将去更新示例以包含转换参数;-)(PR: https: //github.com/SciTools/cartopy/pull/343

华泰