Running a Simple Script from the GRASS Shell

Posted on Updated on

1. The simple GRASS script below will generate the least cost route from point A to point B given just two constraints, elevation and slope. The script can be easily expanded to include more constraints.

2.  To Run the script, copy it and save it as a text file with a name that ends in .sh, e.g. At the GRASS monitor, type “sh” plus the path to the script:

GRASS 7.0.0 (nc_spm_08):~ > sh /Users/xxx/Desktop/


# Author: gisstuff
# Purpose: Script to calculate Least Cost Route Between two points based on slope.
# Data Requirement:  A single Digital Elevation Model

#Start monitor
d.mon start=wx2

#Clear monitor

#Generate slope from Wayne County DEM
r.slope.aspect elevation=wayne slope=slope format=percent –overwrite

#Reclassify slope map
r.reclass –overwrite input=slope output=slope_reclass rules=/Users/xxx/Desktop/reclass.txt title=”Reclass Slope”

#Reclassify slope map
r.reclass –overwrite input=wayne output=wayne_reclass rules=/Users/xxx/Desktop/reclass_elevation.txt title=”Reclass_Elevation”

#Sum the two cost estimates
r.mapcalc “total_surface = slope_reclass + wayne_reclass”

#Get the total accumulated cost surface
r.cost input=total_surface output=costDist start_points=start stop_points=end –overwrite

#Display Total Cost Surface
d.rast costDist

#Generate the least cost path raster
r.drain input=costDist output=slope_path2  –overwrite start_points=start,end

#Convert the least cost raster to vector -s input=slope_path2 output=slope_path2 type=line –overwrite

#Display the least cost vector
d.vect slope_path2

exit 0


The text file, reclass.txt, contains instructions for reclassifying the slope raster. The content of that file is shown below. Copy it and save it as “reclass.txt”.

#Reclass Rules Slope
0 thru 2 = 2
2 thru 5 = 4
5 thru 10 = 8
10 thru 20 = 16
20 thru 30 = 32
30 thru 40 = 8
40 thru 50 = 128
50 thru 60 = 256
60 thru 100 = 512


Watershed and Flood Modeling with GRASS

Posted on Updated on

Watershed and Flood Modeling 

Shell Scripting with GRASS GIS

Posted on Updated on

One of the cool ways to work with GRASS GIS is with shell scripting.  One can write scripts that execute from the the GRASS command prompt.  The results are displayed on a monitor similar to the GRASS GUI.  If you prefer to interact with GRASS through scripts, rather than through menus, you should give GRASS shell scripting a try.  Here are some links to get you started:

Scripting Projects

  • Topographic Surface Analysis (see paper)
  • Watershed delineation
  • Line of site analysis
  • Least cost routing analysis
  • Landslide risk modeling
  • Wildfire susceptibility modeling
  • Avalanches risk mapping
  • Down-scaling climate change models
  • Flood forecasting
  • Erosion modeling using the Revised Universal Soil Loss Equation (RUSLE) equation
  • Traffic noise modeling
  • Site suitability analysis for public facilities
  • Habitat suitability analysis

For more project ideas, see these references: