Drawing on map projections (Python)

From LiteratePrograms

Jump to: navigation, search

Python and basemap matplotlib toolkit provide an efficient way to draw a collection of shapes over real world maps. This article explains how to draw a simple roadmap among some cities in Western Europe. This roadmap is used as an example of Dijkstra's shortest path algorithm.

Contents

Requirement

It is quite easy to install each library. The install procedure is avilable at Installing matplotlib.

How to draw

Data

The example roadmap contains seven european cities -- Barcelona, Narbonne, Paris, Toulouse, Lausanne, Marseille and Geneva. We need to know their coordinates in order to plot their locations on projection map. The exact coordinates can be found at each city's artcle in wikipedia.

Seven cities and their coordinates
City Latitude Longitude
Barcelona 41°23' N 2º11' E
Narbonne 43°11'N 3°00'E
Paris 48°52′N 2°19′E
Toulouse 43°36′N, 1°26′E
Lausanne 46°31′N 6°38′E
Marseille 43°17′N 5°22′E
Geneva 46°12′N 6°09′E

Code

Library information

First, we declare some libraries we are going to use. We also use AGG backend to draw a map in a raster graphic format.

<<include>>=
import matplotlib
matplotlib.use('Agg')

from matplotlib.toolkits.basemap import Basemap
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

Basemap set-up

The next step is basemap set-up. Each argument means:

  • llcrnrlon: longitude of lower left hand corner of the desired map domain.
  • llcrnrlat: latitude of lower left hand corner of the desired map domain.
  • urcrnrlon: longitude of lower upper right corner of the desired map domain.
  • urcrnrlat: latitude of upper right hand corner of the desired map domain.
  • resolution: resolution of boundary data. 'l' stands for 'low.'
  • projection: we will use Transverse Mercator projection -- one of 17 projection maps basemap toolkits supports.
  • lon_0: central longitude of the desired map domain.
  • lat_0: central latitude of the desired map domain.
<<basemap>>=
m = Basemap(llcrnrlon=1, \
            llcrnrlat=40.6, \
            urcrnrlon=8.8, \
            urcrnrlat = 49.6, \
            resolution = 'l', \
            projection = 'tmerc', \
            lon_0 = 4.9, \
            lat_0 = 45.1)

Canvas set-up

Now we set up canvas, add an axis, and reset figure size to have same aspect ratio as map. The width of '8' inches is just a heuristic number.

<<figure_setup>>=
fig = Figure()
canvas = FigureCanvas(fig)
m.ax = fig.add_axes([0, 0, 1, 1])
fig.set_figsize_inches((8/m.aspect, 8.))

Coordinate Data of Cities

The coordinates of seven cities are stored in list.

<<data>>=
lats = [41.38, 43.18, 48.87, 43.60, 46.52, 43.28, 46.20]
lons = [ 2.18,  3.00,  2.32,  1.43,  6.63,  5.37,  6.15]
name = ['Barcelona', 'Narbonne', 'Paris', 'Toulouse', 'Lausanne', 'Marseille', 'Geneva']

Draw and Save

We draw coastlines as well as political boundaries and fill the continent. Then convert both latitude and longitude into map domain coordinates. Finally, we plot each city with a blue dot and save the map in PNG format.

<<draw>>=
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.fillcontinents(color='beige')
x, y = m(lons, lats)
m.plot(x, y, 'bo')
canvas.print_figure('map.png', dpi=100)

The Whole Code

<<draw_europe.py>>=
include
def main():
    basemap

    figure_setup

    data

    draw

if __name__ == "__main__":
    main()

Result

Seven cities in Europe
Enlarge
Seven cities in Europe

External links

Download code
Personal tools