Differences between revisions 1 and 25 (spanning 24 versions)
Revision 1 as of 2011-02-09 19:19:11
Size: 692
Comment:
Revision 25 as of 2014-01-10 15:00:43
Size: 3180
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#acl AdminGroup:read,write,delete,revert EditorGroup:read,write StudentGroup:read,write #acl AdminGroup:read,write,delete,revert EditorGroup:read,write All:read
Line 5: Line 5:
Line 7: Line 6:
{{http://www.scipy.org/Cookbook/Matplotlib/Maps?action=AttachFile&do=get&target=basemap3c.png||height="399px",width="532px"}}
Line 10: Line 10:
== Map and projection basics ==

[[EarthShape | The Earth shape and it's approximation ]]


Line 11: Line 17:
There are different ways to generate maps with geographic information within Python. These are either based on the [[http://www.pyngl.ucar.edu/|PyNGL]] ([[http://www.pyngl.ucar.edu/Examples/gallery.shtml|Gallery]]) or [[http://matplotlib.sourceforge.net/basemap/doc/html/|Basemap]] packages. PyNGL is very flexible to handle and allows for the generation of high quality maps. However, it takes a while to generate a nice looking map in PyNGL. If you just want to visualise your data, Basemap might be a very good alternative in many cases.
Line 12: Line 19:
'''References:'''
 * [[http://www.scipy.org/Cookbook/Matplotlib/Maps | Cookbook / Matplotlib / Maps]]
 * [[http://matplotlib.sourceforge.net/basemap/doc/html/ | Matplotlib Basemap Toolkit documentation]]
=== The Basemap way ===
The first step is the generation of an Basemap object which includes also the definition of the projection. Which projection to choose depends on your application. You might want to have a map with specific properties like e.g. equal area or equal angular and optimized for a specific region like e.g. the polar regions.

You can then draw pre-defined data or use your own data.

{{{#!python
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

#/// define a map projection ///
map = Basemap(projection='ortho', lat_0 = 50, lon_0 = -100,
              resolution = 'l', area_thresh = 1000.)

#to see a list of potential projections, type help(Basemap)
#be aware that each projection has different types of parameters

#... you like coastlines?
map.drawcoastlines()

#... and countries
map.drawcountries()

#... fill continents
map.fillcontinents(color = 'coral')

#... draw coordinates
map.drawmeridians(np.arange(0, 360, 30))
map.drawparallels(np.arange(-90, 90, 30))

}}}
Line 19: Line 54:
You want to plot some point data?
{{{#!python
Line 20: Line 57:
clf()
map.bluemarble()

# lat/lon coordinates of five cities.
lats = [40.02, 32.73, 38.55, 48.25, 17.29]
lons = [-105.16, -117.16, -77.00, -114.21, -88.10]
cities=['Boulder, CO','San Diego, CA',
        'Washington, DC','Whitefish, MT','Belize City, Belize']
# compute the native map projection coordinates for cities.
x,y = map(lons,lats)
# plot filled circles at the locations of the cities.
map.plot(x,y,'ro')
# plot the names of those five cities.
for name,xpt,ypt in zip(cities,x,y):
    plt.text(xpt+50000,ypt+50000,name,color='white')


}}}



=== References: ===
 * [[http://www.scipy.org/Cookbook/Matplotlib/Maps|Cookbook / Matplotlib / Maps]]
 * [[http://matplotlib.sourceforge.net/basemap/doc/html/|Matplotlib Basemap Toolkit documentation]]
 * Snyder: Map projections - a reference manual

Visualising spatial data

http://www.scipy.org/Cookbook/Matplotlib/Maps?action=AttachFile&do=get&target=basemap3c.png

High level programming languages like e.g. Python, Matlab, R, IDL allow for the easy generation of maps by using specific modules. Alternatives to produce maps is special commerical or non-commercial software like e.g. ArcGIS, Generic mapping tool (GMT), ...

Map and projection basics

The Earth shape and it's approximation

How to do it in Python

There are different ways to generate maps with geographic information within Python. These are either based on the PyNGL (Gallery) or Basemap packages. PyNGL is very flexible to handle and allows for the generation of high quality maps. However, it takes a while to generate a nice looking map in PyNGL. If you just want to visualise your data, Basemap might be a very good alternative in many cases.

The Basemap way

The first step is the generation of an Basemap object which includes also the definition of the projection. Which projection to choose depends on your application. You might want to have a map with specific properties like e.g. equal area or equal angular and optimized for a specific region like e.g. the polar regions.

You can then draw pre-defined data or use your own data.

   1 from mpl_toolkits.basemap import Basemap
   2 import matplotlib.pyplot as plt
   3 import numpy as np
   4 
   5 #/// define a map projection ///
   6 map = Basemap(projection='ortho', lat_0 = 50, lon_0 = -100,
   7               resolution = 'l', area_thresh = 1000.)
   8 
   9 #to see a list of potential projections, type help(Basemap)
  10 #be aware that each projection has different types of parameters
  11 
  12 #... you like coastlines?
  13 map.drawcoastlines()
  14 
  15 #... and countries
  16 map.drawcountries()
  17 
  18 #... fill continents
  19 map.fillcontinents(color = 'coral')
  20 
  21 #... draw coordinates
  22 map.drawmeridians(np.arange(0, 360, 30))
  23 map.drawparallels(np.arange(-90, 90, 30))

You want to plot some point data?

   1 clf()
   2 map.bluemarble()
   3 
   4 # lat/lon coordinates of five cities.
   5 lats = [40.02, 32.73, 38.55, 48.25, 17.29]
   6 lons = [-105.16, -117.16, -77.00, -114.21, -88.10]
   7 cities=['Boulder, CO','San Diego, CA',
   8         'Washington, DC','Whitefish, MT','Belize City, Belize']
   9 # compute the native map projection coordinates for cities.
  10 x,y = map(lons,lats)
  11 # plot filled circles at the locations of the cities.
  12 map.plot(x,y,'ro')
  13 # plot the names of those five cities.
  14 for name,xpt,ypt in zip(cities,x,y):
  15     plt.text(xpt+50000,ypt+50000,name,color='white')

References:

LehreWiki: GenMaps (last edited 2014-01-10 15:00:43 by anonymous)