[GRASS-dev] help

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?

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@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@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

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

···

On Tue, Mar 18, 2014 at 10:22 AM, Vaclav Petras <wenzeslaus@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@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@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

On Thu, Mar 20, 2014 at 7:45 PM, Vaclav Petras <wenzeslaus@gmail.com> wrote:

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 ).

… indeed a nice feature! But would it be nice to have not only:

r.profile --script
#!/usr/bin/env python
############################################################################

MODULE: r.profile_wrapper


#% key: null
#% type: string
#% required: no
#% multiple: no
#% description: Character to represent no data cell
#% answer: *
#%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())

… this part of the script but for the truly lazy also the respective
grass.run_command() call included?

Something like

coords = options[‘coordinates’]
grass.run_command(‘r.profile’,
input = input_map,
output = output_file,
profile = coords)

or, perhaps better, the code snippet compliant with pyGRASS

http://grasswiki.osgeo.org/wiki/Python/pygrass
?

(maybe an answer to http://trac.osgeo.org/grass/ticket/2226 )

Markus

Markus Neteler wrote:

.. indeed a nice feature! But would it be nice to have not only:

... this part of the script but for the truly lazy also the respective
grass.run_command() call included?

If you're generating a "front end" for a module, exec_command() may be
more appropriate.

Something like

coords = options['coordinates']
grass.run_command('r.profile',
               input = input_map,
               output = output_file,
               profile = coords)

Choosing variable names has a minor complication, in that there are a
couple of option names which are Python keywords (e.g. "from").

If you just want to get rid of the dictionary syntax, the options
could be stored in an object which supports both attribute-style and
key-style access, so you could use either options.coordinates or
options['coordinates'].

--
Glynn Clements <glynn@gclements.plus.com>

On Wed, Apr 23, 2014 at 12:56 AM, Glynn Clements <glynn@gclements.plus.com> wrote:

Markus Neteler wrote:

… indeed a nice feature! But would it be nice to have not only:

… this part of the script but for the truly lazy also the respective
grass.run_command() call included?

If you’re generating a “front end” for a module, exec_command() may be
more appropriate.

Something like

coords = options[‘coordinates’]
grass.run_command(‘r.profile’,
input = input_map,
output = output_file,
profile = coords)

Choosing variable names has a minor complication, in that there are a
couple of option names which are Python keywords (e.g. “from”).

If you just want to get rid of the dictionary syntax, the options
could be stored in an object which supports both attribute-style and
key-style access, so you could use either options.coordinates or
options[‘coordinates’].

In the past hours I realized that most of it is probably already
offered by pyGRASS:

Example 1

from grass.pygrass.modules import Module
slope_aspect = Module(“r.slope.aspect”)
slope_aspect(elevation=‘elevation’, slope=‘slp’, aspect=‘asp’, format=‘degrees’, overwrite=True)


Example 2

from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules.shortcuts import raster as r

g.message(“Filter elevation map by a threshold…”)

set computational region

input = ‘elevation’
g.region(rast=input)

hardcoded:

r.mapcalc(‘elev_100m = if(elevation > 100, elevation, null())’, overwrite = True)

with variables

output = ‘elev_100m’
thresh = 100.0
r.mapcalc(“%s = if(%s > %d, %s, null())” % (output, input, thresh, input), overwrite = True)
r.colors(map=output, color=“elevation”)

Easier than that… Added to
http://grasswiki.osgeo.org/wiki/GRASS_and_Python#pygrass_Library

Markus