[GRASS-user] grass shell

Glynn Clements glynn at gclements.plus.com
Thu Mar 4 16:17:44 EST 2010

maven apache wrote:

> > > So, what is the problem?
> >
> > With java.lang.Runtime.exec(String cmd, String[] envp), the envp
> > parameter specifies the entire environment for the process (i.e. it
> > doesn't add the strings to the current process' environment). The
> > result is that HOME isn't set, which means that $HOME/.grassrc6 cannot
> I can not understand, my grass command is "grass64 -text
> /home/kk/grass/GrassDataBase/spearfish60/PERMANENT", so I have specified the
> location and mapset, the gisrc file is required at any times?

The grass64 script creates a copy of $HOME/.grassrc6 and sets GISRC to
refer to the copy. If $HOME/.grassrc6 doesn't exist, the copy won't
exist either, and the first-use screen will be shown.

> > Beyond that, I'd suggest that you abandon the GRASS_BATCH_JOB approach
> > and set up the environment yourself, without using the grass64 script
> > at all.
> God!, in fact I tied to set up the env vars by myself, however many people
> suggest me to use the grass_batch _job for easily using. Now I can not make
> a choice.
> Here I can show you my requirements, can you give me some advise?
> ---my requirements----
> I am working with the Web Processing Service, and I want to wrap some grass
> operation ( for example the buffer , the interpolation and ect...) as
> processes in the web service, And my web application is developed by java. I
> changed my work platform from win xp to ubuntu beacuase of the unexpected
> errors when I call the grass command(Actually I can not do the interpolation
> operation in the windows-xp command line, but it can work in the Ubuntu
> shell).
>  I have a general idea:
> Create a class named GrassManager, and it contain a method to set the env (a
> gisrc file for each user) and a method to call the concrete grass command
> (for example r.info .....) basing on the set env.
> However I need to solve the following problems:
> 1) how to set up the grass env in java?

As for what variables need to be set, refer to:

1. My answer to your previous post in the thread "grass env".

2. The thread "can I access mapset outside of grass, by using python?"
by Nikos Alexandris.

3. The variables.html file in the GRASS distribution.

You first need to make a copy of the existing environment, then add or
replace any environment settings required by GRASS.

> 2) after set up the env ,how to call the grass command in java, because it
> reffed to command interpreter and separate executable, I do not know the
> grass command is either?

Once you have created the environment, I suggest using:

	Java.lang.Runtime.exec(String[] cmdarray, String[] envp)
	Java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)

for each GRASS command. The versions which take the command as a
single string are problematic if an argument contains spaces.

> 3) As a web application, I should consider the multiple user situation,
> create a unique gisrc file for each user?

Ideally, you should use a separate $GISRC file and a separate mapset
for each session. If you use a single mapset, the WIND and VAR files
will be shared by all sessions. The VAR file probably won't be an
issue (it only holds the database connection information). You can get
around the WIND issue by setting WIND_OVERRIDE to the name of a
specific region (created with e.g. "g.region save=..."), which will be
used instead of the WIND file.

But the main thing for a web application is to validate all inputs. 
Don't pass values from form fields directly to commands, as the code
uses fixed-size buffers extensively and doesn't perform bounds
checking. Also, shell scripts (and some compiled programs which use
system() or popen()) may misbehave if arguments contain shell

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list