RE: [GRASS5] Grass with PHP (newbie question)

Glynn (or anyone else),

Thanks for your reply. Yes, I do mean running GRASS programs from a PHP
script via "system" calls. I've been playing with setting the environment
variables, but it still doesn't work. I tried doing a simple version call
and piping it into a file. The file gets created, but there is nothing
inside of it. It just creates a zero-length file.

Here is the code I'm using :

<?php
  putenv("GISBASE=/Applications/Grass/grass53");
  putenv("GISRC=/Users/administrator/.grassrc5");
  putenv("GRASS_LOCATION=$location");
  putenv("GRASS_PNGFILE=/Users/administrator/Sharyn.png");
  putenv("DYLD_LIBRARY_PATH=/Applications/Grass/grass53/lib");
  putenv("GRASS_TRUCOLOR=TRUE");
  putenv("GRASS_PNG_COMPRESSION=0");

  system("env > /Users/administrator/Sites/click/environment_vars.txt");

  system("/Applications/Grass/grass53/bin/g.version >
/Users/administrator/Sites/click/version_info.txt");
?>

The environment variables are echoed to the environment_vars.txt file just
fine :

GISRC=/Users/administrator/.grassrc5
GRASS_PNG_COMPRESSION=0
GISBASE=/Applications/Grass/grass53
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices
_=/usr/bin/env
PWD=/Users/administrator/Sites/click
SHLVL=3
DYLD_LIBRARY_PATH=/Applications/Grass/grass53/lib
GRASS_PNGFILE=/Users/administrator/Sharyn.png
GRASS_LOCATION=CADRG_TEST
GRASS_TRUCOLOR=TRUE

Any ideas?

Thanks,
- sharyn

-----Original Message-----
From: Glynn Clements [mailto:glynn.clements@virgin.net]
Sent: Wednesday, August 11, 2004 12:19 PM
To: Namnath, Sharyn
Cc: Grass5 list (grass5@grass.itc.it)
Subject: Re: [GRASS5] Grass with PHP (newbie question)

Namnath, Sharyn wrote:

> I am an utter newbie when it comes to Grass but I have a very
rudimentary
> knowledge of running it. I have a co-worker running grass with C++, but
I
> can't seem to get it to work with PHP. Has anyone implemented Grass
with
> PHP and are there any tips & tricks that anyone can give me?

What exactly do you mean by "get it to work with PHP"? Are you talking
about running GRASS programs from a PHP script with e.g. "system"?

If so, you need to bear in mind that, in order to function, GRASS
programs require a number of environment variables to be set.
Normally, these are set by the etc/Init.sh script before it spawns the
session shell. However, if you are trying to execute GRASS programs
outside of a GRASS session, you will have to set the variables
yourself.

In particular, $GISBASE needs to be the path to the root of the GRASS
installation (e.g. /usr/local/grass5), and $GISRC needs to be the name
of the GRASS session file (5.0 uses $HOME/.grassrc5, 5.3 uses
$HOME/.grassrc53, while 5.7 creates a new session file each time, to
allow for multiple sessions). Also, $GISLOCK is set to the PID of the
process which is running the Init.sh script although, AFAICT, this is
only actually used by v.digit etc for locking the digitiser device, so
it probably isn't relevant to a non-interactive session.

The following variables are also set by Init.sh, but are less
important than $GISBASE and $GISRC:

  GRASS_GNUPLOT
  GRASS_GUI
  GRASS_PAGER
  GRASS_PERL
  GRASS_WISH

Additionally, $PATH and $LD_LIBRARY_PATH have additional directories
added to them.

Apart from the issue of environment variables, you also need a mapset
directory. This must be owned by the UID under which the GRASS
programs are running; simply having write permission isn't sufficient.

No locking is performed, so you cannot have multiple processes using a
single mapset directory concurrently; each session requires its own
mapset directory.

In summary: if you want to run GRASS programs other than from the
shell which is spawned by running the "grass5" script, you will need
to duplicate much of the setup process. Read the etc/Init.sh script
for details.

--
Glynn Clements <glynn.clements@virgin.net>

Namnath, Sharyn wrote:

Thanks for your reply. Yes, I do mean running GRASS programs from a PHP
script via "system" calls. I've been playing with setting the environment
variables, but it still doesn't work. I tried doing a simple version call
and piping it into a file. The file gets created, but there is nothing
inside of it. It just creates a zero-length file.

Here is the code I'm using :

<?php
  putenv("GISBASE=/Applications/Grass/grass53");
  putenv("GISRC=/Users/administrator/.grassrc5");
  putenv("GRASS_LOCATION=$location");
  putenv("GRASS_PNGFILE=/Users/administrator/Sharyn.png");
  putenv("DYLD_LIBRARY_PATH=/Applications/Grass/grass53/lib");
  putenv("GRASS_TRUCOLOR=TRUE");
  putenv("GRASS_PNG_COMPRESSION=0");

  system("env > /Users/administrator/Sites/click/environment_vars.txt");

  system("/Applications/Grass/grass53/bin/g.version > /Users/administrator/Sites/click/version_info.txt");
?>

Have you checked the server/PHP logs for error messages?

The attempt to run g.version may result in error messages being
written to stderr. I don't know how mod_php handles this, but anything
written to stderr by CGI scripts usually end's up in Apache's
error_log file.

Alternatively, you could redirect stderr directly, e.g.:

system(".../g.version > .../version_info.txt 2> .../version_errors.txt");

The environment variables are echoed to the environment_vars.txt file just
fine :

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices

Yuo also need to add $GISBASE/bin and $GISBASE/scripts to PATH; some
GRASS programs run other GRASS programs/scripts; this will fail if
those directories aren't in $PATH.

--
Glynn Clements <glynn.clements@virgin.net>