Smopy lets you retrieve OpenStreetMap image maps from geographical coordinates.
In [2]:
import smopy
%matplotlib inline
Define a Map
object by giving the boundaries of your map: (lat_min, lon_min, lat_max, lon_max)
. Creating this object fetches the image from OpenStreetMap's servers (see usage policy).
In [3]:
map = smopy.Map((48., -1., 52., 3.), z=4)
In the IPython notebook, you can display the image directly with show_ipython
.
In [4]:
map.show_ipython()
You can also save it as a PNG file.
In [5]:
from IPython.display import Image
map.save_png('europe.png')
Image('europe.png')
Out[5]:
We can omit the zoomlevel, to automatically determine a maximum zoomlevel (based on a reasonable number of maximum OSM tiles:
In [6]:
map = smopy.Map((49., -1., 52., 3.))
map.show_ipython()
Zooming out, by manually setting the zoom level:
In [7]:
map = smopy.Map((49., -1., 52., 3.), z=6)
map.show_ipython()
Now, you can create a matplotlib figure from this image. The Map
object comes with a to_pixels
method to convert from geographical coordinates to pixels in this image.
In [8]:
x, y = map.to_pixels(48.86151, 2.33474)
In [9]:
ax = map.show_mpl(figsize=(8, 6))
ax.plot(x, y, 'or', ms=10, mew=2);
The to_pixels
method also works with NumPy arrays, so you can really plot anything on your map (lines, curves, polygons, patches).
Tile servers that use the OSM API can be found at: http://wiki.openstreetmap.org/wiki/Tile_servers
These can be used by smopy
by giving it as initialization parameters of the map:
map = smopy.Map((48.7, 2.1, 49., 2.5), tileserver="http://tile.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png", tilesize=512, maxtiles=16)
CartoDB example:
"Map tiles by CartoDB, under CC BY 3.0. Data by OpenStreetMap, under OD bL."
In [10]:
map = smopy.Map((48.7, 2.1, 49., 2.5), tileserver="http://tile.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png", tilesize=512, maxtiles=16)
map.show_ipython()
In [ ]: