带有html区域链接的卡通国家地图

2024-10-05 14:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在按照用户pelson的指示创建一个填充了国家形状的地图。(Fill countries in python basemap

现在我很想把这一点再往前推进一步,然后创建一个这样的html站点: http://www.ethnologue.com/region/NEU 我不需要那些花哨的弹出窗口,但那些链接(http://www.w3schools.com/tags/att_area_href.asp)对每个国家将是非常好的。 可以用cartopy创建这些坐标列表吗? 我正在寻找一个完全自动的脚本生成一个静态html文件。在


Tags: 用户incomhttp站点htmlwww地图
1条回答
网友
1楼 · 发布于 2024-10-05 14:22:05

是的,这是绝对可能的,但是如果您正在制作基于web的地图,那么您可能需要看看D3.js(特别是,对于地图,请参阅这个优秀的教程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

现在,我们已经掌握了编写代码以生成区域地图所需的信息,我已经着手编写了完整的注释(<;80行)。我把这篇文章作为一个要点(https://gist.github.com/pelson/6308997)发布,这样你可以查看一下,如果你愿意的话,可以试一试。对于结果的实时演示:https://rawgithub.com/pelson/6308997/raw/map.html

相关问题 更多 >