Pyhton get lat and lon data of a cartopy orthographic projection by a MouseClick event

34 views Asked by At

In my Code i transform a Map, which is in the PlateCarree Format into a Orthographic map with cartopy. Now i want to click with the mouse on a point so that the code returns (or prints) the lat and lon coordinates. This is my code:

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

longitude = -90
latitude = 30

img = 'map.png'

#creates the orthografic model in which i can put my map
projection = ccrs.Orthographic(central_longitude=longitude, central_latitude=latitude)
fig, ax = plt.subplots(subplot_kw={'projection': projection})

#transfroms the map into the model
a = ax.imshow(plt.imread(img), origin='upper', extent=[-180, 180, -90, 90], transform=ccrs.PlateCarree())

plt.show()

I tried using the Mouse Click event:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from matplotlib.backend_bases import MouseButton

longitude = -90
latitude = 30

def on_click(event):
    if event.button is MouseButton.LEFT:
        print(f'data coords {event.xdata} {event.ydata}')


img = 'map.png'

projection = ccrs.Orthographic(central_longitude=longitude, central_latitude=latitude)
fig, ax = plt.subplots(subplot_kw={'projection': projection})

a = ax.imshow(plt.imread(img), origin='upper', extent=[-180, 180, -90, 90], transform=ccrs.PlateCarree())

plt.connect('button_press_event', on_click)

plt.show()

But that gave me the wrong coordinates.

To make it clearer:

example

I need the coordinates underlined in blue (top right corner), but i get these underlined in red. I see these coordinates when i hover with my mouse over the graphic, so there must be a way to get them with a mouse click.

0

There are 0 answers