# Solutions

1. Compute topographic index using r.topidx.

```def run_slope(scanned_elev, env, **kwargs):
gscript.run_command('r.topidx', input=scanned_elev, output='topidx', env=env)
```

2. Compute topographic aspect (slope orientation) using r.slope.aspect and reclassify it into 8 main directions.

```def run_aspect(scanned_elev, env, **kwargs):
gscript.run_command('r.slope.aspect', elevation=scanned_elev, aspect='aspect', env=env)
rules = ['45:135:1', '135:225:2', '225:315:3', '315:45:4']
gscript.write_command('r.recode', input='aspect', output='aspect_class', rules='-', stdin='\n'.join(rules), env=env)
# set new color table: green - yellow - red
gscript.run_command('r.colors', map='aspect_class', color='random', env=env)
```

3. Show areas with concave profile and tangential curvature (concave forms have negative curvature).

```def run_curvatures(scanned_elev, env, **kwargs):
gscript.run_command('r.param.scale', input=scanned_elev, output='profile_curv', method='profc', size=11, env=env)
gscript.run_command('r.param.scale', input=scanned_elev, output='tangential_curv', method='crosc', size=11, env=env)
gscript.mapcalc("concave = if (profile_curv < 0 && tangential_curv < 0, 1, null())", env=env)
```

4. Derive peaks using either r.geomorphon or r.param.scale and convert them to points (using r.to.vect and v.to.points). From each of those points compute visibility with observer height of your choice a derive a cumulative viewshed layer where the value of each cell represents the number of peaks the cell is visible from (use r.series).

```def run_viewshed_peaks(scanned_elev, env, **kwargs):
gscript.run_command('r.geomorphon', dem=scanned_elev, forms='landforms',
search=16, skip=6, env=env)
gscript.mapcalc('peaks = if(landforms == 2, 1, null())', env=env)
gscript.run_command('r.to.vect', input='peaks', output='peaks_area', type='area', env=env)
gscript.run_command('v.to.points', input='peaks_area', output='peaks', type='centroid', flags='t', env=env)
coordinates = gscript.read_command('v.out.ascii', input='peaks', format='point', separator=',', env=env).strip()
i = 0
for coords in coordinates.splitlines():
print coords.split(',')[:2]
gscript.run_command('r.viewshed', input=scanned_elev, output='viewshed' + str(i),
coordinates=coords.split(',')[:2], observer_elevation=3, flags='b', env=env)
i += 1
gscript.run_command('r.series', input=['viewshed' + str(j) for j in range(i)], method='sum',
output='cumulative_viewshed', env=env)
gscript.run_command('r.colors', map='cumulative_viewshed', color='bcyr', env=env)
```

5. Find a least cost path between 2 points (for example from x=638360, y=220030 to x=638888, y=220388) where cost is defined as topographic index (trying avoid areas). Use r.topidx.

6. Compute erosion with spatially variable landcover and soil erodibility (use rasters cfactorbare_1m and soils_Kfactor from the provided dataset). Reclassify the result into 7 classes based on severity of erosion and deposition: