4

I normally use bash scripts (ie. #!/bin/bash) to include different Linux-based tools (eg. awk, sed, cut), software (eg. GMT, GDAL, SAGA), and even languages (eg. #!/usr/bin/env python). However, so far I have not found any simple ways to add tools from the GRASS GIS software.

Could you please provide a simple example of how to do this?

In Linux I create a bash script in this way:

  1. Open Linux terminal (eg. Ctrl+Alt+T)
  2. Write: vim myscript.bash
  3. Add: #!/bin/bash -x
  4. Start adding code from here.

3 Answers 3

1
+100

This should work in Ubuntu 20.04:

export GISBASE=/lib/grass78/ 
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib

if [ ! "$PYTHONPATH" ] ; then
   PYTHONPATH="$GISBASE/etc/python"
else
   PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
fi
   export PYTHONPATH
   
export GIS_LOCK=$$
export GISRC=$HOME/.grassrc7+
export GISRC=/tmp/grass7-${USER}-$GIS_LOCK/gisrc
rm -fr /tmp/grass7-${USER}-$GIS_LOCK
mkdir /tmp/grass7-${USER}-$GIS_LOCK
export TMPDIR="/tmp/grass7-${USER}-$GIS_LOCK"
echo "GISDBASE: /home/guillermo" >>$GISRC
echo "LOCATION_NAME: waterkant" >>$GISRC
echo "MAPSET: PERMANENT" >>$GISRC
echo "GRASS_GUI: text" >>$GISRC

g.version
1

I suggest that you read the documentation on the GRASS Wiki page. There are several steps needed to set up the GRASS environment. My answer will mostly be some copy/paste of this page in order to avoid a "link only" answer.

    # Example in bash shell syntax:
 
   # path to GRASS binaries and libraries:
   export GISBASE=/usr/lib64/grass78
 
   export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib
   
   # set PYTHONPATH to include the GRASS Python lib
   if [ ! "$PYTHONPATH" ] ; then
      PYTHONPATH="$GISBASE/etc/python"
   else
      PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
   fi
   export PYTHONPATH
 
   # use process ID (PID) as lock file number:
   export GIS_LOCK=$$
 
   # settings for graphical output to PNG file (optional)
   export GRASS_PNGFILE=/tmp/grass6output.png
   export GRASS_TRUECOLOR=TRUE
   export GRASS_WIDTH=900
   export GRASS_HEIGHT=1200
   export GRASS_PNG_COMPRESSION=1
   export GRASS_MESSAGE_FORMAT=plain

In addition, you must also define a grass session.

    # path to GRASS settings file
   export GISRC=$HOME/.grassrc7

Then you can start with some command lines (e.g. adding new column to mydata)

v.db.addcol mydata="new_col DOUBLE PRECISION"
1

Use the --exec parameter of the grass executable (link to documentation of grass executable):

grass -c elevation.tiff -e /path/to/grassdata/test1/
grass /path/to/grassdata/test1/PERMANENT/ --exec r.external input=elevation.tiff output=elevation
grass /path/to/grassdata/test1/PERMANENT/ --exec r.univar map=elevation

The --exec executable syntax works for both single commands and executable scripts.

If you are scripting in Python (#!/usr/bin/env python), then you can use the above or, after a bootstrap to set PYTHONPATH, use grass.script.setup.init and (optionally) use the rest of the Python API (link to documentation of grass.script.setup).

There is usually no need to set up the environment variables manually just to run the processing tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.