Start a new GRASS project

In this session we are going to see how to start a new grass project using a well standarized dataset: a DEM.

cd /media/sf_LVM_shared/my_SE_data/exercise/

sudo apt-get install -y python3-ipykernel
python3 -m ipykernel install --user --name "system-global" --display-name "Python 3 (System Global)"

wget https://raw.githubusercontent.com/selvaje/SE_data/master/exercise/grass_newproject.ipynb
jupyter-lab /media/sf_LVM_shared/my_SE_data/exercise/grass_newproject.ipynb

Analize the dataset

[1]:
import rasterio
from rasterio.plot import show
# digital elevation model
dem = rasterio.open("/media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif")
show(dem)
../_images/GRASS_grass_newproject_3_0.png
[1]:
<Axes: >

We can see the metadata of the the DEM using gdalinfo.

The tif metadata should have well defined values such as:

  • projection has to be well defined as proj4, EPSG,

  • the extent should set at degree level (e.g. 14.0’) or with one digit after the degree (e.g. 14.1’),

  • the pixel resolution should be also well defined (e.g. 0.25’, 0.008333333’ not 0.008331234’ or 0.05214534’),

  • in case of using more then one data source the raster layers shold have the same extent and the pixel aligned,

[1]:
! gdalinfo /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif
Driver: GTiff/GeoTIFF
Files: /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif
Size is 5880, 8400
Coordinate System is:
GEOGCRS["WGS 84",
    ENSEMBLE["World Geodetic System 1984 ensemble",
        MEMBER["World Geodetic System 1984 (Transit)"],
        MEMBER["World Geodetic System 1984 (G730)"],
        MEMBER["World Geodetic System 1984 (G873)"],
        MEMBER["World Geodetic System 1984 (G1150)"],
        MEMBER["World Geodetic System 1984 (G1674)"],
        MEMBER["World Geodetic System 1984 (G1762)"],
        MEMBER["World Geodetic System 1984 (G2139)"],
        MEMBER["World Geodetic System 1984 (G2296)"],
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]],
        ENSEMBLEACCURACY[2.0]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    USAGE[
        SCOPE["Horizontal component of 3D system."],
        AREA["World."],
        BBOX[-90,-180,90,180]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
Origin = (-83.000000000000000,14.000000000000000)
Pixel Size = (0.008333333333333,-0.008333333333333)
Metadata:
  TIFFTAG_DOCUMENTNAME=geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif
  TIFFTAG_SOFTWARE=pktools 2.6.7.6 by Pieter Kempeneers
  TIFFTAG_DATETIME=2021:05:12 09:34:03
  AREA_OR_POINT=Area
Image Structure Metadata:
  COMPRESSION=DEFLATE
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  ( -83.0000000,  14.0000000) ( 83d 0' 0.00"W, 14d 0' 0.00"N)
Lower Left  ( -83.0000000, -56.0000000) ( 83d 0' 0.00"W, 56d 0' 0.00"S)
Upper Right ( -34.0000000,  14.0000000) ( 34d 0' 0.00"W, 14d 0' 0.00"N)
Lower Right ( -34.0000000, -56.0000000) ( 34d 0' 0.00"W, 56d 0' 0.00"S)
Center      ( -58.5000000, -21.0000000) ( 58d30' 0.00"W, 21d 0' 0.00"S)
Band 1 Block=5880x1 Type=Int16, ColorInterp=Gray
  NoData Value=-9999

Create a LOCATION and MAPSET stored in a temporary folder

Using the flag –tmp-location we can create a LOCATION on the flight that will automaticaly removed when GRASS will be closed.

For this example we are goint to use:

[6]:
%%bash

grass  --text --tmp-location /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif --exec <<'EOF'
r.external -e input=/media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif output=SA_elevation --o --q
g.list raster -p
r.info  map=SA_elevation
r.slope.aspect elevation=SA_elevation slope=slope
r.info  map=slope
# export the "grass slope" to a geotif.
r.out.gdal --o -c -m -f createopt="COMPRESS=DEFLATE,ZLEVEL=9" type=Int16 format=GTiff nodata=-9999  input=slope  output=/home/ubuntu/my_SE_data/exercise/grassdb/slope.tif
EOF
Option --tmp-location is deprecated, use --tmp-project instead
Starting GRASS GIS...

          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\_  \   / / __ / / \__ \
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ /
       \____/_/ |_/_/  |_/____/____/   \____/___//____/

Welcome to GRASS GIS 8.4.2
GRASS GIS homepage:                      https://grass.osgeo.org
This version running through:            Bash Shell (/bin/bash)
Help is available with the command:      g.manual -i
See the licence terms with:              g.version -c
See citation options with:               g.version -x
Start the GUI with:                      g.gui wxpython
When ready to quit enter:                exit

----------------------------------------------
raster files available in mapset <PERMANENT>:
SA_elevation

 +----------------------------------------------------------------------------+
 | Map:      SA_elevation                   Date: Tue Oct  6 06:17:47 2026    |
 | Mapset:   PERMANENT                      Login of Creator: ubuntu          |
 | Project:  tmpproject                                                       |
 | DataBase: /tmp/grass8-ubuntu-14981                                         |
 | Title:                                                                     |
 | Timestamp: none                                                            |
 |----------------------------------------------------------------------------|
 |                                                                            |
 |   Type of Map:  GDAL-link            Number of Categories: 0               |
 |   Data Type:    CELL                 Semantic label: (none)                |
 |   Rows:         8400                                                       |
 |   Columns:      5880                                                       |
 |   Total Cells:  49392000                                                   |
 |        Projection: Latitude-Longitude                                      |
 |            N:        14N    S:        56S   Res: 0:00:30                   |
 |            E:        34W    W:        83W   Res: 0:00:30                   |
 |   Range of data:    min = -400  max = 6736                                 |
 |                                                                            |
 |   Data Source:                                                             |
 |    /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GM |
 |                                                                            |
 |                                                                            |
 |   Data Description:                                                        |
 |    generated by r.external                                                 |
 |                                                                            |
 |   Comments:                                                                |
 |    r.external --overwrite --quiet -e input="/media/sf_LVM_shared/my_SE_\   |
 |    data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif" outp\   |
 |    ut="SA_elevation"                                                       |
 |                                                                            |
 +----------------------------------------------------------------------------+


Slope raster map <slope> complete
 +----------------------------------------------------------------------------+
 | Map:      slope                          Date: Tue Oct  6 06:17:50 2026    |
 | Mapset:   PERMANENT                      Login of Creator: ubuntu          |
 | Project:  tmpproject                                                       |
 | DataBase: /tmp/grass8-ubuntu-14981                                         |
 | Title:    slope in degrees                                                 |
 | Timestamp: none                                                            |
 |----------------------------------------------------------------------------|
 |                                                                            |
 |   Type of Map:  raster               Number of Categories: 62              |
 |   Data Type:    FCELL                Semantic label: (none)                |
 |   Rows:         8400                                                       |
 |   Columns:      5880                                                       |
 |   Total Cells:  49392000                                                   |
 |        Projection: Latitude-Longitude                                      |
 |            N:        14N    S:        56S   Res: 0:00:30                   |
 |            E:        34W    W:        83W   Res: 0:00:30                   |
 |   Range of data:    min = 0  max = 61.86501                                |
 |                                                                            |
 |   Data Source:                                                             |
 |    raster elevation file SA_elevation                                      |
 |                                                                            |
 |                                                                            |
 |   Data Description:                                                        |
 |    generated by r.slope.aspect                                             |
 |                                                                            |
 |   Comments:                                                                |
 |    slope map elev = SA_elevation                                           |
 |    zfactor = 1.00 format = degrees                                         |
 |    min_slope = 0.000000                                                    |
 |                                                                            |
 |    r.slope.aspect elevation="SA_elevation" slope="slope" format="degree\   |
 |    s" precision="FCELL" zscale=1.0 min_slope=0.0 nprocs=1 memory=300       |
 |                                                                            |
 +----------------------------------------------------------------------------+

WARNING: Precision loss: Raster map <slope> of type DCELL to be exported as
         Int16. This can be avoided by using Float64.
WARNING: Forcing raster export
Checking GDAL data type and nodata value...

Using GDAL data type <Int16>
Exporting raster data to GTiff format...

r.out.gdal complete. File
</home/ubuntu/my_SE_data/exercise/grassdb/slope.tif> created.
Done.

Goodbye from GRASS GIS

GRASS rasters support three data types:

32bit signed integer (CELL) wich can be converted in GDAL using Byte/Int16/UInt16/UInt32/Int32
single-precision floating-point (FCELL) wich can be converted in GDAL using Float32
double-precision floating-point (DCELL) wich can be converted in GDAL using Float64

More info at https://grass.osgeo.org/grass83/manuals/r.out.gdal.html

[9]:
import rasterio
from rasterio.plot import show
slope = rasterio.open("/home/ubuntu/my_SE_data/exercise/grassdb/slope.tif")
show(slope)
../_images/GRASS_grass_newproject_9_0.png
[9]:
<Axes: >
[10]:
! qgis /$HOME/my_SE_data/exercise/grassdb/slope.tif
Warning: QSocketNotifier: Can only be used with threads started with QThread
libEGL warning: failed to get driver name for fd -1

libEGL warning: MESA-LOADER: failed to retrieve device information

libEGL warning: failed to get driver name for fd -1

MESA: error: ZINK: failed to choose pdev
libEGL warning: egl: failed to create dri2 screen
True

The temporary folder is removed automaticaly when closing grass.

Create a LOCATION and MAPSET stored in a folder that can be accessed later on.

Using the flag -e we can create a LOCATION on the flight called “south_america” that can be access later on.

[11]:
%%bash
rm -rf /$HOME/my_SE_data/exercise/grassdb/south_america

grass -c /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif  -e /$HOME/my_SE_data/exercise/grassdb/south_america
Creating new GRASS GIS project <south_america>...

At this point the south_america LOCATION has been created with the defoult PERMANENT mapset

[12]:
! ls /$HOME/my_SE_data/exercise/grassdb/south_america/
PERMANENT
[13]:
! ls -l /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT

total 36
-rw-rw-r-- 1 ubuntu ubuntu  328 Oct  6 06:20 DEFAULT_WIND
-rw-rw-r-- 1 ubuntu ubuntu    1 Oct  6 06:20 MYNAME
-rw-rw-r-- 1 ubuntu ubuntu   65 Oct  6 06:20 PROJ_INFO
-rw-rw-r-- 1 ubuntu ubuntu   10 Oct  6 06:20 PROJ_SRID
-rw-rw-r-- 1 ubuntu ubuntu   40 Oct  6 06:20 PROJ_UNITS
-rw-rw-r-- 1 ubuntu ubuntu 1101 Oct  6 06:20 PROJ_WKT
-rw-rw-r-- 1 ubuntu ubuntu   81 Oct  6 06:20 VAR
-rw-rw-r-- 1 ubuntu ubuntu  328 Oct  6 06:20 WIND
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct  6 06:20 sqlite

we can proceed in using the south_america LOCATION & the PERMANENT MAPSET using this syntax:

[14]:
%%bash
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec <<'EOF'
r.external input=/media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif output=SA_elevation --o --q
g.list raster -p
r.info  map=SA_elevation
r.slope.aspect elevation=SA_elevation aspect=aspect --o
r.out.gdal --o -c -m -f createopt="COMPRESS=DEFLATE,ZLEVEL=9" type=Int16 format=GTiff nodata=-9999  input=aspect  output=/home/ubuntu/my_SE_data/exercise/grassdb/aspect.tif
EOF
Starting GRASS GIS...

          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\_  \   / / __ / / \__ \
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ /
       \____/_/ |_/_/  |_/____/____/   \____/___//____/

Welcome to GRASS GIS 8.4.2
GRASS GIS homepage:                      https://grass.osgeo.org
This version running through:            Bash Shell (/bin/bash)
Help is available with the command:      g.manual -i
See the licence terms with:              g.version -c
See citation options with:               g.version -x
Start the GUI with:                      g.gui wxpython
When ready to quit enter:                exit

----------------------------------------------
raster files available in mapset <PERMANENT>:
SA_elevation

 +----------------------------------------------------------------------------+
 | Map:      SA_elevation                   Date: Tue Oct  6 06:21:04 2026    |
 | Mapset:   PERMANENT                      Login of Creator: ubuntu          |
 | Project:  south_america                                                    |
 | DataBase: //home/ubuntu/my_SE_data/exercise/grassdb                        |
 | Title:                                                                     |
 | Timestamp: none                                                            |
 |----------------------------------------------------------------------------|
 |                                                                            |
 |   Type of Map:  GDAL-link            Number of Categories: 0               |
 |   Data Type:    CELL                 Semantic label: (none)                |
 |   Rows:         8400                                                       |
 |   Columns:      5880                                                       |
 |   Total Cells:  49392000                                                   |
 |        Projection: Latitude-Longitude                                      |
 |            N:        14N    S:        56S   Res: 0:00:30                   |
 |            E:        34W    W:        83W   Res: 0:00:30                   |
 |   Range of data:    min = -400  max = 6736                                 |
 |                                                                            |
 |   Data Source:                                                             |
 |    /media/sf_LVM_shared/my_SE_data/exercise/geodata/dem/SA_elevation_mn_GM |
 |                                                                            |
 |                                                                            |
 |   Data Description:                                                        |
 |    generated by r.external                                                 |
 |                                                                            |
 |   Comments:                                                                |
 |    r.external --overwrite --quiet input="/media/sf_LVM_shared/my_SE_dat\   |
 |    a/exercise/geodata/dem/SA_elevation_mn_GMTED2010_mn_msk.tif" output=\   |
 |    "SA_elevation"                                                          |
 |                                                                            |
 +----------------------------------------------------------------------------+


Aspect raster map <aspect> complete
WARNING: Precision loss: Raster map <aspect> of type DCELL to be exported
         as Int16. This can be avoided by using Float64.
WARNING: Forcing raster export
Checking GDAL data type and nodata value...

Using GDAL data type <Int16>
Exporting raster data to GTiff format...

r.out.gdal complete. File
</home/ubuntu/my_SE_data/exercise/grassdb/aspect.tif> created.
Done.

Goodbye from GRASS GIS

[37]:
! qgis /$HOME/my_SE_data/exercise/grassdb/aspect.tif
Default software rendering mode (use -h if accelerated video card installed).
Loading tools from /usr/bin/openev/tools/Tool_Export.py
Loading tools from /usr/bin/openev/tools/Tool_ShapesGrid.py
Loading tools from /usr/bin/openev/tools/Tool_DriverList.py

or with line by line syntax

[15]:
%%bash

grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec r.neighbors input=SA_elevation output=SA_elevation_10c_max method=maximum --o
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec r.info  map=SA_elevation_10c_max

 +----------------------------------------------------------------------------+
 | Map:      SA_elevation_10c_max           Date: Tue Oct  6 06:21:48 2026    |
 | Mapset:   PERMANENT                      Login of Creator: ubuntu          |
 | Project:  south_america                                                    |
 | DataBase: //home/ubuntu/my_SE_data/exercise/grassdb                        |
 | Title:    3x3 neighborhood: maximum of SA_elevation                        |
 | Timestamp: none                                                            |
 |----------------------------------------------------------------------------|
 |                                                                            |
 |   Type of Map:  raster               Number of Categories: 0               |
 |   Data Type:    CELL                 Semantic label: (none)                |
 |   Rows:         8400                                                       |
 |   Columns:      5880                                                       |
 |   Total Cells:  49392000                                                   |
 |        Projection: Latitude-Longitude                                      |
 |            N:        14N    S:        56S   Res: 0:00:30                   |
 |            E:        34W    W:        83W   Res: 0:00:30                   |
 |   Range of data:    min = -153  max = 6736                                 |
 |                                                                            |
 |   Data Description:                                                        |
 |    generated by r.neighbors                                                |
 |                                                                            |
 |   Comments:                                                                |
 |    r.neighbors --overwrite input="SA_elevation" output="SA_elevation_10\   |
 |    c_max" size=3 method="maximum" weighting_function="none" nprocs=1 me\   |
 |    mory=300                                                                |
 |                                                                            |
 +----------------------------------------------------------------------------+

GRASS add-ons

Grass GIS has a modular architecture that allows users to extend its functionality through add-ons or modules. These add-ons cover a wide range of functionalities and are developed and contributed by the Grass GIS community. You can see the full list of add-ons at https://grass.osgeo.org/grass83/manuals/addons/index.html

For this exercise we are going to use r.mapcalc.tiled that integrate r.mapcalc in a multicore enviroment.

Installation

Open a bash terminal and run

sudo apt install grass-dev

when the download has ben done you can proceed with the installation.

For this exercise we are going to use:

Nontheless also consider to install and test the following add-on:

[16]:
%%bash
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec g.extension extension=r.mapcalc.tiled
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec r.slope.aspect elevation=SA_elevation slope=slope nprocs=2 --o
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec time r.mapcalc.tiled expression="slope_msk = if(slope > 10, 10, 1)" width=5880 height=4200  overlap=0 nprocs=2 --o
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec time r.mapcalc       expression="slope_msk = if(slope > 10, 10, 1)" --o
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec r.info  map=slope_msk
# export the "grass slope_msk" to a geotif
grass  --text /$HOME/my_SE_data/exercise/grassdb/south_america/PERMANENT --exec r.out.gdal --o -c -m -f createopt="COMPRESS=DEFLATE,ZLEVEL=9" type=Byte format=GTiff nodata=0 input=slope_msk output=/home/ubuntu/my_SE_data/exercise/grassdb/slope_msk.tif
WARNING: Extension <r.mapcalc.tiled> already installed. Re-installing...
Fetching <r.mapcalc.tiled> from <https://github.com/OSGeo/grass-addons/>
(be patient)...
Already on 'grass8'
Your branch is up to date with 'origin/grass8'.
Compiling...
Installing...
Updating extensions metadata file...
Updating extension modules metadata file...
Installation of <r.mapcalc.tiled> successfully finished

Slope raster map <slope> complete
1.10user 0.22system 0:02.85elapsed 46%CPU (0avgtext+0avgdata 79812maxresident)k
1584inputs+3088outputs (4major+56159minor)pagefaults 0swaps
1.40user 0.18system 0:01.65elapsed 96%CPU (0avgtext+0avgdata 45672maxresident)k
0inputs+3096outputs (0major+69655minor)pagefaults 0swaps
 +----------------------------------------------------------------------------+
 | Map:      slope_msk                      Date: Tue Oct  6 06:30:37 2026    |
 | Mapset:   PERMANENT                      Login of Creator: ubuntu          |
 | Project:  south_america                                                    |
 | DataBase: //home/ubuntu/my_SE_data/exercise/grassdb                        |
 | Title:                                                                     |
 | Timestamp: none                                                            |
 |----------------------------------------------------------------------------|
 |                                                                            |
 |   Type of Map:  raster               Number of Categories: 0               |
 |   Data Type:    CELL                 Semantic label: (none)                |
 |   Rows:         8400                                                       |
 |   Columns:      5880                                                       |
 |   Total Cells:  49392000                                                   |
 |        Projection: Latitude-Longitude                                      |
 |            N:        14N    S:        56S   Res: 0:00:30                   |
 |            E:        34W    W:        83W   Res: 0:00:30                   |
 |   Range of data:    min = 1  max = 10                                      |
 |                                                                            |
 |   Data Description:                                                        |
 |    generated by r.mapcalc                                                  |
 |                                                                            |
 |   Comments:                                                                |
 |    if(slope > 10, 10, 1)                                                   |
 |                                                                            |
 +----------------------------------------------------------------------------+

Checking GDAL data type and nodata value...

Using GDAL data type <Byte>
Exporting raster data to GTiff format...

r.out.gdal complete. File
</home/ubuntu/my_SE_data/exercise/grassdb/slope_msk.tif> created.

As you can see not always the paralellization is beneficial

[16]:
! qgis /$HOME/my_SE_data/exercise/grassdb/slope_msk.tif
Default software rendering mode (use -h if accelerated video card installed).
Loading tools from /usr/bin/openev/tools/Tool_Export.py
Loading tools from /usr/bin/openev/tools/Tool_ShapesGrid.py
Loading tools from /usr/bin/openev/tools/Tool_DriverList.py

Not all the GRASS module have been parallelized some example of parallization can be found here: https://htmlpreview.github.io/?https://github.com/petrasovaa/FUTURES-CONUS-talk/blob/main/foss4g2022.html#/

Acknowledgments

This material has been developed as part of the NSF-funded POSE project TI-2303651: Growing GRASS OSE for Worldwide Access to Multidisciplinary Geospatial Analytics.

[ ]: