Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active May 11, 2021 04:12
Show Gist options
  • Save perrygeo/11350017 to your computer and use it in GitHub Desktop.
Save perrygeo/11350017 to your computer and use it in GitHub Desktop.
PyGRASS
import os
import sys
outdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'output'))
if not os.path.exists(outdir):
os.mkdir(outdir)
input_elevation = "dem.img"
gisbase = os.environ['GISBASE'] = "/usr/local/grass-7.0.0svn"
# putting $GISBASE/lib (with $GISBASE replaced by its actual value)
# into /etc/ld.so.conf then running ldconfig).
# os.environ['LD_LIBRARY_PATH'] = os.path.join(gisbase, 'lib')
# print os.environ['LD_LIBRARY_PATH']
gisdbase = os.path.join('.')
# TODO create these with correct projection info for the input raster(s)
location = "bigsur"
mapset = "PERMANENT"
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
import grass.script.setup as gsetup
gsetup.init(gisbase, gisdbase, location, mapset)
###############################################################################
from grass.pygrass.modules.shortcuts import raster as rast
from grass.pygrass.modules.shortcuts import general
general.mremove(rast="*", f=True)
elev = "dem"
slope = "slope.img"
aspect = "aspect.img"
rast.external(input=input_elevation, output=elev, overwrite=True)
rast.external_out(directory=outdir, format="HFA")
rast.slope_aspect(elevation=elev, slope=slope, aspect=aspect, format='percent', overwrite=True)
rast.topidx(input=elev, output="topidx.img")
rast.sun (elev_in=elev, asp_in=aspect, slope_in=slope,
lin=2.5, alb=0.2, day=172,
#beam_rad=b172b diff_rad=d172b, refl_rad=r172b insol_time=it172b, glob_rad=g172b
insol_time='insoltime_172.img', glob_rad='globalrad_172.img')
@perrygeo
Copy link
Author

Some other useful terrain commands in GRASS 7

r.external ?? 

r.in.gdal input=dem.img output=dem location=bigsur2

r.topidx input=dem output=topidx

r.sunmask --v dem output=sunmask_0427 year=2014 month=4 \
          day=27 hour=16 minute=30 sec=0 timezone=-8

r.sun -s dem lin=2.5 alb=0.2 day=172 \
      beam_rad=b172 diff_rad=d172 \
      refl_rad=r172 insol_time=it172 \
      glob_rad=g172

r.param.scale in=dem output=morphology size=9 param=feature 
r.param.scale in=dem output=minic size=9 param=minic
r.param.scale in=dem output=maxic size=9 param=maxic
r.param.scale in=dem output=crosc size=9 param=crosc
r.param.scale in=dem output=planc size=9 param=planc
r.param.scale in=dem output=profc size=9 param=profc
r.param.scale in=dem output=longc size=9 param=longc
r.param.scale in=dem output=slope size=9 param=slope
r.param.scale in=dem output=aspect size=9 param=aspect


r.mfilter.fp

r.slope.aspect   # compare to param.scale slope= aspect pcurv tcurv

r.flow # *out ... vector output

r.watershed

r.water.outlet

r.viewshed

r.uslek r.usler - USLE model params

r.topmodel

r.stream.extract

r.shaded.relief map=dem shadedmap=dem_shaded

r.terraflow elev=dem filled=dem_filled \
    dir=dem_dir swatershed=dem_watershed \
    accumulation=dem_accu tci=dem_tci memory=2500 \
    stats=terraflow_stats.txt 

r.sim.sediment  # Sediment transport and erosion/deposition simulation
r.sim.water  # Overland flow hydrologic simulation using path sampling method (SIMWE).
r.solute.transport  r.gwflow

r.basins.fill

r.drain

r.fill.dir 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment