从几何坐标获取投影坐标 [英] Get projected coordinates from geometric coordinates

查看:71
本文介绍了从几何坐标获取投影坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Cartopy和Matplotlib渲染的地图.我有一个特定的几何坐标(纬度/经度),我想知道最接近该几何坐标投影的像素坐标(如果它可见),例如在地图上的坐标上绘制图形.

I have a map figure rendered with Cartopy and Matplotlib. I have a specific geometric coordinate (in lat/lon) and I would like to know the pixel coordinate closest to this geometric coordinate's projection (if it is visible), for instance to draw a graphic over the coordinate on the map.

(注意我不想用 Matplotlib 绘制;我将图形导出为位图图像并在管道的不同部分进行绘制.)

(Note I don't want to draw with Matplotlib; I'm exporting the figure as a bitmap image and drawing in a different part of the pipeline.)

此文档建议可能是这样的:

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

Though interestingly, if I plot this point, the figure centers on and zooms into Manhattan, and then the output display coordinates are indeed in the center of the figure at (640, 480).

matplotlib.pyplot.plot(lon, lat, marker='o', color='red', markersize=12,
    alpha=0.7, transform=cartopy.crs.Geodetic())

推荐答案

我刚刚发现在图形处于最终状态之前,转换设置不正确.所以关键还是先画好图,

I just found that the transforms are not properly set until the figure is in its final state. So the key is to first draw the figure,

fig.canvas.draw()

或至少正确应用方面.

ax.apply_aspect()

然后您将获得正确的像素坐标,

Then you will get the correct pixel coordinates out,

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

请注意,这些坐标是指左下角的像素.

Note that those coordinates refer to the pixels from the lower left corner.

这篇关于从几何坐标获取投影坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆