【问题标题】:Projection Problems when Displaying an Image on a Map with Cartopy使用 Cartopy 在地图上显示图像时的投影问题
【发布时间】:2013-09-14 19:43:21
【问题描述】:

我有一些卫星图像数据想使用 Cartopy 显示。我已成功遵循详细的图像示例here。导致这段代码:

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() 

这段代码生成如下图片

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

但是当我用

得到轴对象时
ax = plt.axes(projection=ccrs.Mercator())

我失去了海岸线。

我看到了here 报告的问题。但是

ax.set_global()

此图像的结果:

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

发布讨论更新

主要问题是我没有使用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() 

生成此正确地理投影的卫星图像:

【问题讨论】:

  • 我的 Cartopy 0.7 在升级到 Mpl 1.3 后坏了,所以我不能为你测试。但是您不应该指定绘图数据的坐标吗?如果你不这样做,我认为坐标假定等于轴投影。因此,请尝试将transform=ccrs.PlateCarree() 添加到您的绘图命令中。

标签: python matplotlib cartopy


【解决方案1】:

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

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

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

HTH

【讨论】:

  • 谢谢,我用正确的代码添加了帖子讨论更新。
  • 太棒了。很高兴它有帮助。不错的情节:-)
猜你喜欢
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多