[英]How to convert display coordinates to geographoc coordinates (lat, lon) using cartopy?
鑒於其像素坐標,我想獲得數據點的緯度和經度。 我對 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)。
在 PlateCarree 投影中,X 和 Y 線性映射到經度和緯度。 因此,假設您知道位圖的寬度和高度:
longitude = MIN_LON + x * (MAX_LON-MIN_LON) / width
latitude = MIN_LAT + y * (MAX_LAT-MIN_LAT) / height
您可以使用從顯示坐標(像素)轉換為數據坐標的數據轉換的逆:
ax.transData.inverted().transform((x_pixel, y_pixel))
但是請記住,顯示窗口(圖形)通常會在圖像周圍顯示一些邊距,因此0,0
是圖形的左下角,而不是軸!
交互式后端使用此轉換在右上角顯示當前光標位置的數據坐標:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.