Home > other >  Basemap and zoom boundaries
Basemap and zoom boundaries

Time:04-08

After clicking on the ’Search‘ button and zooming in on a region of the map, how do you get the geographic coordinates in longitude and latitude of the borders of this displayed region?

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np


def on_xlim_change(*args):
    pass
    # Here I would like to get the coordinates in lat and long of the zoomed map

fig = plt.figure(figsize=(7,8),dpi=300,facecolor=(0.3,0.7,0.4,0.2))
ax = fig.add_subplot(111)

long1=-180; long2=180; lat1=-90; lat2=90
m = Basemap(projection='mill',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=long1,urcrnrlon=long2,resolution='i')
m.fillcontinents(color='coral',lake_color='aqua')
m.drawmapboundary(fill_color='aqua')
m.drawcoastlines(linewidth=0.3)
m.drawcountries(linewidth=0.15)
m.drawmeridians(np.arange(-180,180,30),dashes=[1,0],linewidth=0.1,labels=[False,False,True,False],fontname='Times New Roman',fontsize=4)
m.drawparallels(np.arange(-90,90,30),dashes=[1,0],linewidth=0.1,labels=[False,True,False,False],fontname='Times New Roman',fontsize=4)

ax.callbacks.connect('xlim_changed',on_xlim_change)

plt.show()

CodePudding user response:

The plot limits are available through the methods get_xlim and get_ylim from your Axes object:

xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

These limits are in projection coordinates, and you can convert them to geographic coordinates by calling your Basemap object with the additional keyword argument inverse=True:

lonmin, latmin = m(xmin, ymin, inverse=True)
lonmax, latmax = m(xmax, ymax, inverse=True)

CodePudding user response:

Thanks Molinav, If I put your instructions in my program, it gives me wrong values for latmin and latmax, but if I change the function on_xlim_change() in on_ylim_change() with ax.callbacks.connect(‘ylim_changed‘,on_ylim_change) everything becomes perfect. I don't know why and wherefore, but that's the way it is. Thanks again.

  • Related