[GRASS-dev] help

Vaclav Petras wenzeslaus at gmail.com
Thu Mar 20 11:45:43 PDT 2014


I actually forgot one useful thing which is not documented anywhere, as far
as I know: You can run some module which has similar parameters as the
module (script) you want to write (or you can just use any module) with the
parameter --script and you will get the template for your script. This is
useful for building a nice interface for your script (command line
processing ).



For example:

r.regression.line --script

#!/usr/bin/env python
############################################################################
#
# MODULE:       r.regression.line_wrapper
# AUTHOR(S):    vasek
# PURPOSE:      Wrapper for r.regression.line
# COPYRIGHT:    (C) 2014 by vasek, and the GRASS Development Team
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
############################################################################
#%module
#% description: Calculates linear regression from two raster maps: y = a +
b*x.
#% keywords: raster, statistics
#%end
#%flag
#% key: g
#% description: Print in shell script style
#%end
#%option
#% key: map1
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: Map for x coefficient
#% gisprompt: old,cell,raster
#%end
#%option
#% key: map2
#% type: string
#% required: yes
#% multiple: no
#% key_desc: name
#% description: Map for y coefficient
#% gisprompt: old,cell,raster
#%end
#%option
#% key: output
#% type: string
#% required: no
#% multiple: no
#% key_desc: name
#% description: ASCII file for storing regression coefficients (output to
screen if file not specified).
#% gisprompt: new,file,file
#%end
import sys
import grass.script as grass
def main():
    # put code here
    return 0
if __name__ == "__main__":
    options, flags = grass.parser()
    sys.exit(main())


On Tue, Mar 18, 2014 at 10:22 AM, Vaclav Petras <wenzeslaus at gmail.com>wrote:

> Hi,
>
> the scripting interface is appropriate for beginners. You can script in
> Python or in Bash (or other shell). You can also start with Graphical
> Modeler and then export the model as a script and improve that script.
>
> Here is a page about how to call GRASS modules from Python, especially
> note the section Usage Examples:
>
> http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library
>
> You have to run your script from GRASS, i.e. on MS Windows, you go to main
> menu File -> Launch Script and perhaps agree to some dialogs, or you can
> use Python shell in GRASS GUI call your script using execfile; or just
> command line is enough.
>
> The commands (calls of GRASS modules) are usually written in manual and in
> tutorials for command line (including the Command console in GRASS GUI) and
> looks like this:
>
>
> r.profile -g input=mymap output=newfile profile=12244.256,-295112.597,12128.012,-295293.77
>
>
> And this could be rewritten to Python like this:
>
>
> grass.run_command('r.profile',
>                input = input_map,
>                output = output_file,
>                profile = [12244.256,-295112.597,12128.012,-295293.77]
>
>
> This applies to any GRASS module.
>
> There is one new Python interface for GRASS which is more advanced, so if
> you would like to use the latest (future) GRASS 7 (not current GRASS
> 6.4.3), you can try this:
>
> http://grasswiki.osgeo.org/wiki/Python/pygrass
>
> All the best in your studies,
> Vaclav
>
>
>
> On Mon, Mar 17, 2014 at 10:02 PM, 杨泽龙 <yangzelong at whu.edu.cn> wrote:
>
>> Dear Developers,
>>      I'm a student majoring in GIS. Now, I feel interested in the open
>> source GIS. I want to know whether there are secondary development
>> interfaces for newers to easily develop their desired function?
>> _______________________________________________
>> grass-dev mailing list
>> grass-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/grass-dev
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20140320/ee2f5e98/attachment.html>


More information about the grass-dev mailing list