Hack 69. Find Your Way Around GRASS

The full power of GIS can be a little overwhelming at first, but a little patience and a little practice is all it takes to make GRASS do your bidding.

GRASS is a powerful and free set of tools for analyzing and presenting geographic data. For details on how to install GRASS, see http://mappinghacks.com/GRASS/. Once you've done that, it's time to download the global sample data set and try out some basic tasks to get a feel for how GRASS works.

First, make a directory somewhere on your system in which to store your GRASS data, which can be anywhere convenient that you have write access to (I put mine in /home/sderle/gis/grass). Then download the sample global data set from http://grass.itc.it/sampledata/global_grass5data.tar.gz. Unpack the tar file inside your GRASS data directory:

$ cd ~/grass # or wherever you put it
$ tar xvfz ~/global_grass5data.tar.gz

You should see a bunch of files unpack into that directory. Now, start up the GRASS 5 shell by executing grass5 at your shell prompt. You'll be taken to an introductory menu screen that looks like the following:

LOCATION: _ _ _ _ _ (enter list for a list of locations)
MAPSET: _ _ _ _ _ (or mapsets within a location)
DATABASE: /home/sderle/gis/grass _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

A bit of explanation is in order before we proceed. In GRASS, a location is just thatthe place we are doing geographic analysis on. As such, the current GRASS location will include relevant bits of information about the area we're interested in, such as the geographic rectangle (or extents) that covers it, the coordinate system and geodetic datum in use, and the default grid resolution. Similarly, the current region, which we'll get into later, is the geographic subset of the current location.

A GRASS mapset is just a convenient way of grouping pieces of geographic data under a given location. GRASS mapsets allow multiple users to work on the same location without interfering with each other. GRASS supports a special mapset, called PERMANENT, which is used to store map layers that are available across any mapset in that location.

Finally, the GRASS database is just GRASS's name for the directory you set aside to store your GRASS data. Start by using the arrow keys to move down to the DATABASE: prompt, and fill in the path to that directory. Then use the arrow keys to move back up to the LOCATION: prompt, type in list, and hit the spacebar a few times to erase whatever might have been there previously. Then hit the Escape key, followed by the Enter key. You should now see something like the following:

Available locations: 
Hit RETURN -->

This global location was created in GRASS when you unpacked the sample data set in your GRASS directory. Hit Return to go back to the intro menu, and enter the word global as your location. Since we'll be creating a new mapset to experiment in, invent a convenient name for the MAPSET: prompt (your username isn't a bad choice) and use the spacebar to remove whatever else was there previously. Your startup screen will now look something like this:

LOCATION: global _ _ _ _ _ _ (enter list for a list of 
MAPSET: sderle_ _ _ _ _ _ _ (or mapsets within a location)
DATABASE: /home/sderle/gis/grass_ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _

Hit the Escape key, followed by the Enter key. You should see some introductory text, followed by the GRASS prompt:

This version running thru the Bash Shell (/bin/bash)
Help is available with the command: g.help
See the licence terms with: g.version -c
Start the graphical user interface with: tcltkgrass&
When ready to quit enter: exit
GRASS:~/gis >

Congratulations, you're working in GRASS! One interesting thing to note is that the GRASS shell is just a thin layer of environment variables laid on top of your ordinary Unix shell. To demonstrate this, try running g.version -c, which prints out license and version info for GRASS. It scrolls by too quickly to read, right? So pipe the output through your favorite pagere.g., g.version -c | less.

As you can see from this trivial example, GRASS extends the Unix tools philosophysmall programs that each do one thing wellinto the world of Geographic Information Systems. This means, very simply, that everything that you're accustomed to doing in your Unix shellcommand-line history, enviroment variables, command auto-completion, pipelines, scripting, etc.can also be done within GRASS. Pretty neat, huh? Now let's make some maps.

Run the command d.mon start=x0, which should cause a blank window to pop up. This blank window is a GRASS monitor, which is the virtual device that GRASS uses to display graphics. Next, run g.list type=rast. This shows us a list of the raster layers available in our current mapset. Let's view the eco raster layer in our monitor, by running d.rast eco. A map of the world in a strange mosaic of colors should appear in your monitor.

Let's take a closer look, by zooming in on Western Europe. Run d.zoom. You should be presented with this somewhat cryptic menu:

Left: 1. corner
Middle: Unzoom
Right: Quit

GRASS's d.zoom tool takes a little getting used to, so bear with it. Start by left-clicking in the North Atlantic near Iceland. Now we have a rectangle that we can move about. When we select the second corner of this rectangle by middle-clickingnot left-clicking again, as you might expectGRASS attempts to fill the monitor with the selected region. So middle-click somewhere to the right of Italy in the Mediterranean to select the second corner, and GRASS should zoom in on Western Europe. Now you can left-click to begin selecting an area to zoom in on further, middle-click to zoom out and try again, or right-click to exit d.zoom. Hitting Ctrl-C in your GRASS shell will also exit d.zoom.

So what do the strange colors represent, exactly? To find out, use the commmand d.what.rast, and then left-click on a colored point on the monitor. Clicking near London yields the following in our shell:

0:01:19.425E 51:13:37.875N
eco in PERMANENT (31)Non Woods-Warm/Hot Farms/Towns

By the same method, we discover that salmon is "Heath & Moorland," magenta is "Interrupted Woods," light cyan is "Major Woods," and so on. But suppose we don't really care for this color scheme? Right-click on the monitor or hit Ctrl-C in the shell to get out of d.what.rast. Run r.colors to alter the color table in the eco raster layer. When prompted for the name of a raster file, enter eco, and then enter ryg (which stands for "red-yellow-green") as the color table. Hit Enter to accept the defaults for all the other options, and finally r.colors will report:

Color table for [eco] set to ryg

Now run d.redraw to refresh the monitor. Figure 6-26 shows the eco raster layer with the ryg color map applied. If you don't like the resulting color scheme, run r.colors to try out some of the other preset color tables. The random color choice is a particularly interesting option. You can also set the color table from the command line with r.colors map=eco color=random.

Figure 6-26. The eco raster layer with the ryg color map applied

In general, most GRASS commands have two operating modes: interactive and command-line-option based. If you run a GRASS command with no options, expect the interactive mode. If you want to see which command-line options a GRASS command supports, run the command with a -? option (e.g., r.colors -?). If you want specific details on what a command does, well, this is Unixtry reading the manpage!

Now, let's add some context to this ecology map by overlaying national boundaries. Run g.list type=vect to get a list of the vector layers available in this mapset:

vector files available in mapset PERMANENT:

Fancy that! The sample global data set doesn't even include a vector layer consisting of national borders. Well, we'll just have to make our own. Note that our earlier listing of raster layers included a nations layer. Let's view that one in our display now, with d.rast nations. You can use d.what.rast to ensure that GRASS knows which country is which, if you're curious. Now let's use r.to.vect to turn the outlines of each raster area into a polygon on a new vector layer.

GRASS:~/gis > r.to.vect in=nations out=borders feature=area 

In versions of GRASS prior to 5.7, the tool for converting raster areas to vector polygons was called r.poly, and you had to run v.support afterwards on the new vector layer to generate the layer's support files. In GRASS 5.7, v.support is called v.build, and r.to.vect runs it for you automatically.

Now, let's see the new borders layer on its own. It should appear as white lines on a black background.

GRASS:~/gis > d.erase
GRASS:~/gis > d.vect borders type=boundary

Figure 6-27 shows our new borders layer. Sure enough, that's Western Europe, more or less. However, you may see weird lines cutting through some of the countries that hang off the edge of our current viewwhat's going on there? Let's start by saving our current view, using g.region -s Western_Europe, which tells GRASS to save a region under the name Western_Europe. This way, we can poke around and still come back to this exact view later. Now use d.zoom to zoom out.

Figure 6-27. Our new borders vector layer

What's this? Our vector layer of national boundaries got cut off at the edges of our former region! This is, of course, an interesting lesson in the ways of GRASS. As it happens, most operations that act on individual layers only do so within the currently selected region. Sometimes, this is what you want; many operations in GRASS take a long time to run, and restricting them to run only within a constrained region of your whole location will make them run faster, if that's all you're interested in. But you should be aware of this "feature," as it may cause behavior you're not expecting when you're zoomed in on a small portion of your current location.

So let's try making that vector map again, but let's reset our region to the whole world first:

GRASS:~/gis > g.region rast=nations
GRASS:~/gis > r.to.vect in=nations out=borders feature=area
GRASS:~/gis > d.erase
GRASS:~/gis > d.vect borders type=boundary

Okay, that's more like it. Now let's zoom back in on Western Europe and redraw our ecology map with the national borders on top:

GRASS:~/gis > d.erase 
GRASS:~/gis > g.region Western_Europe
GRASS:~/gis > d.rast eco
GRASS:~/gis > d.vect borders type=boundary color=black

By now, you've probably noticed that there's a scheme behind the naming of GRASS commands. Commands that start with g. are general-purpose, d. commands are display related, r. commands work on rasters, v. commands work on vectors, and so on.

Figure 6-28 shows our revised borders layer drawn in black on top of the eco layer. You'll note that the political boundaries and the ecology map don't quite match up around the coastlines. Unfortunately, this is simply a side effect of using very low-resolution sample datanot much we can do about it here. (Also, you'll probably notice that these boundaries date to before 1990, but that's beside the point).

Figure 6-28. The borders and eco layers shown together

Now that we've made this informative and attractive map, what if we want to get back to it later? Fortunately, GRASS provides the d.save command, which generates a shell script of the commands used to generate the current view in our monitor. Run d.save -c > europe.sh and examine the contents of europe.sh:

# Shell Script created by d.save Sun Jun 27 18:16:20 2004
g.region n=60:04:48N s=36:43:12N e=18:48E w=13:07:12W nsres=0:04:48 ewres=0:04:48
d.rast map=eco # 2
d.vect map=borders type=boundary color=black # 1
d.frame -s frame=full_screen

We cover this topic in further detail in [Hack #75] .

6.7.1. See Also

  • [Hack #75]
  • GRASS 5.7 reference manual (http://grass.itc.it/grass57/manuals/html57_user/)
  • GRASS 5.7 online tutorial (http://grass.itc.it/grass57/tutorial/)

Mapping Your Life

Mapping Your Neighborhood

Mapping Your World

Mapping (on) the Web

Mapping with Gadgets

Mapping on Your Desktop

Names and Places

Building the Geospatial Web

Mapping with Other People

Mapping Hacks
Mapping Hacks: Tips & Tools for Electronic Cartography
ISBN: 0596007035
EAN: 2147483647
Year: 2004
Pages: 172

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net