Convert plot co-ords to geographic co-ords

2019-09-02 07:34发布

I am able to make a plot of data points based on their Lat and Long, which looks like:

enter image description here

whereby the orange is made up of points like:

enter image description here

using the code:

m = Basemap(projection='merc',llcrnrlat=-0.5,urcrnrlat=0.5,\
            llcrnrlon=9,urcrnrlon=10,lat_ts=0.25,resolution='i')
m.drawcoastlines()
m.drawcountries()
# draw parallels and meridians.
parallels = np.arange(-9.,10.,0.5)
# Label the meridians and parallels
m.drawparallels(parallels,labels=[False,True,True,False])
# Draw Meridians and Labels
meridians = np.arange(-1.,1.,0.5)
m.drawmeridians(meridians,labels=[True,False,False,True])
m.drawmapboundary(fill_color='white')
x,y = m(X, Y)  # This is the step that transforms the data into the map's projection
scatter = plt.scatter(x,y)
m.scatter(x,y)

where X and Y are numpy arrays.

I want to get the X and Y co-ordinate of a point that I click on.

I can get the co-ord using:

coords = []
def onclick(event):
    if plt.get_current_fig_manager().toolbar.mode != '':
        return
    global coords
    ix, iy = event.x, event.y
    print('x = %d, y = %d'%(ix, iy))

    global coords
    coords.append((ix, iy))

    return coords

cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()

but this seems to return the figure co-ordinates. Is there a way to convert these to their respective lat and long co-ordinates?

I then plan to use these to find the nearest point in the original X and Y arrays to where I click

1条回答
姐就是有狂的资本
2楼-- · 2019-09-02 08:14

First of all you would want to use the data coordinates

ix, iy = event.xdata, event.ydata

Then to get lon/lat coordinates you need to apply the inverse map transform

lon, lat = m(event.xdata, event.ydata, inverse=True)
查看更多
登录 后发表回答