gpt4 book ai didi

python - 带有 html 区域链接的 cartopy 国家 map

转载 作者:行者123 更新时间:2023-11-30 23:34:08 25 4
gpt4 key购买 nike

我一直按照用户 pelson 的指示来创建带有填充国家形状的 map 。 (Fill countries in python basemap)

现在我很好奇,想更进一步并创建一个如下所示的 html 网站: http://www.ethnologue.com/region/NEU我不需要那些花哨的弹出窗口,但每个国家/地区的链接( http://www.w3schools.com/tags/att_area_href.asp )会非常好。是否可以使用 cartopy 创建这些坐标列表?我正在寻找一个生成静态 html 文件的全自动脚本。

最佳答案

是的,这绝对是可能的,但如果您正在制作基于网络的 map ,那么可能值得您看看 D3.js(具体来说,对于 map ,请参阅这个优秀的教程 http://bost.ocks.org/mike/map/ )。

对于 cartopy,我将逐步完成此操作,因为它是 matplotlib 和 cartopy 中转换系统的一个很好的演练。

首先,我们可以得到图中任意点的像素坐标:

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

ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()

# Define a transformation which takes latitude and longitude values,
# and returns pixel coordinates.
ll_to_pixel = ccrs.Geodetic()._as_mpl_transform(ax)

# We need to call draw to ensure that the axes location has been defined
# fully.
plt.draw()

# Now lets figure out the pixel coordinate of Sydney.
x_pix, y_pix = ll_to_pixel.transform_point([151.2111, -33.8600])

# We can even plot these pixel coordinates directly with matplotlib.
plt.plot(x_pix, y_pix, 'ob', markersize=25, transform=None)

plt.savefig('figure_1.png', dpi=plt.gcf().get_dpi())
plt.show()

Sydney in pixel coordinates

现在我们有了编写代码以生成区域 map 所需的信息,我已经编写了完整的注释(<80 行)。我已将其作为要点 ( https://gist.github.com/pelson/6308997 ) 发布,以便您可以查看并尝试一下(如果您愿意)。有关结果的现场演示:https://rawgithub.com/pelson/6308997/raw/map.html

关于python - 带有 html 区域链接的 cartopy 国家 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18380365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com