Difference between revisions of "Using map algebra in GRASS"
Mabel Chua (talk | contribs) (→Subtracting raster to fit into study area: accesssing r.mapcalc) |
|||
(33 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | |||
<big>'''Using Map Algebra and the command line in GRASS'''</big> |
<big>'''Using Map Algebra and the command line in GRASS'''</big> |
||
Line 4: | Line 5: | ||
== Introduction == |
== Introduction == |
||
− | GRASS is |
+ | GRASS is geographic information system (GIS) software that is used for geospatial data management and analysis. If you find yourself working on raster data specifically in GRASS it may be very useful to know how to use map algebra in order to manipulate the data so that you can properly analyze it. The advantages to understanding map algebra in GRASS is that it allows for manipulation of raster layers in a program that is focused on data processing and analysis. Also it is important to know how to use this tool using the command line instead of the graphical user interface (GUI) and so to that end for this whole page all of the examples will be done using the command line commands. |
== Map algebra == |
== Map algebra == |
||
Line 10: | Line 11: | ||
==== What is Map Algebra? ==== |
==== What is Map Algebra? ==== |
||
− | + | Map algebra is a way in GIS to perform arithmetic on raster layers in maps. The way that it works is that it will take two raster layers and it performs an operation on them based on what you tell it to do. The basics to know before starting map algebra is to know that map algebra is like a language and in order to communicate the statement to GRASS, the program follows a rules to effectively read the command given. Using r.mapcalc, the expression can either be typed out in the command line or use the preset buttons on top to assemble the command. |
|
+ | One way access the r.mapcalc is through going to the Raster menu, then to Raster map calculator, and then the dropdown menu will have the option for Raster map calculator (r.mapcalc). |
||
+ | For example, if you wanted to cut a raster image down to the study area of another you could do that by using the following command: |
||
− | r.mapcalc --overwrite rasterAnew=”if(rasterB, rasterA)” |
+ | r.mapcalc --overwrite rasterAnew=”if(rasterB, rasterA)” |
− | this command basically tells the program that for the new raster that you are creating if raster B(the study area) is in that location then put raster A(the raster you are cutting) there, meaning that the end result should be whatever you are measuring in raster A in just the area of raster B. |
+ | this command basically tells the program that for the new raster that you are creating, if raster B(the study area) is in that location then put raster A(the raster you are cutting) there, meaning that the end result should be whatever you are measuring in raster A in just the area of raster B. |
+ | |||
+ | Some basic rules are important to remember when executing map algebra because no matter how basic or complex the command is, GRASS must be able to coherently read it, to execute the command. It is important to remember that map algebra is case sensitive, so knowing when to capitalize and when not to is crucial. Additionally, parentheses are often used to indicate the tool parameters being applied to the input dataset. |
||
==== What can you do with Map Algebra? ==== |
==== What can you do with Map Algebra? ==== |
||
+ | There is a lot that you actually can do with map algebra; it is not just limited to changing the shape of raster files, it can also be used to shift raster files geographically, or to edit the values of each pixel of raster files. You could use it, for example, to subtract or add raster layers to Landsat images. This is one way to create colour photos from Landsat imagery - if you add the red green and blue layers together you get true colour photos. You can also use this technique to make different indexes from satellite images which can be used to tell things like plant life, from transforming the infrared layer and the red layer to create an NDVI index. |
||
+ | |||
+ | [[File:Map calc op.png|500px|thumb|center|this table shows all of the different operations that can be done on raster files using r.mapcalc]] |
||
+ | The method for using these operators works like this: |
||
+ | # Start with the command which in this case is: r.mapcalc |
||
+ | # Then you want to add the file you are creating: newFile= |
||
+ | # Then you add in your operator and variables for example: !fileA which means not file A |
||
+ | r.mapcalc newFile= !fileA |
||
+ | this will give you a raster that is the inverse of file A |
||
+ | Note, that the spacing in the above command is purposeful, to separate the different aspects of the expression in order to produce the needed operation. |
||
− | == Command line == |
||
+ | |||
+ | The other way to do mapcalc is without the command line using the map calculator built into the GUI, which is accessed using the button shown below. |
||
+ | |||
+ | [[File:GRASS map calculator loc.png|400px|thumb|center|location of map calculator]] |
||
+ | |||
+ | |||
+ | [[File:GRASS map calculator.png|400px|thumb|center|map calculator]] |
||
== Examples == |
== Examples == |
||
+ | ==== Subtracting raster to fit into study area ==== |
||
+ | |||
+ | For this first example the DEMs (Digital Elevation Model) that are used can be obtained from the Government of Canada website.https://open.canada.ca/data/en/dataset/6640b919-abbe-405b-a5f6-d4d808cbbcec However, this example will work with any raster file in the same situation. |
||
+ | To start, I have two DEMs one that is smaller than the other as you can see in the picture. |
||
+ | [[File:Ex 1 1.png|500px|thumb|center]] |
||
+ | |||
+ | In GRASS you can access the raster map calculator by going to the raster menu, from the dropdown menu click raster map calculator and then make sure that you are accessing raster map calculator (also shown as r.mapcalc) |
||
+ | In order to subtract the excess from the large DEM to make it the same size as the as the smaller DEM which for this example will be our study area I used the command |
||
+ | |||
+ | "r.mapcalc --overwrite expression=DEM = DEM@elmin if( DEM@elmin, DEM__2_@elmin)" (elmin is the mapset name just happens that is my username on my computer). |
||
+ | |||
+ | [[File:Tutorial revision (example 1).png|thumb|center|subtracting raster in command line]] |
||
+ | |||
+ | the result as you can see in this picture is that both rasters fit in the study area. |
||
+ | [[File:Ex 1 2.png|500px|thumb|center]] |
||
+ | |||
+ | ==== Creating NDVI index using Landsat data ==== |
||
+ | |||
+ | For this example I have obtained some data from USGS which is free to get you just need to create account with them. We will be calculating NDVI which stands for normalized difference vegetation index from Landsat images. This index is used to help tell the health of plants from satellite images and as you can see from the image below you need to start by uploading your Landsat imagery into GRASS. For this particular index you need bands four and five I have bands one through five here. You can use this method to create any index but for this example I have chosen NDVI because it is very common. |
||
+ | [[File:NDVI.png|100px|frame|right]] |
||
+ | |||
+ | [[File:Ex 2 1.png|1000px|thumb|center|This is what the GRASS work station will look like before doing the mapcalc]] |
||
+ | |||
+ | Next, you have to open the command line so that you can enter the following command: |
||
+ | |||
+ | NDVI = float(band5 - band4)/float(band5 + band4) |
||
+ | |||
+ | In the example you can see that I forgot to add the float for each calculation the first time I tried it. It is important to add that because without that your result won't be correct, this is because the actual number for each pixel in an index is not an integer it is usually a decimal number so making sure the type of number is a float is important. |
||
+ | |||
+ | [[File:Ex 2 3.png|800px|thumb|center|My command line]] |
||
+ | |||
+ | If you were to use the mapcalc calculator in the GUI it would like this: |
||
+ | |||
+ | [[File:Ex 2 2.png|800px|thumb|center]] |
||
+ | then after you run the command it will look like this: |
||
+ | [[File:Ex 2 4.png|800px|thumb|center|final result of the map calc]] |
||
+ | That is how you make an index using r.mapcalc. |
||
== Conclusion == |
== Conclusion == |
||
+ | r.mapcalc is a very useful skill to know in GRASS as GRASS is used to do a lot of analysis of raster data and in order to do that you need to know how to manipulate raster data. I made this tutorial because I had trouble with GRASS and hopefully this will help someone else have not as much trouble. Knowing how to operate map algebra in GRASS is a powerful tool as it gives access to various tools and operations within GRASS to analyze raster data. |
||
== References == |
== References == |
Latest revision as of 23:48, 1 October 2021
Using Map Algebra and the command line in GRASS
Contents
Introduction
GRASS is geographic information system (GIS) software that is used for geospatial data management and analysis. If you find yourself working on raster data specifically in GRASS it may be very useful to know how to use map algebra in order to manipulate the data so that you can properly analyze it. The advantages to understanding map algebra in GRASS is that it allows for manipulation of raster layers in a program that is focused on data processing and analysis. Also it is important to know how to use this tool using the command line instead of the graphical user interface (GUI) and so to that end for this whole page all of the examples will be done using the command line commands.
Map algebra
What is Map Algebra?
Map algebra is a way in GIS to perform arithmetic on raster layers in maps. The way that it works is that it will take two raster layers and it performs an operation on them based on what you tell it to do. The basics to know before starting map algebra is to know that map algebra is like a language and in order to communicate the statement to GRASS, the program follows a rules to effectively read the command given. Using r.mapcalc, the expression can either be typed out in the command line or use the preset buttons on top to assemble the command. One way access the r.mapcalc is through going to the Raster menu, then to Raster map calculator, and then the dropdown menu will have the option for Raster map calculator (r.mapcalc). For example, if you wanted to cut a raster image down to the study area of another you could do that by using the following command:
r.mapcalc --overwrite rasterAnew=”if(rasterB, rasterA)”
this command basically tells the program that for the new raster that you are creating, if raster B(the study area) is in that location then put raster A(the raster you are cutting) there, meaning that the end result should be whatever you are measuring in raster A in just the area of raster B.
Some basic rules are important to remember when executing map algebra because no matter how basic or complex the command is, GRASS must be able to coherently read it, to execute the command. It is important to remember that map algebra is case sensitive, so knowing when to capitalize and when not to is crucial. Additionally, parentheses are often used to indicate the tool parameters being applied to the input dataset.
What can you do with Map Algebra?
There is a lot that you actually can do with map algebra; it is not just limited to changing the shape of raster files, it can also be used to shift raster files geographically, or to edit the values of each pixel of raster files. You could use it, for example, to subtract or add raster layers to Landsat images. This is one way to create colour photos from Landsat imagery - if you add the red green and blue layers together you get true colour photos. You can also use this technique to make different indexes from satellite images which can be used to tell things like plant life, from transforming the infrared layer and the red layer to create an NDVI index.
The method for using these operators works like this:
- Start with the command which in this case is: r.mapcalc
- Then you want to add the file you are creating: newFile=
- Then you add in your operator and variables for example: !fileA which means not file A
r.mapcalc newFile= !fileA
this will give you a raster that is the inverse of file A
Note, that the spacing in the above command is purposeful, to separate the different aspects of the expression in order to produce the needed operation.
The other way to do mapcalc is without the command line using the map calculator built into the GUI, which is accessed using the button shown below.
Examples
Subtracting raster to fit into study area
For this first example the DEMs (Digital Elevation Model) that are used can be obtained from the Government of Canada website.https://open.canada.ca/data/en/dataset/6640b919-abbe-405b-a5f6-d4d808cbbcec However, this example will work with any raster file in the same situation. To start, I have two DEMs one that is smaller than the other as you can see in the picture.
In GRASS you can access the raster map calculator by going to the raster menu, from the dropdown menu click raster map calculator and then make sure that you are accessing raster map calculator (also shown as r.mapcalc) In order to subtract the excess from the large DEM to make it the same size as the as the smaller DEM which for this example will be our study area I used the command
"r.mapcalc --overwrite expression=DEM = DEM@elmin if( DEM@elmin, DEM__2_@elmin)" (elmin is the mapset name just happens that is my username on my computer).
the result as you can see in this picture is that both rasters fit in the study area.
Creating NDVI index using Landsat data
For this example I have obtained some data from USGS which is free to get you just need to create account with them. We will be calculating NDVI which stands for normalized difference vegetation index from Landsat images. This index is used to help tell the health of plants from satellite images and as you can see from the image below you need to start by uploading your Landsat imagery into GRASS. For this particular index you need bands four and five I have bands one through five here. You can use this method to create any index but for this example I have chosen NDVI because it is very common.
Next, you have to open the command line so that you can enter the following command:
NDVI = float(band5 - band4)/float(band5 + band4)
In the example you can see that I forgot to add the float for each calculation the first time I tried it. It is important to add that because without that your result won't be correct, this is because the actual number for each pixel in an index is not an integer it is usually a decimal number so making sure the type of number is a float is important.
If you were to use the mapcalc calculator in the GUI it would like this:
then after you run the command it will look like this:
That is how you make an index using r.mapcalc.
Conclusion
r.mapcalc is a very useful skill to know in GRASS as GRASS is used to do a lot of analysis of raster data and in order to do that you need to know how to manipulate raster data. I made this tutorial because I had trouble with GRASS and hopefully this will help someone else have not as much trouble. Knowing how to operate map algebra in GRASS is a powerful tool as it gives access to various tools and operations within GRASS to analyze raster data.
References
“Mapcalc Manual.” GRASS GIS Manual: R.mapcalc, GRASS Development Team, grass.osgeo.org/grass76/manuals/r.mapcalc.html.