Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Python grass
1. Python scripting in GRASS GIS environment
Geospatial Analysis and Modeling – MEA792
Margherita Di Leo
2. Python scripting in GRASS GIS environment
Python + GRASS GIS:
➢ Python scripts that call GRASS functionality from
outside
➢ Running external commands from Python (as a
grass module)
3. Python scripting in GRASS GIS environment
Example
Module: r.ipso
Purpose: Creates the ipsographic curve and the
adimensional ipsometric curve
Requires: Matplotlib
http://svn.osgeo.org/grass/grass-addons/raster/r.ipso/
10. Python scripting in GRASS GIS environment
import
import sys
import os
import matplotlib.pyplot as plt
import grass.script as grass
import numpy as np
Main (1/4)
def main():
# r.stats gives in the first column the elevation and in the second the number of
cells having that elevation
stats = grass.read_command('r.stats', input = options['map'], fs = 'space', nv =
'*', nsteps = '255', flags = 'inc').split('n')[:-1]
# res = cellsize
res = float(grass.read_command('g.region', rast = options['map'], flags =
'm').strip().split('n')[4].split('=')[1])
zn = np.zeros((len(stats),6),float)
kl = np.zeros((len(stats),2),float)
prc = np.zeros((9,2),float)
for i in range(len(stats)):
if i == 0:
zn[i,0], zn[i, 1] = map(float, stats[i].split(' '))
zn[i,2] = zn[i,1]
else:
zn[i,0], zn[i, 1] = map(float, stats[i].split(' '))
zn[i,2] = zn[i,1] + zn[i-1,2]
totcell = sum(zn[:,1])
print "Tot. cells", totcell
13. Python scripting in GRASS GIS environment
Main (4/4)
def plotImage(x,y,image,type,xlabel,ylabel,title):
plt.plot(x, y, type)
plt.ylabel(ylabel)
plt.xlabel(xlabel)
plt.xlim( min(x), max(x) )
plt.ylim( min(y), max(y) )
plt.title(title)
plt.grid(True)
plt.savefig(image)
plt.close('all')
if __name__ == "__main__":
options, flags = grass.parser()
sys.exit(main())
14. Python scripting in GRASS GIS environment
Zn has n rows and 6 columns [len(stats) = n]
Kl has n rows and 2 columns
Prc has 9 rows and 2 columns
Zn
0 1 2 3 4 5
A= B= C = (if i=0, D = (1-C) / E = Di * F = (A –
elevation numbers of Ci=Ai; else numbers of (res^2) / min(A)) /
cells Ci = Ai + B(i- cells 1000000 * (max(A) -
1) ) numbers of min(A))
cells
15. Python scripting in GRASS GIS environment
Zn has n rows and 6 columns [len(stats) = n]
Kl has n rows and 2 columns
Prc has 9 rows and 2 columns
kl
0 1
A= G = 1 – (C /
elevation num of cell)
16. Python scripting in GRASS GIS environment
Prc has 9 rows and 2 columns.
It defines the ipsometric curve by the quantiles of the distribution.
It is built by the function ”findint”
def findint(kl,f):
Xf = np.abs(kl-f); Xf = np.where(Xf==Xf.min())
z1 , z2 , f1 , f2 = kl[float(Xf[0])][0] , kl[float(Xf[0]-1)][0] , kl[float(Xf[0])
][1] , kl[float(Xf[0]-1)][1]
z = z1 + ((z2 - z1) / (f2 - f1)) * (f - f1)
return z
np.abs and np.where are two NumPy routines:
numpy.absolute(x[, out])
Calculate the absolute value element-wise.
numpy.where(condition[, x, y])
Return elements, either from x or y, depending on condition.
If only condition is given, return condition.nonzero().
See NumPy doc at http://docs.scipy.org/doc/
17. Python scripting in GRASS GIS environment
Some useful links:
GRASS and Python on Osgeo wiki:
http://grass.osgeo.org/wiki/GRASS_and_Python
GRASS Python Scripting Library:
http://grass.osgeo.org/programming6/pythonlib.html
Style rules:
http://trac.osgeo.org/grass/browser/grass/trunk/SUBMITTING_PYTHON