[GRASS-user] Python scripting and GRASS

Greetings,

I have some questions concerning Python scripting for GRASS under windows. I'm relatively new to GRASS, and I'm neither familiar with Python nor UNIX (but I have some programming experience, C++, Java). I have six months, in which I have to work on the preprocessing of a hydrologic model with GRASS. For that it will be necessary to write some scripts. The idea was to use Python for this, as it is platform independent and more powerfull than UNIX (I'm working on windows as the model only runs on windows) . However, I have some problems getting started and all what I found on the internet was quite confusing. I would appreciate all advice concerning the following questions:

Which compiler is usually used for Python scripting with GRASS? Is there a standard way?

Is it possible to do the scripting inside GRASS?

Can I use SWIG? Do I have to install it extra, or is there an integrated version?

I found a description how to configure Eclipse for python and GRASS, but up to now I get some Attribut errors while compiling the simple test code (I think there are still some problems with the environment variables).

Can I run python scripts with MySYS?

Can I access to all the GRASS functions with Python?

Is it usefull to use IDLE?

Is the GRASS plugin for QGIS written in Python?

Or is it just easier to write the scripts in UNIX? Is it possible to execute the UNIX scripts with MySYS under windows?

I'm looking for the easiest way, as there is lots of work to do. I would be really happy about some advice!

Many thanks in advance,

Sonja

I'm looking for the easiest way as there is a lot of work to do. So, if it is too complicated, I might as well script with

Is it possible to run python scripts inside GRASS (with MySYS?). What's best to use as developper environment? What I found up to now, was a description how to configure Eclipse for GRASS and Python,

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75

Hi Sonja,

On Thu, Jan 14, 2010 at 11:06 AM, Sonja Jankowfsky
<sonja.jankowfsky@cemagref.fr> wrote:

Greetings,

I have some questions concerning Python scripting for GRASS under windows.

[I don't have much experience on Windows]

I'm relatively new to GRASS, and I'm neither familiar with Python nor UNIX
(but I have some programming experience, C++, Java). I have six months, in
which I have to work on the preprocessing of a hydrologic model with GRASS.
For that it will be necessary to write some scripts. The idea was to use
Python for this, as it is platform independent and more powerfull than UNIX

You mean Shell script: yes, that's right that Python offers way more than
shell scripting. Due to that all shell scripts in GRASS 7 have been rewritten
to Python (they are a good resource to learn how to do it).

(I'm working on windows as the model only runs on windows) . However, I have
some problems getting started and all what I found on the internet was quite
confusing.

In case that GRASS sources are involved, please indicate how to improve
them. We are always happy about detailed feedback.

I would appreciate all advice concerning the following questions:

Which compiler is usually used for Python scripting with GRASS? Is there a
standard way?

Python does not require any compiler. You just need Python installed
(Python 2.x, not 3.x).

Is it possible to do the scripting inside GRASS?

Right. See
http://grass.osgeo.org/wiki/GRASS_and_Python
http://grass.osgeo.org/wiki/Using_Eclipse_to_develop_GRASS_Python_programs

Can I use SWIG? Do I have to install it extra, or is there an integrated
version?

SWIG is an extra software which generates extra Python bindings.
You do not necessarily need it for GRASS-Python programming, it
depends on the level of integration you want to achieve between your
model and GRASS.

I found a description how to configure Eclipse for python and GRASS, but up
to now I get some Attribut errors while compiling the simple test code (I
think there are still some problems with the environment variables).

You will need to post the errors to let us better understand the problem.

Can I run python scripts with MySYS?

I think so (Python needs to be installed).

Can I access to all the GRASS functions with Python?

In general yes, but there are different levels of doing so.
Please see the Python in GRASS 7 for inspiration:
http://svn.osgeo.org/grass/grass/trunk/scripts/
(or your download GRASS 7 source code).

Is it usefull to use IDLE?

Perhaps some GRASS-Python developer can comment:
http://en.wikipedia.org/wiki/IDLE_(Python)

I would say: personal taste: try and decide...

Is the GRASS plugin for QGIS written in Python?

No, it is in C++:
https://svn.osgeo.org/qgis/branches/Release-1_4_0/src/plugins/grass/

Or is it just easier to write the scripts in UNIX?

(you mean Unix shell)
Maybe slightly easier but Python offers way more. So better to invest
in Python.

Is it possible to execute the UNIX scripts with MySYS under windows?

Very limited only. So better Python....

Best
Markus

Markus Neteler wrote:

> Can I use SWIG? Do I have to install it extra, or is there an integrated
> version?

SWIG is an extra software which generates extra Python bindings.
You do not necessarily need it for GRASS-Python programming, it
depends on the level of integration you want to achieve between your
model and GRASS.

Also, the current SWIG bindings should be considered "experimental",
and likely to change.

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

Many thanks for your answers!

Eventually, I successed to execute some Grass commands with a python script through Eclipse.

However, I only reached to call the standalone winGrass and not the Osgeos integrated version, as the Osgeos grass do not has a lib directory in its grass folder.

So, here are some corrections for windows to the wiki:

http://grass.osgeo.org/wiki/Using_Eclipse_to_develop_GRASS_Python_programs

pydev is now located uder: http://pydev.org/updates/

Pydev is not automatically installed, you have to click on the install button after the download.

Before creating a new project you have to add the python interpreters (click auto config and apply) and configure eclipse and pydev.
In windows the grass libraries can be found under the following directories:

C:\GRASS-4-SVN\etc
C:\GRASS-4-SVN\etc\python
C:\GRASS-4-SVN\scripts
C:\GRASS-4-SVN\bin

Concerning the script test.py, I think it should be

import grass.script as grass instead of import grass

I didn't success to run the test.py script, but the following one:

import os
import grass.script as grass

env = grass.gisenv()

print env

#list all vectors of the mapset

r = grass.read_command("g.list", type='vect')

print r

#union of two vector maps, data specific

grass.run_command("v.overlay", ainput = 'ocsol_mercier', binput = 'chaudanne_ocsol', output = 'ocsol_general', operator='or')

The environment variables for windows are as follows (depending on the place of installation):

GISBASE C:\GRASS-4-SVN

GISRC C:\Documents and Settings\jankowfsky\.grassrc6

LD_LIBRARY_PATH C:\GRASS-4-SVN\lib

PATH C:\GRASS-4-SVN\etc;C:\GRASS-4-SVN\etc\python;C:\GRASS-4-SVN\lib;C:\GRASS-4-SVN\bin;C:\GRASS-4-SVN\extralib;C:\GRASS-4-SVN\scripts;C:\Python26;

PYTHONLIB C:\Python26

PYTHONPATH C:\GRASS-4-SVN\etc;C:\GRASS-4-SVN\etc\python;C:\GRASS-4-SVN\lib;C:\GRASS-4-SVN\bin;C:\GRASS-4-SVN\extralib;C:\GRASS-4-SVN\scripts;C:\Python26;

Sonja

Glynn Clements a écrit :

Markus Neteler wrote:

Can I use SWIG? Do I have to install it extra, or is there an integrated
version?
      

SWIG is an extra software which generates extra Python bindings.
You do not necessarily need it for GRASS-Python programming, it
depends on the level of integration you want to achieve between your
model and GRASS.
    
Also, the current SWIG bindings should be considered "experimental",
and likely to change.

--
Sonja Jankowfsky
UR Hydrologie-Hydraulique
Cemagref de Lyon
3 bis quai Chauveau CP 220
69336 Lyon Cedex 09
FRANCE
Tel : (+33)4 72 20 86 11 Fax : (+33)4 78 47 78 75