From GRASS GIS novice to power user (workshop at FOSS4G Boston 2017)
Description: Do you want to use GRASS GIS, but never understood what that location and mapset are? Do you struggle with the computational region? Or perhaps you used GRASS GIS already but you wonder what g.region -a does? Maybe you were never comfortable with GRASS command line? In this workshop, we will explain and practice all these functions and answer questions more advanced users may have. We will help you decide when to use graphical user interface and when to use the power of command line. We will go through simple examples of vector, raster, and image processing functionality and we will try couple of new and old tools such as vector network analysis or image segmentation which might be the reason you want to use GRASS GIS. We aim this workshop at absolute beginners without prior knowledge of GRASS GIS, but we hope it can be useful also to current users looking for deeper understanding of basic concepts or the curious ones who want to try the latest additions to GRASS GIS.
Format and requirements: Participants should bring their laptops with GRASS GIS 7. Beginners are encouraged to try using the latest OSGeo-Live virtual machine. There are no special requirements. Just have your laptop with GRASS GIS or OSGeo-Live virtual machine with you.
All needed software is included.
Install GRASS GIS from packages:
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable sudo apt-get update sudo apt-get install grass
For other Linux distributions other then Ubuntu, please try to find GRASS GIS in their package managers.
Download the standalone GRASS GIS binaries from grass.osgeo.org.
brew tap osgeo/osgeo4mac brew install grass7
Note that the 3D view may not be accessible.
GRASS GIS introduction
Here we provide an overview of the GRASS GIS project: grass.osgeo.org for first time users. It's not necessary to have a full understanding of how to use GRASS GIS. Here we introduce main concepts necessary for running the tutorial:
Structure of the GRASS GIS Spatial Database
GRASS uses unique database terminology and structure (GRASS database) that are important to understand for the set up of this tutorial, as you will need to place the required data (e.g. Location) in a specific GRASS database. In the following we review important terminology and give step by step directions on how to download and place your data in the correct location.
- A GRASS GIS Spatial Database (GRASS database) consists of directory with specific Locations (projects) where data (data layers/maps) are stored.
- Location is a directory with data related to one geographic location or a project. All data within one Location has the same coordinate reference system.
- Mapset is a collection of maps within Location, containing data related to a specific task, user or a smaller project.
Creating a GRASS database for the tutorial
If you are running GRASS GIS from OSGeoLive, you already have nc_spm_08_grass7 dataset (sample GRASS Location for North Carolina). Otherwise, please download the sample GRASS Location for North Carolina, noting where the files are located. Now, create (unless you already have it) a directory named grassdata (GRASS database) in your home folder (or Documents), unzip the downloaded data into this directory. You should now have a Location nc_spm_08_grass7 in grassdata.
Displaying and exploring data
Now that we have the data in the correct GRASS database, we can launch the Graphical User Interface (GUI) in Mapset user1.
The GUI interface allows you to display raster, vector data as well as navigate through zooming in and out. More advanced exploration and visualization is also possible using, e.g., queries and adding legend. The screenshots below depicts how you can add different map layers (left) and display the metadata of your data layers.
To have a better overview of our raster and vector data, we can use Data tab in Layer Manager. From there we can search for data by name and when we right click at the item, we can select from different actions. In this way we can easily copy or remove data, add them to display, or switch between mapsets. Note that by default for safety reasons you can modify data only in current location and mapset. By unlocking you allow editing other mapsets as well.
GRASS GIS modules
One of the advantages of GRASS is the diversity and number of modules that let you analyze all manner of spatial and temporal. GRASS GIS has over 500 different modules in the core distribution and over 230 addon modules that can be used to prepare and analyze data layers.
GRASS functionality is available through modules (tools, functions). Modules respect the following naming conventions:
|r.*||raster processing||: map algebra|
|v.*||vector processing||: topological cleaning|
|i.*||imagery processing||: object recognition|
|db.*||database management||: select values from table|
|r3.*||3D raster processing||: 3D raster statistics|
|t.*||temporal data processing||: temporal aggregation|
|g.*||general data management||: renames map|
|d.*||display||: display raster map|
These are the main groups of modules. There is few more for specific purposes. Note also that some modules have multiple dots in their names. This often suggests further grouping. For example, modules staring with v.net. deal with vector network analysis.
The name of the module helps to understand its function, for example v.in.lidar starts with v so it deals with vector maps, the name follows with in which indicates that the module is for importing the data into GRASS GIS Spatial Database and finally lidar indicates that it deals with lidar point clouds.
Finding and running a module
To find a module for your analysis, type the term into the search box into the Modules tab in the Layer Manager, then keep pressing Enter until you find your module.
Alternatively, you can just browse through the module tree in the Modules tab. You can also browse through the main menu. For example, to find information about a raster map, use: Raster → Reports and statistics → Basic raster metadata.
Running a module as a command
If you already know the name of the module, you can just use it in the command line. The GUI offers a Command console tab with command line specifically build for running GRASS GIS modules. If you type module name there, you will get suggestions for automatic completion of the name. After pressing Enter, you will get GUI dialog for the module.
You can use the command line to run also whole commands for example when you get a command, i.e. module and list of parameters, in the instructions.
Command line vs. GUI interface
GRASS modules can be executed either through a GUI or command line interface. The GUI offers a user-friendly approach to executing modules where the user can navigate to data layers that they would like to analyze and modify processing options with simple check boxes. The GUI also offers an easily accessible manual on how to execute a model. The command line interface allows users to execute a module using command prompts specific to that module. This is handy when you are running similar analyses with minor modification or are familiar with the module commands for quick efficient processing. In this workshop we provide module prompts that can be copy and pasted into the command line for our workflow, but you can use both GUI and command line depending on personal preference. Look howrepresent the same tool.
The same analysis can be done using the following command:
r.neighbors -c input=elevation output=elev_smooth size=5
Conversely, you can fill the GUI dialog parameter by parameter when you have the command.
Before we use a module to compute a new raster map, we must set properly computational region. All raster computations will be performed in the specified extent and with the given resolution.
Computational region is an important raster concept in GRASS GIS. In GRASS a computational region can be set, subsetting larger extent data for quicker testing of analysis or analysis of specific regions based on administrative units. We provide a few points to keep in mind when using the computational region function:
- defined by region extent and raster resolution
- applies to all raster operations
- persists between GRASS sessions, can be different for different mapsets
- advantages: keeps your results consistent, avoid clipping, for computationally demanding tasks set region to smaller extent, check your result is good and then set the computational region to the entire study area and rerun analysis
g.region -por in menu Settings - Region - Display region to see current region settings
Computational region concept: A raster with large extent (blue) is displayed as well as another raster with smaller extent (green). The computational region (red) is now set to match the smaller raster, so all the computations are limited to the smaller raster extent even if the input is the larger raster. (Not shown on the image: Also the resolution, not only the extent, matches the resolution of the smaller raster.)
The numeric values of computational region can be checked using:
After executing the command you will get something like this:
north: 220750 south: 220000 west: 638300 east: 639000 nsres: 1 ewres: 1 rows: 750 cols: 700 cells: 525000
Computational region can be set also using a vector map. In that case, only extent is set (as vector map does not have any resolution - at least not in the way raster map does). In GUI, this can be done in the same way as for the raster map. In the command line, it looks like this:
Resolution can be set separately using the
res parameter of the module. The units are the units of the current location, in our case meters. This can be done in the Resolution tab of the dialog or in the command line in the following way (using also the
-p flag to print the new values):
g.region res=3 -p
The new resolution may be slightly modified in this case to fit into the extent which we are not changing. However, often we want the resolution to be the exact value we provide and we are fine with a slight modification of the extent. That's what
-a flag is for.
The following example command will use the extent from the vector named
lakes, use resolution
10, modify the extent to align it to this 10 meter resolution, and print the values of this new computational region settings:
g.region vector=lakes res=10 -a -p
Find the module for computing slope and aspect in menu or the module tree under Raster → Terrain analysis → Slope and aspect or simply run.
We can explore our study area in 3D view.
- Add elev_lid792_1m and uncheck or remove any other layers.
- Set computational region to this raster. Switch to 3D view (in the right corner on Map Display).
- Adjust the view (perspective, height, vertical exaggeration)
- In Data tab, set Fine mode resolution to 1 and set slope (computed in the previous step) as the color of the surface.
- When finished, switch back to 2D view.
GRASS working environment and working directory
Create location and mapset (from GUI and from command line)
Move data to and from GRASS database
Move data between locations and mapsets
Viewshed case study to show basic raster and vector operations
In this exercise we will use viewshed modeling to explore which cell towers (from OpenStreetMap) need to be inspected and then use network analysis to send a cell tower climber to those towers. Then we will analyze which areas need better cell coverage using Python scripting.
You will learn:
- import data
- work with raster and vector modules
- use raster algebra
- run network analysis
- learn basics of Python API
- run Python script
You can use Wizard to create simple queries on the currently zoomed extent. For example, zoom to a small area and paste this into the Wizard and run the query:
man_made=tower (assuming all these towers are cell towers)
The query was built in the editor and ran so you can now see the results in a map.
Now, paste this query in the editor window:
[out:json][timeout:25]; // gather results ( // query part for: “"communication:mobile_phone"=yes” node["man_made"="tower"](35.68750730,-78.77462049,35.80960938,-78.60830318); ); // print results out body; >; out skel qt;
It finds towers in our study area (you can get the coordinates based on the computational region by running
g.region -lg). When you run the query, the roads appear on the map and we can export them as GeoJSON (Export - Data - as GeoJSON).
In the next step we import the provided data into GRASS GIS. In menu File - Import vector data select Common formats import and in the dialog browse to find and click button Import. Repeat for raster file export.geojson. Note that in this case we need to change the output name from default OGRGeoJSON to towers. This vector data has different CRS, so a dialog informing you about the need to reproject the data appears and we confirm it.
The imported layer should be added to Map Display, if not, add it manually. Then select the layer and right click and select Show attribute data to see what kind of attributes there are.
In our (little naive) scenario we investigate which towers stopped working. From our position (
632745,224297) we don't receive any signal, so we need to find out which towers are not broadcasting and then go and inspect each of them.
We first compute from which cell towers we should be getting signal by approximating signal propagation by line-of-sight analysis (). Use either the GUI or run the following command from GUI command line:
r.viewshed input=elevation output=visible coordinates=632745,224297 target_elevation=50
Next, we convert the resulting raster representing vertical angle into single-value raster map suitable for conversion to vector using raster algebra. The expression says: create new raster visible_1 where each cell is either 1 or NULL depending on values in raster visible.
r.mapcalc expression="visible_1 = if(visible, 1, null())"
Convert the new raster to vector polygon:
r.to.vect input=visible_1 output=visible type=area
Now we select towers which are inside the area:
v.select ainput=towers atype=point binput=visible btype=area output=visible_towers operator=overlap
The resulting vector has 3 points as you can check for example in the metadata.
We will add the viewer position into a new vector map and add it to the towers to use it for the network analysis. Module
632745,224297 into the 'enter values directly' field.
Otherwise we need to create a plain text file with one line having the coordinates. Then we provide the path to that file to v.in.ascii:
v.in.ascii input=path/to/coordinates.txt output=viewpoint separator=comma
Here we show how to do simple network analysis in GUI and what are the actual modules and workflow behind it. First add the following layers (you probably have them already there). You can also paste these individual commands in the GUI command line and press Enter. Don't paste them all at once.
d.rast map=elevation d.vect map=visible color=none fill_color=white width=1 d.vect map=streets_wake width=1 d.vect map=viewpoint fill_color=green width=2 icon=basic/marker size=40 d.vect map=visible_towers fill_color=red width=2 icon=basic/marker size=40
Now select streets_wake in Layer Manager and then go to Map Display - Analyze map - Vector network analysis tool.
- Set Traveling salesman in the top field.
- Go to Parameters tab and make sure you have streets_wake in the first entry.
- Switch tab to Points and click on Add new point so that you have 4 points there (3 points for towers and 1 for viewpoint).
- Click on Insert points from Map Display, select first point and click on Map Display to the place where the viewer is located and then click on each of the visible towers.
- Finally press the top green button to Execute the analysis. Wait for a little bit and then you should be able to see the computed path.
The same can be done using the following module calls.
First create a new vector by merging the visible towers and the viewer position:
v.patch input=visible_towers,viewpoint output=nodes
Then prepare the vector network by inserting new line segments to connect unconnected points to the street network:
v.net input=streets_wake points=nodes output=network operation=connect node_layer=1 threshold=1000
Print the categories of the points in the network map to pass them to the network analysis:
v.category -g input=network type=point option=print
Run the traveling salesman module:
v.net.salesman input=network output=route center_cats=11,12,30,1 node_layer=1
Try out some of the more advanced and latest features
Next steps and alternative workflows can involve:, and .
Running processes in command line on HPC
grass7 -c path/to/database/location/mapset grass7 path/to/database/location/mapset --exec script.py params rm path/to/database/location/mapset