底图和缩放边界

发布于 2025-01-19 16:12:25 字数 1017 浏览 1 评论 0原文

点击“搜索”按钮并放大地图上的某个区域后,如何获取该显示区域边界的经纬度地理坐标?

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2025-01-26 16:12:26

绘图限制可通过 Axes 对象中的 get_xlimget_ylim 方法获得:

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

这些限制位于投影坐标中,您可以将它们转换为通过使用附加关键字参数 inverse=True 调用 Basemap 对象来获取地理坐标:

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

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)
下壹個目標 2025-01-26 16:12:26

感谢莫利纳夫,
如果我将您的说明放在程序中,它为Latmin和latmax提供了错误的值,但是如果我更改 on_ylim_change()的函数on_xlim_change(),则使用 ax.callbacks.connect(' ylim_changed',on_ylim_change)一切变得完美。
我不知道为什么和何时,但这就是这样。
再次感谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文