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.
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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