从几何坐标获取投影坐标
Get projected coordinates from geometric coordinates
我有一个用 Cartopy 和 Matplotlib 渲染的地图。我有一个特定的几何坐标(在 lat/lon 中),我想知道最接近该几何坐标投影的像素坐标(如果它可见),例如在地图上的坐标上绘制图形。
(请注意,我不想 使用 Matplotlib 进行绘制;我将图形导出为位图图像并在管道的不同部分进行绘制。)
This documentation 表明它可能是这样的:
import cartopy, matplotlib.pyplot
fig = matplotlib.pyplot.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=cartopy.crs.Orthographic())
ax.add_feature(cartopy.feature.LAND, facecolor='black')
# Print the location of New York City in display coordinates
lon, lat = -74.0060, 40.7128
trans = cartopy.crs.Geodetic()._as_mpl_transform(ax)
x, y = trans.transform((lon, lat))
print(x, y)
# Or this way
projx, projy = ax.projection.transform_point(lon, lat, cartopy.crs.Geodetic())
x, y = ax.transData.transform((projx, projy))
print(x, y)
不过有趣的是,如果我绘制这个点,图形以曼哈顿为中心并放大到曼哈顿,然后输出显示坐标确实位于图形中心 (640, 480)。
matplotlib.pyplot.plot(lon, lat, marker='o', color='red', markersize=12,
alpha=0.7, transform=cartopy.crs.Geodetic())
我刚刚发现在图形处于最终状态之前,转换设置不正确。所以关键是先画图,
fig.canvas.draw()
或至少正确应用方面。
ax.apply_aspect()
然后你会得到正确的像素坐标,
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, facecolor='black')
ax.set_global()
# before being able to call any of the transforms, the figure needs to be drawn
fig.canvas.draw()
# or
# ax.apply_aspect()
# Print the location of New York City in display coordinates
lon, lat = -74.0060, 40.7128
trans = ccrs.PlateCarree()._as_mpl_transform(ax)
x, y = trans.transform_point((lon, lat))
print(x,y)
plt.show()
这会打印:
188.43377777777778 312.3783111111111
请注意,这些坐标是指从左下角开始的像素。
在我的示例代码中,我没有指定地图的范围。如果我添加
ax.set_global()
那么变换后的坐标是合理的。
我提出了两种计算变换坐标的方法,但是 _as_mpl_transform()
的方法似乎是 return 在纽约市不可见时的中心点。当off-screen.
时用ax.projection.transform_point()
returns NaN的方式
我有一个用 Cartopy 和 Matplotlib 渲染的地图。我有一个特定的几何坐标(在 lat/lon 中),我想知道最接近该几何坐标投影的像素坐标(如果它可见),例如在地图上的坐标上绘制图形。
(请注意,我不想 使用 Matplotlib 进行绘制;我将图形导出为位图图像并在管道的不同部分进行绘制。)
This documentation 表明它可能是这样的:
import cartopy, matplotlib.pyplot
fig = matplotlib.pyplot.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=cartopy.crs.Orthographic())
ax.add_feature(cartopy.feature.LAND, facecolor='black')
# Print the location of New York City in display coordinates
lon, lat = -74.0060, 40.7128
trans = cartopy.crs.Geodetic()._as_mpl_transform(ax)
x, y = trans.transform((lon, lat))
print(x, y)
# Or this way
projx, projy = ax.projection.transform_point(lon, lat, cartopy.crs.Geodetic())
x, y = ax.transData.transform((projx, projy))
print(x, y)
不过有趣的是,如果我绘制这个点,图形以曼哈顿为中心并放大到曼哈顿,然后输出显示坐标确实位于图形中心 (640, 480)。
matplotlib.pyplot.plot(lon, lat, marker='o', color='red', markersize=12,
alpha=0.7, transform=cartopy.crs.Geodetic())
我刚刚发现在图形处于最终状态之前,转换设置不正确。所以关键是先画图,
fig.canvas.draw()
或至少正确应用方面。
ax.apply_aspect()
然后你会得到正确的像素坐标,
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, facecolor='black')
ax.set_global()
# before being able to call any of the transforms, the figure needs to be drawn
fig.canvas.draw()
# or
# ax.apply_aspect()
# Print the location of New York City in display coordinates
lon, lat = -74.0060, 40.7128
trans = ccrs.PlateCarree()._as_mpl_transform(ax)
x, y = trans.transform_point((lon, lat))
print(x,y)
plt.show()
这会打印:
188.43377777777778 312.3783111111111
请注意,这些坐标是指从左下角开始的像素。
在我的示例代码中,我没有指定地图的范围。如果我添加
ax.set_global()
那么变换后的坐标是合理的。
我提出了两种计算变换坐标的方法,但是 _as_mpl_transform()
的方法似乎是 return 在纽约市不可见时的中心点。当off-screen.
ax.projection.transform_point()
returns NaN的方式