Smopy example

Smopy lets you retrieve OpenStreetMap image maps from geographical coordinates.


In [1]:
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 [2]:
map = smopy.Map((48., -1., 52., 3.), z=4)

In the IPython notebook, you can display the image directly with show_ipython.


In [3]:
map.show_ipython()


You can also save it as a PNG file.


In [4]:
from IPython.display import Image
map.save_png('europe.png')
Image('europe.png')


Out[4]:

We can omit the zoomlevel, to automatically determine a maximum zoomlevel (based on a reasonable number of maximum OSM tiles:


In [5]:
map = smopy.Map((49., -1., 52., 3.))
map.show_ipython()


Lowered zoom level to keep map size reasonable. (z = 7)

Zooming out, by manually setting the zoom level:


In [6]:
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 [7]:
x, y = map.to_pixels(48.86151, 2.33474)

In [8]:
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).

Alternate tile servers

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 setting:

smopy.TILE_SERVER = url
smopy.TILE_SIZE = 256  # 512 for @2x tiles
smopy.MAXTILES = 16  # [9, 16, 25]

CartoDB example:

"Map tiles by CartoDB, under CC BY 3.0. Data by OpenStreetMap, under OD bL."


In [9]:
smopy.TILE_SERVER = "http://tile.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png"
smopy.TILE_SIZE = 512
map = smopy.Map((48.7, 2.1, 49., 2.5))
map.show_ipython()


Lowered zoom level to keep map size reasonable. (z = 10)

In [ ]: