Raster Map Class

From GeoMod

Jump to: navigation, search

This class was developed to allow the more efficient use of VPython with geospatial raster maps. It has been used to create numerical models by students the class Introduction to Modeling in the Earth Sciences, as well as in visualizations for the Chertman model.


The code

How to use

  • Save the above code as raster_map.py and in your program code include the line
from raster_map import *

Example Demo

Assuming that;

  1. You have saved the above code as raster_map.py
  2. You have also saved the text file Media:rastert_asciito1.txt in the same directory

Then to import the raster data and display it you use the following program;

from visual import *
from raster_map import *

topog = raster_import("rastert_asciito1.txt")

topog.draw_map(imin=100, imax=150, jmin=100, jmax=150,
               center=1, scale=50)

Which will produce the following map:

Basic example.

Examples of use

  • Chertman Model Instructions - The Chertman agent-based archeological model created to better understand the relationship between Cro-Magnon groups and their environment.

  • Topography model: An interactive model designed to explain topography in introductory Earth Science labs.

Creating/Importing data

You can plug data into the raster_map in 2 ways:

  • by creating an array in the code, or
  • by importing an ArcGIS ASCII raster file

Using a NumPy array

For a regularly spaced grid you can simply plug in an array with elevation data;

topog = raster_map(data_array)


  • topog is a variable name you choose
  • data_array is an array

The other options are,

  • xmin and ymax the co-ordinates of the top left corner of the grid
  • dx the grid spacing (for a square grid)

Importing an ArcGIS ASCII file

To import an ASCII raster produced by ARC/GIS into Python, use one of the following two functions, depending on the format of the text file, to instantiate the class.

1) ARC/GIS text file with header information

An example of this type of text file is Media:rastert_asciito1.txt. In this case use:

topog = raster_import("rastert_asciito1.txt")


  • filename.txt is the name of the text file
  • topog is a variable name you choose

2) x, y, z format

To import from an x, y, z formatted data file (which has no header information) of the form



topog = raster_import_ryan("filename.txt")


  • filename.txt is the name of the text file
  • topog is a variable name you choose

Classes and Methods

This section describes the classes and methods included in the raster_map.py file.

Class raster_map

raster_map(data_array [, xmin=0.0, ymax=0.0, dx=1, zscale=1.0])


  • data_array: a 2d Numeric array


  • xmin: x-coordinate of the upper left cell of the array
  • ymax: y-coordinate of the upper left cell of the array
  • dx: spacing between nodes in the array
  • zscale: vertical exaggeration of output display


Draws a block map of the data in the array. The draw_map method call in the example above sets the vertical exaggeration to 50x and centers the scene on the sub-area between rows 100 and 150, and columns 100 to 150.

draw_map([imin=0, imax=-99999,jmin=0, jmax=-99999,
         scale = 1.0, center = 0,
         bzmin = 99999, bzmax = -99999,
         col_min = 99999, col_max = -99999,
         shade=2, contour_interval = 10.0])


  • None


  • imin, imax, jmin, jmax: These four variables define the indicies of the subset of the array to be drawn.
    • Default: all cells in array are drawn.
  • scale: vertical exaggeration of output display.
    • Default: scale= 1.0
  • center: if center = 1 then center the view on the middle of the displayed area.
    • Default: center = 0.
  • bzmin, bzmax: gives the minimum and maximum values for the color scale.
    • Default: If these values are omitted then the code calculates the minimum and maximum values in the part of the input array to be drawn.
  • col_min, col_max: gives the minimum and maximum colors
  • shade: shade = 2 is the only currently valid option. This creates a random color contouring scheme
  • contour_interval: the contour interval
    • Default: contour_interval = 10.0


Updates the map.


File:Raster map demo02.png
Illustration of line_3d method.

If we replaced the draw_map with line_3d;

from visual import *
from raster_map import *

topog = raster_import("rastert_asciito1.txt")

topog.line_3d(imin=100, imax=150, jmin=100, jmax=150,
               center=1, scale=50)

we would get this. (which is faster to render).


File:Map stripe demo.png
Illustrating the block_contour method.

Creates a block diagram with the contour intervals colored in.


Simply outlines the boundary of input raster file. This is useful if you're only working on a smaller sub area.


Creates a new raster_map instance with a subset of the original raster map.


File:Interpolation figure.png
Interpolation scheme for value at the point p where w1 is the interpolation weight for node n1.

Triangular interpolation of values. The interpolation weights are based on the area of the opposite triangle, such that the value at point p (f(p)):

  • f(p) = w1 f(n1) + w2 f(n2) + w3 f(n3)

The weight for each node is equal to the area of the opposite triangle. For example:

  • w1 = 0.5 * ( (x2 yp - xp y2) + (x3 y2 - x2 y3) + (xp y3 - x3 yp) )
  • Note: the nodes must be ordered in a clockwise direction to get a positive value.

Related programs

Personal tools