2.3. Process

In this module we will extract the flat areas from the DEM.

Adding a DEM to QGIS

The first step needed is to bring the DEM for the Hadley area into QGIS:

  • Open QGIS.

  • Click "add raster layer button" and navigate to the folder "raster1" which contains the data for the demo.

  • Open the folder "stpl_DEM". This raster is in GRID format (ESRI raster, a very common raster format that you will encounter quite often).

  • The image below will guide you through the next steps.

1. In "files of type", choose the option "AIG GRASS and all other files". Note: In linux, this step will not be available, but you will still be able to open the file.

2. This will make a number of files appear in the window, but it is not clear which one of those files contains the actual raster values.

3. A trick we used to figure this out was to open a "File manager" program and navigate to the same "stpl_DEM" folder, displaying the details of the files.

4. The file w001001.adf, the one with the most information, contains the raster values, and the other files are accompanying files with heather/metadata information.

  • Go back to QGIS and open w001001.adf. This raster is in state plane coordinates.

Note: Remember that the "projection on the fly" capability of QGIS only works for vectors and 
not rasters. A workaround is to provide all the rasters in the same projection as the project 
or data frame. You don't need to worry about the vectors, as QGIS is able to reproject them 
"on the fly".
Note: QGIS is not consistent in reading the projection information from the data when you first 
add it to the QGIS table of contents. As a general habit, the first thing you should ALWAYS 
do when you add any layer to QGIS is to define the correct projection for each vector and raster 
data set.
Reminder: To define the projection of raster and vector datasets, go to the layer properties, 
accesible when right-clicking the name of the layer.

Setting the raster display

  • Right-click the raster file in the legend, select properties, and click the "symbology" tab to change the display. Select "Greyscale image" and "pseudocolor" color map.

This is how the raster layer should look!. In this color scheme, blue colors mean low elevation and red show high elevation areas:

Converting to GRASS raster

In order to perform any analysis operation with the GRASS plugin, we need to operate on GRASS format data. That's why the next step is to convert the GRID format raster into a GRASS raster.

  • Activate the GRASS plugin if it is not available already. Create a new mapset in your?GRASSDATA database called "raster1".

REMEMBER: To be able to work with GRASS tools, you  need to have an open mapset. Otherwise, 
the GRASS toolbar won't be available( see this section for a detailed explanation). 
  • Open the GRASS tools, choose "import a GDAL raster layer":

  • Fill up the next menu with the input layer (the grid) and the name of the converted GRASS raster: "hadley_gr"

IMPORTANT: during the import process, the GDAL and OGR import modules, by default, OVER-RIDE 
the original projection of the dataset to match it to the current mapset. In our case, the 
projection of the grid and the mapset are the same, so this is not a problem. If they were 
different, the newly created GRASS raster would have a wrongly defined projection. The correct 
way to import a raster with different projection to GRASS would be:
 A. Create a new location with the original raster's projection, 
 B. Import the raster to that location
 C. Reproject in the GRASS shell with the comand r.proj 
(GRASS shell will be explained later in this module)

Viewing the GRASS raster

  • When you click in the "View output" button in the importing menu, the rasted added to the table of contents is all black. Dont panic yet.

Note: So?far, (due to bugs?) it doesn't always work well to click on the "view output" 
button after executing a module. In vector operations it often provides just the 
opposite to the intended function, and in this particular case, it doesn't add the 
raster properly displayed.

Checking the GRASS raster "properties"

  • Open the properties of the raster layer and go to the metadata tab. Here you can see that the data set is actually a GRASS dataset (our goal), that it has data in it (it has dimensions, etc) and that it has the projection of the current mapset (that also coincides with the original projection of the GRID format). Checking all these details will assure you that the process is working correctly.

Getting additional information with the GRASS browser

Another way to access information about the raster is to open the GRASS tools "browser" and navigate to the GRASS raster (raster1=>raster=>hadley_gr, in my case). Upon selecting each data set, the browser provides information about the extent, the resolution (cell size), or maximum and minimum values. In this case, for example, the values are elevation (in m) and we can see that the maximun elevation is 338 m. This browser also provides information about the commands used to generate the dataset, which input layers were used, etc. (Click here for more information on how to use the GRASS browser).

Assigning a COLORMAP

To display the raster properly, we need to assign a "COLORMAP" or COLOR TABLE.

  • Go to GRASS tools, select the module "Set raster color table". Write "hadley_gr" as the input raster and try different color tables to see the effect that they produce.

  • Click "run" after each color map choice, and go to the "view menu" > "refresh" to see the updated color map:

  • Pick "Blue through yellow to red colors". With this palette, low elevation zones will be blue, and high elevation areas will be red.

Enhancing the DEM display to look 3D

Displaying the DEM layer (after tuning the display to 50% transparency) over an "ASPECT" layer will enhance the appearance of the DEM to look like 3D terrain:

To get this display, you will need to follow these steps:

Adjusting the trasparency of a layer

  • Right click the DEM layer name and go to properties. Move the dial for transparency in the "display" tab to 50%:

Deriving ASPECT from DEM

"Aspect" is the compass direction of a line perpendicular to the slope face. It is useful in analysis related to solar power, geomorphic stability, soil development, vegetation cover, etc, and in our case, to improve the display of the DEM.

  • Go to GRASS tools, find the tool "Generate aspect map from DEM", and fill up the input (hadley_gr) and output fields (hadley_aspect). Also, make sure you click the little red square next to "hadley_gr", as this will tell GRASS tools to render the new layer within the area of the input raster (hadley_gr, in this case). Click "run".

  • Add the "hadley_aspect" raster to the display. Put on top the 50% transparent DEM layer.

Deriving SLOPE from a DEM

Slope is a measure of the steepness of the terrain. It is defined by a plane tangent to a topographic surface. Slope values will be useful for our analysis exercise, because they will reveal flat areas suitable for comercial development.

  • To derive a slope map go to GRASS tools, and choose "Generate slope map from DEM".

  • Fill in the parameters. Call your new layer "hadley_sl", and again, make ? sure to push the red rectangle button to keep your new layer within the old layer.

  • Click "run". In this case clicking the button "view results" will display the slope map without any problems:

Interpreting the slope map

  • Right click in the name of the slope layer in the main screen legend to bring up the properties.

  • Click the second tab ("General") where you will find a -crude- representation of the palette of the layer:

There is no indication of which colours correspond to which values. But we can use the "Identity" button to give us some idea. Close the properties and click the "Identity" button on the top tool bar of the QGIS screen.

  • With the help of the "Identity button", click in different cells of the raster to see their values.

Yellow colors correspond to small slope values, and red colors represent the highest slope values. We can infer that the palette window shows the color values in a top-down vertical direction from minimum to maximum values--i.e. that yellow is the lowest slope, green somewhere in the middle, and the blue and purple with very high angular slope values.

Reclassifying the slope layer

Our ultimate goal in this module is to extract the areas with slopes under 3 degrees. In order to do that we need to reclassify the slope map into two classes: "slope < 3 degrees" and "slope > 3 degrees". While we could find the flat areas with a simpler selection process, learning how to RECLASSIFY is important, as it is a very common raster processing tool.

GRASS Shell

Reclassifying is not a tool readily available in the GRASS tools, but any GRASS command (see here all the available commands: http://www.geog.uni-hannover.de/grass/grass63/manuals/html63_user/index.html) could be run from the "GRASS shell".

  • To open this command-line window, go to "Open GRASS tools" and in modules, select "GRASS shell". This will open a black command prompt similar to DOS or the Konsole in linux. At the prompt, write the path to the mapset where you are working:(in our case:

c://grassdata/raster1/hadley

being "grassdata" the name of the GRASS database, "raster1" the name of the location, and "hadley" the name of the mapset).

  • Hit enter, and if the name of the path is correct, you should get the message:

"sh: <name of the path> is a directory". 
  • At this point, you can type any GRASS command.

  • Writing the word "HELP" after any GRASS command will retrieve the "USAGE" for the command, with useful information about the parameters and the syntax needed to run the command.

  • See the next example with the command "r.reclass":

r.reclass command

You can also get additional information about each command from the GRASS website. This link explains in detail the r.reclass command with examples: r.reclass.

  • Take a time to read through this link to understand what r.reclass does and how it works.

Once you have defined the mapset where you are working, you can go ahead and execute any GRASS command.

  • To run the command in the GRASS shell, you need to write:

r.reclass input=hadley_sl output=hadley_sl3   <enter>
0 thru 3 = 1 flat    <enter>
* = 0 hill      <enter>
end    <enter>
  • Here is the explanation:

In the first line, we are defining the names of the input and output rasters. The second line indicates that the input raster values from 0 to 3 (degrees slope) will have a value of "1" in the output raster (or belong to class "1"), and their label will be "flat". The third line indicates that all other values (higher than 3 degrees) will have a value of "0" in the output raster, and will be labeled "hill". After writing one line per class, we write a last line "end" to tell the program that we are done defining the classes.

The GRASS shell runs the command and creates the output.

  • To see the output, open the GRASS browser and click "refresh" button to see the classified raster "hadley_sl3" in the list of available rasters in the mapset.

In the right window, you can see the range of values and the labels of the new categories.

  • Doble click the name of the output raster to add it to the map. * Right click the name of the raster to bring up the properties and change the display.

  • Choose "pseudocolor" as the colormap to better appreciate the different classes. In this case, areas colored red will be our class 1, which is flat, and areas which are blue will be our class zero, which has a slope greater than 3 degrees:

  • Click on the "identify" tool and click on the different colors of the raster to see the class they belong to.

As you see, red corresponds to flat areas:

Clipping a raster

 Clipping is the action of truncating data or an image by removing all the display elements 
 outside a boundary. The area enclosed by this boundary may be referred to as a clipping 
 window. While other software packages may be able to use a polygon to clip a raster, 
 in QGIS/GRASS that is not possible. The next sections will explain how to do it instead. 
  • We will show how to clip a raster to the boundary of the town of Hadley. You may have different reasons to clip a raster. One is that it enhances the final display of your work. Also, you may want to extract your area of interest from a bigger raster to speed analysis and to keep things more focussed and organized.

  • There is not a clipping command per se in QGIS-GRASS but there is an indirect way to do it, "multiplying" any raster by a raster of the shape of the object we want to use as clipping shape (or a shape of the "cookie cutter"). Such raster's cells have a value of 1 inside the boundary, and a value of "no data" outside of the boundary. When multiplying both rasters, the result is a "clipped" original raster that has preserved the values inside the desired boundary (multiplied times 1) and that has "no data" outside the boundary (any raster value multiplied by "no data" will return a "no data" cell in the resulting raster). This may sound confusing, but it's partly because of the terminology that QGIS-GRASS uses, and will become clearer as we go through it.

  • In our case, we will multiply the "hadley_sl3" raster by a raster with the shape of the boundaries of the town of Hadley. To create such Hadley boundary raster, we will convert a polygon layer of the Hadley town boundary into a raster:

Converting a vector to raster

  • Remove from the legend (and therefore the map), any files except for "hadley_sl" and "hadley_sl3".

  • Add the shapefile "hadley.shp" to QGIS. You will find this file in the "siteselectiondata" directory, i.e. the files that you downloaded from our server.

  • Convert it to a GRASS vector layer named "hadley_perim". This time, rather than selecting "Import GDAL raster layer", you're going to select "Import OGR/PostGIS Vector Layer". Otherwise, the process is the same.

  • Add it to the QGIS table of contents. Make sure to remove the original "hadley.shp" file, or at least make it invisible by clicking the "X" next to it. Right click to open the properties

  • Change the display as shown in this figure:

  • Open the GRASS toolbox and click on the module "convert a vector to raster using constant".

  • Fill in the parameters. The input vector layer will be "hadley_perim", the value of the raster cells will be "1" and the name of the raster "hadley_raster". Also, make sure to go into the browser. Refresh, and you will see a new folder called "vector" with our new GRASS layer "hadley_perim" inside. Select that layer and click the little red box to make sure it's placed inside the map area.

  • Click run

  • Add the new raster to the view and display it with a 50% transparency.

  • In the next sections, we will use "hadley_raster" to clip the classified slope raster multiplying the two rasters together.

Note: In QGIS-GRASS, raster operations and functions are executed in the RASTER MAP CALCULATOR. 
For this wizard to work, there must be a MASK defined in the mapset.

We will first learn what is a mask and how to set it up:

Creating a Mask

Conceptually, a mask is an area that, when "placed" or "applied" to a raster map, will block out certain areas of a map from analysis. In QGIS-GRASS, it is a raster named "MASK" that lives in the mapset. While a mask is active or present, GRASS commands will only act on data falling inside the masked area.

While in some applications it may make sense to use the perimeter of the area of interest as the 
mask, in general we don't recommend it because any raster in the mapset will automatically be 
displayed masked or clipped to that perimeter. That may not be what we want: sometimes we need 
to see the context around the area of interest. Also, some analytical processes (like spatial 
interpolation, for example) will suffer edge effects if data around the borders are excluded 
from the analysis. In general, we recomend to consider applying a mask slightly larger than 
the area of interest, and clip the final products at the end.
  • An easy option is to create a mask of the same area as the GRASS region. To see the current GRASS region, go to Plugings>GRASS>"Display current GRASS region". T?e region appears limited by a red line:

  • If you click again "display current GRASS region" the red line that defines the region will disappear.

  • If you ever want to modify the extent of the region, go to Plugins>GRASS>"Edit current GRASS region").

  • In addition, if you want to set the GRASS region to the perimeter of one of your layers, you can go into GRASS tools, open the browser, select one of the layers, and click the "Set Current region to selected map" icon (the red square). Do this now for hadley_sl3.

  • To create the mask, we will convert the region extent into a GRASS vector polygon. Go to GRASS toolbox and open the module "Create new vector area map with current region extent". Name the vector "mask_poly".

  • Add "mask_poly" to the map.

  • And then, convert this vector to raster as we demonstrated in previous section: GRASS toolbox>Convert a vector to raster using constant". Use "mask_poly" as input, constant = 1, "mask" as output

Now, the mapset has a raster named "mask" with the function of masking all the other rasters in the mapset for displaying and analysis purposes, and the raster map calculator will work (you don't need to add the "mask" raster to the map though, it just needs to be in the mapset).

Raster map calculator

In QGIS-GRASS, the "Raster Map Calculator" is the environment to perform arithmetic operations and mathematical functions inside and among rasters.

Note: For the raster calculator to work, GRASS needs to have a "MASK" defined in the mapset, 
otherwise it won't perform any operations. See the previous section about defining a mask.
  • We will use the map calculator to clip the classified slope raster, multiplying it by the Hadley perimeter raster.

 "Behind the scenes" of the map calculator, the GRASS command  r.mapcalc is at work. 
 Instead of having to write more or less complicated equations, the map calculator makes it easy 
 to construct models or equations in an intuitive way without having to worry about syntax issues. 
 You just need to build a diagram connecting "datasets" to an "output" using "operations". 
 The calculator translates this diagram into a r.mapcalc equation.
  • The next sections show you how to construct diagrams representing the operations that you need to perform:

Adding datasets

To add "datasets",

  • 1. click the "Add map" button,

  • 2. browse the dropdown menu for the desired raster,

  • 3. click, drag and release, to possition the raster in the diagram window:

Adding operations

To add "operations",

  • 1. click the "Add operator or function" button,

  • 2. browse the dropdown menu for the desired operation,

  • 3. click, drag and release, to possition the operation in the diagram window:

Deleting items

If there are unwanted datasets or operations in the diagram window, you can

  • 2. click on the "select item" button,

  • 3. click on the item you want to select,

  • 4. click on the "delete selected item" button.

Note: Once an item is selected, you can also move it inside the diagram window.

Note: It is very important to select the items before attempting to delete them. Otherwise, QGIS will crash.

Adding connections

To add "connections":

  • Click on the "Add connection" button,

  • 1. click on any red circle where you want to establish a connection. The cursor will turn into a "cross hair" sign and the red circle will turn to grey.

  • 2. hold the click and move the cursor towards the operation or dataset you are attempting to connect to.

  • 3. once on your target, position the cursor in the middle of a red circle and release the mouse when the circle turns grey.

  • You may have to practice several times until you are succesful at establishing connections.

Fixing connections and running the calculation

Sometimes the items are not well "connected" (see the example in "1" in the picture below). To get rid of those connections,

  • 2. click on the "select item" button,

  • 3. click on the connection you want to delete until the color changes,

  • 4. click on the "delete selected item" button.

  • 5. Complete all connections and check the diagram to make sure that it reflects the calculation you wish to perform

  • 6. Name the "output"

  • 7. Click on "run".

Viewing the output

  • 1. Click the "Output" tab of the calculator wizard to monitor the progress of the calculations

  • 2. Here you can see the "translated" diagram into an r.mapcalc equation.

  • 3. Click "view output" button or add the created dataset to the map (plugins>grass>add grass raster>"hadley_sl3_clip")

  • 4. Congratulations! You just displayed the classified slope raster CLIPPED to the Hadley perimeter!

Other operations

Many other operations are possible and we invite you to explore what is possible reading about the comand "mapcalc".

Saving and retrieving Raster Map Calculator diagrams

It is possible to save a completed or "in progress" diagram into the mapset to use or revise later.

To save the diagram,

  • 1. click on the "Save" button,

  • 2. give a name to the diagram (it will be stored in the current mapset).

  • 3. To retrieve an existing diagram, click on the "open" button and

  • 4. browse and select the desired mapcalc diagram,

  • 5. click ok.

Extract by attributes

After clipping the classified slope raster, we need to extract the flat areas into a polygon layer. To do this, go into GRASS tools, and select "Convert a raster to vector areas". This will take the values (1 and 0 in this case) and convert them to a polygon that covers every area that has that value. You can see from the diagram below which files to input and output (it'll be called "hadley_sl3_pl"):

Once this is complete, you want to add this to your map. Go to "Add GRASS Vector Layer" and fill in as shown below (the same process you've already done):

Now that the layer has been added, you'll want to make it distinctive so that you can see exactly where the flat areas are. Right-Click on the layer in the legend and go to properties, then click the symbology tab. Fill in as described below. What you are actually doing with these steps is assigning a set of visual characteristics to each value (in this case, 1 and 0). You need to set the symbology to understand this (by setting it to "unique value"), then define the classification field (by scrolling to "value"), and finally by clicking on the "1" (which is our denominator of flat areas) and changing the color to yellow.

Now, after all of that, we have a vector file that we can use to see the flat areas and define a new layer that contains only those areas. To start, you need to select features by attribute. This is in the GRASS tools section. Fill in the values below. As we said above, we need to know the areas marked by the value of "1", and we want to give this area a new name, which we'll call "hadley_flat":

Once you've done that, you can bring "hadley_flat" into the map by "adding a GRASS vector layer". If you've done everything right, and you turn off all the other layers, it should look like this:

Congratulations! You've created a layer that shows only the possible areas of development in the town of Hadley, based on slope.

Moving to other computer? Just copy your GRASSDATA folder and any folder where you have datasets 
that you are using in QGIS, as well as your QGIS project file. If you ?reserve the same file names 
and replicate the "paths", the QGIS project will open without any problems.