Sunday 23 October 2016

Creating a GRASS location from a bash script



I would like to have a GRASS script that creates a new location of its own, instead of using an existing location. This facilitates the usage of the script by other users.


Going through the guide on Working with GRASS without starting it explicitly and the notes on creating locations form the command line, I arrived at the following script:


#!/bin/sh
# path to GRASS binaries and libraries:
export GISBASE=/usr/lib/grass74
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib

# GRASS session variables
GRASSDB="/tmp/grassdata"

GRASSLOC="tmp_location"

mkdir -p "$GRASSDB/$GRASSLOC/PERMANENT"

# Create a new GRASS location
#grass74 -c $PATH_IN"ORCDRC_M_sl1_250m_clip.tif" "$GRASSDB$GRASSLOC"

grass74 -c /path/to/some/file.tif "$GRASSDB$GRASSLOC"

echo "Creation done"


# path to GRASS settings file
export GISRC=/tmp/grass7-${USER}-$GIS_LOCK/gisrc
# remove any leftover files/folder
rm -fr /tmp/grass7-${USER}-$GIS_LOCK
mkdir /tmp/grass7-${USER}-$GIS_LOCK
# set GISDBASE, LOCATION_NAME, and/or MAPSET
echo "GISDBASE: $GRASSDB" >>$GISRC
echo "LOCATION_NAME: $GRASSLOC" >>$GISRC
echo "MAPSET: PERMANENT" >>$GISRC

# start in text mode
echo "GRASS_GUI: text" >>$GISRC

# Test it
g.version

This kind of works, however, the grass74 -c command not only creates a new location, it also starts a new GRASS session and launches the GUI.


Is there another way of creating a location without launching the GUI (and creating a session)?



Answer



Use grass -c -e -text ...



from the Help:


 -e           exit after creation of location or mapset. Only with -c flag
-text use text based interface (skip welcome screen)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...