Python scripting in GRASS GIS environment     Geospatial Analysis and Modeling – MEA792                 Margherita Di Leo
Python scripting in GRASS GIS environmentPython + GRASS GIS:➢ Python scripts that call GRASS functionality fromoutside➢  R...
Python scripting in GRASS GIS environmentExampleModule: r.ipsoPurpose: Creates the ipsographic curve and theadimensional i...
Python scripting in GRASS GIS environmentGUI#%module#% description: Creates the ipsographic and ipsometric curve#% keyword...
Python scripting in GRASS GIS environmentGUI#%module#% description: Creates the ipsographic and ipsometric curve#% keyword...
Python scripting in GRASS GIS environmentpromptGRASS 6.5.svn (Basilicata):~ > r.ipso.py --helpDescription: Creates the ips...
Python scripting in GRASS GIS environmentoutputGRASS   6.5.svn    (Basilicata):~   >   r.ipso.py   map=dem@Fiumarella_data...
Python scripting in GRASS GIS environmentoutput
Python scripting in GRASS GIS environmentoutput
Python scripting in GRASS GIS environmentimportimport   sysimport   osimport   matplotlib.pyplot as pltimport   grass.scri...
Python scripting in GRASS GIS environmentMain (2/4)   for i in range(len(stats)):       zn[i,3] = 1 - (zn[i,2] / sum(zn[:,...
Python scripting in GRASS GIS environmentMain (3/4)   print "==========================="   print "Ipsometric | quantiles"...
Python scripting in GRASS GIS environmentMain (4/4)def plotImage(x,y,image,type,xlabel,ylabel,title):    plt.plot(x, y, ty...
Python scripting in GRASS GIS environmentZn has n rows and 6 columns [len(stats) = n]Kl has n rows and 2 columnsPrc has 9 ...
Python scripting in GRASS GIS environmentZn has n rows and 6 columns [len(stats) = n]Kl has n rows and 2 columnsPrc has 9 ...
Python scripting in GRASS GIS environmentPrc has 9 rows and 2 columns.It defines the ipsometric curve by the quantiles of ...
Python scripting in GRASS GIS environmentSome useful links:GRASS and Python on Osgeo wiki:http://grass.osgeo.org/wiki/GRAS...
Upcoming SlideShare
Loading in …5
×

Python grass

3,292 views

Published on

Lecture at North Carolina State University about python programming in GRASS GIS environment

Published in: Education, Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,292
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
52
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Python grass

  1. 1. Python scripting in GRASS GIS environment Geospatial Analysis and Modeling – MEA792 Margherita Di Leo
  2. 2. Python scripting in GRASS GIS environmentPython + GRASS GIS:➢ Python scripts that call GRASS functionality fromoutside➢ Running external commands from Python (as agrass module)
  3. 3. Python scripting in GRASS GIS environmentExampleModule: r.ipsoPurpose: Creates the ipsographic curve and theadimensional ipsometric curveRequires: Matplotlibhttp://svn.osgeo.org/grass/grass-addons/raster/r.ipso/
  4. 4. Python scripting in GRASS GIS environmentGUI#%module#% description: Creates the ipsographic and ipsometric curve#% keywords: raster#%end#%option#% key: map#% type: string#% gisprompt: old,raster,raster#% key_desc: name#% description: Name of elevation raster map#% required: yes#%end#%option#% key: image#% type: string#% gisprompt: new_file,file,input#% key_desc: image#% description: output graph#% required: yes#%end#%flag#% key: a#% description: generate ipsometric curve#%END#%flag#% key: b#% description: generate ipsographic curve#%END
  5. 5. Python scripting in GRASS GIS environmentGUI#%module#% description: Creates the ipsographic and ipsometric curve#% keywords: raster#%end#%option#% key: map#% type: string#% gisprompt: old,raster,raster#% key_desc: name#% description: Name of elevation raster map#% required: yes#%end#%option#% key: image#% type: string#% gisprompt: new_file,file,input#% key_desc: image#% description: output graph#% required: yes#%end#%flag#% key: a#% description: generate ipsometric curve#%END#%flag#% key: b#% description: generate ipsographic curve#%END
  6. 6. Python scripting in GRASS GIS environmentpromptGRASS 6.5.svn (Basilicata):~ > r.ipso.py --helpDescription: Creates the ipsographic and ipsometric curveKeywords: rasterUsage: r.ipso.py [-ab] map=name image=image [--verbose] [--quiet]Flags: -a generate ipsometric curve -b generate ipsographic curve --v Verbose module output --q Quiet module outputParameters: map Name of elevation raster map image path to output graphGRASS 6.5.svn (Basilicata):~ >
  7. 7. Python scripting in GRASS GIS environmentoutputGRASS 6.5.svn (Basilicata):~ > r.ipso.py map=dem@Fiumarella_dataset -abimage=Fiumarella 100%Tot. cells 172200.0===========================Ipsometric | quantiles===========================994 | 0.025989 | 0.05980 | 0.1960 | 0.25918 | 0.5870 | 0.75882 | 0.7841 | 0.9817 | 0.975Done!GRASS 6.5.svn (Basilicata):~ >
  8. 8. Python scripting in GRASS GIS environmentoutput
  9. 9. Python scripting in GRASS GIS environmentoutput
  10. 10. Python scripting in GRASS GIS environmentimportimport sysimport osimport matplotlib.pyplot as pltimport grass.script as grassimport numpy as npMain (1/4)def main(): # r.stats gives in the first column the elevation and in the second the number ofcells 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
  11. 11. Python scripting in GRASS GIS environmentMain (2/4) for i in range(len(stats)): zn[i,3] = 1 - (zn[i,2] / sum(zn[:,1])) zn[i,4] = zn[i,3] * (((res**2)/1000000)*sum(zn[:,1])) zn[i,5] = ((zn[i,0] - min(zn[:,0])) / (max(zn[:,0]) - min(zn[:,0])) ) kl[i,0] = zn[i,0] kl[i,1] = 1 - (zn[i,2] / totcell) # quantiles prc[0,0] , prc[0,1] = findint(kl,0.025) , 0.025 prc[1,0] , prc[1,1] = findint(kl,0.05) , 0.05 prc[2,0] , prc[2,1] = findint(kl,0.1) , 0.1 prc[3,0] , prc[3,1] = findint(kl,0.25) , 0.25 prc[4,0] , prc[4,1] = findint(kl,0.5) , 0.5 prc[5,0] , prc[5,1] = findint(kl,0.75) , 0.75 prc[6,0] , prc[6,1] = findint(kl,0.9) , 0.9 prc[7,0] , prc[7,1] = findint(kl,0.95) , 0.95 prc[8,0] , prc[8,1] = findint(kl,0.975) , 0.975 # Managing flag & plot if flags[a]: plotImage(zn[:,3], zn[:,5],options[image]+_Ipsometric.png,-,A(i) /A,Z(i) / Zmax,Ipsometric Curve) if flags[b]: plotImage(zn[:,4], zn[:,0],options[image]+_Ipsographic.png,-,A [km^2],Z [m.slm],Ipsographic Curve)
  12. 12. Python scripting in GRASS GIS environmentMain (3/4) print "===========================" print "Ipsometric | quantiles" print "===========================" print %.0f %findint(kl,0.025) , "|", 0.025 print %.0f %findint(kl,0.05) , "|", 0.05 print %.0f %findint(kl,0.1) , "|", 0.1 print %.0f %findint(kl,0.25) , "|", 0.25 print %.0f %findint(kl,0.5) , "|", 0.5 print %.0f %findint(kl,0.75) , "|", 0.75 print %.0f %findint(kl,0.7) , "|", 0.7 print %.0f %findint(kl,0.9) , "|", 0.9 print %.0f %findint(kl,0.975) , "|", 0.975 print n print Done! #print prcdef 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
  13. 13. Python scripting in GRASS GIS environmentMain (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. 14. Python scripting in GRASS GIS environmentZn has n rows and 6 columns [len(stats) = n]Kl has n rows and 2 columnsPrc 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. 15. Python scripting in GRASS GIS environmentZn has n rows and 6 columns [len(stats) = n]Kl has n rows and 2 columnsPrc has 9 rows and 2 columns kl 0 1 A= G = 1 – (C / elevation num of cell)
  16. 16. Python scripting in GRASS GIS environmentPrc 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 znp.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. 17. Python scripting in GRASS GIS environmentSome useful links:GRASS and Python on Osgeo wiki:http://grass.osgeo.org/wiki/GRASS_and_PythonGRASS Python Scripting Library:http://grass.osgeo.org/programming6/pythonlib.htmlStyle rules:http://trac.osgeo.org/grass/browser/grass/trunk/SUBMITTING_PYTHON

×