Question

I have been following instructions from user pelson to create a map with filled country shapes. (Fill countries in python basemap)

Now I was curious on putting this one step further and creating a html site like this: http://www.ethnologue.com/region/NEU I don't need those fancy popups but those links (http://www.w3schools.com/tags/att_area_href.asp) for every country would be real nice. Is it possible to create those lists of coordinates with cartopy? I'm looking for a fully automatic script generating a static html file.

Était-ce utile?

La solution

Yes, this is definitely possible, but if you're producing web based maps, it might be worth you looking at D3.js (specifically, for maps see this excellent tutorial http://bost.ocks.org/mike/map/).

For cartopy however I'll take this through step-by-step, as it is a good walkthrough of the transformation system in matplotlib and cartopy.

First, we can get the pixel coordinate of any point in a figure:

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

Now we have the information necessary to write the code to produce the area map, which I've gone ahead and written with full comments (<80 lines). I've posted this as a gist (https://gist.github.com/pelson/6308997) so that you can check it out and give it a go, if you like. For a live demo of the result: https://rawgithub.com/pelson/6308997/raw/map.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top