【问题标题】:How to convert display coordinates to geographoc coordinates (lat, lon) using cartopy?如何使用 cartopy 将显示坐标转换为地理坐标(纬度、经度)?
【发布时间】:2021-10-29 04:23:57
【问题描述】:

在给定像素坐标的情况下,我想获取数据点的纬度和经度。我对matplotlib转换很困惑,但我想解决问题的关键就在这里。

import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature

MIN_LAT = 5
MAX_LAT = 70
MIN_LON = -100
MAX_LON = 23

# Map

fig = plt.figure(figsize=(12,7))
proj = ccrs.PlateCarree(central_longitude=0)  

ax=plt.axes(projection=proj)

ax.add_feature(cfeature.LAND, color='lightgray')
ax.add_feature(cfeature.OCEAN)

# Select area
ax.set_extent([MIN_LON, MAX_LON, MIN_LAT, MAX_LAT], crs=proj)

fig.canvas.draw()

例如,给定像素坐标 (0, 0)(matplotlib 中的左下角),我希望返回地理坐标 (MIN_LON, MIN_LAT)。

【问题讨论】:

    标签: python matplotlib coordinates cartopy geography


    【解决方案1】:

    在 PlateCarree 投影中,X 和 Y 线性映射到经度和纬度。所以,假设你知道位图的宽度和高度:

    longitude = MIN_LON + x * (MAX_LON-MIN_LON) / width
    latitude = MIN_LAT + y * (MAX_LAT-MIN_LAT) / height
    

    【讨论】:

    • 感谢您的回答。假设我正在使用另一个投影(墨卡托投影),那么我怎样才能获得地理坐标?如果可能的话,我想要一个对任何类型的预测都有效的解决方案
    • 每个投影都是不同的——你不能做出任何概括。你必须谷歌每一个才能找到正确的翻译。
    • 下面matplotlib.org/stable/tutorials/advanced/…和cartopy投影对象,好像有办法将显示坐标映射到地理坐标
    【解决方案2】:

    您可以使用从显示坐标(像素)转换为data coordinates 的逆数据转换:

    ax.transData.inverted().transform((x_pixel, y_pixel))
    

    但是请记住,显示窗口(图)通常会在图像周围显示一些边距,因此0,0 是图的左下角,而不是轴!

    交互式后端使用此转换在右上角显示当前光标位置的数据坐标:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2016-03-21
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多